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

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