Merge "[Audiosharing] Disable the profile state change handling" into main

This commit is contained in:
Yiyi Shen
2025-03-03 01:27:26 -08:00
committed by Android (Google) Code Review
2 changed files with 127 additions and 30 deletions

View File

@@ -58,10 +58,12 @@ import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.HeadsetProfile;
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.LocalBluetoothProfileManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.flags.Flags;
import com.android.settingslib.utils.ThreadUtils;
import java.util.Locale;
@@ -83,6 +85,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
@Nullable private final CachedBluetoothDeviceManager mDeviceManager;
@Nullable private final BluetoothEventManager mEventManager;
@Nullable private final LocalBluetoothProfileManager mProfileManager;
@Nullable private final LocalBluetoothLeBroadcast mBroadcast;
@Nullable private final LocalBluetoothLeBroadcastAssistant mAssistant;
private final Executor mExecutor;
private final MetricsFeatureProvider mMetricsFeatureProvider;
@@ -190,6 +193,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
mEventManager = mBtManager == null ? null : mBtManager.getEventManager();
mDeviceManager = mBtManager == null ? null : mBtManager.getCachedDeviceManager();
mProfileManager = mBtManager == null ? null : mBtManager.getProfileManager();
mBroadcast = mProfileManager == null ? null : mProfileManager.getLeAudioBroadcastProfile();
mAssistant =
mProfileManager == null
? null
@@ -334,23 +338,32 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
@NonNull CachedBluetoothDevice cachedDevice,
@ConnectionState int state,
int bluetoothProfile) {
if (mDialogHandler == null || mAssistant == null || mFragment == null) {
if (mDialogHandler == null || mBroadcast == null || mAssistant == null
|| mFragment == null) {
Log.d(TAG, "Ignore onProfileConnectionStateChanged, not init correctly");
return;
}
if (Flags.promoteAudioSharingForSecondAutoConnectedLeaDevice() && mBroadcast.isEnabled(
null)) {
Log.d(TAG, "Ignore onProfileConnectionStateChanged, in broadcast");
// Device connected in broadcast will be handled in sysui via settingslib
return;
}
if (!isMediaDevice(cachedDevice)) {
Log.d(TAG, "Ignore onProfileConnectionStateChanged, not a media device");
return;
}
// Close related dialogs if the BT remote device is disconnected.
if (state == BluetoothAdapter.STATE_DISCONNECTED) {
boolean isLeAudioSupported = AudioSharingUtils.isLeAudioSupported(cachedDevice);
boolean isLeAudioSupported = BluetoothUtils.isLeAudioSupported(cachedDevice);
if (isLeAudioSupported
&& bluetoothProfile == BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT) {
Log.d(TAG, "closeOpeningDialogsForLeaDevice");
mDialogHandler.closeOpeningDialogsForLeaDevice(cachedDevice);
return;
}
if (!isLeAudioSupported && !cachedDevice.isConnected()) {
Log.d(TAG, "closeOpeningDialogsForNonLeaDevice");
mDialogHandler.closeOpeningDialogsForNonLeaDevice(cachedDevice);
return;
}
@@ -362,6 +375,13 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
handleOnProfileStateChanged(cachedDevice, bluetoothProfile);
}
@Override
public void onBluetoothStateChanged(@AdapterState int bluetoothState) {
if (bluetoothState == BluetoothAdapter.STATE_OFF && mDialogHandler != null) {
mDialogHandler.closeOpeningDialogsOtherThan("");
}
}
@Override
public void onAudioModeChanged() {
mIsAudioModeOngoingCall.set(isAudioModeOngoingCall(mContext));
@@ -421,7 +441,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
private void handleOnProfileStateChanged(
@NonNull CachedBluetoothDevice cachedDevice, int bluetoothProfile) {
boolean isLeAudioSupported = AudioSharingUtils.isLeAudioSupported(cachedDevice);
boolean isLeAudioSupported = BluetoothUtils.isLeAudioSupported(cachedDevice);
// For eligible (LE audio) remote device, we only check its connected LE audio assistant
// profile.
if (isLeAudioSupported
@@ -441,12 +461,8 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
+ " non le audio device");
return;
}
if (DEBUG) {
Log.d(
TAG,
"Start handling onProfileConnectionStateChanged for "
+ cachedDevice.getDevice().getAnonymizedAddress());
}
Log.d(TAG, "Start handling onProfileConnectionStateChanged for "
+ cachedDevice.getDevice().getAnonymizedAddress());
// Check nullability to pass NullAway check
if (mDialogHandler != null) {
mDialogHandler.handleDeviceConnected(cachedDevice, /* userTriggered= */ false);

View File

@@ -51,6 +51,8 @@ import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Looper;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.util.Pair;
@@ -201,8 +203,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onStart_flagOff_doNothing() {
mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mController.onStart(mLifecycleOwner);
verify(mEventManager, never()).registerCallback(any(BluetoothCallback.class));
verify(mDialogHandler, never()).registerCallbacks(any(Executor.class));
@@ -214,8 +216,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onStart_flagOn_registerCallbacks() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mController.onStart(mLifecycleOwner);
verify(mEventManager).registerCallback(any(BluetoothCallback.class));
verify(mDialogHandler).registerCallbacks(any(Executor.class));
@@ -227,8 +229,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onStop_flagOff_doNothing() {
mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mController.onStop(mLifecycleOwner);
verify(mEventManager, never()).unregisterCallback(any(BluetoothCallback.class));
verify(mDialogHandler, never()).unregisterCallbacks();
@@ -238,8 +240,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onStop_flagOn_unregisterCallbacks() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mController.onStop(mLifecycleOwner);
verify(mEventManager).unregisterCallback(any(BluetoothCallback.class));
verify(mDialogHandler).unregisterCallbacks();
@@ -249,16 +251,16 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void displayPreference_flagOff_doNothing() {
mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mController.displayPreference(mScreen);
assertThat(mPreferenceGroup.isVisible()).isFalse();
verify(mBluetoothDeviceUpdater, never()).forceUpdate();
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void displayPreference_flagOn_updateDeviceList() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mController.displayPreference(mScreen);
assertThat(mPreferenceGroup.isVisible()).isFalse();
verify(mBluetoothDeviceUpdater).setPrefContext(mContext);
@@ -271,14 +273,14 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void getAvailabilityStatus_flagOff_returnUnSupported() {
mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void getAvailabilityStatus_flagOn_returnSupported() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
}
@@ -314,6 +316,7 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void onProfileConnectionStateChanged_leaDeviceDisconnected_closeOpeningDialogsForIt() {
// Test when LEA device LE_AUDIO_BROADCAST_ASSISTANT disconnected.
when(mDevice.isConnected()).thenReturn(true);
@@ -326,6 +329,21 @@ public class AudioSharingDevicePreferenceControllerTest {
verify(mDialogHandler).closeOpeningDialogsForLeaDevice(mCachedDevice);
}
@Test
@EnableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void onProfileConnectionStateChanged_leaDeviceDisconnected_broadcastOn_doNothing() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
// Test when LEA device LE_AUDIO_BROADCAST_ASSISTANT disconnected.
when(mDevice.isConnected()).thenReturn(true);
doReturn(ImmutableList.of(mLeAudioProfile)).when(mCachedDevice).getUiAccessibleProfiles();
doReturn(ImmutableList.of(mLeAudioProfile)).when(mCachedDevice).getProfiles();
mController.onProfileConnectionStateChanged(
mCachedDevice,
BluetoothAdapter.STATE_DISCONNECTED,
BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT);
verifyNoInteractions(mDialogHandler);
}
@Test
public void onProfileConnectionStateChanged_assistantProfileConnecting_doNothing() {
// Test when LEA device LE_AUDIO_BROADCAST_ASSISTANT connecting
@@ -361,6 +379,7 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void onProfileConnectionStateChanged_assistantProfileConnected_handle() {
// Test when LEA device LE_AUDIO_BROADCAST_ASSISTANT connected
when(mDevice.isConnected()).thenReturn(true);
@@ -374,6 +393,22 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void onProfileConnectionStateChanged_assistantProfileConnected_broadcastOn_doNothing() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
// Test when LEA device LE_AUDIO_BROADCAST_ASSISTANT connected
when(mDevice.isConnected()).thenReturn(true);
doReturn(ImmutableList.of(mLeAudioProfile)).when(mCachedDevice).getUiAccessibleProfiles();
doReturn(ImmutableList.of(mLeAudioProfile)).when(mCachedDevice).getProfiles();
mController.onProfileConnectionStateChanged(
mCachedDevice,
BluetoothAdapter.STATE_CONNECTED,
BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT);
verifyNoInteractions(mDialogHandler);
}
@Test
@DisableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void
onProfileConnectionStateChanged_nonLeaDeviceDisconnected_closeOpeningDialogsForIt() {
// Test when non-LEA device totally disconnected
@@ -386,6 +421,21 @@ public class AudioSharingDevicePreferenceControllerTest {
verify(mDialogHandler).closeOpeningDialogsForNonLeaDevice(mCachedDevice);
}
@Test
@EnableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void
onProfileConnectionStateChanged_nonLeaDeviceDisconnected_broadcastOn_doNothing() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
// Test when non-LEA device totally disconnected
when(mLeAudioProfile.isEnabled(mDevice)).thenReturn(false);
doReturn(ImmutableList.of(mA2dpProfile)).when(mCachedDevice).getUiAccessibleProfiles();
doReturn(ImmutableList.of(mLeAudioProfile, mA2dpProfile)).when(mCachedDevice).getProfiles();
when(mCachedDevice.isConnected()).thenReturn(false);
mController.onProfileConnectionStateChanged(
mCachedDevice, BluetoothAdapter.STATE_DISCONNECTED, BluetoothProfile.A2DP);
verifyNoInteractions(mDialogHandler);
}
@Test
public void onProfileConnectionStateChanged_nonLeaNotFirstProfileConnected_doNothing() {
// Test when non-LEA device LE_AUDIO_BROADCAST_ASSISTANT connecting
@@ -402,6 +452,7 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@DisableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void onProfileConnectionStateChanged_nonLeaFirstProfileConnected_handle() {
// Test when non-LEA device LE_AUDIO_BROADCAST_ASSISTANT connecting
when(mDevice.isConnected()).thenReturn(true);
@@ -417,8 +468,26 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE)
public void
onProfileConnectionStateChanged_nonLeaFirstProfileConnected_broadcastOn_doNothing() {
when(mBroadcast.isEnabled(null)).thenReturn(true);
// Test when non-LEA device LE_AUDIO_BROADCAST_ASSISTANT connecting
when(mDevice.isConnected()).thenReturn(true);
when(mHeadsetProfile.getConnectionStatus(mDevice))
.thenReturn(BluetoothAdapter.STATE_DISCONNECTED);
doReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile))
.when(mCachedDevice)
.getUiAccessibleProfiles();
doReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile)).when(mCachedDevice).getProfiles();
mController.onProfileConnectionStateChanged(
mCachedDevice, BluetoothAdapter.STATE_CONNECTED, BluetoothProfile.A2DP);
verifyNoInteractions(mDialogHandler);
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void handleDeviceClickFromIntent_noDevice_doNothing() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
Intent intent = new Intent();
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, new Bundle());
doReturn(intent).when(mActivity).getIntent();
@@ -430,8 +499,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void handleDeviceClickFromIntent_profileNotReady_doNothing() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
when(mBroadcast.isProfileReady()).thenReturn(false);
Bundle arg = new Bundle();
arg.putParcelable(EXTRA_BLUETOOTH_DEVICE, mDevice);
@@ -447,8 +516,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void handleDeviceClickFromIntent_intentHandled_handle() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
Bundle arg = new Bundle();
arg.putParcelable(EXTRA_BLUETOOTH_DEVICE, mDevice);
Intent intent = new Intent();
@@ -465,8 +534,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void handleDeviceClickFromIntent_disconnectedDevice_connect() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
Bundle arg = new Bundle();
arg.putParcelable(EXTRA_BLUETOOTH_DEVICE, mDevice);
Intent intent = new Intent();
@@ -479,8 +548,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void handleDeviceClickFromIntent_connectedDevice_handle() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
Bundle arg = new Bundle();
arg.putParcelable(EXTRA_BLUETOOTH_DEVICE, mDevice);
Intent intent = new Intent();
@@ -494,8 +563,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void handleDeviceClickFromIntent_onServiceConnected_handle() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
Bundle arg = new Bundle();
arg.putParcelable(EXTRA_BLUETOOTH_DEVICE, mDevice);
Intent intent = new Intent();
@@ -570,10 +639,10 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API})
@DisableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX)
public void testInCallState_showCallStateTitleAndSetActiveOnDeviceClick() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
mSetFlagsRule.enableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API);
Settings.Secure.putInt(mContext.getContentResolver(),
BLUETOOTH_LE_BROADCAST_PRIMARY_DEVICE_GROUP_ID,
BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
@@ -596,10 +665,10 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API,
Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX})
public void testInCallState_enableHysteresisFix_setAndSaveActiveOnDeviceClick() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
mSetFlagsRule.enableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API);
Settings.Secure.putInt(mContext.getContentResolver(),
BLUETOOTH_LE_BROADCAST_PRIMARY_DEVICE_GROUP_ID,
BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
@@ -619,8 +688,8 @@ public class AudioSharingDevicePreferenceControllerTest {
}
@Test
@EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void testInNormalState_showNormalStateTitleAndDoNothingOnDeviceClick() {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
mController.displayPreference(mScreen);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
@@ -636,6 +705,18 @@ public class AudioSharingDevicePreferenceControllerTest {
verify(mCachedDevice, never()).setActive();
}
@Test
public void onBluetoothStateChanged_stateOff_closeAllOpeningDialogs() {
mController.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
verify(mDialogHandler).closeOpeningDialogsOtherThan("");
}
@Test
public void onBluetoothStateChanged_stateOn_doNothing() {
mController.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
verify(mDialogHandler, never()).closeOpeningDialogsOtherThan("");
}
@NonNull
private BluetoothDevicePreference createBluetoothDevicePreference() {
Drawable drawable = mock(Drawable.class);