From 4e1abdd74dc4127a86d62e7750d01d39bb781c08 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Fri, 15 Jun 2018 10:33:28 -0700 Subject: [PATCH] fix permission requests on pre-M android (#19734) Summary: On pre-M devices, `PermissionsAndroid.request` currently returns a boolean, whereas on newer, it returns GRANTED, DENIED or other constants defined in `PermissionsModule.java` given the example form the [docs](https://facebook.github.io/react-native/docs/permissionsandroid.html) which I guess many people use, this will lead to code that does not work before M (it will tell you that permissions are not given even if they are in the manifest). I believe the author of [this](https://github.com/facebook/react-native/commit/51efaab1209a41254c90dcba596d49e3cbc2d925) forgot to change the resolved value in this one place but changed it in other places, eg [here](https://github.com/facebook/react-native/commit/51efaab1209a41254c90dcba596d49e3cbc2d925#diff-2a74096453bc8faa5d4a1599ad0ab33fL99). The docs are written correctly: > On devices before SDK version 23, the permissions are automatically granted if they appear in the manifest, so check and request should always be true. but the code is not right because we'd need to check for `if (granted === PermissionsAndroid.RESULTS.GRANTED || granted === true) {` Also, the behavior is done correctly in [requestMultiplePermissions](https://github.com/facebook/react-native/blob/26684cf3adf4094eb6c405d345a75bf8c7c0bf88/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java#L148) so returning a boolean is an inconsistency. I tested this locally. The code is the same as on line [148](https://github.com/facebook/react-native/blob/26684cf3adf4094eb6c405d345a75bf8c7c0bf88/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java#L148) where it is done correctly. [ANDROID] [BUGFIX] [PermissionAndroid] - return GRANTED / DENIED instead of true / false on pre-M android Closes https://github.com/facebook/react-native/pull/19734 Differential Revision: D8450402 Pulled By: hramos fbshipit-source-id: 46b0b7f83f81d817d60234f155d43de7f57248c7 --- .../react/modules/permissions/PermissionsModule.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java index 0b943bc6c..26701edd9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java @@ -86,9 +86,9 @@ public class PermissionsModule extends ReactContextBaseJavaModule implements Per } /** - * Request the given permission. successCallback is called with true if the permission had been - * granted, false otherwise. For devices before Android M, this instead checks if the user has - * the permission given or not. + * Request the given permission. successCallback is called with GRANTED if the permission had been + * granted, DENIED or NEVER_ASK_AGAIN otherwise. For devices before Android M, this checks if the user has + * the permission given or not and resolves with GRANTED or DENIED. * See {@link Activity#checkSelfPermission}. */ @ReactMethod @@ -96,7 +96,7 @@ public class PermissionsModule extends ReactContextBaseJavaModule implements Per Context context = getReactApplicationContext().getBaseContext(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { promise.resolve(context.checkPermission(permission, Process.myPid(), Process.myUid()) == - PackageManager.PERMISSION_GRANTED); + PackageManager.PERMISSION_GRANTED ? GRANTED : DENIED); return; } if (context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {