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

AudioRoutePickerDialog.tsx 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import _ from 'lodash';
  2. import React, { Component } from 'react';
  3. import { NativeModules, Text, TextStyle, TouchableHighlight, View, ViewStyle } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { IReduxState, IStore } from '../../../app/types';
  6. import { hideSheet } from '../../../base/dialog/actions';
  7. import BottomSheet from '../../../base/dialog/components/native/BottomSheet';
  8. import { bottomSheetStyles } from '../../../base/dialog/components/native/styles';
  9. import { translate } from '../../../base/i18n/functions';
  10. import Icon from '../../../base/icons/components/Icon';
  11. import {
  12. IconBluetooth,
  13. IconCar,
  14. IconDeviceHeadphone,
  15. IconPhoneRinging,
  16. IconVolumeUp
  17. } from '../../../base/icons/svg';
  18. import { StyleType } from '../../../base/styles/functions.any';
  19. import BaseTheme from '../../../base/ui/components/BaseTheme.native';
  20. import styles from './styles';
  21. const { AudioMode } = NativeModules;
  22. /**
  23. * Type definition for a single entry in the device list.
  24. */
  25. interface IDevice {
  26. /**
  27. * Name of the icon which will be rendered on the right.
  28. */
  29. icon: Function;
  30. /**
  31. * True if the element is selected (will be highlighted in blue),
  32. * false otherwise.
  33. */
  34. selected: boolean;
  35. /**
  36. * Text which will be rendered in the row.
  37. */
  38. text: string;
  39. /**
  40. * Device type.
  41. */
  42. type: string;
  43. /**
  44. * Unique device ID.
  45. */
  46. uid?: string;
  47. }
  48. /**
  49. * "Raw" device, as returned by native.
  50. */
  51. export interface IRawDevice {
  52. /**
  53. * Display name for the device.
  54. */
  55. name?: string;
  56. /**
  57. * Is this device selected?
  58. */
  59. selected: boolean;
  60. /**
  61. * Device type.
  62. */
  63. type: string;
  64. /**
  65. * Unique device ID.
  66. */
  67. uid?: string;
  68. }
  69. /**
  70. * {@code AudioRoutePickerDialog}'s React {@code Component} prop types.
  71. */
  72. interface IProps {
  73. /**
  74. * Object describing available devices.
  75. */
  76. _devices: Array<IRawDevice>;
  77. /**
  78. * Used for hiding the dialog when the selection was completed.
  79. */
  80. dispatch: IStore['dispatch'];
  81. /**
  82. * Invoked to obtain translated strings.
  83. */
  84. t: Function;
  85. }
  86. /**
  87. * {@code AudioRoutePickerDialog}'s React {@code Component} state types.
  88. */
  89. interface IState {
  90. /**
  91. * Array of available devices.
  92. */
  93. devices: Array<IDevice>;
  94. }
  95. /**
  96. * Maps each device type to a display name and icon.
  97. */
  98. const deviceInfoMap = {
  99. BLUETOOTH: {
  100. icon: IconBluetooth,
  101. text: 'audioDevices.bluetooth',
  102. type: 'BLUETOOTH'
  103. },
  104. CAR: {
  105. icon: IconCar,
  106. text: 'audioDevices.car',
  107. type: 'CAR'
  108. },
  109. EARPIECE: {
  110. icon: IconPhoneRinging,
  111. text: 'audioDevices.phone',
  112. type: 'EARPIECE'
  113. },
  114. HEADPHONES: {
  115. icon: IconDeviceHeadphone,
  116. text: 'audioDevices.headphones',
  117. type: 'HEADPHONES'
  118. },
  119. SPEAKER: {
  120. icon: IconVolumeUp,
  121. text: 'audioDevices.speaker',
  122. type: 'SPEAKER'
  123. }
  124. };
  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<IProps, IState> {
  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: IProps) {
  143. const { _devices: devices, t } = props;
  144. if (!devices) {
  145. return null;
  146. }
  147. const audioDevices = [];
  148. for (const device of devices) {
  149. const infoMap = deviceInfoMap[device.type as keyof typeof deviceInfoMap];
  150. // Skip devices with unknown type.
  151. if (!infoMap) {
  152. // eslint-disable-next-line no-continue
  153. continue;
  154. }
  155. let text = t(infoMap.text);
  156. // iOS provides descriptive names for these, use it.
  157. if ((device.type === 'BLUETOOTH' || device.type === 'CAR') && device.name) {
  158. text = device.name;
  159. }
  160. if (infoMap) {
  161. const info = {
  162. ...infoMap,
  163. selected: Boolean(device.selected),
  164. text,
  165. uid: device.uid
  166. };
  167. audioDevices.push(info);
  168. }
  169. }
  170. // Make sure devices is alphabetically sorted.
  171. return {
  172. devices: _.sortBy(audioDevices, 'text')
  173. };
  174. }
  175. /**
  176. * Initializes a new {@code PasswordRequiredPrompt} instance.
  177. *
  178. * @param {IProps} props - The read-only React {@code Component} props with
  179. * which the new instance is to be initialized.
  180. */
  181. constructor(props: IProps) {
  182. super(props);
  183. // Trigger an initial update.
  184. AudioMode.updateDeviceList?.();
  185. }
  186. /**
  187. * Builds and returns a function which handles the selection of a device
  188. * on the sheet. The selected device will be used by {@code AudioMode}.
  189. *
  190. * @param {IDevice} device - Object representing the selected device.
  191. * @private
  192. * @returns {Function}
  193. */
  194. _onSelectDeviceFn(device: IDevice) {
  195. return () => {
  196. this.props.dispatch(hideSheet());
  197. AudioMode.setAudioDevice(device.uid || device.type);
  198. };
  199. }
  200. /**
  201. * Renders a single device.
  202. *
  203. * @param {IDevice} device - Object representing a single device.
  204. * @private
  205. * @returns {ReactElement}
  206. */
  207. _renderDevice(device: IDevice) {
  208. const { icon, selected, text } = device;
  209. const selectedStyle = selected ? styles.selectedText : {};
  210. const borderRadiusHighlightStyles = {
  211. borderTopLeftRadius: 16,
  212. borderTopRightRadius: 16
  213. };
  214. const speakerDeviceIsNotSelected = device.type !== 'SPEAKER';
  215. return (
  216. <TouchableHighlight
  217. key = { device.type }
  218. onPress = { this._onSelectDeviceFn(device) }
  219. style = { speakerDeviceIsNotSelected && borderRadiusHighlightStyles }
  220. underlayColor = { BaseTheme.palette.ui04 } >
  221. <View style = { styles.deviceRow as ViewStyle } >
  222. <Icon
  223. src = { icon }
  224. style = { [ styles.deviceIcon, bottomSheetStyles.buttons.iconStyle, selectedStyle
  225. ] as StyleType[] } />
  226. <Text
  227. style = { [ styles.deviceText, bottomSheetStyles.buttons.labelStyle, selectedStyle
  228. ] as TextStyle[] } >
  229. { text }
  230. </Text>
  231. </View>
  232. </TouchableHighlight>
  233. );
  234. }
  235. /**
  236. * Renders a "fake" device row indicating there are no devices.
  237. *
  238. * @private
  239. * @returns {ReactElement}
  240. */
  241. _renderNoDevices() {
  242. const { t } = this.props;
  243. return (
  244. <View style = { styles.deviceRow as ViewStyle } >
  245. <Icon
  246. src = { deviceInfoMap.SPEAKER.icon }
  247. style = { [ styles.deviceIcon, bottomSheetStyles.buttons.iconStyle ] as StyleType[] } />
  248. <Text style = { [ styles.deviceText, bottomSheetStyles.buttons.labelStyle ] as TextStyle[] } >
  249. { t('audioDevices.none') }
  250. </Text>
  251. </View>
  252. );
  253. }
  254. /**
  255. * Implements React's {@link Component#render()}.
  256. *
  257. * @inheritdoc
  258. * @returns {ReactElement}
  259. */
  260. render() {
  261. const { devices } = this.state;
  262. let content;
  263. if (devices.length === 0) {
  264. content = this._renderNoDevices();
  265. } else {
  266. content = this.state.devices.map(this._renderDevice, this);
  267. }
  268. return (
  269. <BottomSheet>
  270. { content }
  271. </BottomSheet>
  272. );
  273. }
  274. }
  275. /**
  276. * Maps part of the Redux state to the props of this component.
  277. *
  278. * @param {Object} state - The Redux state.
  279. * @returns {Object}
  280. */
  281. function _mapStateToProps(state: IReduxState) {
  282. return {
  283. _devices: state['features/mobile/audio-mode'].devices
  284. };
  285. }
  286. export default translate(connect(_mapStateToProps)(AudioRoutePickerDialog));