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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 JitsiThemePaperProvider from '../../base/ui/components/JitsiThemeProvider.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. * An object of colors that override the default colors of the app/sdk.
  26. */
  27. colorScheme: ?Object,
  28. /**
  29. * Identifier for this app on the native side.
  30. */
  31. externalAPIScope: string,
  32. /**
  33. * An object with the feature flags.
  34. */
  35. flags: Object,
  36. /**
  37. * An object with user information (display name, email, avatar URL).
  38. */
  39. userInfo: ?Object
  40. };
  41. /**
  42. * Root app {@code Component} on mobile/React Native.
  43. *
  44. * @extends AbstractApp
  45. */
  46. export class App extends AbstractApp {
  47. _init: Promise<*>;
  48. /**
  49. * Initializes a new {@code App} instance.
  50. *
  51. * @param {Props} props - The read-only React {@code Component} props with
  52. * which the new instance is to be initialized.
  53. */
  54. constructor(props: Props) {
  55. super(props);
  56. // In the Release configuration, React Native will (intentionally) throw
  57. // an unhandled JavascriptException for an unhandled JavaScript error.
  58. // This will effectively kill the app. In accord with the Web, do not
  59. // kill the app.
  60. this._maybeDisableExceptionsManager();
  61. // Bind event handler so it is only bound once per instance.
  62. this._onDimensionsChanged = this._onDimensionsChanged.bind(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. <JitsiThemePaperProvider>
  107. <DimensionsDetector
  108. onDimensionsChanged = { this._onDimensionsChanged }>
  109. { super._createMainElement(component, props) }
  110. </DimensionsDetector>
  111. </JitsiThemePaperProvider>
  112. );
  113. }
  114. /**
  115. * Attempts to disable the use of React Native
  116. * {@link ExceptionsManager#handleException} on platforms and in
  117. * configurations on/in which the use of the method in questions has been
  118. * determined to be undesirable. For example, React Native will
  119. * (intentionally) throw an unhandled {@code JavascriptException} for an
  120. * unhandled JavaScript error in the Release configuration. This will
  121. * effectively kill the app. In accord with the Web, do not kill the app.
  122. *
  123. * @private
  124. * @returns {void}
  125. */
  126. _maybeDisableExceptionsManager() {
  127. if (__DEV__) {
  128. // As mentioned above, only the Release configuration was observed
  129. // to suffer.
  130. return;
  131. }
  132. if (Platform.OS !== 'android') {
  133. // A solution based on RTCSetFatalHandler was implemented on iOS and
  134. // it is preferred because it is at a later step of the
  135. // error/exception handling and it is specific to fatal
  136. // errors/exceptions which were observed to kill the app. The
  137. // solution implemented below was tested on Android only so it is
  138. // considered safest to use it there only.
  139. return;
  140. }
  141. const oldHandler = global.ErrorUtils.getGlobalHandler();
  142. const newHandler = _handleException;
  143. if (!oldHandler || oldHandler !== newHandler) {
  144. newHandler.next = oldHandler;
  145. global.ErrorUtils.setGlobalHandler(newHandler);
  146. }
  147. }
  148. _onDimensionsChanged: (width: number, height: number) => void;
  149. /**
  150. * Updates the known available size for the app to occupy.
  151. *
  152. * @param {number} width - The component's current width.
  153. * @param {number} height - The component's current height.
  154. * @private
  155. * @returns {void}
  156. */
  157. _onDimensionsChanged(width: number, height: number) {
  158. const { dispatch } = this.state.store;
  159. dispatch(clientResized(width, height));
  160. }
  161. /**
  162. * Renders the platform specific dialog container.
  163. *
  164. * @returns {React$Element}
  165. */
  166. _renderDialogContainer() {
  167. return (
  168. <DialogContainer />
  169. );
  170. }
  171. }
  172. /**
  173. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  174. * from converting a fatal error into an unhandled native exception which will
  175. * kill the app.
  176. *
  177. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  178. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  179. * otherwise, {@code false}.
  180. * @private
  181. * @returns {void}
  182. */
  183. function _handleException(error, fatal) {
  184. if (fatal) {
  185. // In the Release configuration, React Native will (intentionally) throw
  186. // an unhandled JavascriptException for an unhandled JavaScript error.
  187. // This will effectively kill the app. In accord with the Web, do not
  188. // kill the app.
  189. logger.error(error);
  190. } else {
  191. // Forward to the next globalHandler of ErrorUtils.
  192. const { next } = _handleException;
  193. typeof next === 'function' && next(error, fatal);
  194. }
  195. }