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 4.8KB

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