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

Commit 85c1adef authored by Chelsea Hao's avatar Chelsea Hao Committed by Android (Google) Code Review
Browse files

Merge changes from topic "audioMode" into main

* changes:
  When an audio sharing session is active, devices should not be classified as audio sharing device during phone call.
  Pass in `isOngoingCall` to avoid calling `AudioManager.getMode` for better performance
parents 2190f8fa 7909b5e8
Loading
Loading
Loading
Loading
+6 −16
Original line number Diff line number Diff line
@@ -555,22 +555,17 @@ public class BluetoothUtils {
     * connected 2) is Hearing Aid or LE Audio OR 3) connected profile matches currentAudioProfile
     *
     * @param cachedDevice the CachedBluetoothDevice
     * @param audioManager audio manager to get the current audio profile
     * @param isOngoingCall get the current audio profile based on if in phone call
     * @return if the device is AvailableMediaBluetoothDevice
     */
    @WorkerThread
    public static boolean isAvailableMediaBluetoothDevice(
            CachedBluetoothDevice cachedDevice, AudioManager audioManager) {
        int audioMode = audioManager.getMode();
            CachedBluetoothDevice cachedDevice, boolean isOngoingCall) {
        int currentAudioProfile;

        if (audioMode == AudioManager.MODE_RINGTONE
                || audioMode == AudioManager.MODE_IN_CALL
                || audioMode == AudioManager.MODE_IN_COMMUNICATION) {
            // in phone call
        if (isOngoingCall) {
            currentAudioProfile = BluetoothProfile.HEADSET;
        } else {
            // without phone call
            currentAudioProfile = BluetoothProfile.A2DP;
        }

@@ -859,22 +854,17 @@ public class BluetoothUtils {
     * currentAudioProfile
     *
     * @param cachedDevice the CachedBluetoothDevice
     * @param audioManager audio manager to get the current audio profile
     * @param isOngoingCall get the current audio profile based on if in phone call
     * @return if the device is AvailableMediaBluetoothDevice
     */
    @WorkerThread
    public static boolean isConnectedBluetoothDevice(
            CachedBluetoothDevice cachedDevice, AudioManager audioManager) {
        int audioMode = audioManager.getMode();
            CachedBluetoothDevice cachedDevice, boolean isOngoingCall) {
        int currentAudioProfile;

        if (audioMode == AudioManager.MODE_RINGTONE
                || audioMode == AudioManager.MODE_IN_CALL
                || audioMode == AudioManager.MODE_IN_COMMUNICATION) {
            // in phone call
        if (isOngoingCall) {
            currentAudioProfile = BluetoothProfile.HEADSET;
        } else {
            // without phone call
            currentAudioProfile = BluetoothProfile.A2DP;
        }

+14 −22
Original line number Diff line number Diff line
@@ -87,7 +87,6 @@ public class BluetoothUtilsTest {
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private BluetoothDevice mBluetoothDevice;

    @Mock private AudioManager mAudioManager;
    @Mock private PackageManager mPackageManager;
    @Mock private LeAudioProfile mA2dpProfile;
    @Mock private LeAudioProfile mLeAudioProfile;
@@ -446,13 +445,12 @@ public class BluetoothUtilsTest {

        assertThat(
                        BluetoothUtils.isAvailableMediaBluetoothDevice(
                                mCachedBluetoothDevice, mAudioManager))
                                mCachedBluetoothDevice, /* isOngoingCall= */ false))
                .isTrue();
    }

    @Test
    public void isAvailableMediaBluetoothDevice_isHeadset_isConnectedA2dpDevice_returnFalse() {
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_RINGTONE);
        when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
@@ -460,13 +458,12 @@ public class BluetoothUtilsTest {

        assertThat(
                        BluetoothUtils.isAvailableMediaBluetoothDevice(
                                mCachedBluetoothDevice, mAudioManager))
                                mCachedBluetoothDevice,  /* isOngoingCall= */ true))
                .isFalse();
    }

    @Test
    public void isAvailableMediaBluetoothDevice_isA2dp_isConnectedA2dpDevice_returnTrue() {
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_NORMAL);
        when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
@@ -474,13 +471,12 @@ public class BluetoothUtilsTest {

        assertThat(
                        BluetoothUtils.isAvailableMediaBluetoothDevice(
                                mCachedBluetoothDevice, mAudioManager))
                                mCachedBluetoothDevice,  /* isOngoingCall= */ false))
                .isTrue();
    }

    @Test
    public void isAvailableMediaBluetoothDevice_isHeadset_isConnectedHfpDevice_returnTrue() {
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_RINGTONE);
        when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
@@ -488,7 +484,7 @@ public class BluetoothUtilsTest {

        assertThat(
                        BluetoothUtils.isAvailableMediaBluetoothDevice(
                                mCachedBluetoothDevice, mAudioManager))
                                mCachedBluetoothDevice, /* isOngoingCall= */ true))
                .isTrue();
    }

@@ -499,56 +495,52 @@ public class BluetoothUtilsTest {
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mBluetoothDevice.isConnected()).thenReturn(true);

        assertThat(BluetoothUtils.isConnectedBluetoothDevice(mCachedBluetoothDevice, mAudioManager))
                .isFalse();
        assertThat(BluetoothUtils.isConnectedBluetoothDevice(
                mCachedBluetoothDevice, /* isOngoingCall= */ false)).isFalse();
    }

    @Test
    public void isConnectedBluetoothDevice_isHeadset_isConnectedA2dpDevice_returnTrue() {
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_RINGTONE);
        when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mBluetoothDevice.isConnected()).thenReturn(true);

        assertThat(BluetoothUtils.isConnectedBluetoothDevice(mCachedBluetoothDevice, mAudioManager))
                .isTrue();
        assertThat(BluetoothUtils.isConnectedBluetoothDevice(
                mCachedBluetoothDevice,  /* isOngoingCall= */ true)).isTrue();
    }

    @Test
    public void isConnectedBluetoothDevice_isA2dp_isConnectedA2dpDevice_returnFalse() {
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_NORMAL);
        when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mBluetoothDevice.isConnected()).thenReturn(true);

        assertThat(BluetoothUtils.isConnectedBluetoothDevice(mCachedBluetoothDevice, mAudioManager))
                .isFalse();
        assertThat(BluetoothUtils.isConnectedBluetoothDevice(
                mCachedBluetoothDevice, /* isOngoingCall= */ false)).isFalse();
    }

    @Test
    public void isConnectedBluetoothDevice_isHeadset_isConnectedHfpDevice_returnFalse() {
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_RINGTONE);
        when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mBluetoothDevice.isConnected()).thenReturn(true);

        assertThat(BluetoothUtils.isConnectedBluetoothDevice(mCachedBluetoothDevice, mAudioManager))
                .isFalse();
        assertThat(BluetoothUtils.isConnectedBluetoothDevice(
                mCachedBluetoothDevice,  /* isOngoingCall= */ true)).isFalse();
    }

    @Test
    public void isConnectedBluetoothDevice_isNotConnected_returnFalse() {
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_RINGTONE);
        when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mBluetoothDevice.isConnected()).thenReturn(false);

        assertThat(BluetoothUtils.isConnectedBluetoothDevice(mCachedBluetoothDevice, mAudioManager))
                .isFalse();
        assertThat(BluetoothUtils.isConnectedBluetoothDevice(
                mCachedBluetoothDevice,  /* isOngoingCall= */ true)).isFalse();
    }

    @Test
+15 −6
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.os.Handler;
import android.platform.test.annotations.EnableFlags;
import android.provider.Settings;
import android.testing.TestableLooper;
@@ -75,6 +74,8 @@ import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.res.R;
import com.android.systemui.statusbar.phone.SystemUIDialog;
import com.android.systemui.statusbar.phone.SystemUIDialogManager;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;

import org.junit.After;
import org.junit.Before;
@@ -107,6 +108,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    private static final String TEST_LABEL = "label";
    private static final int TEST_PRESET_INDEX = 1;
    private static final String TEST_PRESET_NAME = "test_preset";
    private final FakeExecutor mExecutor = new FakeExecutor(new FakeSystemClock());

    @Mock
    private SystemUIDialogManager mSystemUIDialogManager;
@@ -150,11 +152,9 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    private SystemUIDialog mDialog;
    private SystemUIDialog.Factory mDialogFactory;
    private HearingDevicesDialogDelegate mDialogDelegate;
    private TestableLooper mTestableLooper;

    @Before
    public void setUp() {
        mTestableLooper = TestableLooper.get(this);
        when(mLocalBluetoothManager.getBluetoothAdapter()).thenReturn(mLocalBluetoothAdapter);
        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
        when(mProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile);
@@ -186,6 +186,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    public void clickPairNewDeviceButton_intentActionMatch() {
        setUpDeviceDialogWithPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();

        getPairNewDeviceButton(mDialog).performClick();

@@ -232,6 +233,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();

        assertToolsUi(0);
    }
@@ -246,6 +248,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();

        assertToolsUi(1);
    }
@@ -267,6 +270,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();

        assertToolsUi(2);
    }
@@ -278,6 +282,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();

        ViewGroup presetLayout = getPresetLayout(mDialog);
        assertThat(presetLayout.getVisibility()).isEqualTo(View.GONE);
@@ -291,7 +296,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mTestableLooper.processAllMessages();
        mExecutor.runAllReady();

        ViewGroup presetLayout = getPresetLayout(mDialog);
        assertThat(presetLayout.getVisibility()).isEqualTo(View.VISIBLE);
@@ -306,6 +311,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();

        ViewGroup ambientLayout = getAmbientLayout(mDialog);
        assertThat(ambientLayout.getVisibility()).isEqualTo(View.GONE);
@@ -318,6 +324,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();

        ViewGroup ambientLayout = getAmbientLayout(mDialog);
        assertThat(ambientLayout.getVisibility()).isEqualTo(View.GONE);
@@ -343,6 +350,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    public void onActiveDeviceChanged_presetExist_presetSelected() {
        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mExecutor.runAllReady();
        BluetoothHapPresetInfo info = getTestPresetInfo();
        when(mHapClientProfile.getAllPresetInfo(mDevice)).thenReturn(List.of(info));
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(TEST_PRESET_INDEX);
@@ -351,7 +359,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        assertThat(spinner.getSelectedItemPosition()).isEqualTo(-1);

        mDialogDelegate.onActiveDeviceChanged(mCachedDevice, BluetoothProfile.LE_AUDIO);
        mTestableLooper.processAllMessages();
        mExecutor.runAllReady();

        ViewGroup presetLayout = getPresetLayout(mDialog);
        assertThat(presetLayout.getVisibility()).isEqualTo(View.VISIBLE);
@@ -381,7 +389,8 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
                mActivityStarter,
                mDialogTransitionAnimator,
                mLocalBluetoothManager,
                new Handler(mTestableLooper.getLooper()),
                mExecutor,
                mExecutor,
                mAudioManager,
                mUiEventLogger
        );
+57 −37
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
@@ -49,6 +48,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

@@ -57,7 +57,6 @@ import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.utils.ThreadUtils;
import com.android.systemui.accessibility.hearingaid.HearingDevicesListAdapter.HearingDeviceItemCallback;
import com.android.systemui.animation.DialogTransitionAnimator;
import com.android.systemui.bluetooth.qsdialog.ActiveHearingDeviceItemFactory;
@@ -67,6 +66,7 @@ import com.android.systemui.bluetooth.qsdialog.DeviceItem;
import com.android.systemui.bluetooth.qsdialog.DeviceItemFactory;
import com.android.systemui.bluetooth.qsdialog.DeviceItemType;
import com.android.systemui.bluetooth.qsdialog.SavedHearingDeviceItemFactory;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.res.R;
@@ -79,6 +79,7 @@ import dagger.assisted.AssistedInject;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

/**
@@ -101,7 +102,8 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
    private final DialogTransitionAnimator mDialogTransitionAnimator;
    private final ActivityStarter mActivityStarter;
    private final LocalBluetoothManager mLocalBluetoothManager;
    private final Handler mMainHandler;
    private final Executor mMainExecutor;
    private final Executor mBgExecutor;
    private final AudioManager mAudioManager;
    private final LocalBluetoothProfileManager mProfileManager;
    private final HearingDevicesUiEventLogger mUiEventLogger;
@@ -109,8 +111,6 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
    private final int mLaunchSourceId;

    private SystemUIDialog mDialog;

    private List<DeviceItem> mHearingDeviceItemList;
    private HearingDevicesListAdapter mDeviceListAdapter;

    private View mPresetLayout;
@@ -122,14 +122,14 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
                @Override
                public void onPresetInfoUpdated(List<BluetoothHapPresetInfo> presetInfos,
                        int activePresetIndex) {
                    mMainHandler.post(
                    mMainExecutor.execute(
                            () -> refreshPresetUi(presetInfos, activePresetIndex));
                }

                @Override
                public void onPresetCommandFailed(int reason) {
                    mPresetController.refreshPresetInfo();
                    mMainHandler.post(() -> {
                    mMainExecutor.execute(() -> {
                        showErrorToast(R.string.hearing_devices_presets_error);
                    });
                }
@@ -166,7 +166,8 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
            ActivityStarter activityStarter,
            DialogTransitionAnimator dialogTransitionAnimator,
            @Nullable LocalBluetoothManager localBluetoothManager,
            @Main Handler handler,
            @Main Executor mainExecutor,
            @Background Executor bgExecutor,
            AudioManager audioManager,
            HearingDevicesUiEventLogger uiEventLogger) {
        mShowPairNewDevice = showPairNewDevice;
@@ -174,7 +175,8 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
        mActivityStarter = activityStarter;
        mDialogTransitionAnimator = dialogTransitionAnimator;
        mLocalBluetoothManager = localBluetoothManager;
        mMainHandler = handler;
        mMainExecutor = mainExecutor;
        mBgExecutor = bgExecutor;
        mAudioManager = audioManager;
        mProfileManager = localBluetoothManager.getProfileManager();
        mUiEventLogger = uiEventLogger;
@@ -227,9 +229,10 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
    @Override
    public void onActiveDeviceChanged(@Nullable CachedBluetoothDevice activeDevice,
            int bluetoothProfile) {
        refreshDeviceUi();
        mMainHandler.post(() -> {
            CachedBluetoothDevice device = getActiveHearingDevice();
        List<DeviceItem> hearingDeviceItemList = getHearingDeviceItemList();
        refreshDeviceUi(hearingDeviceItemList);
        mMainExecutor.execute(() -> {
            CachedBluetoothDevice device = getActiveHearingDevice(hearingDeviceItemList);
            if (mPresetController != null) {
                mPresetController.setDevice(device);
                mPresetLayout.setVisibility(
@@ -244,13 +247,15 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
    @Override
    public void onProfileConnectionStateChanged(@NonNull CachedBluetoothDevice cachedDevice,
            int state, int bluetoothProfile) {
        refreshDeviceUi();
        List<DeviceItem> hearingDeviceItemList = getHearingDeviceItemList();
        refreshDeviceUi(hearingDeviceItemList);
    }

    @Override
    public void onAclConnectionStateChanged(@NonNull CachedBluetoothDevice cachedDevice,
            int state) {
        refreshDeviceUi();
        List<DeviceItem> hearingDeviceItemList = getHearingDeviceItemList();
        refreshDeviceUi(hearingDeviceItemList);
    }

    @Override
@@ -280,18 +285,25 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,

        mUiEventLogger.log(HearingDevicesUiEvent.HEARING_DEVICES_DIALOG_SHOW, mLaunchSourceId);

        setupDeviceListView(dialog);
        mBgExecutor.execute(() -> {
            List<DeviceItem> hearingDeviceItemList = getHearingDeviceItemList();
            CachedBluetoothDevice activeHearingDevice = getActiveHearingDevice(
                    hearingDeviceItemList);
            mMainExecutor.execute(() -> {
                setupDeviceListView(dialog, hearingDeviceItemList);
                setupPairNewDeviceButton(dialog);
        setupPresetSpinner(dialog);
                setupPresetSpinner(dialog, activeHearingDevice);
                if (com.android.settingslib.flags.Flags.hearingDevicesAmbientVolumeControl()) {
            setupAmbientControls();
                    setupAmbientControls(activeHearingDevice);
                }
                setupRelatedToolsView(dialog);
            });
        });
    }

    @Override
    public void onStart(@NonNull SystemUIDialog dialog) {
        ThreadUtils.postOnBackgroundThread(() -> {
        mBgExecutor.execute(() -> {
            if (mLocalBluetoothManager != null) {
                mLocalBluetoothManager.getEventManager().registerCallback(this);
            }
@@ -306,7 +318,7 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,

    @Override
    public void onStop(@NonNull SystemUIDialog dialog) {
        ThreadUtils.postOnBackgroundThread(() -> {
        mBgExecutor.execute(() -> {
            if (mLocalBluetoothManager != null) {
                mLocalBluetoothManager.getEventManager().unregisterCallback(this);
            }
@@ -319,17 +331,18 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
        });
    }

    private void setupDeviceListView(SystemUIDialog dialog) {
    private void setupDeviceListView(SystemUIDialog dialog,
            List<DeviceItem> hearingDeviceItemList) {
        final RecyclerView deviceList = dialog.requireViewById(R.id.device_list);
        deviceList.setLayoutManager(new LinearLayoutManager(dialog.getContext()));
        mHearingDeviceItemList = getHearingDeviceItemList();
        mDeviceListAdapter = new HearingDevicesListAdapter(mHearingDeviceItemList, this);
        mDeviceListAdapter = new HearingDevicesListAdapter(hearingDeviceItemList, this);
        deviceList.setAdapter(mDeviceListAdapter);
    }

    private void setupPresetSpinner(SystemUIDialog dialog) {
    private void setupPresetSpinner(SystemUIDialog dialog,
            CachedBluetoothDevice activeHearingDevice) {
        mPresetController = new HearingDevicesPresetsController(mProfileManager, mPresetCallback);
        mPresetController.setDevice(getActiveHearingDevice());
        mPresetController.setDevice(activeHearingDevice);

        mPresetSpinner = dialog.requireViewById(R.id.preset_spinner);
        mPresetInfoAdapter = new HearingDevicesSpinnerAdapter(dialog.getContext());
@@ -367,12 +380,12 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
        mPresetLayout.setVisibility(mPresetController.isPresetControlAvailable() ? VISIBLE : GONE);
    }

    private void setupAmbientControls() {
    private void setupAmbientControls(CachedBluetoothDevice activeHearingDevice) {
        final AmbientVolumeLayout ambientLayout = mDialog.requireViewById(R.id.ambient_layout);
        mAmbientController = new AmbientVolumeUiController(
                mDialog.getContext(), mLocalBluetoothManager, ambientLayout);
        mAmbientController.setShowUiWhenLocalDataExist(false);
        mAmbientController.loadDevice(getActiveHearingDevice());
        mAmbientController.loadDevice(activeHearingDevice);
    }

    private void setupPairNewDeviceButton(SystemUIDialog dialog) {
@@ -429,10 +442,11 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
        }
    }

    private void refreshDeviceUi() {
        mHearingDeviceItemList = getHearingDeviceItemList();
        mMainHandler.post(() -> {
            mDeviceListAdapter.refreshDeviceItemList(mHearingDeviceItemList);
    private void refreshDeviceUi(List<DeviceItem> hearingDeviceItemList) {
        mMainExecutor.execute(() -> {
            if (mDeviceListAdapter != null) {
                mDeviceListAdapter.refreshDeviceItemList(hearingDeviceItemList);
            }
        });
    }

@@ -463,21 +477,27 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
    }

    @Nullable
    private CachedBluetoothDevice getActiveHearingDevice() {
        return mHearingDeviceItemList.stream()
    private static CachedBluetoothDevice getActiveHearingDevice(
            List<DeviceItem> hearingDeviceItemList) {
        return hearingDeviceItemList.stream()
                .filter(item -> item.getType() == DeviceItemType.ACTIVE_MEDIA_BLUETOOTH_DEVICE)
                .map(DeviceItem::getCachedBluetoothDevice)
                .findFirst()
                .orElse(null);
    }

    @WorkerThread
    private DeviceItem createHearingDeviceItem(CachedBluetoothDevice cachedDevice) {
        final Context context = mDialog.getContext();
        if (cachedDevice == null) {
            return null;
        }
        int mode = mAudioManager.getMode();
        boolean isOngoingCall = mode == AudioManager.MODE_RINGTONE
                || mode == AudioManager.MODE_IN_CALL
                || mode == AudioManager.MODE_IN_COMMUNICATION;
        for (DeviceItemFactory itemFactory : mHearingDeviceItemFactoryList) {
            if (itemFactory.isFilterMatched(context, cachedDevice, mAudioManager)) {
            if (itemFactory.isFilterMatched(context, cachedDevice, isOngoingCall)) {
                return itemFactory.create(context, cachedDevice);
            }
        }
+3 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import com.android.app.tracing.coroutines.launchTraced as launch
import com.android.internal.jank.InteractionJankMonitor
import com.android.internal.logging.UiEventLogger
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast
import com.android.settingslib.volume.domain.interactor.AudioModeInteractor
import com.android.systemui.Prefs
import com.android.systemui.animation.DialogCuj
import com.android.systemui.animation.DialogTransitionAnimator
@@ -71,6 +72,7 @@ constructor(
    private val bluetoothStateInteractor: BluetoothStateInteractor,
    private val bluetoothAutoOnInteractor: BluetoothAutoOnInteractor,
    private val audioSharingInteractor: AudioSharingInteractor,
    private val audioModeInteractor: AudioModeInteractor,
    private val audioSharingButtonViewModelFactory: AudioSharingButtonViewModel.Factory,
    private val bluetoothDeviceMetadataInteractor: BluetoothDeviceMetadataInteractor,
    private val dialogTransitionAnimator: DialogTransitionAnimator,
@@ -167,6 +169,7 @@ constructor(
                // the device item list and animate the progress bar.
                merge(
                        deviceItemInteractor.deviceItemUpdateRequest,
                        audioModeInteractor.isOngoingCall,
                        bluetoothDeviceMetadataInteractor.metadataUpdate,
                        if (
                            audioSharingInteractor.audioSharingAvailable() &&
Loading