Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AudioRoutePickerDialog.js 8.5KB

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