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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import React from 'react';
  2. import { NativeModules, Platform, StyleSheet, View } from 'react-native';
  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 BottomSheetContainer from '../../base/dialog/components/native/BottomSheetContainer';
  7. import { updateFlags } from '../../base/flags/actions';
  8. import { CALL_INTEGRATION_ENABLED, SERVER_URL_CHANGE_ENABLED } from '../../base/flags/constants';
  9. import { getFeatureFlag } from '../../base/flags/functions';
  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. const { AppInfo } = NativeModules;
  21. const DialogContainerWrapper = Platform.select({
  22. default: View
  23. });
  24. /**
  25. * The type of React {@code Component} props of {@link App}.
  26. */
  27. type Props = AbstractAppProps & {
  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. const liteTxt = AppInfo.isLiteSDK ? ' (lite)' : '';
  75. logger.info(`Loaded SDK ${AppInfo.sdkVersion}${liteTxt}`);
  76. }
  77. /**
  78. * Initializes feature flags and updates settings.
  79. *
  80. * @returns {void}
  81. */
  82. async _extraInit() {
  83. const { dispatch, getState } = this.state.store;
  84. // We set these early enough so then we avoid any unnecessary re-renders.
  85. dispatch(updateFlags(this.props.flags));
  86. const route = await _getRouteToRender();
  87. // We need the root navigator to be set early.
  88. await this._navigate(route);
  89. // HACK ALERT!
  90. // Wait until the root navigator is ready.
  91. // We really need to break the inheritance relationship between App,
  92. // AbstractApp and BaseApp, it's very inflexible and cumbersome right now.
  93. const rootNavigationReady = new Promise(resolve => {
  94. const i = setInterval(() => {
  95. const { ready } = getState()['features/app'] || {};
  96. if (ready) {
  97. clearInterval(i);
  98. resolve();
  99. }
  100. }, 50);
  101. });
  102. await rootNavigationReady;
  103. // Check if serverURL is configured externally and not allowed to change.
  104. const serverURLChangeEnabled = getFeatureFlag(getState(), SERVER_URL_CHANGE_ENABLED, true);
  105. if (!serverURLChangeEnabled) {
  106. // As serverURL is provided externally, so we push it to settings.
  107. if (typeof this.props.url !== 'undefined') {
  108. const { serverURL } = this.props.url;
  109. if (typeof serverURL !== 'undefined') {
  110. dispatch(updateSettings({ serverURL }));
  111. }
  112. }
  113. }
  114. dispatch(updateSettings(this.props.userInfo || {}));
  115. // Update settings with feature-flag.
  116. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  117. if (typeof callIntegrationEnabled !== 'undefined') {
  118. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  119. }
  120. }
  121. /**
  122. * Overrides the parent method to inject {@link DimensionsDetector} as
  123. * the top most component.
  124. *
  125. * @override
  126. */
  127. _createMainElement(component, props) {
  128. return (
  129. <SafeAreaProvider>
  130. <DimensionsDetector
  131. onDimensionsChanged = { this._onDimensionsChanged }
  132. onSafeAreaInsetsChanged = { this._onSafeAreaInsetsChanged }>
  133. { super._createMainElement(component, props) }
  134. </DimensionsDetector>
  135. </SafeAreaProvider>
  136. );
  137. }
  138. /**
  139. * Attempts to disable the use of React Native
  140. * {@link ExceptionsManager#handleException} on platforms and in
  141. * configurations on/in which the use of the method in questions has been
  142. * determined to be undesirable. For example, React Native will
  143. * (intentionally) throw an unhandled {@code JavascriptException} for an
  144. * unhandled JavaScript error in the Release configuration. This will
  145. * effectively kill the app. In accord with the Web, do not kill the app.
  146. *
  147. * @private
  148. * @returns {void}
  149. */
  150. _maybeDisableExceptionsManager() {
  151. if (__DEV__) {
  152. // As mentioned above, only the Release configuration was observed
  153. // to suffer.
  154. return;
  155. }
  156. if (Platform.OS !== 'android') {
  157. // A solution based on RTCSetFatalHandler was implemented on iOS and
  158. // it is preferred because it is at a later step of the
  159. // error/exception handling and it is specific to fatal
  160. // errors/exceptions which were observed to kill the app. The
  161. // solution implemented below was tested on Android only so it is
  162. // considered safest to use it there only.
  163. return;
  164. }
  165. const oldHandler = global.ErrorUtils.getGlobalHandler();
  166. const newHandler = _handleException;
  167. if (!oldHandler || oldHandler !== newHandler) {
  168. newHandler.next = oldHandler;
  169. global.ErrorUtils.setGlobalHandler(newHandler);
  170. }
  171. }
  172. _onDimensionsChanged: (width: number, height: number) => void;
  173. /**
  174. * Updates the known available size for the app to occupy.
  175. *
  176. * @param {number} width - The component's current width.
  177. * @param {number} height - The component's current height.
  178. * @private
  179. * @returns {void}
  180. */
  181. _onDimensionsChanged(width: number, height: number) {
  182. const { dispatch } = this.state.store;
  183. dispatch(clientResized(width, height));
  184. }
  185. /**
  186. * Updates the safe are insets values.
  187. *
  188. * @param {Object} insets - The insets.
  189. * @param {number} insets.top - The top inset.
  190. * @param {number} insets.right - The right inset.
  191. * @param {number} insets.bottom - The bottom inset.
  192. * @param {number} insets.left - The left inset.
  193. * @private
  194. * @returns {void}
  195. */
  196. _onSafeAreaInsetsChanged(insets) {
  197. const { dispatch } = this.state.store;
  198. dispatch(setSafeAreaInsets(insets));
  199. }
  200. /**
  201. * Renders the platform specific dialog container.
  202. *
  203. * @returns {React$Element}
  204. */
  205. _renderDialogContainer() {
  206. return (
  207. <DialogContainerWrapper
  208. pointerEvents = 'box-none'
  209. style = { StyleSheet.absoluteFill }>
  210. <BottomSheetContainer />
  211. <DialogContainer />
  212. </DialogContainerWrapper>
  213. );
  214. }
  215. }
  216. /**
  217. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  218. * from converting a fatal error into an unhandled native exception which will
  219. * kill the app.
  220. *
  221. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  222. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  223. * otherwise, {@code false}.
  224. * @private
  225. * @returns {void}
  226. */
  227. function _handleException(error, fatal) {
  228. if (fatal) {
  229. // In the Release configuration, React Native will (intentionally) throw
  230. // an unhandled JavascriptException for an unhandled JavaScript error.
  231. // This will effectively kill the app. In accord with the Web, do not
  232. // kill the app.
  233. logger.error(error);
  234. } else {
  235. // Forward to the next globalHandler of ErrorUtils.
  236. const { next } = _handleException;
  237. typeof next === 'function' && next(error, fatal);
  238. }
  239. }