Move enable/disable LE Audio to Utils

Bug: 381353150
Test: atest UtilsTest
Flag: EXEMPT minor refactor
Change-Id: Id017f2b5a0f51d19878bf91b219334baeda97d6d
This commit is contained in:
Haijie Hong
2025-03-18 11:25:33 +08:00
parent 16fa7c1e33
commit a333827d6d
4 changed files with 313 additions and 106 deletions

View File

@@ -419,32 +419,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
mContext,
SettingsEnums.ACTION_BLUETOOTH_PROFILE_LE_AUDIO_OFF,
isCurrentDeviceInOrByPassAllowList());
LocalBluetoothProfile asha = mProfileManager.getHearingAidProfile();
LocalBluetoothProfile broadcastAssistant =
mProfileManager.getLeAudioBroadcastAssistantProfile();
for (CachedBluetoothDevice leAudioDevice : mProfileDeviceMap.get(profile.toString())) {
Log.d(TAG,
"device:" + leAudioDevice.getDevice().getAnonymizedAddress()
+ " disable LE profile");
profile.setEnabled(leAudioDevice.getDevice(), false);
if (asha != null) {
asha.setEnabled(leAudioDevice.getDevice(), true);
}
if (broadcastAssistant != null) {
Log.d(TAG,
"device:" + leAudioDevice.getDevice().getAnonymizedAddress()
+ " disable LE broadcast assistant profile");
broadcastAssistant.setEnabled(leAudioDevice.getDevice(), false);
}
}
if (!SystemProperties.getBoolean(ENABLE_DUAL_MODE_AUDIO, false)) {
Log.i(TAG, "Enabling classic audio profiles because dual mode is disabled");
enableProfileAfterUserDisablesLeAudio(mProfileManager.getA2dpProfile());
enableProfileAfterUserDisablesLeAudio(mProfileManager.getHeadsetProfile());
}
Utils.setLeAudioEnabled(mManager, List.copyOf(mCachedDeviceGroup), false);
}
/**
@@ -462,75 +437,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
mContext,
SettingsEnums.ACTION_BLUETOOTH_PROFILE_LE_AUDIO_ON,
isCurrentDeviceInOrByPassAllowList());
if (!SystemProperties.getBoolean(ENABLE_DUAL_MODE_AUDIO, false)) {
Log.i(TAG, "Disabling classic audio profiles because dual mode is disabled");
disableProfileBeforeUserEnablesLeAudio(mProfileManager.getA2dpProfile());
disableProfileBeforeUserEnablesLeAudio(mProfileManager.getHeadsetProfile());
}
LocalBluetoothProfile asha = mProfileManager.getHearingAidProfile();
LocalBluetoothProfile broadcastAssistant =
mProfileManager.getLeAudioBroadcastAssistantProfile();
for (CachedBluetoothDevice leAudioDevice : mProfileDeviceMap.get(profile.toString())) {
Log.d(TAG,
"device:" + leAudioDevice.getDevice().getAnonymizedAddress()
+ " enable LE profile");
profile.setEnabled(leAudioDevice.getDevice(), true);
if (asha != null) {
asha.setEnabled(leAudioDevice.getDevice(), false);
}
if (broadcastAssistant != null) {
Log.d(TAG,
"device:" + leAudioDevice.getDevice().getAnonymizedAddress()
+ " enable LE broadcast assistant profile");
broadcastAssistant.setEnabled(leAudioDevice.getDevice(), true);
}
}
}
private void disableProfileBeforeUserEnablesLeAudio(LocalBluetoothProfile profile) {
if (profile != null && mProfileDeviceMap.get(profile.toString()) != null) {
Log.d(TAG, "Disable " + profile.toString() + " before user enables LE");
for (CachedBluetoothDevice profileDevice : mProfileDeviceMap.get(profile.toString())) {
if (profile.isEnabled(profileDevice.getDevice())) {
Log.d(TAG, "The " + profileDevice.getDevice().getAnonymizedAddress() + ":"
+ profile.toString() + " set disable");
profile.setEnabled(profileDevice.getDevice(), false);
} else {
Log.d(TAG, "The " + profileDevice.getDevice().getAnonymizedAddress() + ":"
+ profile.toString() + " profile is disabled. Do nothing.");
}
}
} else {
if (profile == null) {
Log.w(TAG, "profile is null");
} else {
Log.w(TAG, profile.toString() + " is not in " + mProfileDeviceMap);
}
}
}
private void enableProfileAfterUserDisablesLeAudio(LocalBluetoothProfile profile) {
if (profile != null && mProfileDeviceMap.get(profile.toString()) != null) {
Log.d(TAG, "enable " + profile.toString() + "after user disables LE");
for (CachedBluetoothDevice profileDevice : mProfileDeviceMap.get(profile.toString())) {
if (!profile.isEnabled(profileDevice.getDevice())) {
Log.d(TAG, "The " + profileDevice.getDevice().getAnonymizedAddress() + ":"
+ profile.toString() + " set enable");
profile.setEnabled(profileDevice.getDevice(), true);
} else {
Log.d(TAG, "The " + profileDevice.getDevice().getAnonymizedAddress() + ":"
+ profile.toString() + " profile is enabled. Do nothing.");
}
}
} else {
if (profile == null) {
Log.w(TAG, "profile is null");
} else {
Log.w(TAG, profile.toString() + " is not in " + mProfileDeviceMap);
}
}
Utils.setLeAudioEnabled(mManager, List.copyOf(mCachedDeviceGroup), true);
}
/**

View File

@@ -28,6 +28,7 @@ import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
@@ -45,15 +46,20 @@ import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback;
import com.android.settingslib.bluetooth.LocalBluetoothProfile;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.utils.ThreadUtils;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@@ -70,6 +76,7 @@ import java.util.stream.Collectors;
public final class Utils {
private static final String TAG = "BluetoothUtils";
private static final String ENABLE_DUAL_MODE_AUDIO = "persist.bluetooth.enable_dual_mode_audio";
static final boolean V = BluetoothUtils.V; // verbose logging
static final boolean D = BluetoothUtils.D; // regular logging
@@ -360,4 +367,119 @@ public final class Utils {
dialog.show();
return dialog;
}
/** Enables/disables LE Audio profile for the device. */
public static void setLeAudioEnabled(
@NonNull LocalBluetoothManager manager,
@NonNull CachedBluetoothDevice cachedDevice,
boolean enable) {
List<CachedBluetoothDevice> devices =
List.copyOf(findAllCachedBluetoothDevicesByGroupId(manager, cachedDevice));
setLeAudioEnabled(manager, devices, enable);
}
/** Enables/disables LE Audio profile for the devices in the same csip group. */
public static void setLeAudioEnabled(
@NonNull LocalBluetoothManager manager,
@NonNull List<CachedBluetoothDevice> devicesWithSameGroupId,
boolean enable) {
LocalBluetoothProfileManager profileManager = manager.getProfileManager();
LeAudioProfile leAudioProfile = profileManager.getLeAudioProfile();
List<CachedBluetoothDevice> leAudioDevices =
getDevicesWithProfile(devicesWithSameGroupId, leAudioProfile);
if (leAudioDevices.isEmpty()) {
Log.i(TAG, "Fail to setLeAudioEnabled, no LE Audio profile found.");
}
boolean dualModeEnabled = SystemProperties.getBoolean(ENABLE_DUAL_MODE_AUDIO, false);
if (enable && !dualModeEnabled) {
Log.i(TAG, "Disabling classic audio profiles because dual mode is disabled");
setProfileEnabledWhenChangingLeAudio(
devicesWithSameGroupId, profileManager.getA2dpProfile(), false);
setProfileEnabledWhenChangingLeAudio(
devicesWithSameGroupId, profileManager.getHeadsetProfile(), false);
}
HearingAidProfile asha = profileManager.getHearingAidProfile();
LocalBluetoothLeBroadcastAssistant broadcastAssistant =
profileManager.getLeAudioBroadcastAssistantProfile();
for (CachedBluetoothDevice leAudioDevice : leAudioDevices) {
Log.d(
TAG,
"device:"
+ leAudioDevice.getDevice().getAnonymizedAddress()
+ " set LE profile enabled: "
+ enable);
leAudioProfile.setEnabled(leAudioDevice.getDevice(), enable);
if (asha != null) {
asha.setEnabled(leAudioDevice.getDevice(), !enable);
}
if (broadcastAssistant != null) {
Log.d(
TAG,
"device:"
+ leAudioDevice.getDevice().getAnonymizedAddress()
+ " enable LE broadcast assistant profile: "
+ enable);
broadcastAssistant.setEnabled(leAudioDevice.getDevice(), enable);
}
}
if (!enable && !dualModeEnabled) {
Log.i(TAG, "Enabling classic audio profiles because dual mode is disabled");
setProfileEnabledWhenChangingLeAudio(
devicesWithSameGroupId, profileManager.getA2dpProfile(), true);
setProfileEnabledWhenChangingLeAudio(
devicesWithSameGroupId, profileManager.getHeadsetProfile(), true);
}
}
private static List<CachedBluetoothDevice> getDevicesWithProfile(
List<CachedBluetoothDevice> devices, LocalBluetoothProfile profile) {
List<CachedBluetoothDevice> devicesWithProfile = new ArrayList<>();
for (CachedBluetoothDevice device : devices) {
for (LocalBluetoothProfile currentProfile : device.getProfiles()) {
if (currentProfile.toString().equals(profile.toString())) {
devicesWithProfile.add(device);
}
}
}
return devicesWithProfile;
}
private static void setProfileEnabledWhenChangingLeAudio(
List<CachedBluetoothDevice> devices,
@Nullable LocalBluetoothProfile profile,
boolean enable) {
if (profile == null) {
Log.i(TAG, "profile is null");
return;
}
List<CachedBluetoothDevice> deviceWithProfile = getDevicesWithProfile(devices, profile);
Log.d(TAG, "Set " + profile + " enabled:" + enable + " when switching LE Audio");
for (CachedBluetoothDevice profileDevice : deviceWithProfile) {
if (profile.isEnabled(profileDevice.getDevice()) != enable) {
Log.d(
TAG,
"The "
+ profileDevice.getDevice().getAnonymizedAddress()
+ ":"
+ profile
+ " set to "
+ enable);
profile.setEnabled(profileDevice.getDevice(), enable);
} else {
Log.d(
TAG,
"The "
+ profileDevice.getDevice().getAnonymizedAddress()
+ ":"
+ profile
+ " profile is already "
+ enable
+ ". Do nothing.");
}
}
}
}