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

Conference.native.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // @flow
  2. import React, { Component } from 'react';
  3. // eslint-disable-next-line react-native/split-platform-components
  4. import { BackAndroid, BackHandler, StatusBar, View } from 'react-native';
  5. import { connect as reactReduxConnect } from 'react-redux';
  6. import { appNavigate } from '../../app';
  7. import { connect, disconnect } from '../../base/connection';
  8. import { DialogContainer } from '../../base/dialog';
  9. import { getParticipantCount } from '../../base/participants';
  10. import { Container, LoadingIndicator, TintedView } from '../../base/react';
  11. import {
  12. isNarrowAspectRatio,
  13. makeAspectRatioAware
  14. } from '../../base/responsive-ui';
  15. import { TestConnectionInfo } from '../../base/testing';
  16. import { createDesiredLocalTracks } from '../../base/tracks';
  17. import { ConferenceNotification } from '../../calendar-sync';
  18. import { FILMSTRIP_SIZE, Filmstrip, isFilmstripVisible } from '../../filmstrip';
  19. import { LargeVideo } from '../../large-video';
  20. import { CalleeInfoContainer } from '../../invite';
  21. import { NotificationsContainer } from '../../notifications';
  22. import { Captions } from '../../subtitles';
  23. import { setToolboxVisible, Toolbox } from '../../toolbox';
  24. import styles from './styles';
  25. /**
  26. * The type of the React {@code Component} props of {@link Conference}.
  27. */
  28. type Props = {
  29. /**
  30. * The indicator which determines that we are still connecting to the
  31. * conference which includes establishing the XMPP connection and then
  32. * joining the room. If truthy, then an activity/loading indicator will be
  33. * rendered.
  34. *
  35. * @private
  36. */
  37. _connecting: boolean,
  38. /**
  39. * Set to {@code true} when the filmstrip is currently visible.
  40. *
  41. * @private
  42. */
  43. _filmstripVisible: boolean,
  44. /**
  45. * Current conference's full URL.
  46. *
  47. * @private
  48. */
  49. _locationURL: URL,
  50. /**
  51. * The handler which dispatches the (redux) action connect.
  52. *
  53. * @private
  54. * @returns {void}
  55. */
  56. _onConnect: Function,
  57. /**
  58. * The handler which dispatches the (redux) action disconnect.
  59. *
  60. * @private
  61. * @returns {void}
  62. */
  63. _onDisconnect: Function,
  64. /**
  65. * Handles a hardware button press for back navigation. Leaves the
  66. * associated {@code Conference}.
  67. *
  68. * @private
  69. * @returns {boolean} As the associated conference is unconditionally left
  70. * and exiting the app while it renders a {@code Conference} is undesired,
  71. * {@code true} is always returned.
  72. */
  73. _onHardwareBackPress: Function,
  74. /**
  75. * The number of participants in the conference.
  76. *
  77. * @private
  78. */
  79. _participantCount: number,
  80. /**
  81. * The indicator which determines whether the UI is reduced (to accommodate
  82. * smaller display areas).
  83. *
  84. * @private
  85. */
  86. _reducedUI: boolean,
  87. /**
  88. * The current conference room name.
  89. *
  90. * @private
  91. */
  92. _room: string,
  93. /**
  94. * The handler which dispatches the (redux) action {@link setToolboxVisible}
  95. * to show/hide the {@link Toolbox}.
  96. *
  97. * @param {boolean} visible - {@code true} to show the {@code Toolbox} or
  98. * {@code false} to hide it.
  99. * @private
  100. * @returns {void}
  101. */
  102. _setToolboxVisible: Function,
  103. /**
  104. * The indicator which determines whether the Toolbox is visible.
  105. *
  106. * @private
  107. */
  108. _toolboxVisible: boolean,
  109. /**
  110. * The indicator which determines whether the Toolbox is always visible.
  111. *
  112. * @private
  113. */
  114. _toolboxAlwaysVisible: boolean
  115. };
  116. /**
  117. * The conference page of the mobile (i.e. React Native) application.
  118. */
  119. class Conference extends Component<Props> {
  120. _backHandler: ?BackHandler;
  121. /**
  122. * Initializes a new Conference instance.
  123. *
  124. * @param {Object} props - The read-only properties with which the new
  125. * instance is to be initialized.
  126. */
  127. constructor(props) {
  128. super(props);
  129. // Bind event handlers so they are only bound once per instance.
  130. this._onClick = this._onClick.bind(this);
  131. this._onHardwareBackPress = this._onHardwareBackPress.bind(this);
  132. }
  133. /**
  134. * Implements {@link Component#componentDidMount()}. Invoked immediately
  135. * after this component is mounted.
  136. *
  137. * @inheritdoc
  138. * @returns {void}
  139. */
  140. componentDidMount() {
  141. // Set handling any hardware button presses for back navigation up.
  142. const backHandler = BackHandler || BackAndroid;
  143. if (backHandler) {
  144. this._backHandler = backHandler;
  145. backHandler.addEventListener(
  146. 'hardwareBackPress',
  147. this._onHardwareBackPress);
  148. }
  149. // Show the toolbox if we are the only participant; otherwise, the whole
  150. // UI looks too unpopulated the LargeVideo visible.
  151. const { _participantCount, _setToolboxVisible } = this.props;
  152. _participantCount === 1 && _setToolboxVisible(true);
  153. }
  154. /**
  155. * Implements {@link Component#componentWillMount()}. Invoked immediately
  156. * before mounting occurs. Connects the conference described by the redux
  157. * store/state.
  158. *
  159. * @inheritdoc
  160. * @returns {void}
  161. */
  162. componentWillMount() {
  163. this.props._onConnect();
  164. }
  165. /**
  166. * Notifies this mounted React {@code Component} that it will receive new
  167. * props. Check if we need to show / hide the toolbox based on the
  168. * participant count.
  169. *
  170. * @inheritdoc
  171. * @param {Props} nextProps - The read-only React {@code Component} props
  172. * that this instance will receive.
  173. * @returns {void}
  174. */
  175. componentWillReceiveProps(nextProps: Props) {
  176. const {
  177. _locationURL: oldLocationURL,
  178. _participantCount: oldParticipantCount,
  179. _room: oldRoom,
  180. _setToolboxVisible
  181. } = this.props;
  182. const {
  183. _locationURL: newLocationURL,
  184. _participantCount: newParticipantCount,
  185. _room: newRoom
  186. } = nextProps;
  187. // If the location URL changes we need to reconnect.
  188. oldLocationURL !== newLocationURL && this.props._onDisconnect();
  189. // Start the connection process when there is a (valid) room.
  190. oldRoom !== newRoom && newRoom && this.props._onConnect();
  191. if (oldParticipantCount === 1) {
  192. newParticipantCount > 1 && _setToolboxVisible(false);
  193. } else if (oldParticipantCount > 1) {
  194. newParticipantCount === 1 && _setToolboxVisible(true);
  195. }
  196. }
  197. /**
  198. * Implements {@link Component#componentWillUnmount()}. Invoked immediately
  199. * before this component is unmounted and destroyed. Disconnects the
  200. * conference described by the redux store/state.
  201. *
  202. * @inheritdoc
  203. * @returns {void}
  204. */
  205. componentWillUnmount() {
  206. // Tear handling any hardware button presses for back navigation down.
  207. const backHandler = this._backHandler;
  208. if (backHandler) {
  209. this._backHandler = undefined;
  210. backHandler.removeEventListener(
  211. 'hardwareBackPress',
  212. this._onHardwareBackPress);
  213. }
  214. this.props._onDisconnect();
  215. }
  216. /**
  217. * Implements React's {@link Component#render()}.
  218. *
  219. * @inheritdoc
  220. * @returns {ReactElement}
  221. */
  222. render() {
  223. return (
  224. <Container style = { styles.conference }>
  225. <StatusBar
  226. barStyle = 'light-content'
  227. hidden = { true }
  228. translucent = { true } />
  229. {/*
  230. * The LargeVideo is the lowermost stacking layer.
  231. */}
  232. <LargeVideo onPress = { this._onClick } />
  233. {/*
  234. * If there is a ringing call, show the callee's info.
  235. */
  236. this.props._reducedUI || <CalleeInfoContainer />
  237. }
  238. {/*
  239. * The activity/loading indicator goes above everything, except
  240. * the toolbox/toolbars and the dialogs.
  241. */
  242. this.props._connecting
  243. && <TintedView>
  244. <LoadingIndicator />
  245. </TintedView>
  246. }
  247. <View
  248. pointerEvents = 'box-none'
  249. style = { styles.toolboxAndFilmstripContainer }>
  250. {/*
  251. * Notifications are rendered on the very top of other
  252. * components like subtitles, toolbox and filmstrip.
  253. */
  254. this._renderNotificationsContainer()
  255. }
  256. <Captions onPress = { this._onClick } />
  257. {/*
  258. * The Toolbox is in a stacking layer bellow the Filmstrip.
  259. */}
  260. <Toolbox />
  261. {/*
  262. * The Filmstrip is in a stacking layer above the
  263. * LargeVideo. The LargeVideo and the Filmstrip form what
  264. * the Web/React app calls "videospace". Presumably, the
  265. * name and grouping stem from the fact that these two
  266. * React Components depict the videos of the conference's
  267. * participants.
  268. */}
  269. <Filmstrip />
  270. </View>
  271. <TestConnectionInfo />
  272. {
  273. this._renderConferenceNotification()
  274. }
  275. {/*
  276. * The dialogs are in the topmost stacking layers.
  277. */
  278. this.props._reducedUI || <DialogContainer />
  279. }
  280. </Container>
  281. );
  282. }
  283. _onClick: () => void;
  284. /**
  285. * Changes the value of the toolboxVisible state, thus allowing us to switch
  286. * between Toolbox and Filmstrip and change their visibility.
  287. *
  288. * @private
  289. * @returns {void}
  290. */
  291. _onClick() {
  292. if (this.props._toolboxAlwaysVisible) {
  293. return;
  294. }
  295. const toolboxVisible = !this.props._toolboxVisible;
  296. this.props._setToolboxVisible(toolboxVisible);
  297. }
  298. _onHardwareBackPress: () => boolean;
  299. /**
  300. * Handles a hardware button press for back navigation.
  301. *
  302. * @returns {boolean} If the hardware button press for back navigation was
  303. * handled by this {@code Conference}, then {@code true}; otherwise,
  304. * {@code false}.
  305. */
  306. _onHardwareBackPress() {
  307. return this._backHandler && this.props._onHardwareBackPress();
  308. }
  309. /**
  310. * Renders the conference notification badge if the feature is enabled.
  311. *
  312. * @private
  313. * @returns {React$Node}
  314. */
  315. _renderConferenceNotification() {
  316. // XXX If the calendar feature is disabled on a platform, then we don't
  317. // have its components exported so an undefined check is necessary.
  318. return (
  319. !this.props._reducedUI && ConferenceNotification
  320. ? <ConferenceNotification />
  321. : undefined);
  322. }
  323. /**
  324. * Renders a container for notifications to be displayed by the
  325. * base/notifications feature.
  326. *
  327. * @private
  328. * @returns {React$Element}
  329. */
  330. _renderNotificationsContainer() {
  331. const notificationsStyle = {};
  332. // In the landscape mode (wide) there's problem with notifications being
  333. // shadowed by the filmstrip rendered on the right. This makes the "x"
  334. // button not clickable. In order to avoid that a margin of the
  335. // filmstrip's size is added to the right.
  336. //
  337. // Pawel: after many attempts I failed to make notifications adjust to
  338. // their contents width because of column and rows being used in the
  339. // flex layout. The only option that seemed to limit the notification's
  340. // size was explicit 'width' value which is not better than the margin
  341. // added here.
  342. if (this.props._filmstripVisible && !isNarrowAspectRatio(this)) {
  343. notificationsStyle.marginRight = FILMSTRIP_SIZE;
  344. }
  345. return <NotificationsContainer style = { notificationsStyle } />;
  346. }
  347. }
  348. /**
  349. * Maps dispatching of some action to React component props.
  350. *
  351. * @param {Function} dispatch - Redux action dispatcher.
  352. * @private
  353. * @returns {{
  354. * _onConnect: Function,
  355. * _onDisconnect: Function,
  356. * _onHardwareBackPress: Function,
  357. * _setToolboxVisible: Function
  358. * }}
  359. */
  360. function _mapDispatchToProps(dispatch) {
  361. return {
  362. /**
  363. * Dispatches actions to create the desired local tracks and for
  364. * connecting to the conference.
  365. *
  366. * @private
  367. * @returns {void}
  368. */
  369. _onConnect() {
  370. dispatch(createDesiredLocalTracks());
  371. dispatch(connect());
  372. },
  373. /**
  374. * Dispatches an action disconnecting from the conference.
  375. *
  376. * @private
  377. * @returns {void}
  378. */
  379. _onDisconnect() {
  380. dispatch(disconnect());
  381. },
  382. /**
  383. * Handles a hardware button press for back navigation. Leaves the
  384. * associated {@code Conference}.
  385. *
  386. * @returns {boolean} As the associated conference is unconditionally
  387. * left and exiting the app while it renders a {@code Conference} is
  388. * undesired, {@code true} is always returned.
  389. */
  390. _onHardwareBackPress() {
  391. dispatch(appNavigate(undefined));
  392. return true;
  393. },
  394. /**
  395. * Dispatches an action changing the visibility of the {@link Toolbox}.
  396. *
  397. * @param {boolean} visible - {@code true} to show the {@code Toolbox}
  398. * or {@code false} to hide it.
  399. * @private
  400. * @returns {void}
  401. */
  402. _setToolboxVisible(visible) {
  403. dispatch(setToolboxVisible(visible));
  404. }
  405. };
  406. }
  407. /**
  408. * Maps (parts of) the redux state to the associated {@code Conference}'s props.
  409. *
  410. * @param {Object} state - The redux state.
  411. * @private
  412. * @returns {{
  413. * _connecting: boolean,
  414. * _filmstripVisible: boolean,
  415. * _locationURL: URL,
  416. * _participantCount: number,
  417. * _reducedUI: boolean,
  418. * _room: string,
  419. * _toolboxVisible: boolean,
  420. * _toolboxAlwaysVisible: boolean
  421. * }}
  422. */
  423. function _mapStateToProps(state) {
  424. const { connecting, connection, locationURL }
  425. = state['features/base/connection'];
  426. const {
  427. conference,
  428. joining,
  429. leaving,
  430. room
  431. } = state['features/base/conference'];
  432. const { reducedUI } = state['features/base/responsive-ui'];
  433. const { alwaysVisible, visible } = state['features/toolbox'];
  434. // XXX There is a window of time between the successful establishment of the
  435. // XMPP connection and the subsequent commencement of joining the MUC during
  436. // which the app does not appear to be doing anything according to the redux
  437. // state. In order to not toggle the _connecting props during the window of
  438. // time in question, define _connecting as follows:
  439. // - the XMPP connection is connecting, or
  440. // - the XMPP connection is connected and the conference is joining, or
  441. // - the XMPP connection is connected and we have no conference yet, nor we
  442. // are leaving one.
  443. const connecting_
  444. = connecting || (connection && (joining || (!conference && !leaving)));
  445. return {
  446. /**
  447. * The indicator which determines that we are still connecting to the
  448. * conference which includes establishing the XMPP connection and then
  449. * joining the room. If truthy, then an activity/loading indicator will
  450. * be rendered.
  451. *
  452. * @private
  453. * @type {boolean}
  454. */
  455. _connecting: Boolean(connecting_),
  456. /**
  457. * Is {@code true} when the filmstrip is currently visible.
  458. */
  459. _filmstripVisible: isFilmstripVisible(state),
  460. /**
  461. * Current conference's full URL.
  462. *
  463. * @private
  464. * @type {URL}
  465. */
  466. _locationURL: locationURL,
  467. /**
  468. * The number of participants in the conference.
  469. *
  470. * @private
  471. * @type {number}
  472. */
  473. _participantCount: getParticipantCount(state),
  474. /**
  475. * The indicator which determines whether the UI is reduced (to
  476. * accommodate smaller display areas).
  477. *
  478. * @private
  479. * @type {boolean}
  480. */
  481. _reducedUI: reducedUI,
  482. /**
  483. * The current conference room name.
  484. *
  485. * @private
  486. * @type {string}
  487. */
  488. _room: room,
  489. /**
  490. * The indicator which determines whether the Toolbox is visible.
  491. *
  492. * @private
  493. * @type {boolean}
  494. */
  495. _toolboxVisible: visible,
  496. /**
  497. * The indicator which determines whether the Toolbox is always visible.
  498. *
  499. * @private
  500. * @type {boolean}
  501. */
  502. _toolboxAlwaysVisible: alwaysVisible
  503. };
  504. }
  505. // $FlowFixMe
  506. export default reactReduxConnect(_mapStateToProps, _mapDispatchToProps)(
  507. makeAspectRatioAware(Conference));