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.

middleware.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // @flow
  2. import { Immersive } from 'react-native-immersive';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../base/app';
  4. import { getCurrentConference } from '../../base/conference';
  5. import { Platform } from '../../base/react';
  6. import { MiddlewareRegistry, StateListenerRegistry } from '../../base/redux';
  7. import { _setImmersiveListener as _setImmersiveListenerA } from './actions';
  8. import { _SET_IMMERSIVE_LISTENER } from './actionTypes';
  9. /**
  10. * Middleware that captures conference actions and activates or deactivates the
  11. * full screen mode. On iOS it hides the status bar, and on Android it uses the
  12. * immersive mode:
  13. * https://developer.android.com/training/system-ui/immersive.html
  14. * In immersive mode the status and navigation bars are hidden and thus the
  15. * entire screen will be covered by our application.
  16. *
  17. * @param {Store} store - The redux store.
  18. * @returns {Function}
  19. */
  20. MiddlewareRegistry.register(store => next => action => {
  21. switch (action.type) {
  22. case _SET_IMMERSIVE_LISTENER:
  23. return _setImmersiveListenerF(store, next, action);
  24. case APP_WILL_MOUNT: {
  25. const result = next(action);
  26. store.dispatch(
  27. _setImmersiveListenerA(_onImmersiveChange.bind(undefined, store)));
  28. return result;
  29. }
  30. case APP_WILL_UNMOUNT:
  31. store.dispatch(_setImmersiveListenerA(undefined));
  32. break;
  33. }
  34. return next(action);
  35. });
  36. StateListenerRegistry.register(
  37. /* selector */ state => {
  38. const { enabled: audioOnly } = state['features/base/audio-only'];
  39. const conference = getCurrentConference(state);
  40. return conference ? !audioOnly : false;
  41. },
  42. /* listener */ fullScreen => _setFullScreen(fullScreen)
  43. );
  44. /**
  45. * Handler for Immersive mode changes. This will be called when Android's
  46. * immersive mode changes. This can happen without us wanting, so re-evaluate if
  47. * immersive mode is desired and reactivate it if needed.
  48. *
  49. * @param {Object} store - The redux store.
  50. * @private
  51. * @returns {void}
  52. */
  53. function _onImmersiveChange({ getState }) {
  54. const state = getState();
  55. const { appState } = state['features/background'];
  56. if (appState === 'active') {
  57. const { enabled: audioOnly } = state['features/base/audio-only'];
  58. const conference = getCurrentConference(state);
  59. const fullScreen = conference ? !audioOnly : false;
  60. _setFullScreen(fullScreen);
  61. }
  62. }
  63. /**
  64. * Activates/deactivates the full screen mode. On iOS it will hide the status
  65. * bar, and on Android it will turn immersive mode on.
  66. *
  67. * @param {boolean} fullScreen - True to set full screen mode, false to
  68. * deactivate it.
  69. * @private
  70. * @returns {void}
  71. */
  72. function _setFullScreen(fullScreen: boolean) {
  73. // XXX The React Native module Immersive is only implemented on Android and
  74. // throws on other platforms.
  75. if (Platform.OS === 'android') {
  76. fullScreen ? Immersive.on() : Immersive.off();
  77. }
  78. }
  79. /**
  80. * Notifies the feature filmstrip that the action
  81. * {@link _SET_IMMERSIVE_LISTENER} is being dispatched within a specific redux
  82. * store.
  83. *
  84. * @param {Store} store - The redux store in which the specified action is being
  85. * dispatched.
  86. * @param {Dispatch} next - The redux dispatch function to dispatch the
  87. * specified action to the specified store.
  88. * @param {Action} action - The redux action {@code _SET_IMMERSIVE_LISTENER}
  89. * which is being dispatched in the specified store.
  90. * @private
  91. * @returns {Object} The value returned by {@code next(action)}.
  92. */
  93. function _setImmersiveListenerF({ getState }, next, action) {
  94. // XXX The React Native module Immersive is only implemented on Android and
  95. // throws on other platforms.
  96. if (Platform.OS === 'android') {
  97. // Remove the old Immersive listener and add the new one.
  98. const { listener: oldListener } = getState()['features/full-screen'];
  99. const result = next(action);
  100. const { listener: newListener } = getState()['features/full-screen'];
  101. if (oldListener !== newListener) {
  102. oldListener && Immersive.removeImmersiveListener(oldListener);
  103. newListener && Immersive.addImmersiveListener(newListener);
  104. }
  105. return result;
  106. }
  107. return next(action);
  108. }