react-native-navigation的迁移库

NavigationActivity.java 12KB

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