您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 4.6KB

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