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.3KB

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