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.

AudioRoutePickerDialog.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // @flow
  2. import _ from 'lodash';
  3. import React, { Component } from 'react';
  4. import { NativeModules, Text, TouchableHighlight, View } from 'react-native';
  5. import { ColorSchemeRegistry } from '../../../base/color-scheme';
  6. import { hideDialog, BottomSheet } from '../../../base/dialog';
  7. import { translate } from '../../../base/i18n';
  8. import { Icon } from '../../../base/font-icons';
  9. import { connect } from '../../../base/redux';
  10. import { ColorPalette, type StyleType } from '../../../base/styles';
  11. import styles from './styles';
  12. const { AudioMode } = NativeModules;
  13. /**
  14. * Type definition for a single entry in the device list.
  15. */
  16. type Device = {
  17. /**
  18. * Name of the icon which will be rendered on the right.
  19. */
  20. iconName: string,
  21. /**
  22. * True if the element is selected (will be highlighted in blue),
  23. * false otherwise.
  24. */
  25. selected: boolean,
  26. /**
  27. * Text which will be rendered in the row.
  28. */
  29. text: string,
  30. /**
  31. * Device type.
  32. */
  33. type: string,
  34. /**
  35. * Unique device ID.
  36. */
  37. uid: ?string
  38. };
  39. /**
  40. * "Raw" device, as returned by native.
  41. */
  42. type RawDevice = {
  43. /**
  44. * Display name for the device.
  45. */
  46. name: ?string,
  47. /**
  48. * is this device selected?
  49. */
  50. selected: boolean,
  51. /**
  52. * Device type.
  53. */
  54. type: string,
  55. /**
  56. * Unique device ID.
  57. */
  58. uid: ?string
  59. };
  60. /**
  61. * {@code AudioRoutePickerDialog}'s React {@code Component} prop types.
  62. */
  63. type Props = {
  64. /**
  65. * Style of the bottom sheet feature.
  66. */
  67. _bottomSheetStyles: StyleType,
  68. /**
  69. * Object describing available devices.
  70. */
  71. _devices: Array<RawDevice>,
  72. /**
  73. * Used for hiding the dialog when the selection was completed.
  74. */
  75. dispatch: Function,
  76. /**
  77. * Invoked to obtain translated strings.
  78. */
  79. t: Function
  80. };
  81. /**
  82. * {@code AudioRoutePickerDialog}'s React {@code Component} state types.
  83. */
  84. type State = {
  85. /**
  86. * Array of available devices.
  87. */
  88. devices: Array<Device>
  89. };
  90. /**
  91. * Maps each device type to a display name and icon.
  92. */
  93. const deviceInfoMap = {
  94. BLUETOOTH: {
  95. iconName: 'bluetooth',
  96. text: 'audioDevices.bluetooth',
  97. type: 'BLUETOOTH'
  98. },
  99. EARPIECE: {
  100. iconName: 'phone-talk',
  101. text: 'audioDevices.phone',
  102. type: 'EARPIECE'
  103. },
  104. HEADPHONES: {
  105. iconName: 'headset',
  106. text: 'audioDevices.headphones',
  107. type: 'HEADPHONES'
  108. },
  109. SPEAKER: {
  110. iconName: 'volume',
  111. text: 'audioDevices.speaker',
  112. type: 'SPEAKER'
  113. }
  114. };
  115. /**
  116. * The exported React {@code Component}.
  117. */
  118. let AudioRoutePickerDialog_; // eslint-disable-line prefer-const
  119. /**
  120. * Implements a React {@code Component} which prompts the user when a password
  121. * is required to join a conference.
  122. */
  123. class AudioRoutePickerDialog extends Component<Props, State> {
  124. state = {
  125. /**
  126. * Available audio devices, it will be set in
  127. * {@link #getDerivedStateFromProps()}.
  128. */
  129. devices: []
  130. };
  131. /**
  132. * Implements React's {@link Component#getDerivedStateFromProps()}.
  133. *
  134. * @inheritdoc
  135. */
  136. static getDerivedStateFromProps(props: Props) {
  137. const { _devices: devices } = props;
  138. if (!devices) {
  139. return null;
  140. }
  141. const audioDevices = [];
  142. for (const device of devices) {
  143. const infoMap = deviceInfoMap[device.type];
  144. const text = device.type === 'BLUETOOTH' && device.name ? device.name : infoMap.text;
  145. if (infoMap) {
  146. const info = {
  147. ...infoMap,
  148. selected: Boolean(device.selected),
  149. text: props.t(text),
  150. uid: device.uid
  151. };
  152. audioDevices.push(info);
  153. }
  154. }
  155. // Make sure devices is alphabetically sorted.
  156. return {
  157. devices: _.sortBy(audioDevices, 'text')
  158. };
  159. }
  160. /**
  161. * Initializes a new {@code PasswordRequiredPrompt} instance.
  162. *
  163. * @param {Props} props - The read-only React {@code Component} props with
  164. * which the new instance is to be initialized.
  165. */
  166. constructor(props: Props) {
  167. super(props);
  168. // Bind event handlers so they are only bound once per instance.
  169. this._onCancel = this._onCancel.bind(this);
  170. // Trigger an initial update.
  171. AudioMode.updateDeviceList && AudioMode.updateDeviceList();
  172. }
  173. /**
  174. * Dispatches a redux action to hide this sheet.
  175. *
  176. * @returns {void}
  177. */
  178. _hide() {
  179. this.props.dispatch(hideDialog(AudioRoutePickerDialog_));
  180. }
  181. _onCancel: () => void;
  182. /**
  183. * Cancels the dialog by hiding it.
  184. *
  185. * @private
  186. * @returns {void}
  187. */
  188. _onCancel() {
  189. this._hide();
  190. }
  191. _onSelectDeviceFn: (Device) => Function;
  192. /**
  193. * Builds and returns a function which handles the selection of a device
  194. * on the sheet. The selected device will be used by {@code AudioMode}.
  195. *
  196. * @param {Device} device - Object representing the selected device.
  197. * @private
  198. * @returns {Function}
  199. */
  200. _onSelectDeviceFn(device: Device) {
  201. return () => {
  202. this._hide();
  203. AudioMode.setAudioDevice(device.uid || device.type);
  204. };
  205. }
  206. /**
  207. * Renders a single device.
  208. *
  209. * @param {Device} device - Object representing a single device.
  210. * @private
  211. * @returns {ReactElement}
  212. */
  213. _renderDevice(device: Device) {
  214. const { _bottomSheetStyles } = this.props;
  215. const { iconName, selected, text } = device;
  216. const selectedStyle = selected ? styles.selectedText : {};
  217. return (
  218. <TouchableHighlight
  219. key = { device.type }
  220. onPress = { this._onSelectDeviceFn(device) }
  221. underlayColor = { ColorPalette.overflowMenuItemUnderlay } >
  222. <View style = { styles.deviceRow } >
  223. <Icon
  224. name = { iconName }
  225. style = { [ styles.deviceIcon, _bottomSheetStyles.iconStyle, selectedStyle ] } />
  226. <Text style = { [ styles.deviceText, _bottomSheetStyles.labelStyle, selectedStyle ] } >
  227. { text }
  228. </Text>
  229. </View>
  230. </TouchableHighlight>
  231. );
  232. }
  233. /**
  234. * Renders a "fake" device row indicating there are no devices.
  235. *
  236. * @private
  237. * @returns {ReactElement}
  238. */
  239. _renderNoDevices() {
  240. const { _bottomSheetStyles, t } = this.props;
  241. return (
  242. <View style = { styles.deviceRow } >
  243. <Icon
  244. name = { deviceInfoMap.SPEAKER.iconName }
  245. style = { [ styles.deviceIcon, _bottomSheetStyles.iconStyle ] } />
  246. <Text style = { [ styles.deviceText, _bottomSheetStyles.labelStyle ] } >
  247. { t('audioDevices.none') }
  248. </Text>
  249. </View>
  250. );
  251. }
  252. /**
  253. * Implements React's {@link Component#render()}.
  254. *
  255. * @inheritdoc
  256. * @returns {ReactElement}
  257. */
  258. render() {
  259. const { devices } = this.state;
  260. let content;
  261. if (devices.length === 0) {
  262. content = this._renderNoDevices();
  263. } else {
  264. content = this.state.devices.map(this._renderDevice, this);
  265. }
  266. return (
  267. <BottomSheet onCancel = { this._onCancel }>
  268. { content }
  269. </BottomSheet>
  270. );
  271. }
  272. }
  273. /**
  274. * Maps part of the Redux state to the props of this component.
  275. *
  276. * @param {Object} state - The Redux state.
  277. * @returns {Object}
  278. */
  279. function _mapStateToProps(state) {
  280. return {
  281. _bottomSheetStyles: ColorSchemeRegistry.get(state, 'BottomSheet'),
  282. _devices: state['features/mobile/audio-mode'].devices
  283. };
  284. }
  285. AudioRoutePickerDialog_ = translate(connect(_mapStateToProps)(AudioRoutePickerDialog));
  286. export default AudioRoutePickerDialog_;