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

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