Permalink
Browse files

Allow code to check android permissions when run from a Service as we…

…ll as Activity

Summary:
This allows the React JS code that's running from a Service (ie GcmListenerService) to check permissions (ie check for VIBRATE permissions before delivering notifications)

**Test plan (required)**

I've run this code from a GcmListenerService subclass, and it works correctly.
Closes #10229

Differential Revision: D3980853

fbshipit-source-id: 026b1f0c953d7093b5af2bec0b4a93ebd228f62e
  • Loading branch information...
1 parent ccea759 commit 1502e66c313a22caa4812a43ce8445abf0c1046d @mikelambert mikelambert committed with Facebook Github Bot Oct 6, 2016
@@ -10,6 +10,7 @@
package com.facebook.react.modules.permissions;
import android.app.Activity;
+import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Process;
@@ -49,13 +50,13 @@ public String getName() {
*/
@ReactMethod
public void checkPermission(final String permission, final Promise promise) {
- PermissionAwareActivity activity = getPermissionAwareActivity();
+ Context context = getReactApplicationContext().getBaseContext();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
- promise.resolve(activity.checkPermission(permission, Process.myPid(), Process.myUid()) ==
+ promise.resolve(context.checkPermission(permission, Process.myPid(), Process.myUid()) ==
PackageManager.PERMISSION_GRANTED);
return;
}
- promise.resolve(activity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
+ promise.resolve(context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
}
/**
@@ -83,13 +84,13 @@ public void shouldShowRequestPermissionRationale(final String permission, final
*/
@ReactMethod
public void requestPermission(final String permission, final Promise promise) {
- PermissionAwareActivity activity = getPermissionAwareActivity();
+ Context context = getReactApplicationContext().getBaseContext();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
- promise.resolve(activity.checkPermission(permission, Process.myPid(), Process.myUid()) ==
+ promise.resolve(context.checkPermission(permission, Process.myPid(), Process.myUid()) ==
PackageManager.PERMISSION_GRANTED);
return;
}
- if (activity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
+ if (context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
promise.resolve(true);
return;
}
@@ -102,6 +103,7 @@ public void invoke(Object... args) {
}
});
+ PermissionAwareActivity activity = getPermissionAwareActivity();
activity.requestPermissions(new String[]{permission}, mRequestCode, this);
mRequestCode++;
}

0 comments on commit 1502e66

Please sign in to comment.