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

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