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

AudioRoutePickerDialog.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. const text = device.type === 'BLUETOOTH' && device.name ? device.name : infoMap.text;
  151. if (infoMap) {
  152. const info = {
  153. ...infoMap,
  154. selected: Boolean(device.selected),
  155. text: props.t(text),
  156. uid: device.uid
  157. };
  158. audioDevices.push(info);
  159. }
  160. }
  161. // Make sure devices is alphabetically sorted.
  162. return {
  163. devices: _.sortBy(audioDevices, 'text')
  164. };
  165. }
  166. /**
  167. * Initializes a new {@code PasswordRequiredPrompt} instance.
  168. *
  169. * @param {Props} props - The read-only React {@code Component} props with
  170. * which the new instance is to be initialized.
  171. */
  172. constructor(props: Props) {
  173. super(props);
  174. // Bind event handlers so they are only bound once per instance.
  175. this._onCancel = this._onCancel.bind(this);
  176. // Trigger an initial update.
  177. AudioMode.updateDeviceList && AudioMode.updateDeviceList();
  178. }
  179. /**
  180. * Dispatches a redux action to hide this sheet.
  181. *
  182. * @returns {void}
  183. */
  184. _hide() {
  185. this.props.dispatch(hideDialog(AudioRoutePickerDialog_));
  186. }
  187. _onCancel: () => void;
  188. /**
  189. * Cancels the dialog by hiding it.
  190. *
  191. * @private
  192. * @returns {void}
  193. */
  194. _onCancel() {
  195. this._hide();
  196. }
  197. _onSelectDeviceFn: (Device) => Function;
  198. /**
  199. * Builds and returns a function which handles the selection of a device
  200. * on the sheet. The selected device will be used by {@code AudioMode}.
  201. *
  202. * @param {Device} device - Object representing the selected device.
  203. * @private
  204. * @returns {Function}
  205. */
  206. _onSelectDeviceFn(device: Device) {
  207. return () => {
  208. this._hide();
  209. AudioMode.setAudioDevice(device.uid || device.type);
  210. };
  211. }
  212. /**
  213. * Renders a single device.
  214. *
  215. * @param {Device} device - Object representing a single device.
  216. * @private
  217. * @returns {ReactElement}
  218. */
  219. _renderDevice(device: Device) {
  220. const { _bottomSheetStyles } = this.props;
  221. const { icon, selected, text } = device;
  222. const selectedStyle = selected ? styles.selectedText : {};
  223. return (
  224. <TouchableHighlight
  225. key = { device.type }
  226. onPress = { this._onSelectDeviceFn(device) }
  227. underlayColor = { ColorPalette.overflowMenuItemUnderlay } >
  228. <View style = { styles.deviceRow } >
  229. <Icon
  230. src = { icon }
  231. style = { [ styles.deviceIcon, _bottomSheetStyles.buttons.iconStyle, selectedStyle ] } />
  232. <Text style = { [ styles.deviceText, _bottomSheetStyles.buttons.labelStyle, selectedStyle ] } >
  233. { text }
  234. </Text>
  235. </View>
  236. </TouchableHighlight>
  237. );
  238. }
  239. /**
  240. * Renders a "fake" device row indicating there are no devices.
  241. *
  242. * @private
  243. * @returns {ReactElement}
  244. */
  245. _renderNoDevices() {
  246. const { _bottomSheetStyles, t } = this.props;
  247. return (
  248. <View style = { styles.deviceRow } >
  249. <Icon
  250. src = { deviceInfoMap.SPEAKER.icon }
  251. style = { [ styles.deviceIcon, _bottomSheetStyles.buttons.iconStyle ] } />
  252. <Text style = { [ styles.deviceText, _bottomSheetStyles.buttons.labelStyle ] } >
  253. { t('audioDevices.none') }
  254. </Text>
  255. </View>
  256. );
  257. }
  258. /**
  259. * Implements React's {@link Component#render()}.
  260. *
  261. * @inheritdoc
  262. * @returns {ReactElement}
  263. */
  264. render() {
  265. const { devices } = this.state;
  266. let content;
  267. if (devices.length === 0) {
  268. content = this._renderNoDevices();
  269. } else {
  270. content = this.state.devices.map(this._renderDevice, this);
  271. }
  272. return (
  273. <BottomSheet onCancel = { this._onCancel }>
  274. { content }
  275. </BottomSheet>
  276. );
  277. }
  278. }
  279. /**
  280. * Maps part of the Redux state to the props of this component.
  281. *
  282. * @param {Object} state - The Redux state.
  283. * @returns {Object}
  284. */
  285. function _mapStateToProps(state) {
  286. return {
  287. _bottomSheetStyles: ColorSchemeRegistry.get(state, 'BottomSheet'),
  288. _devices: state['features/mobile/audio-mode'].devices
  289. };
  290. }
  291. AudioRoutePickerDialog_ = translate(connect(_mapStateToProps)(AudioRoutePickerDialog));
  292. export default AudioRoutePickerDialog_;