ソースを参照

inject devPermissionRequest

Daniel Zlotin 7 年 前
コミット
dc94e5db71

+ 17
- 16
android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java ファイルの表示

@@ -8,22 +8,18 @@ import com.facebook.react.ReactApplication;
8 8
 import com.facebook.react.ReactNativeHost;
9 9
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
10 10
 import com.reactnativenavigation.controllers.ActivityLifecycleDelegate;
11
+import com.reactnativenavigation.react.DevPermissionRequestImpl;
11 12
 import com.reactnativenavigation.react.NavigationReactNativeHost;
12 13
 
13 14
 public abstract class NavigationApplication extends Application implements ReactApplication {
14 15
 	public static NavigationApplication instance;
15 16
 
16 17
 	public static class Config {
17
-		public final ReactNativeHost reactNativeHost;
18
-		public final ActivityLifecycleDelegate activityLifecycleDelegate;
19
-
20
-		public Config(ReactNativeHost reactNativeHost, ActivityLifecycleDelegate activityLifecycleDelegate) {
21
-			this.reactNativeHost = reactNativeHost;
22
-			this.activityLifecycleDelegate = activityLifecycleDelegate;
23
-		}
18
+		public ReactNativeHost reactNativeHost;
19
+		public ActivityLifecycleDelegate activityLifecycleDelegate;
24 20
 	}
25 21
 
26
-	public Config config;
22
+	private Config config;
27 23
 
28 24
 	@Override
29 25
 	public void onCreate() {
@@ -34,7 +30,7 @@ public abstract class NavigationApplication extends Application implements React
34 30
 		registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
35 31
 			@Override
36 32
 			public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
37
-				config.activityLifecycleDelegate.onActivityCreated(activity);
33
+				getConfig().activityLifecycleDelegate.onActivityCreated(activity);
38 34
 			}
39 35
 
40 36
 			@Override
@@ -44,12 +40,12 @@ public abstract class NavigationApplication extends Application implements React
44 40
 
45 41
 			@Override
46 42
 			public void onActivityResumed(Activity activity) {
47
-				config.activityLifecycleDelegate.onActivityResumed(activity, (DefaultHardwareBackBtnHandler) activity);
43
+				getConfig().activityLifecycleDelegate.onActivityResumed(activity, (DefaultHardwareBackBtnHandler) activity);
48 44
 			}
49 45
 
50 46
 			@Override
51 47
 			public void onActivityPaused(Activity activity) {
52
-				config.activityLifecycleDelegate.onActivityPaused(activity);
48
+				getConfig().activityLifecycleDelegate.onActivityPaused(activity);
53 49
 			}
54 50
 
55 51
 			@Override
@@ -64,14 +60,18 @@ public abstract class NavigationApplication extends Application implements React
64 60
 
65 61
 			@Override
66 62
 			public void onActivityDestroyed(Activity activity) {
67
-				config.activityLifecycleDelegate.onActivityDestroyed(activity);
63
+				getConfig().activityLifecycleDelegate.onActivityDestroyed(activity);
68 64
 			}
69 65
 		});
70 66
 	}
71 67
 
68
+	public final Config getConfig() {
69
+		return config;
70
+	}
71
+
72 72
 	@Override
73 73
 	public ReactNativeHost getReactNativeHost() {
74
-		return config.reactNativeHost;
74
+		return getConfig().reactNativeHost;
75 75
 	}
76 76
 
77 77
 	public abstract boolean isDebug();
@@ -82,8 +82,9 @@ public abstract class NavigationApplication extends Application implements React
82 82
 	 * @return the Config
83 83
 	 */
84 84
 	protected Config createConfig() {
85
-		ReactNativeHost reactNativeHost = new NavigationReactNativeHost(this, isDebug());
86
-		ActivityLifecycleDelegate activityLifecycleDelegate = new ActivityLifecycleDelegate(reactNativeHost.getReactInstanceManager());
87
-		return new Config(reactNativeHost, activityLifecycleDelegate);
85
+		Config config = new Config();
86
+		config.reactNativeHost = new NavigationReactNativeHost(this, isDebug());
87
+		config.activityLifecycleDelegate = new ActivityLifecycleDelegate(config.reactNativeHost.getReactInstanceManager(), new DevPermissionRequestImpl());
88
+		return config;
88 89
 	}
89 90
 }

+ 6
- 4
android/app/src/main/java/com/reactnativenavigation/controllers/ActivityLifecycleDelegate.java ファイルの表示

@@ -5,18 +5,20 @@ import android.app.Activity;
5 5
 import com.facebook.react.ReactInstanceManager;
6 6
 import com.facebook.react.bridge.ReactContext;
7 7
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
8
+import com.reactnativenavigation.react.DevPermissionRequest;
8 9
 import com.reactnativenavigation.react.NavigationEventEmitter;
9
-import com.reactnativenavigation.react.ReactDevPermission;
10 10
 
11 11
 import java.util.concurrent.atomic.AtomicBoolean;
12 12
 
13 13
 public class ActivityLifecycleDelegate {
14 14
 
15 15
 	private final ReactInstanceManager reactInstanceManager;
16
+	private final DevPermissionRequest devPermissionRequest;
16 17
 	private final AtomicBoolean appLaunchEmitted = new AtomicBoolean(false);
17 18
 
18
-	public ActivityLifecycleDelegate(ReactInstanceManager reactInstanceManager) {
19
+	public ActivityLifecycleDelegate(ReactInstanceManager reactInstanceManager, DevPermissionRequest devPermissionRequest) {
19 20
 		this.reactInstanceManager = reactInstanceManager;
21
+		this.devPermissionRequest = devPermissionRequest;
20 22
 	}
21 23
 
22 24
 	public void onActivityCreated(Activity activity) {
@@ -24,8 +26,8 @@ public class ActivityLifecycleDelegate {
24 26
 	}
25 27
 
26 28
 	public void onActivityResumed(Activity activity, DefaultHardwareBackBtnHandler backBtnHandler) {
27
-		if (ReactDevPermission.shouldAskPermission()) {
28
-			ReactDevPermission.askPermission(activity);
29
+		if (devPermissionRequest.shouldAskPermission()) {
30
+			devPermissionRequest.askPermission();
29 31
 		} else {
30 32
 			reactInstanceManager.onHostResume(activity, backBtnHandler);
31 33
 			prepareReactApp();

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/react/DevPermissionRequest.java ファイルの表示

@@ -0,0 +1,8 @@
1
+package com.reactnativenavigation.react;
2
+
3
+public interface DevPermissionRequest {
4
+
5
+	boolean shouldAskPermission();
6
+
7
+	void askPermission();
8
+}

android/app/src/main/java/com/reactnativenavigation/react/ReactDevPermission.java → android/app/src/main/java/com/reactnativenavigation/react/DevPermissionRequestImpl.java ファイルの表示

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

+ 1
- 2
android/app/src/test/java/com/reactnativenavigation/NavigationActivityTest.java ファイルの表示

@@ -2,7 +2,6 @@ package com.reactnativenavigation;
2 2
 
3 3
 import android.view.View;
4 4
 
5
-import org.junit.Ignore;
6 5
 import org.junit.Test;
7 6
 import org.robolectric.Robolectric;
8 7
 import org.robolectric.android.controller.ActivityController;
@@ -10,8 +9,8 @@ import org.robolectric.android.controller.ActivityController;
10 9
 import static org.assertj.core.api.Java6Assertions.assertThat;
11 10
 
12 11
 public class NavigationActivityTest extends BaseTest {
12
+
13 13
 	@Test
14
-	@Ignore
15 14
 	public void holdsContentView() throws Exception {
16 15
 		NavigationActivity activity = Robolectric.setupActivity(NavigationActivity.class);
17 16
 		assertThat(activity.getContentView()).isNull();

+ 2
- 3
android/app/src/test/java/com/reactnativenavigation/NavigationApplicationTest.java ファイルの表示

@@ -28,12 +28,11 @@ public class NavigationApplicationTest extends BaseTest {
28 28
 
29 29
 	@Test
30 30
 	public void holdsConfiguration() throws Exception {
31
-		assertThat(NavigationApplication.instance.config).isInstanceOf(NavigationApplication.Config.class);
31
+		assertThat(NavigationApplication.instance.getConfig()).isInstanceOf(NavigationApplication.Config.class);
32 32
 	}
33 33
 
34 34
 	@Test
35 35
 	public void configProvidesActivityLifecycleDelegate() throws Exception {
36
-		NavigationApplication.Config config = NavigationApplication.instance.config;
37
-		assertThat(config.activityLifecycleDelegate).isInstanceOf(ActivityLifecycleDelegate.class);
36
+		assertThat(NavigationApplication.instance.getConfig().activityLifecycleDelegate).isInstanceOf(ActivityLifecycleDelegate.class);
38 37
 	}
39 38
 }

+ 5
- 3
android/app/src/test/java/com/reactnativenavigation/TestApplication.java ファイルの表示

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation;
2 2
 
3 3
 import com.reactnativenavigation.controllers.ActivityLifecycleDelegate;
4
+import com.reactnativenavigation.mocks.TestDevPermissionRequest;
4 5
 import com.reactnativenavigation.mocks.TestReactNativeHost;
5 6
 
6 7
 public class TestApplication extends NavigationApplication {
@@ -12,8 +13,9 @@ public class TestApplication extends NavigationApplication {
12 13
 
13 14
 	@Override
14 15
 	protected Config createConfig() {
15
-		TestReactNativeHost host = new TestReactNativeHost(this, isDebug());
16
-		ActivityLifecycleDelegate delegate = new ActivityLifecycleDelegate(host.getReactInstanceManager());
17
-		return new Config(host, delegate);
16
+		Config config = new Config();
17
+		config.reactNativeHost = new TestReactNativeHost(this, isDebug());
18
+		config.activityLifecycleDelegate = new ActivityLifecycleDelegate(config.reactNativeHost.getReactInstanceManager(), new TestDevPermissionRequest());
19
+		return config;
18 20
 	}
19 21
 }

+ 15
- 0
android/app/src/test/java/com/reactnativenavigation/mocks/TestDevPermissionRequest.java ファイルの表示

@@ -0,0 +1,15 @@
1
+package com.reactnativenavigation.mocks;
2
+
3
+import com.reactnativenavigation.react.DevPermissionRequest;
4
+
5
+public class TestDevPermissionRequest implements DevPermissionRequest {
6
+	@Override
7
+	public boolean shouldAskPermission() {
8
+		return false;
9
+	}
10
+
11
+	@Override
12
+	public void askPermission() {
13
+		// nothing
14
+	}
15
+}