[Audiosharing] Block pairing during audio sharing

When a) there are two sinks in sharing b) there is at least one temp
bond device connected

Test: atest
Flag: com.android.settingslib.flags.enable_temporary_bond_devices_ui
Bug: 392004799
Change-Id: Id093b4c0b67bdc1db621e0241e79900abbd26c0f
This commit is contained in:
Yiyi Shen
2025-02-10 17:53:34 +08:00
parent 5732773031
commit 93f9061794
4 changed files with 126 additions and 6 deletions

View File

@@ -13942,6 +13942,12 @@
<string name="audio_sharing_incompatible_dialog_title">Can\'t share audio with <xliff:g example="My buds" id="device_name">%1$s</xliff:g></string> <string name="audio_sharing_incompatible_dialog_title">Can\'t share audio with <xliff:g example="My buds" id="device_name">%1$s</xliff:g></string>
<!-- Content for audio sharing incompatible device dialog [CHAR LIMIT=none]--> <!-- Content for audio sharing incompatible device dialog [CHAR LIMIT=none]-->
<string name="audio_sharing_incompatible_dialog_content">Audio sharing only works with headphones that support LE Audio</string> <string name="audio_sharing_incompatible_dialog_content">Audio sharing only works with headphones that support LE Audio</string>
<!-- Title for block pairing dialog in audio sharing [CHAR LIMIT=none]-->
<string name="audio_sharing_block_pairing_dialog_title">Turn off Audio Sharing</string>
<!-- Content for block pairing dialog in audio sharing [CHAR LIMIT=none]-->
<string name="audio_sharing_block_pairing_dialog_content">To pair a new device, turn off Audio Sharing first.</string>
<!-- Text for audio sharing turn off button [CHAR LIMIT=none]-->
<string name="audio_sharing_turn_off_button_label">Turn off</string>
<!-- Title for audio streams preference category [CHAR LIMIT=none]--> <!-- Title for audio streams preference category [CHAR LIMIT=none]-->
<string name="audio_streams_category_title">Connect to a LE audio stream</string> <string name="audio_streams_category_title">Connect to a LE audio stream</string>

View File

@@ -48,6 +48,7 @@ import com.android.settings.widget.GearPreference;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.flags.Flags;
import com.android.settingslib.utils.ThreadUtils; import com.android.settingslib.utils.ThreadUtils;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@@ -93,6 +94,7 @@ public final class BluetoothDevicePreference extends GearPreference {
private final int mType; private final int mType;
private AlertDialog mDisconnectDialog; private AlertDialog mDisconnectDialog;
@Nullable private AlertDialog mBlockPairingDialog;
private String contentDescription = null; private String contentDescription = null;
private boolean mHideSecondTarget = false; private boolean mHideSecondTarget = false;
private boolean mIsCallbackRemoved = true; private boolean mIsCallbackRemoved = true;
@@ -409,13 +411,24 @@ public final class BluetoothDevicePreference extends GearPreference {
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_CONNECT); SettingsEnums.ACTION_SETTINGS_BLUETOOTH_CONNECT);
mCachedDevice.connect(); mCachedDevice.connect();
} else if (bondState == BluetoothDevice.BOND_NONE) { } else if (bondState == BluetoothDevice.BOND_NONE) {
metricsFeatureProvider.action(context, var unused = ThreadUtils.postOnBackgroundThread(() -> {
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR); if (Flags.enableTemporaryBondDevicesUi() && Utils.shouldBlockPairingInAudioSharing(
if (!mCachedDevice.hasHumanReadableName()) { mLocalBtManager)) {
// TODO: collect metric
context.getMainExecutor().execute(() ->
mBlockPairingDialog =
Utils.showBlockPairingDialog(context, mBlockPairingDialog,
mLocalBtManager));
return;
}
metricsFeatureProvider.action(context, metricsFeatureProvider.action(context,
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES); SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR);
} if (!mCachedDevice.hasHumanReadableName()) {
pair(); metricsFeatureProvider.action(context,
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
}
context.getMainExecutor().execute(() -> pair());
});
} }
} }

View File

@@ -34,6 +34,7 @@ import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@@ -44,6 +45,7 @@ import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener; import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant; import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback; import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback;
@@ -324,4 +326,33 @@ public final class Utils {
.map(d -> BluetoothUtils.getGroupId(deviceManager.findDevice(d))).collect( .map(d -> BluetoothUtils.getGroupId(deviceManager.findDevice(d))).collect(
Collectors.toSet()).size() >= 2); Collectors.toSet()).size() >= 2);
} }
/**
* Show block pairing dialog during audio sharing
* @param context The dialog context
* @param dialog The dialog if already exists
* @param localBtManager {@link LocalBluetoothManager}
* @return The block pairing dialog
*/
@Nullable
static AlertDialog showBlockPairingDialog(@NonNull Context context,
@Nullable AlertDialog dialog, @Nullable LocalBluetoothManager localBtManager) {
if (!com.android.settingslib.flags.Flags.enableTemporaryBondDevicesUi()) return null;
if (dialog != null && dialog.isShowing()) return dialog;
if (dialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setNegativeButton(android.R.string.cancel, null)
.setTitle(R.string.audio_sharing_block_pairing_dialog_title)
.setMessage(R.string.audio_sharing_block_pairing_dialog_content);
LocalBluetoothLeBroadcast broadcast = localBtManager == null ? null :
localBtManager.getProfileManager().getLeAudioBroadcastProfile();
if (broadcast != null) {
builder.setPositiveButton(R.string.audio_sharing_turn_off_button_label,
(dlg, which) -> broadcast.stopLatestBroadcast());
}
dialog = builder.create();
}
dialog.show();
return dialog;
}
} }

View File

@@ -28,11 +28,17 @@ import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.os.UserManager; import android.os.UserManager;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.util.Pair; import android.util.Pair;
import androidx.appcompat.app.AlertDialog;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -42,8 +48,13 @@ import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils; import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.flags.Flags;
import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@@ -57,7 +68,9 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule; import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList; import java.util.ArrayList;
@@ -81,6 +94,8 @@ public class BluetoothDevicePreferenceTest {
@Rule @Rule
public final MockitoRule mockito = MockitoJUnit.rule(); public final MockitoRule mockito = MockitoJUnit.rule();
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
@Mock @Mock
private CachedBluetoothDevice mCachedBluetoothDevice; private CachedBluetoothDevice mCachedBluetoothDevice;
@Mock @Mock
@@ -107,6 +122,7 @@ public class BluetoothDevicePreferenceTest {
private CachedBluetoothDeviceManager mDeviceManager; private CachedBluetoothDeviceManager mDeviceManager;
private Context mContext = ApplicationProvider.getApplicationContext(); private Context mContext = ApplicationProvider.getApplicationContext();
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private FakeFeatureFactory mFakeFeatureFactory; private FakeFeatureFactory mFakeFeatureFactory;
private MetricsFeatureProvider mMetricsFeatureProvider; private MetricsFeatureProvider mMetricsFeatureProvider;
@@ -166,6 +182,7 @@ public class BluetoothDevicePreferenceTest {
when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(true); when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(true);
mPreference.onClicked(); mPreference.onClicked();
Shadows.shadowOf(Looper.getMainLooper()).idle();
verify(mMetricsFeatureProvider) verify(mMetricsFeatureProvider)
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR); .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
@@ -182,6 +199,7 @@ public class BluetoothDevicePreferenceTest {
when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(false); when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(false);
mPreference.onClicked(); mPreference.onClicked();
Shadows.shadowOf(Looper.getMainLooper()).idle();
verify(mMetricsFeatureProvider) verify(mMetricsFeatureProvider)
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR); .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
@@ -190,6 +208,58 @@ public class BluetoothDevicePreferenceTest {
MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES); MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
} }
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING, Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI})
public void onClicked_deviceNotBonded_blockPairing() {
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true);
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
LocalBluetoothProfileManager profileManager = mock(LocalBluetoothProfileManager.class);
LocalBluetoothLeBroadcast broadcast = mock(LocalBluetoothLeBroadcast.class);
LocalBluetoothLeBroadcastAssistant assistant = mock(
LocalBluetoothLeBroadcastAssistant.class);
when(mLocalBluetoothManager.getProfileManager()).thenReturn(profileManager);
when(profileManager.getLeAudioBroadcastProfile()).thenReturn(broadcast);
when(profileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(assistant);
when(broadcast.isEnabled(null)).thenReturn(true);
when(broadcast.getLatestBroadcastId()).thenReturn(1);
BluetoothDevice device1 = mock(BluetoothDevice.class);
BluetoothDevice device2 = mock(BluetoothDevice.class);
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
when(cachedDevice1.getDevice()).thenReturn(device1);
when(cachedDevice2.getDevice()).thenReturn(device2);
when(cachedDevice1.getGroupId()).thenReturn(1);
when(cachedDevice2.getGroupId()).thenReturn(2);
when(mDeviceManager.findDevice(device1)).thenReturn(cachedDevice1);
when(mDeviceManager.findDevice(device2)).thenReturn(cachedDevice2);
when(assistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(device1, device2));
BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
when(state.getBroadcastId()).thenReturn(1);
when(assistant.getAllSources(any())).thenReturn(ImmutableList.of(state));
when(mCachedBluetoothDevice.isConnected()).thenReturn(false);
when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
when(mCachedBluetoothDevice.startPairing()).thenReturn(true);
when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(true);
mPreference.onClicked();
Shadows.shadowOf(Looper.getMainLooper()).idle();
AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
assertThat(dialog).isNotNull();
ShadowAlertDialogCompat shadowAlertDialog = ShadowAlertDialogCompat.shadowOf(dialog);
assertThat(shadowAlertDialog.getTitle().toString()).isEqualTo(
mContext.getString(R.string.audio_sharing_block_pairing_dialog_title));
verify(mMetricsFeatureProvider, never())
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
verify(mCachedBluetoothDevice, never()).startPairing();
}
@Test @Test
public void getSecondTargetResource_shouldBeGearIconLayout() { public void getSecondTargetResource_shouldBeGearIconLayout() {
assertThat(mPreference.getSecondTargetResId()).isEqualTo(R.layout.preference_widget_gear); assertThat(mPreference.getSecondTargetResId()).isEqualTo(R.layout.preference_widget_gear);