Browse Source

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>
Tim Krüger 3 years ago
parent
commit
dedbe40cc0

+ 18 - 13
app/src/main/java/com/nextcloud/talk/webrtc/MagicAudioManager.java

@@ -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");
@@ -112,7 +111,13 @@ public class MagicAudioManager {
      * Construction.
      */
     public static MagicAudioManager create(Context context, boolean useProximitySensor) {
-        return new MagicAudioManager(context, useProximitySensor);
+       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();
     }
 
     /**
@@ -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);
 
-        bluetoothManager.stop();
+        if(bluetoothManager.started()) {
+            bluetoothManager.stop();
+        }
 
         // Restore previously stored audio states.
         setSpeakerphoneOn(savedIsSpeakerPhoneOn);

+ 9 - 1
app/src/main/java/com/nextcloud/talk/webrtc/MagicBluetoothManager.java

@@ -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.