Daniel Zlotin 7 лет назад
Родитель
Сommit
c2d74334fa

+ 0
- 12
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ApplicationLifecycleTest.java Просмотреть файл

@@ -4,7 +4,6 @@ import android.content.Intent;
4 4
 import android.net.Uri;
5 5
 import android.provider.Settings;
6 6
 import android.support.test.uiautomator.By;
7
-import android.view.KeyEvent;
8 7
 
9 8
 import org.junit.FixMethodOrder;
10 9
 import org.junit.Test;
@@ -76,17 +75,6 @@ public class ApplicationLifecycleTest extends BaseTest {
76 75
 		assertMainShown();
77 76
 	}
78 77
 
79
-	@Test
80
-	public void _6_reloadReactNativeApp() throws Exception {
81
-		launchTheApp();
82
-		assertMainShown();
83
-		elementByText("PUSH").click();
84
-		assertExists(By.text("Pushed Screen"));
85
-		device().pressKeyCode(KeyEvent.KEYCODE_R);
86
-		device().pressKeyCode(KeyEvent.KEYCODE_R);
87
-		assertMainShown();
88
-	}
89
-
90 78
 	private void killAppSaveInstanceState_ByTogglingPermissions() throws Exception {
91 79
 		device().pressHome();
92 80
 

+ 42
- 0
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ReactEventsTest.java Просмотреть файл

@@ -0,0 +1,42 @@
1
+package com.reactnativenavigation.e2e.androide2e;
2
+
3
+import android.support.test.uiautomator.By;
4
+import android.view.KeyEvent;
5
+
6
+import org.junit.Test;
7
+
8
+public class ReactEventsTest extends BaseTest {
9
+
10
+	@Test
11
+	public void pressingMenuOpensDevMenu() throws Exception {
12
+		launchTheApp();
13
+		assertMainShown();
14
+		device().pressKeyCode(KeyEvent.KEYCODE_MENU);
15
+		assertExists(By.text("Debug JS Remotely"));
16
+	}
17
+
18
+	@Test
19
+	public void pressingRTwiceInSuccessionReloadsReactNative() throws Exception {
20
+		launchTheApp();
21
+		assertMainShown();
22
+		elementByText("PUSH").click();
23
+		assertExists(By.text("Pushed Screen"));
24
+
25
+		device().pressKeyCode(KeyEvent.KEYCODE_R);
26
+		device().pressKeyCode(KeyEvent.KEYCODE_R);
27
+		assertMainShown();
28
+	}
29
+
30
+	@Test
31
+	public void pressingRTwiceWithDelayDoesNothing() throws Exception {
32
+		launchTheApp();
33
+		assertMainShown();
34
+		elementByText("PUSH").click();
35
+		assertExists(By.text("Pushed Screen"));
36
+
37
+		device().pressKeyCode(KeyEvent.KEYCODE_R);
38
+		Thread.sleep(1000);
39
+		device().pressKeyCode(KeyEvent.KEYCODE_R);
40
+		assertExists(By.text("Pushed Screen"));
41
+	}
42
+}

+ 10
- 10
lib/android/app/src/main/java/com/reactnativenavigation/NavigationActivity.java Просмотреть файл

@@ -4,7 +4,6 @@ import android.os.Bundle;
4 4
 import android.support.annotation.Nullable;
5 5
 import android.support.v7.app.AppCompatActivity;
6 6
 import android.view.KeyEvent;
7
-import android.widget.Toast;
8 7
 
9 8
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
10 9
 import com.reactnativenavigation.viewcontrollers.Navigator;
@@ -15,6 +14,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
15 14
 	@Override
16 15
 	protected void onCreate(@Nullable Bundle savedInstanceState) {
17 16
 		super.onCreate(savedInstanceState);
17
+		app().getReactGateway().onActivityCreated(this);
18 18
 		navigator = new Navigator(this);
19 19
 		navigator.onActivityCreated();
20 20
 	}
@@ -22,18 +22,21 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
22 22
 	@Override
23 23
 	protected void onResume() {
24 24
 		super.onResume();
25
+		app().getReactGateway().onActivityResumed(this);
25 26
 		navigator.onActivityResumed();
26 27
 	}
27 28
 
28 29
 	@Override
29 30
 	protected void onPause() {
30 31
 		super.onPause();
32
+		app().getReactGateway().onActivityPaused(this);
31 33
 		navigator.onActivityPaused();
32 34
 	}
33 35
 
34 36
 	@Override
35 37
 	protected void onDestroy() {
36 38
 		super.onDestroy();
39
+		app().getReactGateway().onActivityDestroyed(this);
37 40
 		navigator.onActivityDestroyed();
38 41
 	}
39 42
 
@@ -49,16 +52,13 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
49 52
 		}
50 53
 	}
51 54
 
52
-	
53 55
 	@Override
54
-	public boolean onKeyMultiple(final int keyCode, final int repeatCount, final KeyEvent event) {
55
-		Toast.makeText(this, "onKeyMultiple", Toast.LENGTH_SHORT).show();
56
-		if (keyCode == KeyEvent.KEYCODE_R && repeatCount == 2) {
57
-			Toast.makeText(this, "RR!!!!", Toast.LENGTH_SHORT).show();
58
-			return true;
59
-		} else {
60
-			return super.onKeyMultiple(keyCode, repeatCount, event);
61
-		}
56
+	public boolean onKeyUp(final int keyCode, final KeyEvent event) {
57
+		return app().getReactGateway().onKeyUp(keyCode) || super.onKeyUp(keyCode, event);
58
+	}
59
+
60
+	private NavigationApplication app() {
61
+		return (NavigationApplication) getApplication();
62 62
 	}
63 63
 
64 64
 	public Navigator getNavigator() {

+ 9
- 51
lib/android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java Просмотреть файл

@@ -1,71 +1,29 @@
1 1
 package com.reactnativenavigation;
2 2
 
3
-import android.app.Activity;
4 3
 import android.app.Application;
5
-import android.os.Bundle;
6 4
 
7 5
 import com.facebook.react.ReactApplication;
8 6
 import com.facebook.react.ReactNativeHost;
9
-import com.reactnativenavigation.react.NavigationReactInitializer;
10
-import com.reactnativenavigation.react.NavigationReactNativeHost;
7
+import com.reactnativenavigation.react.ReactGateway;
11 8
 
12
-public abstract class NavigationApplication extends Application implements ReactApplication, Application.ActivityLifecycleCallbacks {
9
+public abstract class NavigationApplication extends Application implements ReactApplication {
13 10
 
14
-	private NavigationReactNativeHost reactNativeHost;
15
-	private NavigationReactInitializer initializer;
11
+	private ReactGateway reactGateway;
16 12
 
17 13
 	@Override
18 14
 	public void onCreate() {
19 15
 		super.onCreate();
20
-		reactNativeHost = new NavigationReactNativeHost(this, isDebug());
21
-		initializer = new NavigationReactInitializer(reactNativeHost.getReactInstanceManager(), isDebug());
22
-		registerActivityLifecycleCallbacks(this);
16
+		reactGateway = new ReactGateway(this, isDebug());
23 17
 	}
24 18
 
25
-	@Override
26
-	public ReactNativeHost getReactNativeHost() {
27
-		return reactNativeHost;
28
-	}
29
-
30
-	public abstract boolean isDebug();
31
-
32
-	@Override
33
-	public void onActivityCreated(final Activity activity, final Bundle savedInstanceState) {
34
-		if (activity instanceof NavigationActivity) {
35
-			initializer.onActivityCreated((NavigationActivity) activity);
36
-		}
37
-	}
38
-
39
-	@Override
40
-	public void onActivityStarted(final Activity activity) {
19
+	public ReactGateway getReactGateway() {
20
+		return reactGateway;
41 21
 	}
42 22
 
43 23
 	@Override
44
-	public void onActivityResumed(final Activity activity) {
45
-		if (activity instanceof NavigationActivity) {
46
-			initializer.onActivityResumed((NavigationActivity) activity);
47
-		}
48
-	}
49
-
50
-	@Override
51
-	public void onActivityPaused(final Activity activity) {
52
-		if (activity instanceof NavigationActivity) {
53
-			initializer.onActivityPaused((NavigationActivity) activity);
54
-		}
55
-	}
56
-
57
-	@Override
58
-	public void onActivityStopped(final Activity activity) {
59
-	}
60
-
61
-	@Override
62
-	public void onActivitySaveInstanceState(final Activity activity, final Bundle outState) {
24
+	public ReactNativeHost getReactNativeHost() {
25
+		return getReactGateway().getReactNativeHost();
63 26
 	}
64 27
 
65
-	@Override
66
-	public void onActivityDestroyed(final Activity activity) {
67
-		if (activity instanceof NavigationActivity) {
68
-			initializer.onActivityDestroyed((NavigationActivity) activity);
69
-		}
70
-	}
28
+	public abstract boolean isDebug();
71 29
 }

+ 45
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/JsDevReloadHandler.java Просмотреть файл

@@ -0,0 +1,45 @@
1
+package com.reactnativenavigation.react;
2
+
3
+import android.view.KeyEvent;
4
+
5
+import com.facebook.react.ReactInstanceManager;
6
+import com.facebook.react.bridge.ReactContext;
7
+
8
+public class JsDevReloadHandler {
9
+	private final ReactInstanceManager reactInstanceManager;
10
+	private long firstRTimestamp = 0;
11
+
12
+	public JsDevReloadHandler(final ReactInstanceManager reactInstanceManager) {
13
+		this.reactInstanceManager = reactInstanceManager;
14
+		reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
15
+			@Override
16
+			public void onReactContextInitialized(final ReactContext context) {
17
+				//this is too late
18
+			}
19
+		});
20
+	}
21
+
22
+	public boolean onKeyUp(int keyCode) {
23
+		if (!reactInstanceManager.getDevSupportManager().getDevSupportEnabled()) {
24
+			return false;
25
+		}
26
+
27
+		if (keyCode == KeyEvent.KEYCODE_MENU) {
28
+			reactInstanceManager.getDevSupportManager().showDevOptionsDialog();
29
+			return true;
30
+		}
31
+
32
+		if (keyCode == KeyEvent.KEYCODE_R) {
33
+			if (moreThan500MillisSinceLastR()) {
34
+				reactInstanceManager.getDevSupportManager().handleReloadJS();
35
+				return true;
36
+			}
37
+			firstRTimestamp = System.currentTimeMillis();
38
+		}
39
+		return false;
40
+	}
41
+
42
+	private boolean moreThan500MillisSinceLastR() {
43
+		return firstRTimestamp != 0 && System.currentTimeMillis() - firstRTimestamp < 500;
44
+	}
45
+}

+ 40
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/ReactGateway.java Просмотреть файл

@@ -0,0 +1,40 @@
1
+package com.reactnativenavigation.react;
2
+
3
+import com.reactnativenavigation.NavigationActivity;
4
+import com.reactnativenavigation.NavigationApplication;
5
+
6
+public class ReactGateway {
7
+	private final NavigationReactNativeHost reactNativeHost;
8
+	private final NavigationReactInitializer initializer;
9
+	private final JsDevReloadHandler jsDevReloadHandler;
10
+
11
+	public ReactGateway(final NavigationApplication application, final boolean isDebug) {
12
+		reactNativeHost = new NavigationReactNativeHost(application, isDebug);
13
+		initializer = new NavigationReactInitializer(reactNativeHost.getReactInstanceManager(), isDebug);
14
+		jsDevReloadHandler = new JsDevReloadHandler(reactNativeHost.getReactInstanceManager());
15
+	}
16
+
17
+	public NavigationReactNativeHost getReactNativeHost() {
18
+		return reactNativeHost;
19
+	}
20
+
21
+	public void onActivityCreated(NavigationActivity activity) {
22
+		initializer.onActivityCreated(activity);
23
+	}
24
+
25
+	public void onActivityResumed(NavigationActivity activity) {
26
+		initializer.onActivityResumed(activity);
27
+	}
28
+
29
+	public void onActivityPaused(NavigationActivity activity) {
30
+		initializer.onActivityPaused(activity);
31
+	}
32
+
33
+	public void onActivityDestroyed(NavigationActivity activity) {
34
+		initializer.onActivityDestroyed(activity);
35
+	}
36
+
37
+	public boolean onKeyUp(final int keyCode) {
38
+		return jsDevReloadHandler.onKeyUp(keyCode);
39
+	}
40
+}

+ 0
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java Просмотреть файл

@@ -51,7 +51,6 @@ public class Navigator extends ParentController {
51 51
 	}
52 52
 
53 53
 	public void onActivityDestroyed() {
54
-		//
55 54
 	}
56 55
 
57 56
 	@Override