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

App.native.tsx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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, SERVER_URL_CHANGE_ENABLED } from '../../base/flags/constants';
  10. import { getFeatureFlag } from '../../base/flags/functions';
  11. import { clientResized, setSafeAreaInsets } from '../../base/responsive-ui/actions';
  12. import DimensionsDetector from '../../base/responsive-ui/components/DimensionsDetector.native';
  13. import { updateSettings } from '../../base/settings/actions';
  14. import JitsiThemePaperProvider from '../../base/ui/components/JitsiThemeProvider.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. async componentDidMount() {
  70. await super.componentDidMount();
  71. SplashScreen.hide();
  72. const liteTxt = AppInfo.isLiteSDK ? ' (lite)' : '';
  73. logger.info(`Loaded SDK ${AppInfo.sdkVersion}${liteTxt}`);
  74. }
  75. /**
  76. * Implements React's {@link Component#render()}.
  77. *
  78. * @inheritdoc
  79. * @returns {ReactElement}
  80. */
  81. 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 = {} } = this.props;
  96. // CallKit does not work on the simulator, make sure we disable it.
  97. if (Platform.OS === 'ios' && DeviceInfo.isEmulatorSync()) {
  98. flags['call-integration.enabled'] = false;
  99. logger.info('Disabling CallKit because this is a simulator');
  100. }
  101. // We set these early enough so then we avoid any unnecessary re-renders.
  102. dispatch?.(updateFlags(flags));
  103. const route = await _getRouteToRender();
  104. // We need the root navigator to be set early.
  105. await this._navigate(route);
  106. // HACK ALERT!
  107. // Wait until the root navigator is ready.
  108. // We really need to break the inheritance relationship between App,
  109. // AbstractApp and BaseApp, it's very inflexible and cumbersome right now.
  110. const rootNavigationReady = new Promise<void>(resolve => {
  111. const i = setInterval(() => {
  112. // @ts-ignore
  113. const { ready } = getState()['features/app'] || {};
  114. if (ready) {
  115. clearInterval(i);
  116. resolve();
  117. }
  118. }, 50);
  119. });
  120. await rootNavigationReady;
  121. // Check if serverURL is configured externally and not allowed to change.
  122. const serverURLChangeEnabled = getState && getFeatureFlag(getState(), SERVER_URL_CHANGE_ENABLED, true);
  123. if (!serverURLChangeEnabled) {
  124. // As serverURL is provided externally, so we push it to settings.
  125. if (typeof this.props.url !== 'undefined') {
  126. // @ts-ignore
  127. const { serverURL } = this.props.url;
  128. if (typeof serverURL !== 'undefined') {
  129. dispatch?.(updateSettings({ serverURL }));
  130. }
  131. }
  132. }
  133. dispatch?.(updateSettings(this.props.userInfo || {}));
  134. // Update settings with feature-flag.
  135. const callIntegrationEnabled = flags[CALL_INTEGRATION_ENABLED as keyof typeof flags];
  136. if (typeof callIntegrationEnabled !== 'undefined') {
  137. dispatch?.(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  138. }
  139. }
  140. /**
  141. * Overrides the parent method to inject {@link DimensionsDetector} as
  142. * the top most component.
  143. *
  144. * @override
  145. */
  146. _createMainElement(component: ComponentType<any>, props: Object) {
  147. return (
  148. <SafeAreaProvider>
  149. <DimensionsDetector
  150. onDimensionsChanged = { this._onDimensionsChanged }
  151. onSafeAreaInsetsChanged = { this._onSafeAreaInsetsChanged }>
  152. { super._createMainElement(component, props) }
  153. </DimensionsDetector>
  154. </SafeAreaProvider>
  155. );
  156. }
  157. /**
  158. * Attempts to disable the use of React Native
  159. * {@link ExceptionsManager#handleException} on platforms and in
  160. * configurations on/in which the use of the method in questions has been
  161. * determined to be undesirable. For example, React Native will
  162. * (intentionally) throw an unhandled {@code JavascriptException} for an
  163. * unhandled JavaScript error in the Release configuration. This will
  164. * effectively kill the app. In accord with the Web, do not kill the app.
  165. *
  166. * @private
  167. * @returns {void}
  168. */
  169. _maybeDisableExceptionsManager() {
  170. if (__DEV__) {
  171. // As mentioned above, only the Release configuration was observed
  172. // to suffer.
  173. return;
  174. }
  175. if (Platform.OS !== 'android') {
  176. // A solution based on RTCSetFatalHandler was implemented on iOS and
  177. // it is preferred because it is at a later step of the
  178. // error/exception handling and it is specific to fatal
  179. // errors/exceptions which were observed to kill the app. The
  180. // solution implemented below was tested on Android only so it is
  181. // considered safest to use it there only.
  182. return;
  183. }
  184. // @ts-ignore
  185. const oldHandler = global.ErrorUtils.getGlobalHandler();
  186. const newHandler = _handleException;
  187. if (!oldHandler || oldHandler !== newHandler) {
  188. // @ts-ignore
  189. newHandler.next = oldHandler;
  190. // @ts-ignore
  191. global.ErrorUtils.setGlobalHandler(newHandler);
  192. }
  193. }
  194. /**
  195. * Updates the known available size for the app to occupy.
  196. *
  197. * @param {number} width - The component's current width.
  198. * @param {number} height - The component's current height.
  199. * @private
  200. * @returns {void}
  201. */
  202. _onDimensionsChanged(width: number, height: number) {
  203. const { dispatch } = this.state.store ?? {};
  204. dispatch?.(clientResized(width, height));
  205. }
  206. /**
  207. * Updates the safe are insets values.
  208. *
  209. * @param {Object} insets - The insets.
  210. * @param {number} insets.top - The top inset.
  211. * @param {number} insets.right - The right inset.
  212. * @param {number} insets.bottom - The bottom inset.
  213. * @param {number} insets.left - The left inset.
  214. * @private
  215. * @returns {void}
  216. */
  217. _onSafeAreaInsetsChanged(insets: Object) {
  218. const { dispatch } = this.state.store ?? {};
  219. dispatch?.(setSafeAreaInsets(insets));
  220. }
  221. /**
  222. * Renders the platform specific dialog container.
  223. *
  224. * @returns {React$Element}
  225. */
  226. _renderDialogContainer() {
  227. return (
  228. <DialogContainerWrapper
  229. pointerEvents = 'box-none'
  230. style = { StyleSheet.absoluteFill }>
  231. <BottomSheetContainer />
  232. <DialogContainer />
  233. </DialogContainerWrapper>
  234. );
  235. }
  236. }
  237. /**
  238. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  239. * from converting a fatal error into an unhandled native exception which will
  240. * kill the app.
  241. *
  242. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  243. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  244. * otherwise, {@code false}.
  245. * @private
  246. * @returns {void}
  247. */
  248. function _handleException(error: Error, fatal: boolean) {
  249. if (fatal) {
  250. // In the Release configuration, React Native will (intentionally) throw
  251. // an unhandled JavascriptException for an unhandled JavaScript error.
  252. // This will effectively kill the app. In accord with the Web, do not
  253. // kill the app.
  254. logger.error(error);
  255. } else {
  256. // Forward to the next globalHandler of ErrorUtils.
  257. // @ts-ignore
  258. const { next } = _handleException;
  259. typeof next === 'function' && next(error, fatal);
  260. }
  261. }