|
@@ -9,123 +9,114 @@ import com.facebook.react.bridge.ReactContext;
|
9
|
9
|
import com.reactnativenavigation.NavigationActivity;
|
10
|
10
|
import com.reactnativenavigation.react.NavigationEventEmitter;
|
11
|
11
|
import com.reactnativenavigation.react.ReactDevPermission;
|
12
|
|
-import com.reactnativenavigation.utils.UiThread;
|
13
|
|
-import com.reactnativenavigation.views.NavigationSplashView;
|
14
|
12
|
|
15
|
|
-import java.util.concurrent.atomic.AtomicLong;
|
|
13
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
16
|
14
|
|
17
|
15
|
public class NavigationActivityLifecycleHandler implements Application.ActivityLifecycleCallbacks {
|
18
|
|
- private interface OnContextCreated {
|
19
|
|
- void onContextCreated(long timeElapsed);
|
20
|
|
- }
|
21
|
|
-
|
22
|
|
- private final ReactInstanceManager reactInstanceManager;
|
23
|
|
-
|
24
|
|
- public NavigationActivityLifecycleHandler(ReactInstanceManager reactInstanceManager) {
|
25
|
|
- this.reactInstanceManager = reactInstanceManager;
|
26
|
|
- }
|
27
|
|
-
|
28
|
|
- @Override
|
29
|
|
- public void onActivityCreated(final Activity activity, Bundle bundle) {
|
30
|
|
- }
|
31
|
|
-
|
32
|
|
- @Override
|
33
|
|
- public void onActivityStarted(Activity activity) {
|
34
|
|
- }
|
35
|
|
-
|
36
|
|
- @Override
|
37
|
|
- public void onActivityResumed(Activity activity) {
|
38
|
|
- if (activity instanceof NavigationActivity) {
|
39
|
|
- handleResumed((NavigationActivity) activity);
|
40
|
|
- }
|
41
|
|
- }
|
42
|
|
-
|
43
|
|
- @Override
|
44
|
|
- public void onActivityPaused(Activity activity) {
|
45
|
|
- if (activity instanceof NavigationActivity) {
|
46
|
|
- handlePaused((NavigationActivity) activity);
|
47
|
|
- }
|
48
|
|
- }
|
49
|
|
-
|
50
|
|
- @Override
|
51
|
|
- public void onActivityStopped(Activity activity) {
|
52
|
|
- }
|
53
|
|
-
|
54
|
|
- @Override
|
55
|
|
- public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
|
56
|
|
- }
|
57
|
|
-
|
58
|
|
- @Override
|
59
|
|
- public void onActivityDestroyed(Activity activity) {
|
60
|
|
- if (activity instanceof NavigationActivity) {
|
61
|
|
- handleDestroyed((NavigationActivity) activity);
|
62
|
|
- }
|
63
|
|
- }
|
64
|
|
-
|
65
|
|
- private void handleResumed(NavigationActivity activity) {
|
66
|
|
- if (ReactDevPermission.shouldAskPermission()) {
|
67
|
|
- ReactDevPermission.askPermission(activity);
|
68
|
|
- } else {
|
69
|
|
- reactInstanceManager.onHostResume(activity, activity);
|
70
|
|
- prepareReactApp(activity);
|
71
|
|
- }
|
72
|
|
- }
|
73
|
|
-
|
74
|
|
- private void prepareReactApp(final NavigationActivity activity) {
|
75
|
|
- if (shouldCreateContext()) {
|
76
|
|
- createReactContext(new OnContextCreated() {
|
77
|
|
- @Override
|
78
|
|
- public void onContextCreated(long timeElapsed) {
|
79
|
|
- emitAppLaunchedAfterDelay(activity.splashMinimumDuration() - timeElapsed);
|
80
|
|
- }
|
81
|
|
- });
|
82
|
|
- } else if (waitingForAppLaunchedEvent(activity)) {
|
83
|
|
- emitAppLaunchedAfterDelay(activity.splashMinimumDuration());
|
84
|
|
- }
|
85
|
|
- }
|
86
|
|
-
|
87
|
|
- /**
|
88
|
|
- * @return true if we are a newly created activity, but react context already exists
|
89
|
|
- */
|
90
|
|
- private boolean waitingForAppLaunchedEvent(NavigationActivity activity) {
|
91
|
|
- return activity.getContentView() instanceof NavigationSplashView;
|
92
|
|
- }
|
93
|
|
-
|
94
|
|
-
|
95
|
|
- private void createReactContext(final OnContextCreated onContextCreated) {
|
96
|
|
- final AtomicLong startTime = new AtomicLong(System.currentTimeMillis());
|
97
|
|
- reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
|
98
|
|
- @Override
|
99
|
|
- public void onReactContextInitialized(final ReactContext context) {
|
100
|
|
- reactInstanceManager.removeReactInstanceEventListener(this);
|
101
|
|
- onContextCreated.onContextCreated(System.currentTimeMillis() - startTime.get());
|
102
|
|
- }
|
103
|
|
- });
|
104
|
|
- reactInstanceManager.createReactContextInBackground();
|
105
|
|
- }
|
106
|
|
-
|
107
|
|
- private boolean shouldCreateContext() {
|
108
|
|
- return !reactInstanceManager.hasStartedCreatingInitialContext();
|
109
|
|
- }
|
110
|
|
-
|
111
|
|
- private void handlePaused(NavigationActivity activity) {
|
112
|
|
- if (reactInstanceManager.hasStartedCreatingInitialContext()) {
|
113
|
|
- reactInstanceManager.onHostPause(activity);
|
114
|
|
- }
|
115
|
|
- }
|
116
|
|
-
|
117
|
|
- private void handleDestroyed(NavigationActivity activity) {
|
118
|
|
- if (reactInstanceManager.hasStartedCreatingInitialContext()) {
|
119
|
|
- reactInstanceManager.onHostDestroy(activity);
|
120
|
|
- }
|
121
|
|
- }
|
122
|
|
-
|
123
|
|
- private void emitAppLaunchedAfterDelay(long delay) {
|
124
|
|
- UiThread.postDelayed(new Runnable() {
|
125
|
|
- @Override
|
126
|
|
- public void run() {
|
127
|
|
- new NavigationEventEmitter(reactInstanceManager.getCurrentReactContext()).emitAppLaunched();
|
128
|
|
- }
|
129
|
|
- }, delay);
|
130
|
|
- }
|
|
16
|
+ private final ReactInstanceManager reactInstanceManager;
|
|
17
|
+ private final AtomicBoolean appLaunchEmitted = new AtomicBoolean(false);
|
|
18
|
+
|
|
19
|
+ public NavigationActivityLifecycleHandler(ReactInstanceManager reactInstanceManager) {
|
|
20
|
+ this.reactInstanceManager = reactInstanceManager;
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ @Override
|
|
24
|
+ public void onActivityCreated(final Activity activity, Bundle bundle) {
|
|
25
|
+ if (activity instanceof NavigationActivity) {
|
|
26
|
+ handleCreated();
|
|
27
|
+ }
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ @Override
|
|
31
|
+ public void onActivityStarted(Activity activity) {
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ @Override
|
|
35
|
+ public void onActivityResumed(Activity activity) {
|
|
36
|
+ if (activity instanceof NavigationActivity) {
|
|
37
|
+ handleResumed((NavigationActivity) activity);
|
|
38
|
+ }
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ @Override
|
|
42
|
+ public void onActivityPaused(Activity activity) {
|
|
43
|
+ if (activity instanceof NavigationActivity) {
|
|
44
|
+ handlePaused((NavigationActivity) activity);
|
|
45
|
+ }
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ @Override
|
|
49
|
+ public void onActivityStopped(Activity activity) {
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ @Override
|
|
53
|
+ public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ @Override
|
|
57
|
+ public void onActivityDestroyed(Activity activity) {
|
|
58
|
+ if (activity instanceof NavigationActivity) {
|
|
59
|
+ handleDestroyed((NavigationActivity) activity);
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ private void handleCreated() {
|
|
64
|
+ appLaunchEmitted.set(false);
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ private void handleResumed(NavigationActivity activity) {
|
|
68
|
+ if (ReactDevPermission.shouldAskPermission()) {
|
|
69
|
+ ReactDevPermission.askPermission(activity);
|
|
70
|
+ } else {
|
|
71
|
+ reactInstanceManager.onHostResume(activity, activity);
|
|
72
|
+ prepareReactApp();
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ private void prepareReactApp() {
|
|
77
|
+ if (shouldCreateContext()) {
|
|
78
|
+ appLaunchEmitted.set(false);
|
|
79
|
+ createReactContext(new Runnable() {
|
|
80
|
+ @Override
|
|
81
|
+ public void run() {
|
|
82
|
+ emitAppLaunchedOnceIfNeeded();
|
|
83
|
+ }
|
|
84
|
+ });
|
|
85
|
+ } else {
|
|
86
|
+ emitAppLaunchedOnceIfNeeded();
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ private void emitAppLaunchedOnceIfNeeded() {
|
|
91
|
+ if (appLaunchEmitted.compareAndSet(false, true)) {
|
|
92
|
+ NavigationEventEmitter.emit(reactInstanceManager.getCurrentReactContext()).appLaunched();
|
|
93
|
+ }
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ private void createReactContext(final Runnable onComplete) {
|
|
97
|
+ reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
|
|
98
|
+ @Override
|
|
99
|
+ public void onReactContextInitialized(final ReactContext context) {
|
|
100
|
+ reactInstanceManager.removeReactInstanceEventListener(this);
|
|
101
|
+ onComplete.run();
|
|
102
|
+ }
|
|
103
|
+ });
|
|
104
|
+ reactInstanceManager.createReactContextInBackground();
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ private boolean shouldCreateContext() {
|
|
108
|
+ return !reactInstanceManager.hasStartedCreatingInitialContext();
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ private void handlePaused(NavigationActivity activity) {
|
|
112
|
+ if (reactInstanceManager.hasStartedCreatingInitialContext()) {
|
|
113
|
+ reactInstanceManager.onHostPause(activity);
|
|
114
|
+ }
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ private void handleDestroyed(NavigationActivity activity) {
|
|
118
|
+ if (reactInstanceManager.hasStartedCreatingInitialContext()) {
|
|
119
|
+ reactInstanceManager.onHostDestroy(activity);
|
|
120
|
+ }
|
|
121
|
+ }
|
131
|
122
|
}
|