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

App.native.tsx 9.9KB

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