Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 009747ce authored by Yiyi Shen's avatar Yiyi Shen Committed by Android (Google) Code Review
Browse files

Merge "[Audiosharing] Block pairing during audio sharing" into main

parents 30af231c 93f90617
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -14016,6 +14016,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>
    <!-- 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>
    <!-- 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]-->
    <string name="audio_streams_category_title">Connect to a LE audio stream</string>
+19 −6
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import com.android.settings.widget.GearPreference;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.flags.Flags;
import com.android.settingslib.utils.ThreadUtils;

import java.lang.annotation.Retention;
@@ -93,6 +94,7 @@ public final class BluetoothDevicePreference extends GearPreference {
    private final int mType;

    private AlertDialog mDisconnectDialog;
    @Nullable private AlertDialog mBlockPairingDialog;
    private String contentDescription = null;
    private boolean mHideSecondTarget = false;
    private boolean mIsCallbackRemoved = true;
@@ -409,13 +411,24 @@ public final class BluetoothDevicePreference extends GearPreference {
                    SettingsEnums.ACTION_SETTINGS_BLUETOOTH_CONNECT);
            mCachedDevice.connect();
        } else if (bondState == BluetoothDevice.BOND_NONE) {
            var unused = ThreadUtils.postOnBackgroundThread(() -> {
                if (Flags.enableTemporaryBondDevicesUi() && Utils.shouldBlockPairingInAudioSharing(
                        mLocalBtManager)) {
                    // TODO: collect metric
                    context.getMainExecutor().execute(() ->
                            mBlockPairingDialog =
                                    Utils.showBlockPairingDialog(context, mBlockPairingDialog,
                                            mLocalBtManager));
                    return;
                }
                metricsFeatureProvider.action(context,
                        SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR);
                if (!mCachedDevice.hasHumanReadableName()) {
                    metricsFeatureProvider.action(context,
                            SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
                }
            pair();
                context.getMainExecutor().execute(() -> pair());
            });
        }
    }

+31 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
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.CachedBluetoothDevice;
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.BluetoothManagerCallback;
@@ -324,4 +326,33 @@ public final class Utils {
                .map(d -> BluetoothUtils.getGroupId(deviceManager.findDevice(d))).collect(
                        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;
    }
}
+70 −0
Original line number Diff line number Diff line
@@ -28,11 +28,17 @@ import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.os.UserManager;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.util.Pair;

import androidx.appcompat.app.AlertDialog;
import androidx.test.core.app.ApplicationProvider;

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.settingslib.bluetooth.CachedBluetoothDevice;
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.LocalBluetoothProfileManager;
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.ImmutableSet;
@@ -57,7 +68,9 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;

import java.util.ArrayList;
@@ -81,6 +94,8 @@ public class BluetoothDevicePreferenceTest {

    @Rule
    public final MockitoRule mockito = MockitoJUnit.rule();
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
    @Mock
    private CachedBluetoothDevice mCachedBluetoothDevice;
    @Mock
@@ -107,6 +122,7 @@ public class BluetoothDevicePreferenceTest {
    private CachedBluetoothDeviceManager mDeviceManager;

    private Context mContext = ApplicationProvider.getApplicationContext();
    private ShadowBluetoothAdapter mShadowBluetoothAdapter;
    private FakeFeatureFactory mFakeFeatureFactory;
    private MetricsFeatureProvider mMetricsFeatureProvider;

@@ -166,6 +182,7 @@ public class BluetoothDevicePreferenceTest {
        when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(true);

        mPreference.onClicked();
        Shadows.shadowOf(Looper.getMainLooper()).idle();

        verify(mMetricsFeatureProvider)
                .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
@@ -182,6 +199,7 @@ public class BluetoothDevicePreferenceTest {
        when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(false);

        mPreference.onClicked();
        Shadows.shadowOf(Looper.getMainLooper()).idle();

        verify(mMetricsFeatureProvider)
                .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
@@ -190,6 +208,58 @@ public class BluetoothDevicePreferenceTest {
                        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
    public void getSecondTargetResource_shouldBeGearIconLayout() {
        assertThat(mPreference.getSecondTargetResId()).isEqualTo(R.layout.preference_widget_gear);