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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // @flow
  2. import React from 'react';
  3. import SplashScreen from 'react-native-splash-screen';
  4. import { setColorScheme } from '../../base/color-scheme';
  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 } from '../../base/responsive-ui';
  11. import { updateSettings } from '../../base/settings';
  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. * @extends AbstractApp
  44. */
  45. export class App extends AbstractApp {
  46. _init: Promise<*>;
  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. window.app_t = this
  63. }
  64. /**
  65. * Initializes the color scheme.
  66. *
  67. * @inheritdoc
  68. *
  69. * @returns {void}
  70. */
  71. componentDidMount() {
  72. super.componentDidMount();
  73. SplashScreen.hide();
  74. this._init.then(() => {
  75. const { dispatch, getState } = this.state.store;
  76. // We set these early enough so then we avoid any unnecessary re-renders.
  77. dispatch(setColorScheme(this.props.colorScheme));
  78. dispatch(updateFlags(this.props.flags));
  79. // Check if serverURL is configured externally and not allowed to change.
  80. const serverURLChangeEnabled = getFeatureFlag(getState(), SERVER_URL_CHANGE_ENABLED, true);
  81. if (!serverURLChangeEnabled) {
  82. // As serverURL is provided externally, so we push it to settings.
  83. if (typeof this.props.url !== 'undefined') {
  84. const { serverURL } = this.props.url;
  85. if (typeof serverURL !== 'undefined') {
  86. dispatch(updateSettings({ serverURL }));
  87. }
  88. }
  89. }
  90. dispatch(updateSettings(this.props.userInfo || {}));
  91. // Update settings with feature-flag.
  92. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  93. if (typeof callIntegrationEnabled !== 'undefined') {
  94. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  95. }
  96. });
  97. }
  98. /**
  99. * Overrides the parent method to inject {@link DimensionsDetector} as
  100. * the top most component.
  101. *
  102. * @override
  103. */
  104. _createMainElement(component, props) {
  105. return (
  106. <DimensionsDetector
  107. onDimensionsChanged = { this._onDimensionsChanged }>
  108. { super._createMainElement(component, props) }
  109. </DimensionsDetector>
  110. );
  111. }
  112. /**
  113. * Attempts to disable the use of React Native
  114. * {@link ExceptionsManager#handleException} on platforms and in
  115. * configurations on/in which the use of the method in questions has been
  116. * determined to be undesirable. For example, React Native will
  117. * (intentionally) throw an unhandled {@code JavascriptException} for an
  118. * unhandled JavaScript error in the Release configuration. This will
  119. * effectively kill the app. In accord with the Web, do not kill the app.
  120. *
  121. * @private
  122. * @returns {void}
  123. */
  124. _maybeDisableExceptionsManager() {
  125. if (__DEV__) {
  126. // As mentioned above, only the Release configuration was observed
  127. // to suffer.
  128. return;
  129. }
  130. if (Platform.OS !== 'android') {
  131. // A solution based on RTCSetFatalHandler was implemented on iOS and
  132. // it is preferred because it is at a later step of the
  133. // error/exception handling and it is specific to fatal
  134. // errors/exceptions which were observed to kill the app. The
  135. // solution implemented below was tested on Android only so it is
  136. // considered safest to use it there only.
  137. return;
  138. }
  139. const oldHandler = global.ErrorUtils.getGlobalHandler();
  140. const newHandler = _handleException;
  141. if (!oldHandler || oldHandler !== newHandler) {
  142. newHandler.next = oldHandler;
  143. global.ErrorUtils.setGlobalHandler(newHandler);
  144. }
  145. }
  146. _onDimensionsChanged: (width: number, height: number) => void;
  147. /**
  148. * Updates the known available size for the app to occupy.
  149. *
  150. * @param {number} width - The component's current width.
  151. * @param {number} height - The component's current height.
  152. * @private
  153. * @returns {void}
  154. */
  155. _onDimensionsChanged(width: number, height: number) {
  156. const { dispatch } = this.state.store;
  157. dispatch(clientResized(width, height));
  158. }
  159. /**
  160. * Renders the platform specific dialog container.
  161. *
  162. * @returns {React$Element}
  163. */
  164. _renderDialogContainer() {
  165. return (
  166. <DialogContainer />
  167. );
  168. }
  169. }
  170. /**
  171. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  172. * from converting a fatal error into an unhandled native exception which will
  173. * kill the app.
  174. *
  175. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  176. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  177. * otherwise, {@code false}.
  178. * @private
  179. * @returns {void}
  180. */
  181. function _handleException(error, fatal) {
  182. if (fatal) {
  183. // In the Release configuration, React Native will (intentionally) throw
  184. // an unhandled JavascriptException for an unhandled JavaScript error.
  185. // This will effectively kill the app. In accord with the Web, do not
  186. // kill the app.
  187. logger.error(error);
  188. } else {
  189. // Forward to the next globalHandler of ErrorUtils.
  190. const { next } = _handleException;
  191. typeof next === 'function' && next(error, fatal);
  192. }
  193. }