android: simplify the creation of AudioManager

Do so on the main thread at startup and pass it along.
This commit is contained in:
Saúl Ibarra Corretgé 2020-03-11 14:59:35 +01:00 committed by Saúl Ibarra Corretgé
parent 05a8591110
commit 025e2b1ecb
4 changed files with 33 additions and 15 deletions

View File

@ -108,7 +108,8 @@ class AudioDeviceHandlerConnectionService implements
*/
private int supportedRouteMask = -1;
public AudioDeviceHandlerConnectionService() {
public AudioDeviceHandlerConnectionService(AudioManager audioManager) {
this.audioManager = audioManager;
}
@Override
@ -136,11 +137,10 @@ class AudioDeviceHandlerConnectionService implements
}
@Override
public void start(Context context, AudioModeModule audioModeModule) {
public void start(AudioModeModule audioModeModule) {
JitsiMeetLogger.i("Using " + TAG + " as the audio device handler");
module = audioModeModule;
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
RNConnectionService rcs = ReactInstanceManagerHolder.getNativeModule(RNConnectionService.class);
if (rcs != null) {

View File

@ -117,7 +117,8 @@ class AudioDeviceHandlerGeneric implements
}
};
public AudioDeviceHandlerGeneric() {
public AudioDeviceHandlerGeneric(AudioManager audioManager) {
this.audioManager = audioManager;
}
/**
@ -178,11 +179,10 @@ class AudioDeviceHandlerGeneric implements
}
@Override
public void start(Context context, AudioModeModule audioModeModule) {
public void start(AudioModeModule audioModeModule) {
JitsiMeetLogger.i("Using " + TAG + " as the audio device handler");
module = audioModeModule;
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
// Setup runtime device change detection.
audioManager.registerAudioDeviceCallback(audioDeviceCallback, null);

View File

@ -59,7 +59,8 @@ class AudioDeviceHandlerLegacy implements
*/
private BluetoothHeadsetMonitor bluetoothHeadsetMonitor;
public AudioDeviceHandlerLegacy() {
public AudioDeviceHandlerLegacy(AudioManager audioManager) {
this.audioManager = audioManager;
}
/**
@ -155,11 +156,11 @@ class AudioDeviceHandlerLegacy implements
}
@Override
public void start(Context context, AudioModeModule audioModeModule) {
public void start(AudioModeModule audioModeModule) {
JitsiMeetLogger.i("Using " + TAG + " as the audio device handler");
module = audioModeModule;
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
Context context = module.getContext();
// Setup runtime device change detection.
//

View File

@ -17,6 +17,7 @@
package org.jitsi.meet.sdk;
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import com.facebook.react.bridge.Arguments;
@ -85,6 +86,12 @@ class AudioModeModule extends ReactContextBaseJavaModule {
return supportsConnectionService && useConnectionService_;
}
/**
* {@link AudioManager} instance used to interact with the Android audio
* subsystem.
*/
private AudioManager audioManager;
private AudioDeviceHandlerInterface audioDeviceHandler;
/**
@ -136,6 +143,8 @@ class AudioModeModule extends ReactContextBaseJavaModule {
*/
public AudioModeModule(ReactApplicationContext reactContext) {
super(reactContext);
audioManager = (AudioManager)reactContext.getSystemService(Context.AUDIO_SERVICE);
}
/**
@ -212,14 +221,14 @@ class AudioModeModule extends ReactContextBaseJavaModule {
}
if (useConnectionService()) {
audioDeviceHandler = new AudioDeviceHandlerConnectionService();
audioDeviceHandler = new AudioDeviceHandlerConnectionService(audioManager);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
audioDeviceHandler = new AudioDeviceHandlerGeneric();
audioDeviceHandler = new AudioDeviceHandlerGeneric(audioManager);
} else {
audioDeviceHandler = new AudioDeviceHandlerLegacy();
audioDeviceHandler = new AudioDeviceHandlerLegacy(audioManager);
}
audioDeviceHandler.start(getReactApplicationContext(), this);
audioDeviceHandler.start(this);
}
/**
@ -418,16 +427,24 @@ class AudioModeModule extends ReactContextBaseJavaModule {
}
}
/**
* Needed on the legacy handler...
*
* @return Context for the application.
*/
Context getContext() {
return getReactApplicationContext();
}
/**
* Interface for the modules implementing the actual audio device management.
*/
interface AudioDeviceHandlerInterface {
/**
* Start detecting audio device changes.
* @param context Android {@link Context} where detection should take place.
* @param audioModeModule Reference to the main {@link AudioModeModule}.
*/
void start(Context context, AudioModeModule audioModeModule);
void start(AudioModeModule audioModeModule);
/**
* Stop audio device detection.