Browse Source

inject devPermissionRequest

Daniel Zlotin 7 years ago
parent
commit
dc94e5db71

+ 17
- 16
android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java View File

8
 import com.facebook.react.ReactNativeHost;
8
 import com.facebook.react.ReactNativeHost;
9
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
9
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
10
 import com.reactnativenavigation.controllers.ActivityLifecycleDelegate;
10
 import com.reactnativenavigation.controllers.ActivityLifecycleDelegate;
11
+import com.reactnativenavigation.react.DevPermissionRequestImpl;
11
 import com.reactnativenavigation.react.NavigationReactNativeHost;
12
 import com.reactnativenavigation.react.NavigationReactNativeHost;
12
 
13
 
13
 public abstract class NavigationApplication extends Application implements ReactApplication {
14
 public abstract class NavigationApplication extends Application implements ReactApplication {
14
 	public static NavigationApplication instance;
15
 	public static NavigationApplication instance;
15
 
16
 
16
 	public static class Config {
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
 	@Override
24
 	@Override
29
 	public void onCreate() {
25
 	public void onCreate() {
34
 		registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
30
 		registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
35
 			@Override
31
 			@Override
36
 			public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
32
 			public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
37
-				config.activityLifecycleDelegate.onActivityCreated(activity);
33
+				getConfig().activityLifecycleDelegate.onActivityCreated(activity);
38
 			}
34
 			}
39
 
35
 
40
 			@Override
36
 			@Override
44
 
40
 
45
 			@Override
41
 			@Override
46
 			public void onActivityResumed(Activity activity) {
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
 			@Override
46
 			@Override
51
 			public void onActivityPaused(Activity activity) {
47
 			public void onActivityPaused(Activity activity) {
52
-				config.activityLifecycleDelegate.onActivityPaused(activity);
48
+				getConfig().activityLifecycleDelegate.onActivityPaused(activity);
53
 			}
49
 			}
54
 
50
 
55
 			@Override
51
 			@Override
64
 
60
 
65
 			@Override
61
 			@Override
66
 			public void onActivityDestroyed(Activity activity) {
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
 	@Override
72
 	@Override
73
 	public ReactNativeHost getReactNativeHost() {
73
 	public ReactNativeHost getReactNativeHost() {
74
-		return config.reactNativeHost;
74
+		return getConfig().reactNativeHost;
75
 	}
75
 	}
76
 
76
 
77
 	public abstract boolean isDebug();
77
 	public abstract boolean isDebug();
82
 	 * @return the Config
82
 	 * @return the Config
83
 	 */
83
 	 */
84
 	protected Config createConfig() {
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 View File

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

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/react/DevPermissionRequest.java View File

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 View File

1
 package com.reactnativenavigation.react;
1
 package com.reactnativenavigation.react;
2
 
2
 
3
 import android.annotation.TargetApi;
3
 import android.annotation.TargetApi;
4
-import android.content.Context;
5
 import android.content.Intent;
4
 import android.content.Intent;
6
 import android.os.Build;
5
 import android.os.Build;
7
 import android.provider.Settings;
6
 import android.provider.Settings;
11
 import com.facebook.react.common.ReactConstants;
10
 import com.facebook.react.common.ReactConstants;
12
 import com.reactnativenavigation.NavigationApplication;
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
 		return NavigationApplication.instance.isDebug() &&
16
 		return NavigationApplication.instance.isDebug() &&
18
 				Build.VERSION.SDK_INT >= 23 &&
17
 				Build.VERSION.SDK_INT >= 23 &&
19
 				!Settings.canDrawOverlays(NavigationApplication.instance);
18
 				!Settings.canDrawOverlays(NavigationApplication.instance);
20
 	}
19
 	}
21
 
20
 
21
+	@Override
22
 	@TargetApi(23)
22
 	@TargetApi(23)
23
-	public static void askPermission(Context context) {
23
+	public void askPermission() {
24
 		if (shouldAskPermission()) {
24
 		if (shouldAskPermission()) {
25
 			Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
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
 			String msg = "Overlay permissions needs to be granted in order for react native apps to run in dev mode";
28
 			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, "======================================\n\n");
29
 			Log.w(ReactConstants.TAG, msg);
30
 			Log.w(ReactConstants.TAG, msg);
30
 			Log.w(ReactConstants.TAG, "\n\n======================================");
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 View File

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

+ 2
- 3
android/app/src/test/java/com/reactnativenavigation/NavigationApplicationTest.java View File

28
 
28
 
29
 	@Test
29
 	@Test
30
 	public void holdsConfiguration() throws Exception {
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
 	@Test
34
 	@Test
35
 	public void configProvidesActivityLifecycleDelegate() throws Exception {
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 View File

1
 package com.reactnativenavigation;
1
 package com.reactnativenavigation;
2
 
2
 
3
 import com.reactnativenavigation.controllers.ActivityLifecycleDelegate;
3
 import com.reactnativenavigation.controllers.ActivityLifecycleDelegate;
4
+import com.reactnativenavigation.mocks.TestDevPermissionRequest;
4
 import com.reactnativenavigation.mocks.TestReactNativeHost;
5
 import com.reactnativenavigation.mocks.TestReactNativeHost;
5
 
6
 
6
 public class TestApplication extends NavigationApplication {
7
 public class TestApplication extends NavigationApplication {
12
 
13
 
13
 	@Override
14
 	@Override
14
 	protected Config createConfig() {
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 View File

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
+}