|
6 | 6 | import android.app.Activity;
|
7 | 7 | import android.content.Context;
|
8 | 8 | import android.content.Intent;
|
| 9 | +import android.net.Uri; |
9 | 10 | import android.os.Build;
|
10 | 11 | import android.os.Bundle;
|
11 | 12 | import android.provider.Settings;
|
|
29 | 30 | * class doesn't implement {@link ReactApplication}.
|
30 | 31 | */
|
31 | 32 | public class ReactActivityDelegate {
|
| 33 | + |
| 34 | + private final int REQUEST_OVERLAY_PERMISSION_CODE = 1111; |
| 35 | + private static final String REDBOX_PERMISSION_GRANTED_MESSAGE = |
| 36 | + "Overlay permissions have been granted."; |
32 | 37 | private static final String REDBOX_PERMISSION_MESSAGE =
|
33 | 38 | "Overlay permissions needs to be granted in order for react native apps to run in dev mode";
|
34 | 39 |
|
@@ -79,17 +84,19 @@ public ReactInstanceManager getReactInstanceManager() {
|
79 | 84 | }
|
80 | 85 |
|
81 | 86 | protected void onCreate(Bundle savedInstanceState) {
|
82 |
| - if (getReactNativeHost().getUseDeveloperSupport() && Build.VERSION.SDK_INT >= 23) { |
| 87 | + boolean needsOverlayPermission = false; |
| 88 | + if (getReactNativeHost().getUseDeveloperSupport() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
83 | 89 | // Get permission to show redbox in dev builds.
|
84 | 90 | if (!Settings.canDrawOverlays(getContext())) {
|
85 |
| - Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); |
86 |
| - getContext().startActivity(serviceIntent); |
| 91 | + needsOverlayPermission = true; |
| 92 | + Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getContext().getPackageName())); |
87 | 93 | FLog.w(ReactConstants.TAG, REDBOX_PERMISSION_MESSAGE);
|
88 | 94 | Toast.makeText(getContext(), REDBOX_PERMISSION_MESSAGE, Toast.LENGTH_LONG).show();
|
| 95 | + ((Activity) getContext()).startActivityForResult(serviceIntent, REQUEST_OVERLAY_PERMISSION_CODE); |
89 | 96 | }
|
90 | 97 | }
|
91 | 98 |
|
92 |
| - if (mMainComponentName != null) { |
| 99 | + if (mMainComponentName != null && !needsOverlayPermission) { |
93 | 100 | loadApp(mMainComponentName);
|
94 | 101 | }
|
95 | 102 | mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer();
|
@@ -140,6 +147,16 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
140 | 147 | if (getReactNativeHost().hasInstance()) {
|
141 | 148 | getReactNativeHost().getReactInstanceManager()
|
142 | 149 | .onActivityResult(getPlainActivity(), requestCode, resultCode, data);
|
| 150 | + } else { |
| 151 | + // Did we request overlay permissions? |
| 152 | + if (requestCode == REQUEST_OVERLAY_PERMISSION_CODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 153 | + if (Settings.canDrawOverlays(getContext())) { |
| 154 | + if (mMainComponentName != null) { |
| 155 | + loadApp(mMainComponentName); |
| 156 | + } |
| 157 | + Toast.makeText(getContext(), REDBOX_PERMISSION_GRANTED_MESSAGE, Toast.LENGTH_LONG).show(); |
| 158 | + } |
| 159 | + } |
143 | 160 | }
|
144 | 161 | }
|
145 | 162 |
|
@@ -191,8 +208,7 @@ public void onRequestPermissionsResult(
|
191 | 208 | mPermissionsCallback = new Callback() {
|
192 | 209 | @Override
|
193 | 210 | public void invoke(Object... args) {
|
194 |
| - if (mPermissionListener != null && |
195 |
| - mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) { |
| 211 | + if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) { |
196 | 212 | mPermissionListener = null;
|
197 | 213 | }
|
198 | 214 | }
|
|
0 commit comments