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

App.native.tsx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import React, { ComponentType } from 'react';
  2. import { NativeModules, Platform, StyleSheet, View } from 'react-native';
  3. import DeviceInfo from 'react-native-device-info';
  4. import { SafeAreaProvider } from 'react-native-safe-area-context';
  5. import SplashScreen from 'react-native-splash-screen';
  6. import BottomSheetContainer from '../../base/dialog/components/native/BottomSheetContainer';
  7. import DialogContainer from '../../base/dialog/components/native/DialogContainer';
  8. import { updateFlags } from '../../base/flags/actions';
  9. import { CALL_INTEGRATION_ENABLED } from '../../base/flags/constants';
  10. import { clientResized, setSafeAreaInsets } from '../../base/responsive-ui/actions';
  11. import DimensionsDetector from '../../base/responsive-ui/components/DimensionsDetector.native';
  12. import { updateSettings } from '../../base/settings/actions';
  13. import JitsiThemePaperProvider from '../../base/ui/components/JitsiThemeProvider.native';
  14. import { isEmbedded } from '../../base/util/embedUtils.native';
  15. import { _getRouteToRender } from '../getRouteToRender.native';
  16. import logger from '../logger';
  17. import { AbstractApp, IProps as AbstractAppProps } from './AbstractApp';
  18. // Register middlewares and reducers.
  19. import '../middlewares.native';
  20. import '../reducers.native';
  21. declare let __DEV__: any;
  22. const { AppInfo } = NativeModules;
  23. const DialogContainerWrapper = Platform.select({
  24. default: View
  25. });
  26. /**
  27. * The type of React {@code Component} props of {@link App}.
  28. */
  29. interface IProps extends AbstractAppProps {
  30. /**
  31. * An object with the feature flags.
  32. */
  33. flags: any;
  34. /**
  35. * An object with user information (display name, email, avatar URL).
  36. */
  37. userInfo?: Object;
  38. }
  39. /**
  40. * Root app {@code Component} on mobile/React Native.
  41. *
  42. * @augments AbstractApp
  43. */
  44. export class App extends AbstractApp<IProps> {
  45. /**
  46. * Initializes a new {@code App} instance.
  47. *
  48. * @param {IProps} props - The read-only React {@code Component} props with
  49. * which the new instance is to be initialized.
  50. */
  51. constructor(props: IProps) {
  52. super(props);
  53. // In the Release configuration, React Native will (intentionally) throw
  54. // an unhandled JavascriptException for an unhandled JavaScript error.
  55. // This will effectively kill the app. In accord with the Web, do not
  56. // kill the app.
  57. this._maybeDisableExceptionsManager();
  58. // Bind event handler so it is only bound once per instance.
  59. this._onDimensionsChanged = this._onDimensionsChanged.bind(this);
  60. this._onSafeAreaInsetsChanged = this._onSafeAreaInsetsChanged.bind(this);
  61. }
  62. /**
  63. * Initializes the color scheme.
  64. *
  65. * @inheritdoc
  66. *
  67. * @returns {void}
  68. */
  69. override async componentDidMount() {
  70. await super.componentDidMount();
  71. SplashScreen.hide();
  72. const liteTxt = AppInfo.isLiteSDK ? ' (lite)' : '';
  73. logger.info(`Loaded SDK ${AppInfo.sdkVersion}${liteTxt} isEmbedded=${isEmbedded()}`);
  74. }
  75. /**
  76. * Implements React's {@link Component#render()}.
  77. *
  78. * @inheritdoc
  79. * @returns {ReactElement}
  80. */
  81. override render() {
  82. return (
  83. <JitsiThemePaperProvider>
  84. { super.render() }
  85. </JitsiThemePaperProvider>
  86. );
  87. }
  88. /**
  89. * Initializes feature flags and updates settings.
  90. *
  91. * @returns {void}
  92. */
  93. async _extraInit() {
  94. const { dispatch, getState } = this.state.store ?? {};
  95. const { flags = {}, url, userInfo } = this.props;
  96. let callIntegrationEnabled = flags[CALL_INTEGRATION_ENABLED as keyof typeof flags];
  97. // CallKit does not work on the simulator, make sure we disable it.
  98. if (Platform.OS === 'ios' && DeviceInfo.isEmulatorSync()) {
  99. flags[CALL_INTEGRATION_ENABLED] = false;
  100. callIntegrationEnabled = false;
  101. logger.info('Disabling CallKit because this is a simulator');
  102. }
  103. // Disable Android ConnectionService by default.
  104. if (Platform.OS === 'android' && typeof callIntegrationEnabled === 'undefined') {
  105. flags[CALL_INTEGRATION_ENABLED] = false;
  106. callIntegrationEnabled = false;
  107. }
  108. // We set these early enough so then we avoid any unnecessary re-renders.
  109. dispatch?.(updateFlags(flags));
  110. const route = await _getRouteToRender();
  111. // We need the root navigator to be set early.
  112. await this._navigate(route);
  113. // HACK ALERT!
  114. // Wait until the root navigator is ready.
  115. // We really need to break the inheritance relationship between App,
  116. // AbstractApp and BaseApp, it's very inflexible and cumbersome right now.
  117. const rootNavigationReady = new Promise<void>(resolve => {
  118. const i = setInterval(() => {
  119. // @ts-ignore
  120. const { ready } = getState()['features/app'] || {};
  121. if (ready) {
  122. clearInterval(i);
  123. resolve();
  124. }
  125. }, 50);
  126. });
  127. await rootNavigationReady;
  128. // Update specified server URL.
  129. if (typeof url !== 'undefined') {
  130. // @ts-ignore
  131. const { serverURL } = url;
  132. if (typeof serverURL !== 'undefined') {
  133. dispatch?.(updateSettings({ serverURL }));
  134. }
  135. }
  136. // @ts-ignore
  137. dispatch?.(updateSettings(userInfo || {}));
  138. // Update settings with feature-flag.
  139. if (typeof callIntegrationEnabled !== 'undefined') {
  140. dispatch?.(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  141. }
  142. }
  143. /**
  144. * Overrides the parent method to inject {@link DimensionsDetector} as
  145. * the top most component.
  146. *
  147. * @override
  148. */
  149. _createMainElement(component: ComponentType<any>, props: Object) {
  150. return (
  151. <SafeAreaProvider>
  152. <DimensionsDetector
  153. onDimensionsChanged = { this._onDimensionsChanged }
  154. onSafeAreaInsetsChanged = { this._onSafeAreaInsetsChanged }>
  155. { super._createMainElement(component, props) }
  156. </DimensionsDetector>
  157. </SafeAreaProvider>
  158. );
  159. }
  160. /**
  161. * Attempts to disable the use of React Native
  162. * {@link ExceptionsManager#handleException} on platforms and in
  163. * configurations on/in which the use of the method in questions has been
  164. * determined to be undesirable. For example, React Native will
  165. * (intentionally) throw an unhandled {@code JavascriptException} for an
  166. * unhandled JavaScript error in the Release configuration. This will
  167. * effectively kill the app. In accord with the Web, do not kill the app.
  168. *
  169. * @private
  170. * @returns {void}
  171. */
  172. _maybeDisableExceptionsManager() {
  173. if (__DEV__) {
  174. // As mentioned above, only the Release configuration was observed
  175. // to suffer.
  176. return;
  177. }
  178. if (Platform.OS !== 'android') {
  179. // A solution based on RTCSetFatalHandler was implemented on iOS and
  180. // it is preferred because it is at a later step of the
  181. // error/exception handling and it is specific to fatal
  182. // errors/exceptions which were observed to kill the app. The
  183. // solution implemented below was tested on Android only so it is
  184. // considered safest to use it there only.
  185. return;
  186. }
  187. // @ts-ignore
  188. const oldHandler = global.ErrorUtils.getGlobalHandler();
  189. const newHandler = _handleException;
  190. if (!oldHandler || oldHandler !== newHandler) {
  191. // @ts-ignore
  192. newHandler.next = oldHandler;
  193. // @ts-ignore
  194. global.ErrorUtils.setGlobalHandler(newHandler);
  195. }
  196. }
  197. /**
  198. * Updates the known available size for the app to occupy.
  199. *
  200. * @param {number} width - The component's current width.
  201. * @param {number} height - The component's current height.
  202. * @private
  203. * @returns {void}
  204. */
  205. _onDimensionsChanged(width: number, height: number) {
  206. const { dispatch } = this.state.store ?? {};
  207. dispatch?.(clientResized(width, height));
  208. }
  209. /**
  210. * Updates the safe are insets values.
  211. *
  212. * @param {Object} insets - The insets.
  213. * @param {number} insets.top - The top inset.
  214. * @param {number} insets.right - The right inset.
  215. * @param {number} insets.bottom - The bottom inset.
  216. * @param {number} insets.left - The left inset.
  217. * @private
  218. * @returns {void}
  219. */
  220. _onSafeAreaInsetsChanged(insets: Object) {
  221. const { dispatch } = this.state.store ?? {};
  222. dispatch?.(setSafeAreaInsets(insets));
  223. }
  224. /**
  225. * Renders the platform specific dialog container.
  226. *
  227. * @returns {React$Element}
  228. */
  229. _renderDialogContainer() {
  230. return (
  231. <DialogContainerWrapper
  232. pointerEvents = 'box-none'
  233. style = { StyleSheet.absoluteFill }>
  234. <BottomSheetContainer />
  235. <DialogContainer />
  236. </DialogContainerWrapper>
  237. );
  238. }
  239. }
  240. /**
  241. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  242. * from converting a fatal error into an unhandled native exception which will
  243. * kill the app.
  244. *
  245. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  246. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  247. * otherwise, {@code false}.
  248. * @private
  249. * @returns {void}
  250. */
  251. function _handleException(error: Error, fatal: boolean) {
  252. if (fatal) {
  253. // In the Release configuration, React Native will (intentionally) throw
  254. // an unhandled JavascriptException for an unhandled JavaScript error.
  255. // This will effectively kill the app. In accord with the Web, do not
  256. // kill the app.
  257. logger.error(error);
  258. } else {
  259. // Forward to the next globalHandler of ErrorUtils.
  260. // @ts-ignore
  261. const { next } = _handleException;
  262. typeof next === 'function' && next(error, fatal);
  263. }
  264. }