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.2KB

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