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

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