Browse Source

fixed BadWindowToken (react needs permission to run in dev mode)

Daniel Zlotin 8 years ago
parent
commit
527bb3a8d9

+ 0
- 3
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

21
 import com.reactnativenavigation.params.TitleBarButtonParams;
21
 import com.reactnativenavigation.params.TitleBarButtonParams;
22
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
22
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
23
 import com.reactnativenavigation.react.JsDevReloadHandler;
23
 import com.reactnativenavigation.react.JsDevReloadHandler;
24
-import com.reactnativenavigation.react.RedboxPermission;
25
 
24
 
26
 import java.util.List;
25
 import java.util.List;
27
 
26
 
50
             return;
49
             return;
51
         }
50
         }
52
 
51
 
53
-        RedboxPermission.permissionToShowRedboxIfNeeded(this);
54
-
55
         activityParams = NavigationCommandsHandler.parseActivityParams(getIntent());
52
         activityParams = NavigationCommandsHandler.parseActivityParams(getIntent());
56
 
53
 
57
         disableActivityShowAnimationIfNeeded();
54
         disableActivityShowAnimationIfNeeded();

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/SplashActivity.java View File

8
 import android.view.View;
8
 import android.view.View;
9
 
9
 
10
 import com.reactnativenavigation.NavigationApplication;
10
 import com.reactnativenavigation.NavigationApplication;
11
+import com.reactnativenavigation.react.ReactDevPermission;
11
 
12
 
12
 public abstract class SplashActivity extends AppCompatActivity {
13
 public abstract class SplashActivity extends AppCompatActivity {
13
 
14
 
15
     protected void onCreate(@Nullable Bundle savedInstanceState) {
16
     protected void onCreate(@Nullable Bundle savedInstanceState) {
16
         super.onCreate(savedInstanceState);
17
         super.onCreate(savedInstanceState);
17
         setSplashLayout();
18
         setSplashLayout();
19
+
20
+        if (ReactDevPermission.shouldAskPermission()) {
21
+            ReactDevPermission.askPermission(this);
22
+            return;
23
+        }
24
+
18
         NavigationApplication.instance.startReactContextOnceInBackgroundAndExecuteJS();
25
         NavigationApplication.instance.startReactContextOnceInBackgroundAndExecuteJS();
19
     }
26
     }
20
 
27
 

android/app/src/main/java/com/reactnativenavigation/react/RedboxPermission.java → android/app/src/main/java/com/reactnativenavigation/react/ReactDevPermission.java View File

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

+ 0
- 97
example-redux/android/app/react.gradle View File

1
-import org.apache.tools.ant.taskdefs.condition.Os
2
-
3
-def config = project.hasProperty("react") ? project.react : [];
4
-
5
-def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
6
-def entryFile = config.entryFile ?: "index.android.js"
7
-
8
-// because elvis operator
9
-def elvisFile(thing) {
10
-    return thing ? file(thing) : null;
11
-}
12
-
13
-def reactRoot = elvisFile(config.root) ?: file("../../")
14
-def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"]
15
-
16
-void runBefore(String dependentTaskName, Task task) {
17
-    Task dependentTask = tasks.findByPath(dependentTaskName);
18
-    if (dependentTask != null) {
19
-        dependentTask.dependsOn task
20
-    }
21
-}
22
-
23
-gradle.projectsEvaluated {
24
-    // Grab all build types and product flavors
25
-    def buildTypes = android.buildTypes.collect { type -> type.name }
26
-    def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
27
-
28
-    // When no product flavors defined, use empty
29
-    if (!productFlavors) productFlavors.add('')
30
-
31
-    productFlavors.each { productFlavorName ->
32
-        buildTypes.each { buildTypeName ->
33
-            // Create variant and target names
34
-            def targetName = "${productFlavorName.capitalize()}${buildTypeName.capitalize()}"
35
-            def targetPath = productFlavorName ?
36
-                    "${productFlavorName}/${buildTypeName}" :
37
-                    "${buildTypeName}"
38
-
39
-            // React js bundle directories
40
-            def jsBundleDirConfigName = "jsBundleDir${targetName}"
41
-            def jsBundleDir = elvisFile(config."$jsBundleDirConfigName") ?:
42
-                    file("$buildDir/intermediates/assets/${targetPath}")
43
-
44
-            def resourcesDirConfigName = "resourcesDir${targetName}"
45
-            def resourcesDir = elvisFile(config."${resourcesDirConfigName}") ?:
46
-                    file("$buildDir/intermediates/res/merged/${targetPath}")
47
-            def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
48
-
49
-            // Bundle task name for variant
50
-            def bundleJsAndAssetsTaskName = "bundle${targetName}JsAndAssets"
51
-
52
-            def currentBundleTask = tasks.create(
53
-                    name: bundleJsAndAssetsTaskName,
54
-                    type: Exec) {
55
-                group = "react"
56
-                description = "bundle JS and assets for ${targetName}."
57
-
58
-                // Create dirs if they are not there (e.g. the "clean" task just ran)
59
-                doFirst {
60
-                    jsBundleDir.mkdirs()
61
-                    resourcesDir.mkdirs()
62
-                }
63
-
64
-                // Set up inputs and outputs so gradle can cache the result
65
-                inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
66
-                outputs.dir jsBundleDir
67
-                outputs.dir resourcesDir
68
-
69
-                // Set up the call to the react-native cli
70
-                workingDir reactRoot
71
-
72
-                // Set up dev mode
73
-                def devEnabled = !targetName.toLowerCase().contains("release")
74
-                if (Os.isFamily(Os.FAMILY_WINDOWS)) {
75
-                    commandLine "cmd", "/c", "react-native", "bundle", "--platform", "android", "--dev", "${devEnabled}",
76
-                            "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir
77
-                } else {
78
-                    commandLine "react-native", "bundle", "--platform", "android", "--dev", "${devEnabled}",
79
-                            "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir
80
-                }
81
-
82
-                enabled config."bundleIn${targetName}" ||
83
-                    config."bundleIn${buildTypeName.capitalize()}" ?:
84
-                            targetName.toLowerCase().contains("release")
85
-            }
86
-
87
-            // Hook bundle${productFlavor}${buildType}JsAndAssets into the android build process
88
-            currentBundleTask.dependsOn("merge${targetName}Resources")
89
-            currentBundleTask.dependsOn("merge${targetName}Assets")
90
-
91
-            runBefore("processArmeabi-v7a${targetName}Resources", currentBundleTask)
92
-            runBefore("processX86${targetName}Resources", currentBundleTask)
93
-            runBefore("processUniversal${targetName}Resources", currentBundleTask)
94
-            runBefore("process${targetName}Resources", currentBundleTask)
95
-        }
96
-    }
97
-}