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.

AudioRouteButton.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // @flow
  2. import React, { Component } from 'react';
  3. import {
  4. findNodeHandle,
  5. requireNativeComponent,
  6. NativeModules,
  7. View
  8. } from 'react-native';
  9. import { connect } from 'react-redux';
  10. import { openDialog } from '../../base/dialog';
  11. import { AudioRoutePickerDialog } from '../../mobile/audio-mode';
  12. import ToolbarButton from './ToolbarButton';
  13. /**
  14. * Define the {@code MPVolumeView} React component. It will only be available
  15. * on iOS.
  16. */
  17. let MPVolumeView;
  18. if (NativeModules.MPVolumeViewManager) {
  19. MPVolumeView = requireNativeComponent('MPVolumeView', null);
  20. }
  21. /**
  22. * Style required to hide the {@code MPVolumeView} view, since it's displayed
  23. * programmatically.
  24. */
  25. const HIDE_VIEW_STYLE = { display: 'none' };
  26. type Props = {
  27. /**
  28. * Used to show the {@code AudioRoutePickerDialog}.
  29. */
  30. dispatch: Function,
  31. /**
  32. * The name of the Icon of this {@code AudioRouteButton}.
  33. */
  34. iconName: string,
  35. /**
  36. * The style of the Icon of this {@code AudioRouteButton}.
  37. */
  38. iconStyle: Object,
  39. /**
  40. * {@code AudioRouteButton} styles.
  41. */
  42. style: Array<*> | Object,
  43. /**
  44. * The color underlying the button.
  45. */
  46. underlayColor: string
  47. };
  48. /**
  49. * A toolbar button which triggers an audio route picker when pressed.
  50. */
  51. class AudioRouteButton extends Component<Props> {
  52. _volumeComponent: ?Object;
  53. /**
  54. * Indicates if there is support for audio device selection via this button.
  55. *
  56. * @returns {boolean} - True if audio device selection is supported, false
  57. * otherwise.
  58. */
  59. static supported() {
  60. return Boolean(MPVolumeView || AudioRoutePickerDialog);
  61. }
  62. /**
  63. * Initializes a new {@code AudioRouteButton} instance.
  64. *
  65. * @param {Object} props - The React {@code Component} props to initialize
  66. * the new {@code AudioRouteButton} instance with.
  67. */
  68. constructor(props) {
  69. super(props);
  70. /**
  71. * The internal reference to the React {@code MPVolumeView} for
  72. * showing the volume control view.
  73. *
  74. * @private
  75. * @type {ReactComponent}
  76. */
  77. this._volumeComponent = null;
  78. // Bind event handlers so they are only bound once per instance.
  79. this._onClick = this._onClick.bind(this);
  80. this._setVolumeComponent = this._setVolumeComponent.bind(this);
  81. }
  82. _onClick: () => void;
  83. /**
  84. * Handles clicking/pressing this {@code AudioRouteButton} by showing an
  85. * audio route picker.
  86. *
  87. * @private
  88. * @returns {void}
  89. */
  90. _onClick() {
  91. if (MPVolumeView) {
  92. const handle = findNodeHandle(this._volumeComponent);
  93. NativeModules.MPVolumeViewManager.show(handle);
  94. } else if (AudioRoutePickerDialog) {
  95. this.props.dispatch(openDialog(AudioRoutePickerDialog));
  96. }
  97. }
  98. _setVolumeComponent: (?Object) => void;
  99. /**
  100. * Sets the internal reference to the React Component wrapping the
  101. * {@code MPVolumeView} component.
  102. *
  103. * @param {ReactComponent} component - React Component.
  104. * @returns {void}
  105. */
  106. _setVolumeComponent(component) {
  107. this._volumeComponent = component;
  108. }
  109. /**
  110. * Implements React's {@link Component#render()}.
  111. *
  112. * @inheritdoc
  113. * @returns {ReactElement}
  114. */
  115. render() {
  116. const { iconName, iconStyle, style, underlayColor } = this.props;
  117. return (
  118. <View>
  119. <ToolbarButton
  120. iconName = { iconName }
  121. iconStyle = { iconStyle }
  122. onClick = { this._onClick }
  123. style = { style }
  124. underlayColor = { underlayColor } />
  125. {
  126. MPVolumeView
  127. && <MPVolumeView
  128. ref = { this._setVolumeComponent }
  129. style = { HIDE_VIEW_STYLE } />
  130. }
  131. </View>
  132. );
  133. }
  134. }
  135. export default connect()(AudioRouteButton);