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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import _ from 'lodash';
  2. import React, { Component } from 'react';
  3. import { NativeModules, Text, TouchableHighlight, View } from 'react-native';
  4. import { BottomSheet, hideSheet } from '../../../base/dialog';
  5. import { bottomSheetStyles } from '../../../base/dialog/components/native/styles';
  6. import { translate } from '../../../base/i18n';
  7. import {
  8. Icon,
  9. IconBluetooth,
  10. IconCar,
  11. IconDeviceHeadphone,
  12. IconPhoneRinging,
  13. IconVolumeUp
  14. } from '../../../base/icons';
  15. import { connect } from '../../../base/redux';
  16. import BaseTheme from '../../../base/ui/components/BaseTheme.native';
  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. * Object describing available devices.
  72. */
  73. _devices: Array<RawDevice>,
  74. /**
  75. * Used for hiding the dialog when the selection was completed.
  76. */
  77. dispatch: Function,
  78. /**
  79. * Invoked to obtain translated strings.
  80. */
  81. t: Function
  82. };
  83. /**
  84. * {@code AudioRoutePickerDialog}'s React {@code Component} state types.
  85. */
  86. type State = {
  87. /**
  88. * Array of available devices.
  89. */
  90. devices: Array<Device>
  91. };
  92. /**
  93. * Maps each device type to a display name and icon.
  94. */
  95. const deviceInfoMap = {
  96. BLUETOOTH: {
  97. icon: IconBluetooth,
  98. text: 'audioDevices.bluetooth',
  99. type: 'BLUETOOTH'
  100. },
  101. CAR: {
  102. icon: IconCar,
  103. text: 'audioDevices.car',
  104. type: 'CAR'
  105. },
  106. EARPIECE: {
  107. icon: IconPhoneRinging,
  108. text: 'audioDevices.phone',
  109. type: 'EARPIECE'
  110. },
  111. HEADPHONES: {
  112. icon: IconDeviceHeadphone,
  113. text: 'audioDevices.headphones',
  114. type: 'HEADPHONES'
  115. },
  116. SPEAKER: {
  117. icon: IconVolumeUp,
  118. text: 'audioDevices.speaker',
  119. type: 'SPEAKER'
  120. }
  121. };
  122. /**
  123. * Implements a React {@code Component} which prompts the user when a password
  124. * is required to join a conference.
  125. */
  126. class AudioRoutePickerDialog extends Component<Props, State> {
  127. state = {
  128. /**
  129. * Available audio devices, it will be set in
  130. * {@link #getDerivedStateFromProps()}.
  131. */
  132. devices: []
  133. };
  134. /**
  135. * Implements React's {@link Component#getDerivedStateFromProps()}.
  136. *
  137. * @inheritdoc
  138. */
  139. static getDerivedStateFromProps(props: Props) {
  140. const { _devices: devices, t } = props;
  141. if (!devices) {
  142. return null;
  143. }
  144. const audioDevices = [];
  145. for (const device of devices) {
  146. const infoMap = deviceInfoMap[device.type];
  147. // Skip devices with unknown type.
  148. if (!infoMap) {
  149. // eslint-disable-next-line no-continue
  150. continue;
  151. }
  152. let text = t(infoMap.text);
  153. // iOS provides descriptive names for these, use it.
  154. if ((device.type === 'BLUETOOTH' || device.type === 'CAR') && device.name) {
  155. text = device.name;
  156. }
  157. if (infoMap) {
  158. const info = {
  159. ...infoMap,
  160. selected: Boolean(device.selected),
  161. text,
  162. uid: device.uid
  163. };
  164. audioDevices.push(info);
  165. }
  166. }
  167. // Make sure devices is alphabetically sorted.
  168. return {
  169. devices: _.sortBy(audioDevices, 'text')
  170. };
  171. }
  172. /**
  173. * Initializes a new {@code PasswordRequiredPrompt} instance.
  174. *
  175. * @param {Props} props - The read-only React {@code Component} props with
  176. * which the new instance is to be initialized.
  177. */
  178. constructor(props: Props) {
  179. super(props);
  180. // Trigger an initial update.
  181. AudioMode.updateDeviceList && AudioMode.updateDeviceList();
  182. }
  183. /**
  184. * Builds and returns a function which handles the selection of a device
  185. * on the sheet. The selected device will be used by {@code AudioMode}.
  186. *
  187. * @param {Device} device - Object representing the selected device.
  188. * @private
  189. * @returns {Function}
  190. */
  191. _onSelectDeviceFn(device: Device) {
  192. return () => {
  193. this.props.dispatch(hideSheet());
  194. AudioMode.setAudioDevice(device.uid || device.type);
  195. };
  196. }
  197. /**
  198. * Renders a single device.
  199. *
  200. * @param {Device} device - Object representing a single device.
  201. * @private
  202. * @returns {ReactElement}
  203. */
  204. _renderDevice(device: Device) {
  205. const { icon, selected, text } = device;
  206. const selectedStyle = selected ? styles.selectedText : {};
  207. const borderRadiusHighlightStyles = {
  208. borderTopLeftRadius: 16,
  209. borderTopRightRadius: 16
  210. };
  211. const speakerDeviceIsNotSelected = device.type !== 'SPEAKER';
  212. return (
  213. <TouchableHighlight
  214. key = { device.type }
  215. onPress = { this._onSelectDeviceFn(device) }
  216. style = { speakerDeviceIsNotSelected && borderRadiusHighlightStyles }
  217. underlayColor = { BaseTheme.palette.underlay01 } >
  218. <View style = { styles.deviceRow } >
  219. <Icon
  220. src = { icon }
  221. style = { [ styles.deviceIcon, bottomSheetStyles.buttons.iconStyle, selectedStyle ] } />
  222. <Text style = { [ styles.deviceText, bottomSheetStyles.buttons.labelStyle, selectedStyle ] } >
  223. { text }
  224. </Text>
  225. </View>
  226. </TouchableHighlight>
  227. );
  228. }
  229. /**
  230. * Renders a "fake" device row indicating there are no devices.
  231. *
  232. * @private
  233. * @returns {ReactElement}
  234. */
  235. _renderNoDevices() {
  236. const { t } = this.props;
  237. return (
  238. <View style = { styles.deviceRow } >
  239. <Icon
  240. src = { deviceInfoMap.SPEAKER.icon }
  241. style = { [ styles.deviceIcon, bottomSheetStyles.buttons.iconStyle ] } />
  242. <Text style = { [ styles.deviceText, bottomSheetStyles.buttons.labelStyle ] } >
  243. { t('audioDevices.none') }
  244. </Text>
  245. </View>
  246. );
  247. }
  248. /**
  249. * Implements React's {@link Component#render()}.
  250. *
  251. * @inheritdoc
  252. * @returns {ReactElement}
  253. */
  254. render() {
  255. const { devices } = this.state;
  256. let content;
  257. if (devices.length === 0) {
  258. content = this._renderNoDevices();
  259. } else {
  260. content = this.state.devices.map(this._renderDevice, this);
  261. }
  262. return (
  263. <BottomSheet>
  264. { content }
  265. </BottomSheet>
  266. );
  267. }
  268. }
  269. /**
  270. * Maps part of the Redux state to the props of this component.
  271. *
  272. * @param {Object} state - The Redux state.
  273. * @returns {Object}
  274. */
  275. function _mapStateToProps(state) {
  276. return {
  277. _devices: state['features/mobile/audio-mode'].devices
  278. };
  279. }
  280. export default translate(connect(_mapStateToProps)(AudioRoutePickerDialog));