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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // @flow
  2. import React from 'react';
  3. import '../../analytics';
  4. import '../../authentication';
  5. import { setColorScheme } from '../../base/color-scheme';
  6. import { DialogContainer } from '../../base/dialog';
  7. import { CALL_INTEGRATION_ENABLED, updateFlags } from '../../base/flags';
  8. import '../../base/jwt';
  9. import { Platform } from '../../base/react';
  10. import '../../base/responsive-ui';
  11. import { updateSettings } from '../../base/settings';
  12. import '../../google-api';
  13. import { HelpView } from '../../help';
  14. import '../../mobile/audio-mode';
  15. import '../../mobile/back-button';
  16. import '../../mobile/background';
  17. import '../../mobile/call-integration';
  18. import '../../mobile/external-api';
  19. import '../../mobile/full-screen';
  20. import '../../mobile/permissions';
  21. import '../../mobile/picture-in-picture';
  22. import '../../mobile/proximity';
  23. import '../../mobile/wake-lock';
  24. import '../../mobile/watchos';
  25. import logger from '../logger';
  26. import { AbstractApp } from './AbstractApp';
  27. import type { Props as AbstractAppProps } from './AbstractApp';
  28. declare var __DEV__;
  29. /**
  30. * The type of React {@code Component} props of {@link App}.
  31. */
  32. type Props = AbstractAppProps & {
  33. /**
  34. * An object of colors that override the default colors of the app/sdk.
  35. */
  36. colorScheme: ?Object,
  37. /**
  38. * Identifier for this app on the native side.
  39. */
  40. externalAPIScope: string,
  41. /**
  42. * An object with the feature flags.
  43. */
  44. flags: Object,
  45. /**
  46. * An object with user information (display name, email, avatar URL).
  47. */
  48. userInfo: ?Object
  49. };
  50. /**
  51. * Root app {@code Component} on mobile/React Native.
  52. *
  53. * @extends AbstractApp
  54. */
  55. export class App extends AbstractApp {
  56. _init: Promise<*>;
  57. /**
  58. * Initializes a new {@code App} instance.
  59. *
  60. * @param {Props} props - The read-only React {@code Component} props with
  61. * which the new instance is to be initialized.
  62. */
  63. constructor(props: Props) {
  64. super(props);
  65. // In the Release configuration, React Native will (intentionally) throw
  66. // an unhandled JavascriptException for an unhandled JavaScript error.
  67. // This will effectively kill the app. In accord with the Web, do not
  68. // kill the app.
  69. this._maybeDisableExceptionsManager();
  70. }
  71. /**
  72. * Initializes the color scheme.
  73. *
  74. * @inheritdoc
  75. *
  76. * @returns {void}
  77. */
  78. componentDidMount() {
  79. super.componentDidMount();
  80. this._init.then(() => {
  81. // We set these early enough so then we avoid any unnecessary re-renders.
  82. const { dispatch } = this.state.store;
  83. dispatch(setColorScheme(this.props.colorScheme));
  84. dispatch(updateFlags(this.props.flags));
  85. dispatch(updateSettings(this.props.userInfo || {}));
  86. // Update settings with feature-flag.
  87. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  88. if (typeof callIntegrationEnabled !== 'undefined') {
  89. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  90. }
  91. });
  92. }
  93. /**
  94. * Renders platform specific extra elements to be added alongside with the main element, if need be.
  95. *
  96. * @inheritdoc
  97. */
  98. _createExtraPlatformSpecificElement() {
  99. return (
  100. <HelpView />
  101. );
  102. }
  103. /**
  104. * Attempts to disable the use of React Native
  105. * {@link ExceptionsManager#handleException} on platforms and in
  106. * configurations on/in which the use of the method in questions has been
  107. * determined to be undesirable. For example, React Native will
  108. * (intentionally) throw an unhandled {@code JavascriptException} for an
  109. * unhandled JavaScript error in the Release configuration. This will
  110. * effectively kill the app. In accord with the Web, do not kill the app.
  111. *
  112. * @private
  113. * @returns {void}
  114. */
  115. _maybeDisableExceptionsManager() {
  116. if (__DEV__) {
  117. // As mentioned above, only the Release configuration was observed
  118. // to suffer.
  119. return;
  120. }
  121. if (Platform.OS !== 'android') {
  122. // A solution based on RTCSetFatalHandler was implemented on iOS and
  123. // it is preferred because it is at a later step of the
  124. // error/exception handling and it is specific to fatal
  125. // errors/exceptions which were observed to kill the app. The
  126. // solution implemented bellow was tested on Android only so it is
  127. // considered safest to use it there only.
  128. return;
  129. }
  130. const oldHandler = global.ErrorUtils.getGlobalHandler();
  131. const newHandler = _handleException;
  132. if (!oldHandler || oldHandler !== newHandler) {
  133. newHandler.next = oldHandler;
  134. global.ErrorUtils.setGlobalHandler(newHandler);
  135. }
  136. }
  137. /**
  138. * Renders the platform specific dialog container.
  139. *
  140. * @returns {React$Element}
  141. */
  142. _renderDialogContainer() {
  143. return (
  144. <DialogContainer />
  145. );
  146. }
  147. }
  148. /**
  149. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  150. * from converting a fatal error into an unhandled native exception which will
  151. * kill the app.
  152. *
  153. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  154. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  155. * otherwise, {@code false}.
  156. * @private
  157. * @returns {void}
  158. */
  159. function _handleException(error, fatal) {
  160. if (fatal) {
  161. // In the Release configuration, React Native will (intentionally) throw
  162. // an unhandled JavascriptException for an unhandled JavaScript error.
  163. // This will effectively kill the app. In accord with the Web, do not
  164. // kill the app.
  165. logger.error(error);
  166. } else {
  167. // Forward to the next globalHandler of ErrorUtils.
  168. const { next } = _handleException;
  169. typeof next === 'function' && next(error, fatal);
  170. }
  171. }