diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java b/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java index 2791564f8..ef4d7c205 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactActivityDelegate.java @@ -6,6 +6,7 @@ import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.Settings; @@ -29,6 +30,10 @@ import javax.annotation.Nullable; * class doesn't implement {@link ReactApplication}. */ public class ReactActivityDelegate { + + private final int REQUEST_OVERLAY_PERMISSION_CODE = 1111; + private static final String REDBOX_PERMISSION_GRANTED_MESSAGE = + "Overlay permissions have been granted."; private static final String REDBOX_PERMISSION_MESSAGE = "Overlay permissions needs to be granted in order for react native apps to run in dev mode"; @@ -79,17 +84,19 @@ public class ReactActivityDelegate { } protected void onCreate(Bundle savedInstanceState) { - if (getReactNativeHost().getUseDeveloperSupport() && Build.VERSION.SDK_INT >= 23) { + boolean needsOverlayPermission = false; + if (getReactNativeHost().getUseDeveloperSupport() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Get permission to show redbox in dev builds. if (!Settings.canDrawOverlays(getContext())) { - Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); - getContext().startActivity(serviceIntent); + needsOverlayPermission = true; + Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getContext().getPackageName())); FLog.w(ReactConstants.TAG, REDBOX_PERMISSION_MESSAGE); Toast.makeText(getContext(), REDBOX_PERMISSION_MESSAGE, Toast.LENGTH_LONG).show(); + ((Activity) getContext()).startActivityForResult(serviceIntent, REQUEST_OVERLAY_PERMISSION_CODE); } } - if (mMainComponentName != null) { + if (mMainComponentName != null && !needsOverlayPermission) { loadApp(mMainComponentName); } mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer(); @@ -140,6 +147,16 @@ public class ReactActivityDelegate { if (getReactNativeHost().hasInstance()) { getReactNativeHost().getReactInstanceManager() .onActivityResult(getPlainActivity(), requestCode, resultCode, data); + } else { + // Did we request overlay permissions? + if (requestCode == REQUEST_OVERLAY_PERMISSION_CODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Settings.canDrawOverlays(getContext())) { + if (mMainComponentName != null) { + loadApp(mMainComponentName); + } + Toast.makeText(getContext(), REDBOX_PERMISSION_GRANTED_MESSAGE, Toast.LENGTH_LONG).show(); + } + } } } @@ -191,8 +208,7 @@ public class ReactActivityDelegate { mPermissionsCallback = new Callback() { @Override public void invoke(Object... args) { - if (mPermissionListener != null && - mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) { + if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) { mPermissionListener = null; } }