[Audiosharing] Disable main toggle till device gets active.

Broadcast <-> unicast costs some time, so we grey out the audio
sharing toggle until the device get active on unicast when users toggle
off the sharing.

Flag: com.android.settingslib.flags.enable_le_audio_sharing
Test: atest
Bug: 359755881
Change-Id: I868a470430f6fb54c142f17552d70fa5fa8a2476
This commit is contained in:
Yiyi Shen
2024-09-02 13:31:05 +08:00
parent a63f6cc528
commit b819fa9020
3 changed files with 91 additions and 8 deletions

View File

@@ -25,6 +25,7 @@ import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -52,7 +53,10 @@ import com.android.settings.bluetooth.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.SettingsMainSwitchBar;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -75,7 +79,8 @@ import java.util.concurrent.atomic.AtomicInteger;
public class AudioSharingSwitchBarController extends BasePreferenceController
implements DefaultLifecycleObserver,
OnCheckedChangeListener,
LocalBluetoothProfileManager.ServiceListener {
LocalBluetoothProfileManager.ServiceListener,
BluetoothCallback {
private static final String TAG = "AudioSharingSwitchCtlr";
private static final String PREF_KEY = "audio_sharing_main_switch";
@@ -99,6 +104,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
private final SettingsMainSwitchBar mSwitchBar;
private final BluetoothAdapter mBluetoothAdapter;
@Nullable private final LocalBluetoothManager mBtManager;
@Nullable private final BluetoothEventManager mEventManager;
@Nullable private final LocalBluetoothProfileManager mProfileManager;
@Nullable private final LocalBluetoothLeBroadcast mBroadcast;
@Nullable private final LocalBluetoothLeBroadcastAssistant mAssistant;
@@ -302,6 +308,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
mBtManager = Utils.getLocalBtManager(context);
mEventManager = mBtManager == null ? null : mBtManager.getEventManager();
mProfileManager = mBtManager == null ? null : mBtManager.getProfileManager();
mBroadcast = mProfileManager == null ? null : mProfileManager.getLeAudioBroadcastProfile();
mAssistant =
@@ -427,6 +434,13 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
// Do nothing.
}
@Override
public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
if (activeDevice != null && bluetoothProfile == BluetoothProfile.LE_AUDIO) {
updateSwitch();
}
}
/**
* Initialize the controller.
*
@@ -447,7 +461,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
Log.d(TAG, "Skip registerCallbacks(). Feature is not available.");
return;
}
if (mBroadcast == null || mAssistant == null) {
if (mBroadcast == null || mAssistant == null || mEventManager == null) {
Log.d(TAG, "Skip registerCallbacks(). Profile not support on this device.");
return;
}
@@ -456,6 +470,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
mSwitchBar.addOnSwitchChangeListener(this);
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
mEventManager.registerCallback(this);
mCallbacksRegistered.set(true);
}
}
@@ -465,7 +480,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
Log.d(TAG, "Skip unregisterCallbacks(). Feature is not available.");
return;
}
if (mBroadcast == null || mAssistant == null) {
if (mBroadcast == null || mAssistant == null || mEventManager == null) {
Log.d(TAG, "Skip unregisterCallbacks(). Profile not support on this device.");
return;
}
@@ -474,6 +489,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
mSwitchBar.removeOnSwitchChangeListener(this);
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
mAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
mEventManager.unregisterCallback(this);
mCallbacksRegistered.set(false);
}
}
@@ -518,10 +534,15 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
ThreadUtils.postOnBackgroundThread(
() -> {
boolean isBroadcasting = BluetoothUtils.isBroadcasting(mBtManager);
boolean hasActiveDevice =
AudioSharingUtils.hasActiveConnectedLeadDevice(mBtManager);
boolean isStateReady =
isBluetoothOn()
&& AudioSharingUtils.isAudioSharingProfileReady(
mProfileManager);
mProfileManager)
// Disable toggle till device gets active after
// broadcast ends.
&& (isBroadcasting || hasActiveDevice);
AudioSharingUtils.postOnMainThread(
mContext,
() -> {

View File

@@ -115,7 +115,7 @@ public class AudioSharingUtils {
* sharing. The active device is placed in the first place if it exists. The devices can be
* filtered by whether it is already in the audio sharing session.
*
* @param localBtManager The BT manager to provide BT functions. *
* @param localBtManager The BT manager to provide BT functions.
* @param groupedConnectedDevices devices connected to broadcast assistant grouped by CSIP group
* id.
* @param filterByInSharing Whether to filter the device by if is already in the sharing
@@ -190,7 +190,7 @@ public class AudioSharingUtils {
* sharing. The active device is placed in the first place if it exists. The devices can be
* filtered by whether it is already in the audio sharing session.
*
* @param localBtManager The BT manager to provide BT functions. *
* @param localBtManager The BT manager to provide BT functions.
* @param groupedConnectedDevices devices connected to broadcast assistant grouped by CSIP group
* id.
* @param filterByInSharing Whether to filter the device by if is already in the sharing
@@ -210,6 +210,22 @@ public class AudioSharingUtils {
.collect(toList());
}
/** Return if there exists active connected lead device. */
public static boolean hasActiveConnectedLeadDevice(
@Nullable LocalBluetoothManager localBtManager) {
CachedBluetoothDeviceManager deviceManager =
localBtManager == null ? null : localBtManager.getCachedDeviceManager();
Map<Integer, List<BluetoothDevice>> groupedConnectedDevices =
fetchConnectedDevicesByGroupId(localBtManager);
for (List<BluetoothDevice> devices : groupedConnectedDevices.values()) {
CachedBluetoothDevice leadDevice = getLeadDevice(deviceManager, devices);
if (isActiveLeAudioDevice(leadDevice)) {
return true;
}
}
return false;
}
/** Build {@link AudioSharingDeviceItem} from {@link CachedBluetoothDevice}. */
public static AudioSharingDeviceItem buildAudioSharingDeviceItem(
CachedBluetoothDevice cachedDevice) {
@@ -225,8 +241,8 @@ public class AudioSharingUtils {
* @param cachedDevice The cached bluetooth device to check.
* @return Whether the device is an active le audio device.
*/
public static boolean isActiveLeAudioDevice(CachedBluetoothDevice cachedDevice) {
return BluetoothUtils.isActiveLeAudioDevice(cachedDevice);
public static boolean isActiveLeAudioDevice(@Nullable CachedBluetoothDevice cachedDevice) {
return cachedDevice != null && BluetoothUtils.isActiveLeAudioDevice(cachedDevice);
}
/** Toast message on main thread. */