Comply w/ coding style
- Maximum of 80 characters per line. - Group first and then sort in alphabetical order. - Fields should begin with a lowercase letter.
This commit is contained in:
parent
2edaaac7bf
commit
ef39baab47
|
@ -3,7 +3,7 @@
|
||||||
android:versionCode="1"
|
android:versionCode="1"
|
||||||
android:versionName="1.0">
|
android:versionName="1.0">
|
||||||
|
|
||||||
<!-- XXX: ACCESS_NETWORK_STATE is needed by react-native-webrtc -->
|
<!-- XXX: ACCESS_NETWORK_STATE is required by WebRTC. -->
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH" />
|
<uses-permission android:name="android.permission.BLUETOOTH" />
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
|
|
|
@ -22,33 +22,43 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Module implementing a simple API to select the appropriate audio device for a conference call.
|
* Module implementing a simple API to select the appropriate audio device for a
|
||||||
|
* conference call.
|
||||||
*
|
*
|
||||||
* Audio calls should use <tt>AudioModeModule.AUDIO_CALL</tt>, which uses the builtin earpiece,
|
* Audio calls should use <tt>AudioModeModule.AUDIO_CALL</tt>, which uses the
|
||||||
* wired headset or bluetooth headset. The builtin earpiece is the default audio device.
|
* builtin earpiece, wired headset or bluetooth headset. The builtin earpiece is
|
||||||
|
* the default audio device.
|
||||||
*
|
*
|
||||||
* Video calls should should use <tt>AudioModeModule.VIDEO_CALL</tt>, which uses the builtin
|
* Video calls should should use <tt>AudioModeModule.VIDEO_CALL</tt>, which uses
|
||||||
* speaker, earpiece, wired headset or bluetooth headset. The builtin speaker is the default
|
* the builtin speaker, earpiece, wired headset or bluetooth headset. The
|
||||||
* audio device.
|
* builtin speaker is the default audio device.
|
||||||
*
|
*
|
||||||
* Before a call has started and after it has ended the <tt>AudioModeModule.DEFAULT</tt> mode
|
* Before a call has started and after it has ended the
|
||||||
* should be used.
|
* <tt>AudioModeModule.DEFAULT</tt> mode should be used.
|
||||||
*/
|
*/
|
||||||
public class AudioModeModule extends ReactContextBaseJavaModule {
|
public class AudioModeModule extends ReactContextBaseJavaModule {
|
||||||
/**
|
/**
|
||||||
* Constants representing the audio mode.
|
* Constants representing the audio mode.
|
||||||
* - DEFAULT: Used before and after every call. It represents the default audio routing scheme.
|
* - DEFAULT: Used before and after every call. It represents the default
|
||||||
* - AUDIO_CALL: Used for audio only calls. It will use the earpiece by default, unless a
|
* audio routing scheme.
|
||||||
* wired or Bluetooth headset is connected.
|
* - AUDIO_CALL: Used for audio only calls. It will use the earpiece by
|
||||||
* - VIDEO_CALL: Used for video calls. It will use the speaker by default, unless a wired or
|
* default, unless a wired or Bluetooth headset is connected.
|
||||||
* Bluetooth headset is connected.
|
* - VIDEO_CALL: Used for video calls. It will use the speaker by default,
|
||||||
|
* unless a wired or Bluetooth headset is connected.
|
||||||
*/
|
*/
|
||||||
private static final int DEFAULT = 0;
|
private static final int DEFAULT = 0;
|
||||||
private static final int AUDIO_CALL = 1;
|
private static final int AUDIO_CALL = 1;
|
||||||
private static final int VIDEO_CALL = 2;
|
private static final int VIDEO_CALL = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final String ACTION_HEADSET_PLUG
|
||||||
|
= (android.os.Build.VERSION.SDK_INT >= 21)
|
||||||
|
? AudioManager.ACTION_HEADSET_PLUG
|
||||||
|
: Intent.ACTION_HEADSET_PLUG;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* React Native module name.
|
* React Native module name.
|
||||||
*/
|
*/
|
||||||
|
@ -60,15 +70,17 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
||||||
static final String TAG = MODULE_NAME;
|
static final String TAG = MODULE_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Audio mode currently in use.
|
* {@link AudioManager} instance used to interact with the Android audio
|
||||||
*/
|
* subsystem.
|
||||||
private int mode = -1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link AudioManager} instance used to interact with the Android audio subsystem.
|
|
||||||
*/
|
*/
|
||||||
private final AudioManager audioManager;
|
private final AudioManager audioManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link BluetoothHeadsetMonitor} for detecting Bluetooth device changes in
|
||||||
|
* old (< M) Android versions.
|
||||||
|
*/
|
||||||
|
private BluetoothHeadsetMonitor bluetoothHeadsetMonitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Handler} for running all operations on the main thread.
|
* {@link Handler} for running all operations on the main thread.
|
||||||
*/
|
*/
|
||||||
|
@ -80,27 +92,23 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
||||||
private final Runnable mainThreadRunner;
|
private final Runnable mainThreadRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link BluetoothHeadsetMonitor} for detecting Bluetooth device changes in old (< M)
|
* Audio mode currently in use.
|
||||||
* Android versions.
|
|
||||||
*/
|
*/
|
||||||
private BluetoothHeadsetMonitor bluetoothHeadsetMonitor;
|
private int mode = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Initializes a new module instance. There shall be a single instance of
|
||||||
|
* this module throughout the lifetime of the application.
|
||||||
*
|
*
|
||||||
*/
|
* @param reactContext the {@link ReactApplicationContext} where this module
|
||||||
private static final String ACTION_HEADSET_PLUG = (android.os.Build.VERSION.SDK_INT >= 21) ?
|
* is created.
|
||||||
AudioManager.ACTION_HEADSET_PLUG : Intent.ACTION_HEADSET_PLUG;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new module instance. There shall be a single instance of this module throughout
|
|
||||||
* the lifetime of the application.
|
|
||||||
*
|
|
||||||
* @param reactContext the {@link ReactApplicationContext} where this module is created.
|
|
||||||
*/
|
*/
|
||||||
public AudioModeModule(ReactApplicationContext reactContext) {
|
public AudioModeModule(ReactApplicationContext reactContext) {
|
||||||
super(reactContext);
|
super(reactContext);
|
||||||
|
|
||||||
audioManager = ((AudioManager) reactContext.getSystemService(Context.AUDIO_SERVICE));
|
audioManager
|
||||||
|
= (AudioManager)
|
||||||
|
reactContext.getSystemService(Context.AUDIO_SERVICE);
|
||||||
mainThreadHandler = new Handler(Looper.getMainLooper());
|
mainThreadHandler = new Handler(Looper.getMainLooper());
|
||||||
mainThreadRunner = new Runnable() {
|
mainThreadRunner = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -111,10 +119,27 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Setup runtime device change detection
|
// Setup runtime device change detection.
|
||||||
setupAudioRouteChangeDetection();
|
setupAudioRouteChangeDetection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a mapping with the constants this module is exporting.
|
||||||
|
*
|
||||||
|
* @return a {@link Map} mapping the constants to be exported with their
|
||||||
|
* values.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getConstants() {
|
||||||
|
Map<String, Object> constants = new HashMap<>();
|
||||||
|
|
||||||
|
constants.put("AUDIO_CALL", AUDIO_CALL);
|
||||||
|
constants.put("DEFAULT", DEFAULT);
|
||||||
|
constants.put("VIDEO_CALL", VIDEO_CALL);
|
||||||
|
|
||||||
|
return constants;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the name for this module, to be used in the React Native bridge.
|
* Gets the name for this module, to be used in the React Native bridge.
|
||||||
*
|
*
|
||||||
|
@ -126,24 +151,126 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a mapping with the constants this module is exporting.
|
* Helper method to trigger an audio route update when devices change. It
|
||||||
*
|
* makes sure the operation is performed on the main thread.
|
||||||
* @return a {@link Map} mapping the constants to be exported with their values.
|
|
||||||
*/
|
*/
|
||||||
|
void onAudioDeviceChange() {
|
||||||
|
mainThreadHandler.post(mainThreadRunner);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to set the output route to a Bluetooth device.
|
||||||
|
*
|
||||||
|
* @param enabled true if Bluetooth should use used, false otherwise.
|
||||||
|
*/
|
||||||
|
private void setBluetoothAudioRoute(boolean enabled) {
|
||||||
|
if (enabled) {
|
||||||
|
audioManager.startBluetoothSco();
|
||||||
|
audioManager.setBluetoothScoOn(true);
|
||||||
|
} else {
|
||||||
|
audioManager.setBluetoothScoOn(false);
|
||||||
|
audioManager.stopBluetoothSco();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public method to set the current audio mode.
|
||||||
|
*
|
||||||
|
* @param mode the desired audio mode.
|
||||||
|
* @param promise a {@link Promise} which will be resolved if the audio mode
|
||||||
|
* could be updated successfully, and it will be rejected otherwise.
|
||||||
|
*/
|
||||||
|
@ReactMethod
|
||||||
|
public void setMode(final int mode, final Promise promise) {
|
||||||
|
if (mode != DEFAULT && mode != AUDIO_CALL && mode != VIDEO_CALL) {
|
||||||
|
promise.reject("setMode", "Invalid audio mode " + mode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Runnable r = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> getConstants() {
|
public void run() {
|
||||||
Map<String, Object> constants = new HashMap<>();
|
if (updateAudioRoute(mode)) {
|
||||||
constants.put("DEFAULT", DEFAULT);
|
AudioModeModule.this.mode = mode;
|
||||||
constants.put("AUDIO_CALL", AUDIO_CALL);
|
promise.resolve(null);
|
||||||
constants.put("VIDEO_CALL", VIDEO_CALL);
|
} else {
|
||||||
return constants;
|
promise.reject(
|
||||||
|
"setMode",
|
||||||
|
"Failed to set the requested audio mode");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
mainThreadHandler.post(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup the audio route change detection mechanism. We use the
|
||||||
|
* {@link android.media.AudioDeviceCallback} API on Android >= 23 only.
|
||||||
|
*/
|
||||||
|
private void setupAudioRouteChangeDetection() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
setupAudioRouteChangeDetectionM();
|
||||||
|
} else {
|
||||||
|
setupAudioRouteChangeDetectionPreM();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Audio route change detection mechanism for Android API >= 23.
|
||||||
|
*/
|
||||||
|
@TargetApi(Build.VERSION_CODES.M)
|
||||||
|
private void setupAudioRouteChangeDetectionM() {
|
||||||
|
android.media.AudioDeviceCallback audioDeviceCallback =
|
||||||
|
new android.media.AudioDeviceCallback() {
|
||||||
|
@Override
|
||||||
|
public void onAudioDevicesAdded(
|
||||||
|
AudioDeviceInfo[] addedDevices) {
|
||||||
|
Log.d(TAG, "Audio devices added");
|
||||||
|
onAudioDeviceChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAudioDevicesRemoved(
|
||||||
|
AudioDeviceInfo[] removedDevices) {
|
||||||
|
Log.d(TAG, "Audio devices removed");
|
||||||
|
onAudioDeviceChange();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
audioManager.registerAudioDeviceCallback(audioDeviceCallback, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Audio route change detection mechanism for Android API < 23.
|
||||||
|
*/
|
||||||
|
private void setupAudioRouteChangeDetectionPreM() {
|
||||||
|
ReactContext reactContext = getReactApplicationContext();
|
||||||
|
|
||||||
|
// Detect changes in wired headset connections.
|
||||||
|
IntentFilter wiredHeadSetFilter = new IntentFilter(ACTION_HEADSET_PLUG);
|
||||||
|
BroadcastReceiver wiredHeadsetReceiver = new BroadcastReceiver() {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
Log.d(TAG, "Wired headset added / removed");
|
||||||
|
onAudioDeviceChange();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reactContext.registerReceiver(wiredHeadsetReceiver, wiredHeadSetFilter);
|
||||||
|
|
||||||
|
// Detect Bluetooth device changes.
|
||||||
|
bluetoothHeadsetMonitor
|
||||||
|
= new BluetoothHeadsetMonitor(
|
||||||
|
this,
|
||||||
|
getReactApplicationContext());
|
||||||
|
bluetoothHeadsetMonitor.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the audio route for the given mode.
|
* Updates the audio route for the given mode.
|
||||||
*
|
*
|
||||||
* @param mode the audio mode to be used when computing the audio route.
|
* @param mode the audio mode to be used when computing the audio route.
|
||||||
* @return true if the audio route was updated successfully, false otherwise.
|
* @return true if the audio route was updated successfully, false
|
||||||
|
* otherwise.
|
||||||
*/
|
*/
|
||||||
private boolean updateAudioRoute(int mode) {
|
private boolean updateAudioRoute(int mode) {
|
||||||
Log.d(TAG, "Update audio route for mode: " + mode);
|
Log.d(TAG, "Update audio route for mode: " + mode);
|
||||||
|
@ -173,133 +300,29 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
||||||
boolean useSpeaker = (mode == VIDEO_CALL);
|
boolean useSpeaker = (mode == VIDEO_CALL);
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
// On Android >= M we use the AudioDeviceCallback API, so turn on Bluetooth SCO
|
// On Android >= M we use the AudioDeviceCallback API, so turn on
|
||||||
// from the start.
|
// Bluetooth SCO from the start.
|
||||||
if (audioManager.isBluetoothScoAvailableOffCall()) {
|
if (audioManager.isBluetoothScoAvailableOffCall()) {
|
||||||
audioManager.startBluetoothSco();
|
audioManager.startBluetoothSco();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// On older Android versions we must set the Bluetooth route manually. Also disable
|
// On older Android versions we must set the Bluetooth route
|
||||||
// the speaker in that case.
|
// manually. Also disable the speaker in that case.
|
||||||
setBluetoothAudioRoute(bluetoothHeadsetMonitor.isHeadsetAvailable());
|
setBluetoothAudioRoute(
|
||||||
|
bluetoothHeadsetMonitor.isHeadsetAvailable());
|
||||||
if (bluetoothHeadsetMonitor.isHeadsetAvailable()) {
|
if (bluetoothHeadsetMonitor.isHeadsetAvailable()) {
|
||||||
useSpeaker = false;
|
useSpeaker = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: isWiredHeadsetOn is not deprecated when used just for knowing if there is a wired
|
// XXX: isWiredHeadsetOn is not deprecated when used just for knowing if
|
||||||
// headset connected, regardless of audio being routed to it.
|
// there is a wired headset connected, regardless of audio being routed
|
||||||
audioManager.setSpeakerphoneOn(useSpeaker &&
|
// to it.
|
||||||
!(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothScoOn()));
|
audioManager.setSpeakerphoneOn(
|
||||||
|
useSpeaker
|
||||||
|
&& !(audioManager.isWiredHeadsetOn()
|
||||||
|
|| audioManager.isBluetoothScoOn()));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Public method to set the current audio mode.
|
|
||||||
*
|
|
||||||
* @param mode the desired audio mode.
|
|
||||||
* @param promise a {@link Promise} which will be resolved if the audio mode could be updated
|
|
||||||
* successfully, and it will be rejected otherwise.
|
|
||||||
*/
|
|
||||||
@ReactMethod
|
|
||||||
public void setMode(final int mode, final Promise promise) {
|
|
||||||
if (mode != DEFAULT && mode != AUDIO_CALL && mode != VIDEO_CALL) {
|
|
||||||
promise.reject("setMode", "Invalid audio mode " + mode);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Runnable r = new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (updateAudioRoute(mode)) {
|
|
||||||
AudioModeModule.this.mode = mode;
|
|
||||||
promise.resolve(null);
|
|
||||||
} else {
|
|
||||||
promise.reject("setMode", "Failed to set the requested audio mode");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
mainThreadHandler.post(r);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setup the audio route change detection mechanism. We use the
|
|
||||||
* {@link android.media.AudioDeviceCallback} API on Android >= 23 only.
|
|
||||||
*/
|
|
||||||
private void setupAudioRouteChangeDetection() {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
setupAudioRouteChangeDetectionM();
|
|
||||||
} else {
|
|
||||||
setupAudioRouteChangeDetectionOld();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Audio route change detection mechanism for Android API >= 23.
|
|
||||||
*/
|
|
||||||
@TargetApi(Build.VERSION_CODES.M)
|
|
||||||
private void setupAudioRouteChangeDetectionM() {
|
|
||||||
android.media.AudioDeviceCallback audioDeviceCallback =
|
|
||||||
new android.media.AudioDeviceCallback() {
|
|
||||||
@Override
|
|
||||||
public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
|
|
||||||
Log.d(TAG, "Audio devices added");
|
|
||||||
onAudioDeviceChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
|
|
||||||
Log.d(TAG, "Audio devices removed");
|
|
||||||
onAudioDeviceChange();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
audioManager.registerAudioDeviceCallback(audioDeviceCallback, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Audio route change detection mechanism for Android API < 23.
|
|
||||||
*/
|
|
||||||
private void setupAudioRouteChangeDetectionOld() {
|
|
||||||
ReactContext reactContext = getReactApplicationContext();
|
|
||||||
|
|
||||||
// Detect changes in wired headset connections
|
|
||||||
IntentFilter wiredHeadSetFilter = new IntentFilter(ACTION_HEADSET_PLUG);
|
|
||||||
BroadcastReceiver wiredHeadsetReceiver = new BroadcastReceiver() {
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
Log.d(TAG, "Wired headset added / removed");
|
|
||||||
onAudioDeviceChange();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
reactContext.registerReceiver(wiredHeadsetReceiver, wiredHeadSetFilter);
|
|
||||||
|
|
||||||
// Detect Bluetooth device changes
|
|
||||||
bluetoothHeadsetMonitor =
|
|
||||||
new BluetoothHeadsetMonitor(this, this.getReactApplicationContext());
|
|
||||||
bluetoothHeadsetMonitor.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper method to set the output route to a Bluetooth device.
|
|
||||||
* @param enabled true if Bluetooth should use used, false otherwise.
|
|
||||||
*/
|
|
||||||
private void setBluetoothAudioRoute(boolean enabled) {
|
|
||||||
if (enabled) {
|
|
||||||
audioManager.startBluetoothSco();
|
|
||||||
audioManager.setBluetoothScoOn(true);
|
|
||||||
} else {
|
|
||||||
audioManager.setBluetoothScoOn(false);
|
|
||||||
audioManager.stopBluetoothSco();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper method to trigger an audio route update when devices change. It makes sure the
|
|
||||||
* operation is performed on the main thread.
|
|
||||||
*/
|
|
||||||
void onAudioDeviceChange() {
|
|
||||||
mainThreadHandler.post(mainThreadRunner);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,19 +10,10 @@ import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class AudioModePackage implements ReactPackage {
|
/**
|
||||||
/**
|
* Implements {@link ReactPackage} for {@link AudioModeModule}.
|
||||||
* {@inheritDoc}
|
|
||||||
* @return List of native modules to be exposed by React Native.
|
|
||||||
*/
|
*/
|
||||||
@Override
|
public class AudioModePackage implements ReactPackage {
|
||||||
public List<NativeModule> createNativeModules(
|
|
||||||
ReactApplicationContext reactContext) {
|
|
||||||
List<NativeModule> modules = new ArrayList<>();
|
|
||||||
modules.add(new AudioModeModule(reactContext));
|
|
||||||
return modules;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -33,9 +24,25 @@ public class AudioModePackage implements ReactPackage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* @return List of native modules to be exposed by React Native.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
public List<NativeModule> createNativeModules(
|
||||||
|
ReactApplicationContext reactContext) {
|
||||||
|
List<NativeModule> modules = new ArrayList<>();
|
||||||
|
|
||||||
|
modules.add(new AudioModeModule(reactContext));
|
||||||
|
|
||||||
|
return modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<ViewManager> createViewManagers(
|
||||||
|
ReactApplicationContext reactContext) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,28 +18,25 @@ import com.facebook.react.bridge.ReactContext;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class to detect and handle Bluetooth device changes. It monitors Bluetooth headsets
|
* Helper class to detect and handle Bluetooth device changes. It monitors
|
||||||
* being connected / disconnected and notifies the module about device changes when this occurs.
|
* Bluetooth headsets being connected / disconnected and notifies the module
|
||||||
|
* about device changes when this occurs.
|
||||||
*/
|
*/
|
||||||
public class BluetoothHeadsetMonitor {
|
public class BluetoothHeadsetMonitor {
|
||||||
/**
|
/**
|
||||||
* {@link AudioModeModule} where this monitor reports.
|
* {@link AudioManager} instance used to interact with the Android audio
|
||||||
*/
|
* subsystem.
|
||||||
private final AudioModeModule AudioModeModule;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link AudioManager} instance used to interact with the Android audio subsystem.
|
|
||||||
*/
|
*/
|
||||||
private final AudioManager audioManager;
|
private final AudioManager audioManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ReactContext} instance where the main module runs.
|
* {@link AudioModeModule} where this monitor reports.
|
||||||
*/
|
*/
|
||||||
private final ReactContext reactContext;
|
private final AudioModeModule audioModeModule;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reference to the Bluetooth adapter, needed for managing <tt>BluetoothProfile.HEADSET</tt>
|
* Reference to the Bluetooth adapter, needed for managing
|
||||||
* devices.
|
* <tt>BluetoothProfile.HEADSET</tt> devices.
|
||||||
*/
|
*/
|
||||||
private BluetoothAdapter bluetoothAdapter;
|
private BluetoothAdapter bluetoothAdapter;
|
||||||
|
|
||||||
|
@ -49,37 +46,56 @@ public class BluetoothHeadsetMonitor {
|
||||||
private BluetoothHeadset bluetoothHeadset;
|
private BluetoothHeadset bluetoothHeadset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listener for Bluetooth service profiles, allows us to get the proxy object to
|
* Listener for Bluetooth service profiles, allows us to get the proxy
|
||||||
* {@link BluetoothHeadset}.
|
* object to {@link BluetoothHeadset}.
|
||||||
*/
|
*/
|
||||||
private BluetoothProfile.ServiceListener bluetoothProfileListener;
|
private BluetoothProfile.ServiceListener bluetoothProfileListener;
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link Handler} for running all operations on the main thread.
|
|
||||||
*/
|
|
||||||
private final Handler mainThreadHandler;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper for running Bluetooth operations on the main thread.
|
* Helper for running Bluetooth operations on the main thread.
|
||||||
*/
|
*/
|
||||||
private Runnable bluetoothRunnable;
|
private Runnable bluetoothRunnable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flag indicating if there are any Bluetooth headset devices currently available.
|
* Flag indicating if there are any Bluetooth headset devices currently
|
||||||
|
* available.
|
||||||
*/
|
*/
|
||||||
private boolean headsetAvailable = false;
|
private boolean headsetAvailable = false;
|
||||||
|
|
||||||
public BluetoothHeadsetMonitor(AudioModeModule audioModeModule, ReactContext reactContext) {
|
/**
|
||||||
this.AudioModeModule = audioModeModule;
|
* {@link Handler} for running all operations on the main thread.
|
||||||
|
*/
|
||||||
|
private final Handler mainThreadHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link ReactContext} instance where the main module runs.
|
||||||
|
*/
|
||||||
|
private final ReactContext reactContext;
|
||||||
|
|
||||||
|
public BluetoothHeadsetMonitor(
|
||||||
|
AudioModeModule audioModeModule,
|
||||||
|
ReactContext reactContext) {
|
||||||
|
this.audioModeModule = audioModeModule;
|
||||||
this.reactContext = reactContext;
|
this.reactContext = reactContext;
|
||||||
|
|
||||||
audioManager = ((AudioManager) reactContext.getSystemService(Context.AUDIO_SERVICE));
|
audioManager
|
||||||
|
= (AudioManager)
|
||||||
|
reactContext.getSystemService(Context.AUDIO_SERVICE);
|
||||||
bluetoothAdapter = null;
|
bluetoothAdapter = null;
|
||||||
bluetoothHeadset = null;
|
bluetoothHeadset = null;
|
||||||
bluetoothProfileListener = null;
|
bluetoothProfileListener = null;
|
||||||
mainThreadHandler = new Handler(Looper.getMainLooper());
|
mainThreadHandler = new Handler(Looper.getMainLooper());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current headset availability.
|
||||||
|
*
|
||||||
|
* @return true if there is a Bluetooth headset connected, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isHeadsetAvailable() {
|
||||||
|
return headsetAvailable;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start monitoring Bluetooth device activity.
|
* Start monitoring Bluetooth device activity.
|
||||||
*/
|
*/
|
||||||
|
@ -90,10 +106,11 @@ public class BluetoothHeadsetMonitor {
|
||||||
if (bluetoothHeadset == null) {
|
if (bluetoothHeadset == null) {
|
||||||
headsetAvailable = false;
|
headsetAvailable = false;
|
||||||
} else {
|
} else {
|
||||||
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
|
List<BluetoothDevice> devices
|
||||||
|
= bluetoothHeadset.getConnectedDevices();
|
||||||
headsetAvailable = !devices.isEmpty();
|
headsetAvailable = !devices.isEmpty();
|
||||||
}
|
}
|
||||||
BluetoothHeadsetMonitor.this.AudioModeModule.onAudioDeviceChange();
|
audioModeModule.onAudioDeviceChange();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -108,11 +125,14 @@ public class BluetoothHeadsetMonitor {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: The profile listener listens for system services of the given type being available
|
// XXX: The profile listener listens for system services of the given
|
||||||
// to the application. That is, if our Bluetooth adapter has the "headset" profile.
|
// type being available to the application. That is, if our Bluetooth
|
||||||
|
// adapter has the "headset" profile.
|
||||||
bluetoothProfileListener = new BluetoothProfile.ServiceListener() {
|
bluetoothProfileListener = new BluetoothProfile.ServiceListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(int profile, BluetoothProfile proxy) {
|
public void onServiceConnected(
|
||||||
|
int profile,
|
||||||
|
BluetoothProfile proxy) {
|
||||||
if (profile == BluetoothProfile.HEADSET) {
|
if (profile == BluetoothProfile.HEADSET) {
|
||||||
bluetoothHeadset = (BluetoothHeadset) proxy;
|
bluetoothHeadset = (BluetoothHeadset) proxy;
|
||||||
updateDevices();
|
updateDevices();
|
||||||
|
@ -133,33 +153,47 @@ public class BluetoothHeadsetMonitor {
|
||||||
|
|
||||||
IntentFilter bluetoothFilter = new IntentFilter();
|
IntentFilter bluetoothFilter = new IntentFilter();
|
||||||
bluetoothFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
|
bluetoothFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
|
||||||
bluetoothFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
|
bluetoothFilter.addAction(
|
||||||
|
BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
|
||||||
BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
|
BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
final String action = intent.getAction();
|
final String action = intent.getAction();
|
||||||
if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
|
if (action.equals(
|
||||||
// XXX: This action will be fired when a Bluetooth headset is connected or
|
BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
|
||||||
// disconnected to the system. This is not related to audio routing.
|
// XXX: This action will be fired when a Bluetooth headset
|
||||||
final int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -99);
|
// is connected or disconnected to the system. This is not
|
||||||
|
// related to audio routing.
|
||||||
|
final int state
|
||||||
|
= intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -99);
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case BluetoothHeadset.STATE_CONNECTED:
|
case BluetoothHeadset.STATE_CONNECTED:
|
||||||
case BluetoothHeadset.STATE_DISCONNECTED:
|
case BluetoothHeadset.STATE_DISCONNECTED:
|
||||||
Log.d(AudioModeModule.TAG, "BT headset connection state changed: " + state);
|
Log.d(
|
||||||
|
AudioModeModule.TAG,
|
||||||
|
"BT headset connection state changed: "
|
||||||
|
+ state);
|
||||||
updateDevices();
|
updateDevices();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)) {
|
} else if (action.equals(
|
||||||
// XXX: This action will be fired when the connection established with a
|
AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)) {
|
||||||
// Bluetooth headset (called a SCO connection) changes state. When the SCO
|
// XXX: This action will be fired when the connection
|
||||||
// connection is active we route audio to it.
|
// established with a Bluetooth headset (called a SCO
|
||||||
final int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -99);
|
// connection) changes state. When the SCO connection is
|
||||||
|
// active we route audio to it.
|
||||||
|
final int state
|
||||||
|
= intent.getIntExtra(
|
||||||
|
AudioManager.EXTRA_SCO_AUDIO_STATE,
|
||||||
|
-99);
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
|
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
|
||||||
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
|
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
|
||||||
Log.d(AudioModeModule.TAG, "BT SCO connection state changed: " + state);
|
Log.d(
|
||||||
|
AudioModeModule.TAG,
|
||||||
|
"BT SCO connection state changed: " + state);
|
||||||
updateDevices();
|
updateDevices();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -170,23 +204,15 @@ public class BluetoothHeadsetMonitor {
|
||||||
};
|
};
|
||||||
reactContext.registerReceiver(bluetoothReceiver, bluetoothFilter);
|
reactContext.registerReceiver(bluetoothReceiver, bluetoothFilter);
|
||||||
|
|
||||||
// Initial detection
|
// Initial detection.
|
||||||
updateDevices();
|
updateDevices();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Detect if there are new devices connected / disconnected and fires the
|
* Detects if there are new devices connected / disconnected and fires the
|
||||||
* <tt>onAudioDeviceChange</tt> callback.
|
* {@link AudioModeModule#onAudioDeviceChange()} callback.
|
||||||
*/
|
*/
|
||||||
private void updateDevices() {
|
private void updateDevices() {
|
||||||
mainThreadHandler.post(bluetoothRunnable);
|
mainThreadHandler.post(bluetoothRunnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the current headset availability.
|
|
||||||
* @return true if there is a Bluetooth headset connected, false otherwise.
|
|
||||||
*/
|
|
||||||
public boolean isHeadsetAvailable() {
|
|
||||||
return headsetAvailable;
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue