You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.native.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // @flow
  2. import React from 'react';
  3. import { SafeAreaProvider } from 'react-native-safe-area-context';
  4. import SplashScreen from 'react-native-splash-screen';
  5. import { DialogContainer } from '../../base/dialog';
  6. import { updateFlags } from '../../base/flags/actions';
  7. import { CALL_INTEGRATION_ENABLED, SERVER_URL_CHANGE_ENABLED } from '../../base/flags/constants';
  8. import { getFeatureFlag } from '../../base/flags/functions';
  9. import { Platform } from '../../base/react';
  10. import { DimensionsDetector, clientResized, setSafeAreaInsets } from '../../base/responsive-ui';
  11. import { updateSettings } from '../../base/settings';
  12. import { _getRouteToRender } from '../getRouteToRender.native';
  13. import logger from '../logger';
  14. import { AbstractApp } from './AbstractApp';
  15. import type { Props as AbstractAppProps } from './AbstractApp';
  16. // Register middlewares and reducers.
  17. import '../middlewares';
  18. import '../reducers';
  19. declare var __DEV__;
  20. /**
  21. * The type of React {@code Component} props of {@link App}.
  22. */
  23. type Props = AbstractAppProps & {
  24. /**
  25. * Identifier for this app on the native side.
  26. */
  27. externalAPIScope: string,
  28. /**
  29. * An object with the feature flags.
  30. */
  31. flags: Object,
  32. /**
  33. * An object with user information (display name, email, avatar URL).
  34. */
  35. userInfo: ?Object
  36. };
  37. /**
  38. * Root app {@code Component} on mobile/React Native.
  39. *
  40. * @augments AbstractApp
  41. */
  42. export class App extends AbstractApp {
  43. /**
  44. * The deferred for the initialisation {{promise, resolve, reject}}.
  45. */
  46. _init: Object;
  47. /**
  48. * Initializes a new {@code App} instance.
  49. *
  50. * @param {Props} props - The read-only React {@code Component} props with
  51. * which the new instance is to be initialized.
  52. */
  53. constructor(props: Props) {
  54. super(props);
  55. // In the Release configuration, React Native will (intentionally) throw
  56. // an unhandled JavascriptException for an unhandled JavaScript error.
  57. // This will effectively kill the app. In accord with the Web, do not
  58. // kill the app.
  59. this._maybeDisableExceptionsManager();
  60. // Bind event handler so it is only bound once per instance.
  61. this._onDimensionsChanged = this._onDimensionsChanged.bind(this);
  62. this._onSafeAreaInsetsChanged = this._onSafeAreaInsetsChanged.bind(this);
  63. }
  64. /**
  65. * Initializes the color scheme.
  66. *
  67. * @inheritdoc
  68. *
  69. * @returns {void}
  70. */
  71. async componentDidMount() {
  72. await super.componentDidMount();
  73. SplashScreen.hide();
  74. }
  75. /**
  76. * Initializes feature flags and updates settings.
  77. *
  78. * @returns {void}
  79. */
  80. async _extraInit() {
  81. const { dispatch, getState } = this.state.store;
  82. // We set these early enough so then we avoid any unnecessary re-renders.
  83. dispatch(updateFlags(this.props.flags));
  84. const route = await _getRouteToRender();
  85. // We need the root navigator to be set early.
  86. await this._navigate(route);
  87. // HACK ALERT!
  88. // Wait until the root navigator is ready.
  89. // We really need to break the inheritance relationship between App,
  90. // AbstractApp and BaseApp, it's very inflexible and cumbersome right now.
  91. const rootNavigationReady = new Promise(resolve => {
  92. const i = setInterval(() => {
  93. const { ready } = getState()['features/app'] || {};
  94. if (ready) {
  95. clearInterval(i);
  96. resolve();
  97. }
  98. }, 50);
  99. });
  100. await rootNavigationReady;
  101. // Check if serverURL is configured externally and not allowed to change.
  102. const serverURLChangeEnabled = getFeatureFlag(getState(), SERVER_URL_CHANGE_ENABLED, true);
  103. if (!serverURLChangeEnabled) {
  104. // As serverURL is provided externally, so we push it to settings.
  105. if (typeof this.props.url !== 'undefined') {
  106. const { serverURL } = this.props.url;
  107. if (typeof serverURL !== 'undefined') {
  108. dispatch(updateSettings({ serverURL }));
  109. }
  110. }
  111. }
  112. dispatch(updateSettings(this.props.userInfo || {}));
  113. // Update settings with feature-flag.
  114. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  115. if (typeof callIntegrationEnabled !== 'undefined') {
  116. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  117. }
  118. }
  119. /**
  120. * Overrides the parent method to inject {@link DimensionsDetector} as
  121. * the top most component.
  122. *
  123. * @override
  124. */
  125. _createMainElement(component, props) {
  126. return (
  127. <SafeAreaProvider>
  128. <DimensionsDetector
  129. onDimensionsChanged = { this._onDimensionsChanged }
  130. onSafeAreaInsetsChanged = { this._onSafeAreaInsetsChanged }>
  131. { super._createMainElement(component, props) }
  132. </DimensionsDetector>
  133. </SafeAreaProvider>
  134. );
  135. }
  136. /**
  137. * Attempts to disable the use of React Native
  138. * {@link ExceptionsManager#handleException} on platforms and in
  139. * configurations on/in which the use of the method in questions has been
  140. * determined to be undesirable. For example, React Native will
  141. * (intentionally) throw an unhandled {@code JavascriptException} for an
  142. * unhandled JavaScript error in the Release configuration. This will
  143. * effectively kill the app. In accord with the Web, do not kill the app.
  144. *
  145. * @private
  146. * @returns {void}
  147. */
  148. _maybeDisableExceptionsManager() {
  149. if (__DEV__) {
  150. // As mentioned above, only the Release configuration was observed
  151. // to suffer.
  152. return;
  153. }
  154. if (Platform.OS !== 'android') {
  155. // A solution based on RTCSetFatalHandler was implemented on iOS and
  156. // it is preferred because it is at a later step of the
  157. // error/exception handling and it is specific to fatal
  158. // errors/exceptions which were observed to kill the app. The
  159. // solution implemented below was tested on Android only so it is
  160. // considered safest to use it there only.
  161. return;
  162. }
  163. const oldHandler = global.ErrorUtils.getGlobalHandler();
  164. const newHandler = _handleException;
  165. if (!oldHandler || oldHandler !== newHandler) {
  166. newHandler.next = oldHandler;
  167. global.ErrorUtils.setGlobalHandler(newHandler);
  168. }
  169. }
  170. _onDimensionsChanged: (width: number, height: number) => void;
  171. /**
  172. * Updates the known available size for the app to occupy.
  173. *
  174. * @param {number} width - The component's current width.
  175. * @param {number} height - The component's current height.
  176. * @private
  177. * @returns {void}
  178. */
  179. _onDimensionsChanged(width: number, height: number) {
  180. const { dispatch } = this.state.store;
  181. dispatch(clientResized(width, height));
  182. }
  183. /**
  184. * Updates the safe are insets values.
  185. *
  186. * @param {Object} insets - The insets.
  187. * @param {number} insets.top - The top inset.
  188. * @param {number} insets.right - The right inset.
  189. * @param {number} insets.bottom - The bottom inset.
  190. * @param {number} insets.left - The left inset.
  191. * @private
  192. * @returns {void}
  193. */
  194. _onSafeAreaInsetsChanged(insets) {
  195. const { dispatch } = this.state.store;
  196. dispatch(setSafeAreaInsets(insets));
  197. }
  198. /**
  199. * Renders the platform specific dialog container.
  200. *
  201. * @returns {React$Element}
  202. */
  203. _renderDialogContainer() {
  204. return (
  205. <DialogContainer />
  206. );
  207. }
  208. }
  209. /**
  210. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  211. * from converting a fatal error into an unhandled native exception which will
  212. * kill the app.
  213. *
  214. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  215. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  216. * otherwise, {@code false}.
  217. * @private
  218. * @returns {void}
  219. */
  220. function _handleException(error, fatal) {
  221. if (fatal) {
  222. // In the Release configuration, React Native will (intentionally) throw
  223. // an unhandled JavascriptException for an unhandled JavaScript error.
  224. // This will effectively kill the app. In accord with the Web, do not
  225. // kill the app.
  226. logger.error(error);
  227. } else {
  228. // Forward to the next globalHandler of ErrorUtils.
  229. const { next } = _handleException;
  230. typeof next === 'function' && next(error, fatal);
  231. }
  232. }