|
@@ -0,0 +1,339 @@
|
|
1
|
+package com.reactnativenavigation.controllers;
|
|
2
|
+
|
|
3
|
+import android.annotation.TargetApi;
|
|
4
|
+import android.content.Intent;
|
|
5
|
+import android.os.Build;
|
|
6
|
+import android.os.Bundle;
|
|
7
|
+import android.support.annotation.Nullable;
|
|
8
|
+import android.support.v7.app.AppCompatActivity;
|
|
9
|
+import android.view.KeyEvent;
|
|
10
|
+
|
|
11
|
+import com.facebook.react.bridge.Callback;
|
|
12
|
+import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
|
|
13
|
+import com.facebook.react.modules.core.PermissionAwareActivity;
|
|
14
|
+import com.facebook.react.modules.core.PermissionListener;
|
|
15
|
+import com.reactnativenavigation.NavigationApplication;
|
|
16
|
+import com.reactnativenavigation.events.Event;
|
|
17
|
+import com.reactnativenavigation.events.EventBus;
|
|
18
|
+import com.reactnativenavigation.events.JsDevReloadEvent;
|
|
19
|
+import com.reactnativenavigation.events.ModalDismissedEvent;
|
|
20
|
+import com.reactnativenavigation.events.Subscriber;
|
|
21
|
+import com.reactnativenavigation.layouts.BottomTabsLayout;
|
|
22
|
+import com.reactnativenavigation.layouts.Layout;
|
|
23
|
+import com.reactnativenavigation.layouts.LayoutFactory;
|
|
24
|
+import com.reactnativenavigation.params.ActivityParams;
|
|
25
|
+import com.reactnativenavigation.params.AppStyle;
|
|
26
|
+import com.reactnativenavigation.params.ContextualMenuParams;
|
|
27
|
+import com.reactnativenavigation.params.FabParams;
|
|
28
|
+import com.reactnativenavigation.params.ScreenParams;
|
|
29
|
+import com.reactnativenavigation.params.SnackbarParams;
|
|
30
|
+import com.reactnativenavigation.params.TitleBarButtonParams;
|
|
31
|
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
|
|
32
|
+import com.reactnativenavigation.react.JsDevReloadHandler;
|
|
33
|
+import com.reactnativenavigation.views.SideMenu.Side;
|
|
34
|
+
|
|
35
|
+import java.util.List;
|
|
36
|
+
|
|
37
|
+public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, Subscriber, PermissionAwareActivity {
|
|
38
|
+
|
|
39
|
+ /**
|
|
40
|
+ * Although we start multiple activities, we make sure to pass Intent.CLEAR_TASK | Intent.NEW_TASK
|
|
41
|
+ * So that we actually have only 1 instance of the activity running at one time.
|
|
42
|
+ * We hold the currentActivity (resume->pause) so we know when we need to destroy the javascript context
|
|
43
|
+ * (when currentActivity is null, ie pause and destroy was called without resume).
|
|
44
|
+ * This is somewhat weird, and in the future we better use a single activity with changing contentView similar to ReactNative impl.
|
|
45
|
+ * Along with that, we should handle commands from the bridge using onNewIntent
|
|
46
|
+ */
|
|
47
|
+ static NavigationActivity currentActivity;
|
|
48
|
+
|
|
49
|
+ private ActivityParams activityParams;
|
|
50
|
+ private ModalController modalController;
|
|
51
|
+ private Layout layout;
|
|
52
|
+ @Nullable private PermissionListener mPermissionListener;
|
|
53
|
+
|
|
54
|
+ @Override
|
|
55
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
56
|
+ super.onCreate(savedInstanceState);
|
|
57
|
+
|
|
58
|
+ if (!NavigationApplication.instance.isReactContextInitialized()) {
|
|
59
|
+ NavigationApplication.instance.startReactContextOnceInBackgroundAndExecuteJS();
|
|
60
|
+ return;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ activityParams = NavigationCommandsHandler.parseActivityParams(getIntent());
|
|
64
|
+
|
|
65
|
+ disableActivityShowAnimationIfNeeded();
|
|
66
|
+ createLayout();
|
|
67
|
+ createModalController();
|
|
68
|
+ NavigationApplication.instance.getActivityCallbacks().onActivityCreated(this, savedInstanceState);
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ private void disableActivityShowAnimationIfNeeded() {
|
|
72
|
+ if (!activityParams.animateShow) {
|
|
73
|
+ overridePendingTransition(0, 0);
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ private void createModalController() {
|
|
78
|
+ modalController = new ModalController(this);
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ private void createLayout() {
|
|
82
|
+ layout = LayoutFactory.create(this, activityParams);
|
|
83
|
+ if (hasBackgroundColor()) {
|
|
84
|
+ layout.asView().setBackgroundColor(AppStyle.appStyle.screenBackgroundColor.getColor());
|
|
85
|
+ }
|
|
86
|
+ setContentView(layout.asView());
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ private boolean hasBackgroundColor() {
|
|
90
|
+ return AppStyle.appStyle.screenBackgroundColor != null &&
|
|
91
|
+ AppStyle.appStyle.screenBackgroundColor.hasColor();
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ @Override
|
|
95
|
+ protected void onStart() {
|
|
96
|
+ super.onStart();
|
|
97
|
+ NavigationApplication.instance.getActivityCallbacks().onActivityStarted(this);
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ @Override
|
|
101
|
+ protected void onResume() {
|
|
102
|
+ super.onResume();
|
|
103
|
+ if (isFinishing() || !NavigationApplication.instance.isReactContextInitialized()) {
|
|
104
|
+ return;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ currentActivity = this;
|
|
108
|
+ setDeepLinkData();
|
|
109
|
+ NavigationApplication.instance.getReactGateway().onResumeActivity(this, this);
|
|
110
|
+ NavigationApplication.instance.getActivityCallbacks().onActivityResumed(this);
|
|
111
|
+ EventBus.instance.register(this);
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ private void setDeepLinkData() {
|
|
115
|
+ if (IntentDataHandler.hasIntentData()) {
|
|
116
|
+ IntentDataHandler.setIntentData(getIntent());
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ @Override
|
|
121
|
+ protected void onPause() {
|
|
122
|
+ super.onPause();
|
|
123
|
+ currentActivity = null;
|
|
124
|
+ NavigationApplication.instance.getReactGateway().onPauseActivity();
|
|
125
|
+ NavigationApplication.instance.getActivityCallbacks().onActivityPaused(this);
|
|
126
|
+ EventBus.instance.unregister(this);
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ @Override
|
|
130
|
+ protected void onStop() {
|
|
131
|
+ super.onStop();
|
|
132
|
+ NavigationApplication.instance.getActivityCallbacks().onActivityStopped(this);
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ @Override
|
|
136
|
+ protected void onDestroy() {
|
|
137
|
+ destroyLayouts();
|
|
138
|
+ destroyJsIfNeeded();
|
|
139
|
+ NavigationApplication.instance.getActivityCallbacks().onActivityDestroyed(this);
|
|
140
|
+ super.onDestroy();
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ private void destroyLayouts() {
|
|
144
|
+ if (modalController != null) {
|
|
145
|
+ modalController.destroy();
|
|
146
|
+ }
|
|
147
|
+ if (layout != null) {
|
|
148
|
+ layout.destroy();
|
|
149
|
+ layout = null;
|
|
150
|
+ }
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ private void destroyJsIfNeeded() {
|
|
154
|
+ if (currentActivity == null || currentActivity.isFinishing()) {
|
|
155
|
+ NavigationApplication.instance.getReactGateway().onDestroyApp();
|
|
156
|
+ }
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ @Override
|
|
160
|
+ public void invokeDefaultOnBackPressed() {
|
|
161
|
+ super.onBackPressed();
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ @Override
|
|
165
|
+ public void onBackPressed() {
|
|
166
|
+ if (layout != null && !layout.onBackPressed()) {
|
|
167
|
+ NavigationApplication.instance.getReactGateway().onBackPressed();
|
|
168
|
+ }
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ @Override
|
|
172
|
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
173
|
+ NavigationApplication.instance.getReactGateway().onActivityResult(requestCode, resultCode, data);
|
|
174
|
+ NavigationApplication.instance.getActivityCallbacks().onActivityResult(requestCode, resultCode, data);
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ @Override
|
|
178
|
+ public boolean onKeyUp(int keyCode, KeyEvent event) {
|
|
179
|
+ return JsDevReloadHandler.onKeyUp(getCurrentFocus(), keyCode) || super.onKeyUp(keyCode, event);
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ void push(ScreenParams params) {
|
|
183
|
+ if (modalController.containsNavigator(params.getNavigatorId())) {
|
|
184
|
+ modalController.push(params);
|
|
185
|
+ } else {
|
|
186
|
+ layout.push(params);
|
|
187
|
+ }
|
|
188
|
+ }
|
|
189
|
+
|
|
190
|
+ void pop(ScreenParams params) {
|
|
191
|
+ if (modalController.containsNavigator(params.getNavigatorId())) {
|
|
192
|
+ modalController.pop(params);
|
|
193
|
+ } else {
|
|
194
|
+ layout.pop(params);
|
|
195
|
+ }
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ void popToRoot(ScreenParams params) {
|
|
199
|
+ if (modalController.containsNavigator(params.getNavigatorId())) {
|
|
200
|
+ modalController.popToRoot(params);
|
|
201
|
+ } else {
|
|
202
|
+ layout.popToRoot(params);
|
|
203
|
+ }
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ void newStack(ScreenParams params) {
|
|
207
|
+ if (modalController.containsNavigator(params.getNavigatorId())) {
|
|
208
|
+ modalController.newStack(params);
|
|
209
|
+ } else {
|
|
210
|
+ layout.newStack(params);
|
|
211
|
+ }
|
|
212
|
+ }
|
|
213
|
+
|
|
214
|
+ void showModal(ScreenParams screenParams) {
|
|
215
|
+ modalController.showModal(screenParams);
|
|
216
|
+ }
|
|
217
|
+
|
|
218
|
+ void dismissTopModal() {
|
|
219
|
+ modalController.dismissTopModal();
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ void dismissAllModals() {
|
|
223
|
+ modalController.dismissAllModals();
|
|
224
|
+ }
|
|
225
|
+
|
|
226
|
+ //TODO all these setters should be combined to something like setStyle
|
|
227
|
+ void setTopBarVisible(String screenInstanceId, boolean hidden, boolean animated) {
|
|
228
|
+ layout.setTopBarVisible(screenInstanceId, hidden, animated);
|
|
229
|
+ modalController.setTopBarVisible(screenInstanceId, hidden, animated);
|
|
230
|
+ }
|
|
231
|
+
|
|
232
|
+ void setBottomTabsVisible(boolean hidden, boolean animated) {
|
|
233
|
+ if (layout instanceof BottomTabsLayout) {
|
|
234
|
+ ((BottomTabsLayout) layout).setBottomTabsVisible(hidden, animated);
|
|
235
|
+ }
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ void setTitleBarTitle(String screenInstanceId, String title) {
|
|
239
|
+ layout.setTitleBarTitle(screenInstanceId, title);
|
|
240
|
+ modalController.setTitleBarTitle(screenInstanceId, title);
|
|
241
|
+ }
|
|
242
|
+
|
|
243
|
+ public void setTitleBarSubtitle(String screenInstanceId, String subtitle) {
|
|
244
|
+ layout.setTitleBarSubtitle(screenInstanceId, subtitle);
|
|
245
|
+ modalController.setTitleBarSubtitle(screenInstanceId, subtitle);
|
|
246
|
+ }
|
|
247
|
+
|
|
248
|
+ void setTitleBarButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
|
|
249
|
+ layout.setTitleBarRightButtons(screenInstanceId, navigatorEventId, titleBarButtons);
|
|
250
|
+ modalController.setTitleBarRightButtons(screenInstanceId, navigatorEventId, titleBarButtons);
|
|
251
|
+ }
|
|
252
|
+
|
|
253
|
+ void setTitleBarLeftButton(String screenInstanceId, String navigatorEventId, TitleBarLeftButtonParams titleBarLeftButton) {
|
|
254
|
+ layout.setTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarLeftButton);
|
|
255
|
+ modalController.setTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarLeftButton);
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ void setScreenFab(String screenInstanceId, String navigatorEventId, FabParams fab) {
|
|
259
|
+ layout.setFab(screenInstanceId, navigatorEventId, fab);
|
|
260
|
+ }
|
|
261
|
+
|
|
262
|
+ public void toggleSideMenuVisible(boolean animated, Side side) {
|
|
263
|
+ layout.toggleSideMenuVisible(animated, side);
|
|
264
|
+ }
|
|
265
|
+
|
|
266
|
+ public void setSideMenuVisible(boolean animated, boolean visible, Side side) {
|
|
267
|
+ layout.setSideMenuVisible(animated, visible, side);
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ public void selectBottomTabByTabIndex(Integer index) {
|
|
271
|
+ if (layout instanceof BottomTabsLayout) {
|
|
272
|
+ ((BottomTabsLayout) layout).selectBottomTabByTabIndex(index);
|
|
273
|
+ }
|
|
274
|
+ }
|
|
275
|
+
|
|
276
|
+ public void selectBottomTabByNavigatorId(String navigatorId) {
|
|
277
|
+ if (layout instanceof BottomTabsLayout) {
|
|
278
|
+ ((BottomTabsLayout) layout).selectBottomTabByNavigatorId(navigatorId);
|
|
279
|
+ }
|
|
280
|
+ }
|
|
281
|
+
|
|
282
|
+ public void setBottomTabBadgeByIndex(Integer index, String badge) {
|
|
283
|
+ if (layout instanceof BottomTabsLayout) {
|
|
284
|
+ ((BottomTabsLayout) layout).setBottomTabBadgeByIndex(index, badge);
|
|
285
|
+ }
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ public void setBottomTabBadgeByNavigatorId(String navigatorId, String badge) {
|
|
289
|
+ if (layout instanceof BottomTabsLayout) {
|
|
290
|
+ ((BottomTabsLayout) layout).setBottomTabBadgeByNavigatorId(navigatorId, badge);
|
|
291
|
+ }
|
|
292
|
+ }
|
|
293
|
+
|
|
294
|
+ public void showSnackbar(SnackbarParams params) {
|
|
295
|
+ layout.showSnackbar(params);
|
|
296
|
+ }
|
|
297
|
+
|
|
298
|
+ public void showContextualMenu(String screenInstanceId, ContextualMenuParams params, Callback onButtonClicked) {
|
|
299
|
+ layout.showContextualMenu(screenInstanceId, params, onButtonClicked);
|
|
300
|
+ modalController.showContextualMenu(screenInstanceId, params, onButtonClicked);
|
|
301
|
+ }
|
|
302
|
+
|
|
303
|
+ public void dismissContextualMenu(String screenInstanceId) {
|
|
304
|
+ layout.dismissContextualMenu(screenInstanceId);
|
|
305
|
+ modalController.dismissContextualMenu(screenInstanceId);
|
|
306
|
+ }
|
|
307
|
+
|
|
308
|
+ @Override
|
|
309
|
+ public void onEvent(Event event) {
|
|
310
|
+ if (event.getType().equals(ModalDismissedEvent.TYPE)) {
|
|
311
|
+ handleModalDismissedEvent();
|
|
312
|
+ } else if (event.getType().equals(JsDevReloadEvent.TYPE)) {
|
|
313
|
+ handleJsDevReloadEvent();
|
|
314
|
+ }
|
|
315
|
+ }
|
|
316
|
+
|
|
317
|
+ private void handleModalDismissedEvent() {
|
|
318
|
+ if (!modalController.isShowing()) {
|
|
319
|
+ layout.onModalDismissed();
|
|
320
|
+ }
|
|
321
|
+ }
|
|
322
|
+
|
|
323
|
+ private void handleJsDevReloadEvent() {
|
|
324
|
+ modalController.destroy();
|
|
325
|
+ layout.destroy();
|
|
326
|
+ }
|
|
327
|
+
|
|
328
|
+ @TargetApi(Build.VERSION_CODES.M)
|
|
329
|
+ public void requestPermissions(String[] permissions, int requestCode, PermissionListener listener) {
|
|
330
|
+ mPermissionListener = listener;
|
|
331
|
+ requestPermissions(permissions, requestCode);
|
|
332
|
+ }
|
|
333
|
+
|
|
334
|
+ public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
335
|
+ if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
|
|
336
|
+ mPermissionListener = null;
|
|
337
|
+ }
|
|
338
|
+ }
|
|
339
|
+}
|