소스 검색

Requesting overlay permission and isDebug

Guy Carmeli 7 년 전
부모
커밋
3072a90284

+ 45
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java 파일 보기

@@ -3,12 +3,56 @@ package com.reactnativenavigation.controllers;
3 3
 import android.os.Bundle;
4 4
 import android.support.v7.app.AppCompatActivity;
5 5
 
6
+import com.facebook.react.ReactNativeHost;
7
+import com.facebook.react.ReactPackage;
8
+import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
9
+import com.facebook.react.shell.MainReactPackage;
6 10
 import com.reactnativenavigation.R;
11
+import com.reactnativenavigation.react.NavigationPackage;
12
+import com.reactnativenavigation.react.ReactDevPermission;
13
+
14
+import java.util.Arrays;
15
+import java.util.List;
16
+
17
+public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
18
+
19
+    private ReactNativeHost host;
7 20
 
8
-public class NavigationActivity extends AppCompatActivity {
9 21
     @Override
10 22
     public void onCreate(Bundle savedInstanceState) {
11 23
         super.onCreate(savedInstanceState);
12 24
         setContentView(R.layout.splash);
25
+        host = new ReactNativeHost(getApplication()) {
26
+            @Override
27
+            protected boolean getUseDeveloperSupport() {
28
+                return NavigationApplication.instance.isDebug();
29
+            }
30
+
31
+            @Override
32
+            protected List<ReactPackage> getPackages() {
33
+                return Arrays.asList(
34
+                        new MainReactPackage(),
35
+                        new NavigationPackage()
36
+                );
37
+            }
38
+        };
39
+
40
+        if (ReactDevPermission.shouldAskPermission()) {
41
+            ReactDevPermission.askPermission(this);
42
+            return;
43
+        }
44
+
45
+        host.getReactInstanceManager().createReactContextInBackground();
46
+    }
47
+
48
+    @Override
49
+    protected void onResume() {
50
+        super.onResume();
51
+        host.getReactInstanceManager().onHostResume(this, this);
52
+    }
53
+
54
+    @Override
55
+    public void invokeDefaultOnBackPressed() {
56
+        onBackPressed();
13 57
     }
14 58
 }

+ 15
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationApplication.java 파일 보기

@@ -0,0 +1,15 @@
1
+package com.reactnativenavigation.controllers;
2
+
3
+import android.app.Application;
4
+
5
+public abstract class NavigationApplication extends Application {
6
+    public static NavigationApplication instance;
7
+
8
+    @Override
9
+    public void onCreate() {
10
+        super.onCreate();
11
+        instance = this;
12
+    }
13
+
14
+    public abstract boolean isDebug();
15
+}

+ 17
- 0
android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java 파일 보기

@@ -0,0 +1,17 @@
1
+package com.reactnativenavigation.react;
2
+
3
+import com.facebook.react.bridge.ReactApplicationContext;
4
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
5
+
6
+public class NavigationModule extends ReactContextBaseJavaModule {
7
+    public static final String NAME = "NavigationModule";
8
+
9
+    public NavigationModule(ReactApplicationContext reactContext) {
10
+        super(reactContext);
11
+    }
12
+
13
+    @Override
14
+    public String getName() {
15
+        return NAME;
16
+    }
17
+}

+ 32
- 0
android/app/src/main/java/com/reactnativenavigation/react/NavigationPackage.java 파일 보기

@@ -0,0 +1,32 @@
1
+package com.reactnativenavigation.react;
2
+
3
+import com.facebook.react.ReactPackage;
4
+import com.facebook.react.bridge.JavaScriptModule;
5
+import com.facebook.react.bridge.NativeModule;
6
+import com.facebook.react.bridge.ReactApplicationContext;
7
+import com.facebook.react.uimanager.ViewManager;
8
+
9
+import java.util.Arrays;
10
+import java.util.Collections;
11
+import java.util.List;
12
+
13
+public class NavigationPackage implements ReactPackage {
14
+
15
+    @Override
16
+    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
17
+        return Arrays.<NativeModule>asList(
18
+                new NavigationModule(reactContext)
19
+        );
20
+    }
21
+
22
+    @Override
23
+    public List<Class<? extends JavaScriptModule>> createJSModules() {
24
+        return Collections.emptyList();
25
+    }
26
+
27
+    @Override
28
+    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
29
+        return Collections.emptyList();
30
+    }
31
+}
32
+

+ 36
- 0
android/app/src/main/java/com/reactnativenavigation/react/ReactDevPermission.java 파일 보기

@@ -0,0 +1,36 @@
1
+package com.reactnativenavigation.react;
2
+
3
+import android.annotation.TargetApi;
4
+import android.content.Context;
5
+import android.content.Intent;
6
+import android.os.Build;
7
+import android.provider.Settings;
8
+import android.util.Log;
9
+import android.widget.Toast;
10
+
11
+import com.facebook.react.common.ReactConstants;
12
+import com.reactnativenavigation.controllers.NavigationApplication;
13
+
14
+public class ReactDevPermission {
15
+
16
+    public static boolean shouldAskPermission() {
17
+        return NavigationApplication.instance.isDebug() &&
18
+                Build.VERSION.SDK_INT >= 23 &&
19
+               !Settings.canDrawOverlays(NavigationApplication.instance);
20
+    }
21
+
22
+    @TargetApi(23)
23
+    public static void askPermission(Context context) {
24
+        if (shouldAskPermission()) {
25
+            Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
26
+            context.startActivity(serviceIntent);
27
+            String msg = "Overlay permissions needs to be granted in order for react native apps to run in dev mode";
28
+            Log.w(ReactConstants.TAG, "======================================\n\n");
29
+            Log.w(ReactConstants.TAG, msg);
30
+            Log.w(ReactConstants.TAG, "\n\n======================================");
31
+            for (int i = 0; i < 5; i++) {
32
+                Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
33
+            }
34
+        }
35
+    }
36
+}

+ 7
- 3
playground/android/app/src/main/java/com/reactnativenavigation/playground/MainApplication.java 파일 보기

@@ -1,7 +1,11 @@
1 1
 package com.reactnativenavigation.playground;
2 2
 
3
-import android.app.Application;
3
+import com.reactnativenavigation.controllers.NavigationApplication;
4 4
 
5
-public class MainApplication extends Application {
6
-    
5
+public class MainApplication extends NavigationApplication {
6
+
7
+    @Override
8
+    public boolean isDebug() {
9
+        return BuildConfig.DEBUG;
10
+    }
7 11
 }