Launch 'MagicBluetoothManager' independently

This change is needed to make the 'MagicBluetoothManager' startable
after the from Android SDK 31 introduced BLUETOOTH_CONNECT is granted by
the user.

Before this change the 'MagicBluetoothManager' was only started in
'MagicAudioManager#start'. Now the new method
'MagicAudioManager#startBluetoothManager' can be used to start the
'MagicBluetoothManager'.

This change is also a preperation to fix #1309 and #2114.

See: #2132, #1309, #2124

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2022-06-15 13:38:36 +02:00
parent 4821b02729
commit dedbe40cc0
No known key found for this signature in database
GPG key ID: FECE3A7222C52A4E
2 changed files with 27 additions and 14 deletions

View file

@ -2,6 +2,8 @@
* Nextcloud Talk application
*
* @author Mario Danic
* @author Tim Krüger
* Copyright (C) 2022 Tim Krüger <t@timkrueger.me>
* Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
@ -52,15 +54,12 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* MagicAudioManager manages all audio related parts of the AppRTC demo.
*/
public class MagicAudioManager {
private static final String TAG = "MagicAudioManager";
private static final String TAG = MagicAudioManager.class.getCanonicalName();
private final Context magicContext;
private final MagicBluetoothManager bluetoothManager;
private boolean useProximitySensor;
private AudioManager audioManager;
private final boolean useProximitySensor;
private final AudioManager audioManager;
private AudioManagerListener audioManagerListener;
private AudioManagerState amState;
private int savedAudioMode = AudioManager.MODE_INVALID;
@ -75,10 +74,10 @@ public class MagicAudioManager {
private Set<AudioDevice> audioDevices = new HashSet<>();
private BroadcastReceiver wiredHeadsetReceiver;
private final BroadcastReceiver wiredHeadsetReceiver;
private AudioManager.OnAudioFocusChangeListener audioFocusChangeListener;
private PowerManagerUtils powerManagerUtils;
private final PowerManagerUtils powerManagerUtils;
private MagicAudioManager(Context context, boolean useProximitySensor) {
Log.d(TAG, "ctor");
@ -115,6 +114,12 @@ public class MagicAudioManager {
return new MagicAudioManager(context, useProximitySensor);
}
public void startBluetoothManager() {
// Initialize and start Bluetooth if a BT device is available or initiate
// detection of new (enabled) BT devices.
bluetoothManager.start();
}
/**
* This method is called when the proximity sensor reports a state change, e.g. from "NEAR to FAR" or from "FAR to
* NEAR".
@ -228,9 +233,7 @@ public class MagicAudioManager {
currentAudioDevice = AudioDevice.NONE;
audioDevices.clear();
// Initialize and start Bluetooth if a BT device is available or initiate
// detection of new (enabled) BT devices.
bluetoothManager.start();
startBluetoothManager();
// Do initial selection of audio device. This setting can later be changed
// either by adding/removing a BT or wired headset or by covering/uncovering
@ -256,7 +259,9 @@ public class MagicAudioManager {
unregisterReceiver(wiredHeadsetReceiver);
if(bluetoothManager.started()) {
bluetoothManager.stop();
}
// Restore previously stored audio states.
setSpeakerphoneOn(savedIsSpeakerPhoneOn);

View file

@ -2,6 +2,8 @@
* Nextcloud Talk application
*
* @author Mario Danic
* @author Tim Krüger
* Copyright (C) 2022 Tim Krüger <t@timkrueger.me>
* Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
@ -57,7 +59,7 @@ import java.util.Set;
import androidx.core.app.ActivityCompat;
public class MagicBluetoothManager {
private static final String TAG = "MagicBluetoothManager";
private static final String TAG = MagicBluetoothManager.class.getCanonicalName();
// Timeout interval for starting or stopping audio to a Bluetooth SCO device.
private static final int BLUETOOTH_SCO_TIMEOUT_MS = 4000;
@ -78,6 +80,7 @@ public class MagicBluetoothManager {
// startScoAudio() or stopScoAudio() because we're not guaranteed to get a
// callback after those calls.
private final Runnable bluetoothTimeoutRunnable = this::bluetoothTimeout;
private boolean started = false;
protected MagicBluetoothManager(Context context, MagicAudioManager audioManager) {
Log.d(TAG, "ctor");
@ -165,6 +168,7 @@ public class MagicBluetoothManager {
+ stateToString(bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)));
Log.d(TAG, "Bluetooth proxy for headset profile has started");
bluetoothState = State.HEADSET_UNAVAILABLE;
started = true;
Log.d(TAG, "start done: BT state=" + bluetoothState);
}
@ -454,6 +458,10 @@ public class MagicBluetoothManager {
}
}
public boolean started() {
return started;
}
// Bluetooth connection state.
public enum State {
// Bluetooth is not available; no adapter or Bluetooth is off.