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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 } 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(({ dispatch, getState }) => next => action => {
  28. const result = next(action);
  29. let fullScreen = null;
  30. switch (action.type) {
  31. case _SET_IMMERSIVE_LISTENER:
  32. // XXX The React Native module Immersive is only implemented on Android
  33. // and throws on other platforms.
  34. if (Platform.OS === 'android') {
  35. // Remove the current/old Immersive listener.
  36. const { listener } = getState()['features/full-screen'];
  37. listener && Immersive.removeImmersiveListener(listener);
  38. // Add the new listener.
  39. action.listener && Immersive.addImmersiveListener(action.listener);
  40. }
  41. break;
  42. case APP_WILL_MOUNT: {
  43. const context = {
  44. dispatch,
  45. getState
  46. };
  47. dispatch(
  48. _setImmersiveListener(_onImmersiveChange.bind(undefined, context)));
  49. break;
  50. }
  51. case APP_WILL_UNMOUNT:
  52. _setImmersiveListener(undefined);
  53. break;
  54. case CONFERENCE_WILL_JOIN:
  55. case CONFERENCE_JOINED:
  56. case SET_AUDIO_ONLY: {
  57. const { audioOnly, conference, joining }
  58. = getState()['features/base/conference'];
  59. fullScreen = conference || joining ? !audioOnly : false;
  60. break;
  61. }
  62. case CONFERENCE_FAILED:
  63. case CONFERENCE_LEFT:
  64. fullScreen = false;
  65. break;
  66. }
  67. fullScreen !== null && _setFullScreen(fullScreen);
  68. return result;
  69. });
  70. /**
  71. * Handler for Immersive mode changes. This will be called when Android's
  72. * immersive mode changes. This can happen without us wanting, so re-evaluate if
  73. * immersive mode is desired and reactivate it if needed.
  74. *
  75. * @param {Object} store - The redux store.
  76. * @private
  77. * @returns {void}
  78. */
  79. function _onImmersiveChange({ getState }) {
  80. const state = getState();
  81. const { appState } = state['features/background'];
  82. if (appState === 'active') {
  83. const { audioOnly, conference, joining }
  84. = state['features/base/conference'];
  85. const fullScreen = conference || joining ? !audioOnly : false;
  86. _setFullScreen(fullScreen);
  87. }
  88. }
  89. /**
  90. * Activates/deactivates the full screen mode. On iOS it will hide the status
  91. * bar, and on Android it will turn immersive mode on.
  92. *
  93. * @param {boolean} fullScreen - True to set full screen mode, false to
  94. * deactivate it.
  95. * @private
  96. * @returns {void}
  97. */
  98. function _setFullScreen(fullScreen: boolean) {
  99. // XXX The React Native module Immersive is only implemented on Android and
  100. // throws on other platforms.
  101. if (Platform.OS === 'android') {
  102. fullScreen ? Immersive.on() : Immersive.off();
  103. return;
  104. }
  105. // On platforms other than Android go with whatever React Native itself
  106. // supports.
  107. StatusBar.setHidden(fullScreen, 'slide');
  108. }