您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

App.native.js 6.2KB

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