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.

Conference.tsx 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. import { useIsFocused } from '@react-navigation/native';
  2. import React, { useEffect } from 'react';
  3. import {
  4. BackHandler,
  5. NativeModules,
  6. Platform,
  7. SafeAreaView,
  8. StatusBar,
  9. View,
  10. ViewStyle
  11. } from 'react-native';
  12. import { EdgeInsets, withSafeAreaInsets } from 'react-native-safe-area-context';
  13. import { connect } from 'react-redux';
  14. import { appNavigate } from '../../../app/actions';
  15. import { IReduxState, IStore } from '../../../app/types';
  16. import { FULLSCREEN_ENABLED, PIP_ENABLED } from '../../../base/flags/constants';
  17. import { getFeatureFlag } from '../../../base/flags/functions';
  18. import { getParticipantCount } from '../../../base/participants/functions';
  19. import Container from '../../../base/react/components/native/Container';
  20. import LoadingIndicator from '../../../base/react/components/native/LoadingIndicator';
  21. import TintedView from '../../../base/react/components/native/TintedView';
  22. import {
  23. ASPECT_RATIO_NARROW,
  24. ASPECT_RATIO_WIDE
  25. } from '../../../base/responsive-ui/constants';
  26. import { StyleType } from '../../../base/styles/functions.any';
  27. import TestConnectionInfo from '../../../base/testing/components/TestConnectionInfo';
  28. import { isCalendarEnabled } from '../../../calendar-sync/functions.native';
  29. import DisplayNameLabel from '../../../display-name/components/native/DisplayNameLabel';
  30. import BrandingImageBackground from '../../../dynamic-branding/components/native/BrandingImageBackground';
  31. import Filmstrip from '../../../filmstrip/components/native/Filmstrip';
  32. import TileView from '../../../filmstrip/components/native/TileView';
  33. import { FILMSTRIP_SIZE } from '../../../filmstrip/constants';
  34. import { isFilmstripVisible } from '../../../filmstrip/functions.native';
  35. import CalleeInfoContainer from '../../../invite/components/callee-info/CalleeInfoContainer';
  36. import LargeVideo from '../../../large-video/components/LargeVideo.native';
  37. import { startKnocking } from '../../../lobby/actions.any';
  38. import { getIsLobbyVisible } from '../../../lobby/functions';
  39. import { navigate }
  40. from '../../../mobile/navigation/components/conference/ConferenceNavigationContainerRef';
  41. import { shouldEnableAutoKnock } from '../../../mobile/navigation/functions';
  42. import { screen } from '../../../mobile/navigation/routes';
  43. import { setPictureInPictureEnabled } from '../../../mobile/picture-in-picture/functions';
  44. import Captions from '../../../subtitles/components/native/Captions';
  45. import { setToolboxVisible } from '../../../toolbox/actions';
  46. import Toolbox from '../../../toolbox/components/native/Toolbox';
  47. import { isToolboxVisible } from '../../../toolbox/functions';
  48. import {
  49. AbstractConference,
  50. abstractMapStateToProps
  51. } from '../AbstractConference';
  52. import type { AbstractProps } from '../AbstractConference';
  53. import { isConnecting } from '../functions';
  54. import AlwaysOnLabels from './AlwaysOnLabels';
  55. import ExpandedLabelPopup from './ExpandedLabelPopup';
  56. import LonelyMeetingExperience from './LonelyMeetingExperience';
  57. import TitleBar from './TitleBar';
  58. import { EXPANDED_LABEL_TIMEOUT } from './constants';
  59. import styles from './styles';
  60. /**
  61. * The type of the React {@code Component} props of {@link Conference}.
  62. */
  63. interface IProps extends AbstractProps {
  64. /**
  65. * Application's aspect ratio.
  66. */
  67. _aspectRatio: Symbol;
  68. /**
  69. * Whether the audio only is enabled or not.
  70. */
  71. _audioOnlyEnabled: boolean;
  72. /**
  73. * Branding styles for conference.
  74. */
  75. _brandingStyles: StyleType;
  76. /**
  77. * Whether the calendar feature is enabled or not.
  78. */
  79. _calendarEnabled: boolean;
  80. /**
  81. * The indicator which determines that we are still connecting to the
  82. * conference which includes establishing the XMPP connection and then
  83. * joining the room. If truthy, then an activity/loading indicator will be
  84. * rendered.
  85. */
  86. _connecting: boolean;
  87. /**
  88. * Set to {@code true} when the filmstrip is currently visible.
  89. */
  90. _filmstripVisible: boolean;
  91. /**
  92. * The indicator which determines whether fullscreen (immersive) mode is enabled.
  93. */
  94. _fullscreenEnabled: boolean;
  95. /**
  96. * The indicator which determines if the conference type is one to one.
  97. */
  98. _isOneToOneConference: boolean;
  99. /**
  100. * The indicator which determines if the participants pane is open.
  101. */
  102. _isParticipantsPaneOpen: boolean;
  103. /**
  104. * The ID of the participant currently on stage (if any).
  105. */
  106. _largeVideoParticipantId: string;
  107. /**
  108. * Local participant's display name.
  109. */
  110. _localParticipantDisplayName: string;
  111. /**
  112. * Whether Picture-in-Picture is enabled.
  113. */
  114. _pictureInPictureEnabled: boolean;
  115. /**
  116. * The indicator which determines whether the UI is reduced (to accommodate
  117. * smaller display areas).
  118. */
  119. _reducedUI: boolean;
  120. /**
  121. * Indicates if we should auto-knock.
  122. */
  123. _shouldEnableAutoKnock: boolean;
  124. /**
  125. * Indicates whether the lobby screen should be visible.
  126. */
  127. _showLobby: boolean;
  128. /**
  129. * Indicates whether the car mode is enabled.
  130. */
  131. _startCarMode: boolean;
  132. /**
  133. * The indicator which determines whether the Toolbox is visible.
  134. */
  135. _toolboxVisible: boolean;
  136. /**
  137. * The redux {@code dispatch} function.
  138. */
  139. dispatch: IStore['dispatch'];
  140. /**
  141. * Object containing the safe area insets.
  142. */
  143. insets: EdgeInsets;
  144. /**
  145. * Default prop for navigating between screen components(React Navigation).
  146. */
  147. navigation: any;
  148. }
  149. type State = {
  150. /**
  151. * The label that is currently expanded.
  152. */
  153. visibleExpandedLabel?: string;
  154. };
  155. /**
  156. * The conference page of the mobile (i.e. React Native) application.
  157. */
  158. class Conference extends AbstractConference<IProps, State> {
  159. /**
  160. * Timeout ref.
  161. */
  162. _expandedLabelTimeout: any;
  163. /**
  164. * Initializes a new Conference instance.
  165. *
  166. * @param {Object} props - The read-only properties with which the new
  167. * instance is to be initialized.
  168. */
  169. constructor(props: IProps) {
  170. super(props);
  171. this.state = {
  172. visibleExpandedLabel: undefined
  173. };
  174. this._expandedLabelTimeout = React.createRef<number>();
  175. // Bind event handlers so they are only bound once per instance.
  176. this._onClick = this._onClick.bind(this);
  177. this._onHardwareBackPress = this._onHardwareBackPress.bind(this);
  178. this._setToolboxVisible = this._setToolboxVisible.bind(this);
  179. this._createOnPress = this._createOnPress.bind(this);
  180. }
  181. /**
  182. * Implements {@link Component#componentDidMount()}. Invoked immediately
  183. * after this component is mounted.
  184. *
  185. * @inheritdoc
  186. * @returns {void}
  187. */
  188. componentDidMount() {
  189. const {
  190. _audioOnlyEnabled,
  191. _startCarMode,
  192. navigation
  193. } = this.props;
  194. BackHandler.addEventListener('hardwareBackPress', this._onHardwareBackPress);
  195. if (_audioOnlyEnabled && _startCarMode) {
  196. navigation.navigate(screen.conference.carmode);
  197. }
  198. }
  199. /**
  200. * Implements {@code Component#componentDidUpdate}.
  201. *
  202. * @inheritdoc
  203. */
  204. componentDidUpdate(prevProps: IProps) {
  205. const {
  206. _shouldEnableAutoKnock,
  207. _showLobby,
  208. dispatch
  209. } = this.props;
  210. if (!prevProps._showLobby && _showLobby) {
  211. navigate(screen.lobby.root);
  212. if (_shouldEnableAutoKnock) {
  213. dispatch(startKnocking());
  214. }
  215. }
  216. if (prevProps._showLobby && !_showLobby) {
  217. navigate(screen.conference.main);
  218. }
  219. }
  220. /**
  221. * Implements {@link Component#componentWillUnmount()}. Invoked immediately
  222. * before this component is unmounted and destroyed. Disconnects the
  223. * conference described by the redux store/state.
  224. *
  225. * @inheritdoc
  226. * @returns {void}
  227. */
  228. componentWillUnmount() {
  229. // Tear handling any hardware button presses for back navigation down.
  230. BackHandler.removeEventListener('hardwareBackPress', this._onHardwareBackPress);
  231. clearTimeout(this._expandedLabelTimeout.current ?? 0);
  232. }
  233. /**
  234. * Implements React's {@link Component#render()}.
  235. *
  236. * @inheritdoc
  237. * @returns {ReactElement}
  238. */
  239. render() {
  240. const {
  241. _brandingStyles,
  242. _fullscreenEnabled
  243. } = this.props;
  244. return (
  245. <Container
  246. style = { [
  247. styles.conference,
  248. _brandingStyles
  249. ] }>
  250. <BrandingImageBackground />
  251. {
  252. Platform.OS === 'android'
  253. && <StatusBar
  254. barStyle = 'light-content'
  255. hidden = { _fullscreenEnabled }
  256. translucent = { _fullscreenEnabled } />
  257. }
  258. { this._renderContent() }
  259. </Container>
  260. );
  261. }
  262. /**
  263. * Changes the value of the toolboxVisible state, thus allowing us to switch
  264. * between Toolbox and Filmstrip and change their visibility.
  265. *
  266. * @private
  267. * @returns {void}
  268. */
  269. _onClick() {
  270. this._setToolboxVisible(!this.props._toolboxVisible);
  271. }
  272. /**
  273. * Handles a hardware button press for back navigation. Enters Picture-in-Picture mode
  274. * (if supported) or leaves the associated {@code Conference} otherwise.
  275. *
  276. * @returns {boolean} Exiting the app is undesired, so {@code true} is always returned.
  277. */
  278. _onHardwareBackPress() {
  279. let p;
  280. if (this.props._pictureInPictureEnabled) {
  281. const { PictureInPicture } = NativeModules;
  282. p = PictureInPicture.enterPictureInPicture();
  283. } else {
  284. p = Promise.reject(new Error('PiP not enabled'));
  285. }
  286. p.catch(() => {
  287. this.props.dispatch(appNavigate(undefined));
  288. });
  289. return true;
  290. }
  291. /**
  292. * Creates a function to be invoked when the onPress of the touchables are
  293. * triggered.
  294. *
  295. * @param {string} label - The identifier of the label that's onLayout is
  296. * triggered.
  297. * @returns {Function}
  298. */
  299. _createOnPress(label: string) {
  300. return () => {
  301. const { visibleExpandedLabel } = this.state;
  302. const newVisibleExpandedLabel
  303. = visibleExpandedLabel === label ? undefined : label;
  304. clearTimeout(this._expandedLabelTimeout.current);
  305. this.setState({
  306. visibleExpandedLabel: newVisibleExpandedLabel
  307. });
  308. if (newVisibleExpandedLabel) {
  309. this._expandedLabelTimeout.current = setTimeout(() => {
  310. this.setState({
  311. visibleExpandedLabel: undefined
  312. });
  313. }, EXPANDED_LABEL_TIMEOUT);
  314. }
  315. };
  316. }
  317. /**
  318. * Renders the content for the Conference container.
  319. *
  320. * @private
  321. * @returns {React$Element}
  322. */
  323. _renderContent() {
  324. const {
  325. _aspectRatio,
  326. _connecting,
  327. _filmstripVisible,
  328. _isOneToOneConference,
  329. _largeVideoParticipantId,
  330. _reducedUI,
  331. _shouldDisplayTileView,
  332. _toolboxVisible
  333. } = this.props;
  334. let alwaysOnTitleBarStyles;
  335. if (_reducedUI) {
  336. return this._renderContentForReducedUi();
  337. }
  338. if (_aspectRatio === ASPECT_RATIO_WIDE) {
  339. alwaysOnTitleBarStyles
  340. = !_shouldDisplayTileView && _filmstripVisible
  341. ? styles.alwaysOnTitleBarWide
  342. : styles.alwaysOnTitleBar;
  343. } else {
  344. alwaysOnTitleBarStyles = styles.alwaysOnTitleBar;
  345. }
  346. return (
  347. <>
  348. {/*
  349. * The LargeVideo is the lowermost stacking layer.
  350. */
  351. _shouldDisplayTileView
  352. ? <TileView onClick = { this._onClick } />
  353. : <LargeVideo onClick = { this._onClick } />
  354. }
  355. {/*
  356. * If there is a ringing call, show the callee's info.
  357. */
  358. <CalleeInfoContainer />
  359. }
  360. {/*
  361. * The activity/loading indicator goes above everything, except
  362. * the toolbox/toolbars and the dialogs.
  363. */
  364. _connecting
  365. && <TintedView>
  366. <LoadingIndicator />
  367. </TintedView>
  368. }
  369. <View
  370. pointerEvents = 'box-none'
  371. style = { styles.toolboxAndFilmstripContainer as ViewStyle }>
  372. <Captions onPress = { this._onClick } />
  373. {
  374. _shouldDisplayTileView || (
  375. !_isOneToOneConference
  376. && <Container style = { styles.displayNameContainer }>
  377. <DisplayNameLabel
  378. participantId = { _largeVideoParticipantId } />
  379. </Container>
  380. )
  381. }
  382. <LonelyMeetingExperience />
  383. {
  384. _shouldDisplayTileView
  385. || <>
  386. <Filmstrip />
  387. { this._renderNotificationsContainer() }
  388. <Toolbox />
  389. </>
  390. }
  391. </View>
  392. <SafeAreaView
  393. pointerEvents = 'box-none'
  394. style = {
  395. (_toolboxVisible
  396. ? styles.titleBarSafeViewColor
  397. : styles.titleBarSafeViewTransparent) as ViewStyle }>
  398. <TitleBar _createOnPress = { this._createOnPress } />
  399. </SafeAreaView>
  400. <SafeAreaView
  401. pointerEvents = 'box-none'
  402. style = {
  403. (_toolboxVisible
  404. ? [ styles.titleBarSafeViewTransparent, { top: this.props.insets.top + 50 } ]
  405. : styles.titleBarSafeViewTransparent) as ViewStyle
  406. }>
  407. <View
  408. pointerEvents = 'box-none'
  409. style = { styles.expandedLabelWrapper }>
  410. <ExpandedLabelPopup visibleExpandedLabel = { this.state.visibleExpandedLabel } />
  411. </View>
  412. <View
  413. pointerEvents = 'box-none'
  414. style = { alwaysOnTitleBarStyles as ViewStyle }>
  415. {/* eslint-disable-next-line react/jsx-no-bind */}
  416. <AlwaysOnLabels createOnPress = { this._createOnPress } />
  417. </View>
  418. </SafeAreaView>
  419. <TestConnectionInfo />
  420. {
  421. _shouldDisplayTileView
  422. && <>
  423. { this._renderNotificationsContainer() }
  424. <Toolbox />
  425. </>
  426. }
  427. </>
  428. );
  429. }
  430. /**
  431. * Renders the content for the Conference container when in "reduced UI" mode.
  432. *
  433. * @private
  434. * @returns {React$Element}
  435. */
  436. _renderContentForReducedUi() {
  437. const { _connecting } = this.props;
  438. return (
  439. <>
  440. <LargeVideo onClick = { this._onClick } />
  441. {
  442. _connecting
  443. && <TintedView>
  444. <LoadingIndicator />
  445. </TintedView>
  446. }
  447. </>
  448. );
  449. }
  450. /**
  451. * Renders a container for notifications to be displayed by the
  452. * base/notifications feature.
  453. *
  454. * @private
  455. * @returns {React$Element}
  456. */
  457. _renderNotificationsContainer() {
  458. const notificationsStyle: ViewStyle = {};
  459. // In the landscape mode (wide) there's problem with notifications being
  460. // shadowed by the filmstrip rendered on the right. This makes the "x"
  461. // button not clickable. In order to avoid that a margin of the
  462. // filmstrip's size is added to the right.
  463. //
  464. // Pawel: after many attempts I failed to make notifications adjust to
  465. // their contents width because of column and rows being used in the
  466. // flex layout. The only option that seemed to limit the notification's
  467. // size was explicit 'width' value which is not better than the margin
  468. // added here.
  469. const { _aspectRatio, _filmstripVisible } = this.props;
  470. if (_filmstripVisible && _aspectRatio !== ASPECT_RATIO_NARROW) {
  471. notificationsStyle.marginRight = FILMSTRIP_SIZE;
  472. }
  473. return super.renderNotificationsContainer(
  474. {
  475. shouldDisplayTileView: this.props._shouldDisplayTileView,
  476. style: notificationsStyle,
  477. toolboxVisible: this.props._toolboxVisible
  478. }
  479. );
  480. }
  481. /**
  482. * Dispatches an action changing the visibility of the {@link Toolbox}.
  483. *
  484. * @private
  485. * @param {boolean} visible - Pass {@code true} to show the
  486. * {@code Toolbox} or {@code false} to hide it.
  487. * @returns {void}
  488. */
  489. _setToolboxVisible(visible: boolean) {
  490. this.props.dispatch(setToolboxVisible(visible));
  491. }
  492. }
  493. /**
  494. * Maps (parts of) the redux state to the associated {@code Conference}'s props.
  495. *
  496. * @param {Object} state - The redux state.
  497. * @param {any} _ownProps - Component's own props.
  498. * @private
  499. * @returns {IProps}
  500. */
  501. function _mapStateToProps(state: IReduxState, _ownProps: any) {
  502. const { isOpen } = state['features/participants-pane'];
  503. const { aspectRatio, reducedUI } = state['features/base/responsive-ui'];
  504. const { backgroundColor } = state['features/dynamic-branding'];
  505. const { startCarMode } = state['features/base/settings'];
  506. const { enabled: audioOnlyEnabled } = state['features/base/audio-only'];
  507. const participantCount = getParticipantCount(state);
  508. const brandingStyles = backgroundColor ? {
  509. backgroundColor
  510. } : undefined;
  511. return {
  512. ...abstractMapStateToProps(state),
  513. _aspectRatio: aspectRatio,
  514. _audioOnlyEnabled: Boolean(audioOnlyEnabled),
  515. _brandingStyles: brandingStyles,
  516. _calendarEnabled: isCalendarEnabled(state),
  517. _connecting: isConnecting(state),
  518. _filmstripVisible: isFilmstripVisible(state),
  519. _fullscreenEnabled: getFeatureFlag(state, FULLSCREEN_ENABLED, true),
  520. _isOneToOneConference: Boolean(participantCount === 2),
  521. _isParticipantsPaneOpen: isOpen,
  522. _largeVideoParticipantId: state['features/large-video'].participantId,
  523. _pictureInPictureEnabled: getFeatureFlag(state, PIP_ENABLED),
  524. _reducedUI: reducedUI,
  525. _shouldEnableAutoKnock: shouldEnableAutoKnock(state),
  526. _showLobby: getIsLobbyVisible(state),
  527. _startCarMode: startCarMode,
  528. _toolboxVisible: isToolboxVisible(state)
  529. };
  530. }
  531. export default withSafeAreaInsets(connect(_mapStateToProps)(props => {
  532. const isFocused = useIsFocused();
  533. useEffect(() => {
  534. if (isFocused) {
  535. setPictureInPictureEnabled(true);
  536. } else {
  537. setPictureInPictureEnabled(false);
  538. }
  539. // We also need to disable PiP when we are back on the WelcomePage
  540. return () => setPictureInPictureEnabled(false);
  541. }, [ isFocused ]);
  542. return ( // @ts-ignore
  543. <Conference { ...props } />
  544. );
  545. }));