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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 { Icon } from '../../../base/font-icons';
  9. import { connect } from '../../../base/redux';
  10. import { ColorPalette, type StyleType } from '../../../base/styles';
  11. import styles from './styles';
  12. /**
  13. * Type definition for a single entry in the device list.
  14. */
  15. type Device = {
  16. /**
  17. * Name of the icon which will be rendered on the right.
  18. */
  19. iconName: string,
  20. /**
  21. * True if the element is selected (will be highlighted in blue),
  22. * false otherwise.
  23. */
  24. selected: boolean,
  25. /**
  26. * Text which will be rendered in the row.
  27. */
  28. text: string,
  29. /**
  30. * Device type.
  31. */
  32. type: string
  33. };
  34. /**
  35. * {@code AudioRoutePickerDialog}'s React {@code Component} prop types.
  36. */
  37. type Props = {
  38. /**
  39. * Style of the bottom sheet feature.
  40. */
  41. _bottomSheetStyles: StyleType,
  42. /**
  43. * Used for hiding the dialog when the selection was completed.
  44. */
  45. dispatch: Function,
  46. /**
  47. * Invoked to obtain translated strings.
  48. */
  49. t: Function
  50. };
  51. /**
  52. * {@code AudioRoutePickerDialog}'s React {@code Component} state types.
  53. */
  54. type State = {
  55. /**
  56. * Array of available devices.
  57. */
  58. devices: Array<Device>
  59. };
  60. const { AudioMode } = NativeModules;
  61. /**
  62. * Maps each device type to a display name and icon.
  63. */
  64. const deviceInfoMap = {
  65. BLUETOOTH: {
  66. iconName: 'bluetooth',
  67. text: 'audioDevices.bluetooth',
  68. type: 'BLUETOOTH'
  69. },
  70. EARPIECE: {
  71. iconName: 'phone-talk',
  72. text: 'audioDevices.phone',
  73. type: 'EARPIECE'
  74. },
  75. HEADPHONES: {
  76. iconName: 'headset',
  77. text: 'audioDevices.headphones',
  78. type: 'HEADPHONES'
  79. },
  80. SPEAKER: {
  81. iconName: 'volume',
  82. text: 'audioDevices.speaker',
  83. type: 'SPEAKER'
  84. }
  85. };
  86. /**
  87. * The exported React {@code Component}. {@code AudioRoutePickerDialog} is
  88. * exported only if the {@code AudioMode} module has the capability to get / set
  89. * audio devices.
  90. */
  91. let AudioRoutePickerDialog_;
  92. /**
  93. * Implements a React {@code Component} which prompts the user when a password
  94. * is required to join a conference.
  95. */
  96. class AudioRoutePickerDialog extends Component<Props, State> {
  97. state = {
  98. /**
  99. * Available audio devices, it will be set in
  100. * {@link #componentDidMount()}.
  101. */
  102. devices: []
  103. };
  104. /**
  105. * Initializes a new {@code PasswordRequiredPrompt} instance.
  106. *
  107. * @param {Props} props - The read-only React {@code Component} props with
  108. * which the new instance is to be initialized.
  109. */
  110. constructor(props: Props) {
  111. super(props);
  112. // Bind event handlers so they are only bound once per instance.
  113. this._onCancel = this._onCancel.bind(this);
  114. }
  115. /**
  116. * Initializes the device list by querying {@code AudioMode}.
  117. *
  118. * @inheritdoc
  119. */
  120. componentDidMount() {
  121. AudioMode.getAudioDevices().then(({ devices, selected }) => {
  122. const audioDevices = [];
  123. if (devices) {
  124. for (const device of devices) {
  125. if (deviceInfoMap[device]) {
  126. const info = Object.assign({}, deviceInfoMap[device]);
  127. info.selected = device === selected;
  128. info.text = this.props.t(info.text);
  129. audioDevices.push(info);
  130. }
  131. }
  132. }
  133. if (audioDevices) {
  134. // Make sure devices is alphabetically sorted.
  135. this.setState({
  136. devices: _.sortBy(audioDevices, 'text')
  137. });
  138. }
  139. });
  140. }
  141. /**
  142. * Dispatches a redux action to hide this sheet.
  143. *
  144. * @returns {void}
  145. */
  146. _hide() {
  147. this.props.dispatch(hideDialog(AudioRoutePickerDialog_));
  148. }
  149. _onCancel: () => void;
  150. /**
  151. * Cancels the dialog by hiding it.
  152. *
  153. * @private
  154. * @returns {void}
  155. */
  156. _onCancel() {
  157. this._hide();
  158. }
  159. _onSelectDeviceFn: (Device) => Function;
  160. /**
  161. * Builds and returns a function which handles the selection of a device
  162. * on the sheet. The selected device will be used by {@code AudioMode}.
  163. *
  164. * @param {Device} device - Object representing the selected device.
  165. * @private
  166. * @returns {Function}
  167. */
  168. _onSelectDeviceFn(device: Device) {
  169. return () => {
  170. this._hide();
  171. AudioMode.setAudioDevice(device.type);
  172. };
  173. }
  174. /**
  175. * Renders a single device.
  176. *
  177. * @param {Device} device - Object representing a single device.
  178. * @private
  179. * @returns {ReactElement}
  180. */
  181. _renderDevice(device: Device) {
  182. const { _bottomSheetStyles } = this.props;
  183. const { iconName, selected, text } = device;
  184. const selectedStyle = selected ? styles.selectedText : {};
  185. return (
  186. <TouchableHighlight
  187. key = { device.type }
  188. onPress = { this._onSelectDeviceFn(device) }
  189. underlayColor = { ColorPalette.overflowMenuItemUnderlay } >
  190. <View style = { styles.deviceRow } >
  191. <Icon
  192. name = { iconName }
  193. style = { [ styles.deviceIcon, _bottomSheetStyles.iconStyle, selectedStyle ] } />
  194. <Text style = { [ styles.deviceText, _bottomSheetStyles.labelStyle, selectedStyle ] } >
  195. { text }
  196. </Text>
  197. </View>
  198. </TouchableHighlight>
  199. );
  200. }
  201. /**
  202. * Implements React's {@link Component#render()}.
  203. *
  204. * @inheritdoc
  205. * @returns {ReactElement}
  206. */
  207. render() {
  208. const { devices } = this.state;
  209. if (!devices.length) {
  210. return null;
  211. }
  212. return (
  213. <BottomSheet onCancel = { this._onCancel }>
  214. { this.state.devices.map(this._renderDevice, this) }
  215. </BottomSheet>
  216. );
  217. }
  218. }
  219. /**
  220. * Maps part of the Redux state to the props of this component.
  221. *
  222. * @param {Object} state - The Redux state.
  223. * @returns {Object}
  224. */
  225. function _mapStateToProps(state) {
  226. return {
  227. _bottomSheetStyles: ColorSchemeRegistry.get(state, 'BottomSheet')
  228. };
  229. }
  230. // Only export the dialog if we have support for getting / setting audio devices
  231. // in AudioMode.
  232. if (AudioMode.getAudioDevices && AudioMode.setAudioDevice) {
  233. AudioRoutePickerDialog_ = translate(connect(_mapStateToProps)(AudioRoutePickerDialog));
  234. }
  235. export default AudioRoutePickerDialog_;