Skip to content

Commit

Permalink
Allow code to check android permissions when run from a Service as we…
Browse files Browse the repository at this point in the history
…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
mikelambert authored and Facebook Github Bot committed Oct 6, 2016
1 parent ccea759 commit 1502e66
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -102,6 +103,7 @@ public void invoke(Object... args) {
}
});

PermissionAwareActivity activity = getPermissionAwareActivity();
activity.requestPermissions(new String[]{permission}, mRequestCode, this);
mRequestCode++;
}
Expand Down

0 comments on commit 1502e66

Please sign in to comment.