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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* global __DEV__ */
  2. import React from 'react';
  3. import { Linking } from 'react-native';
  4. import { Platform } from '../../base/react';
  5. import '../../mobile/audio-mode';
  6. import '../../mobile/background';
  7. import '../../mobile/external-api';
  8. import '../../mobile/full-screen';
  9. import '../../mobile/proximity';
  10. import '../../mobile/wake-lock';
  11. import { AbstractApp } from './AbstractApp';
  12. import { appSetWelcomePageDisabled } from '../actions';
  13. /**
  14. * Root application component.
  15. *
  16. * @extends AbstractApp
  17. */
  18. export class App extends AbstractApp {
  19. /**
  20. * App component's property types.
  21. *
  22. * @static
  23. */
  24. static propTypes = {
  25. ...AbstractApp.propTypes,
  26. /**
  27. * Indicates if the welcome page should be shown when not in a
  28. * conference.
  29. */
  30. disableWelcomePage: React.PropTypes.bool
  31. };
  32. /**
  33. * Initializes a new App instance.
  34. *
  35. * @param {Object} props - The read-only React Component props with which
  36. * the new instance is to be initialized.
  37. */
  38. constructor(props) {
  39. super(props);
  40. // Bind event handlers so they are only bound once for every instance.
  41. this._onLinkingURL = this._onLinkingURL.bind(this);
  42. // In the Release configuration, React Native will (intentionally) throw
  43. // an unhandled JavascriptException for an unhandled JavaScript error.
  44. // This will effectively kill the application. In accord with the Web,
  45. // do not kill the application.
  46. this._maybeDisableExceptionsManager();
  47. }
  48. /**
  49. * Subscribe to notifications about activating URLs registered to be handled
  50. * by this app.
  51. *
  52. * @inheritdoc
  53. * @returns {void}
  54. * @see https://facebook.github.io/react-native/docs/linking.html
  55. */
  56. componentWillMount() {
  57. super.componentWillMount();
  58. Linking.addEventListener('url', this._onLinkingURL);
  59. // Store the desire to use the welcome page or not in the Redux store.
  60. const dispatch = this._getStore().dispatch;
  61. dispatch(
  62. appSetWelcomePageDisabled(
  63. this, Boolean(this.props.disableWelcomePage)));
  64. }
  65. /**
  66. * Unsubscribe from notifications about activating URLs registered to be
  67. * handled by this app.
  68. *
  69. * @inheritdoc
  70. * @returns {void}
  71. * @see https://facebook.github.io/react-native/docs/linking.html
  72. */
  73. componentWillUnmount() {
  74. Linking.removeEventListener('url', this._onLinkingURL);
  75. super.componentWillUnmount();
  76. }
  77. /**
  78. * Attempts to disable the use of React Native
  79. * {@link ExceptionsManager#handleException} on platforms and in
  80. * configurations on/in which the use of the method in questions has been
  81. * determined to be undesirable. For example, React Native will
  82. * (intentionally) throw an unhandled JavascriptException for an
  83. * unhandled JavaScript error in the Release configuration. This will
  84. * effectively kill the application. In accord with the Web, do not kill the
  85. * application.
  86. *
  87. * @private
  88. * @returns {void}
  89. */
  90. _maybeDisableExceptionsManager() {
  91. if (__DEV__) {
  92. // As mentioned above, only the Release configuration was observed
  93. // to suffer.
  94. return;
  95. }
  96. if (Platform.OS !== 'android') {
  97. // A solution based on RTCSetFatalHandler was implemented on iOS and
  98. // it is preferred because it is at a later step of the
  99. // error/exception handling and it is specific to fatal
  100. // errors/exceptions which were observed to kill the application.
  101. // The solution implemented bellow was tested on Android only so it
  102. // is considered safest to use it there only.
  103. return;
  104. }
  105. const oldHandler = global.ErrorUtils.getGlobalHandler();
  106. const newHandler = _handleException;
  107. if (!oldHandler || oldHandler !== newHandler) {
  108. newHandler.next = oldHandler;
  109. global.ErrorUtils.setGlobalHandler(newHandler);
  110. }
  111. }
  112. /**
  113. * Notified by React's Linking API that a specific URL registered to be
  114. * handled by this App was activated.
  115. *
  116. * @param {Object} event - The details of the notification/event.
  117. * @param {string} event.url - The URL registered to be handled by this App
  118. * which was activated.
  119. * @private
  120. * @returns {void}
  121. */
  122. _onLinkingURL(event) {
  123. this._openURL(event.url);
  124. }
  125. }
  126. /**
  127. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  128. * from converting a fatal error into an unhandled native exception which will
  129. * kill the application.
  130. *
  131. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  132. * @param {boolean} fatal - True if the specified error is fatal; otherwise,
  133. * false.
  134. * @private
  135. * @returns {void}
  136. */
  137. function _handleException(error, fatal) {
  138. if (fatal) {
  139. // In the Release configuration, React Native will (intentionally) throw
  140. // an unhandled JavascriptException for an unhandled JavaScript error.
  141. // This will effectively kill the application. In accord with the Web,
  142. // do not kill the application.
  143. console.error(error);
  144. } else {
  145. // Forward to the next globalHandler of ErrorUtils.
  146. const next = _handleException.next;
  147. typeof next === 'function' && next(error, fatal);
  148. }
  149. }