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

Commit f39bb2f8 authored by jasonwshsu's avatar jasonwshsu
Browse files

Preset dropdown menu is not disabled when LE audio is toggled off

Root Cause: Preset spinner should update visibility at the right time.

Solution: Check if HapClientProfile is connected when active device
changed and when preset spinner is created.

Bug: 347629387
Test: atest HearingDevicesDialogDelegateTest
Flag: EXEMPT bugfix
Change-Id: Ic3becf2185a8bb26f5c3c3bbf737959fe5019b20
parent 19e9179b
Loading
Loading
Loading
Loading
+16 −7
Original line number Diff line number Diff line
@@ -226,8 +226,8 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
            final int activePresetIndex = mPresetsController.getActivePresetIndex();
            refreshPresetInfoAdapter(presetInfos, activePresetIndex);
            mPresetSpinner.setVisibility(
                    (activeHearingDevice != null && !mPresetInfoAdapter.isEmpty()) ? VISIBLE
                            : GONE);
                    (activeHearingDevice != null && activeHearingDevice.isConnectedHapClientDevice()
                            && !mPresetInfoAdapter.isEmpty()) ? VISIBLE : GONE);
        });
    }

@@ -303,6 +303,11 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
        mLocalBluetoothManager.getEventManager().unregisterCallback(this);
    }

    @VisibleForTesting
    void setHearingDevicesPresetsController(HearingDevicesPresetsController controller) {
        mPresetsController = controller;
    }

    private void setupDeviceListView(SystemUIDialog dialog) {
        mDeviceList.setLayoutManager(new LinearLayoutManager(dialog.getContext()));
        mHearingDeviceItemList = getHearingDevicesList();
@@ -311,12 +316,15 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
    }

    private void setupPresetSpinner(SystemUIDialog dialog) {
        mPresetsController = new HearingDevicesPresetsController(mProfileManager, mPresetCallback);
        if (mPresetsController == null) {
            mPresetsController = new HearingDevicesPresetsController(mProfileManager,
                    mPresetCallback);
        }
        final CachedBluetoothDevice activeHearingDevice = getActiveHearingDevice(
                mHearingDeviceItemList);
        mPresetsController.setActiveHearingDevice(activeHearingDevice);

        mPresetInfoAdapter = new ArrayAdapter<String>(dialog.getContext(),
        mPresetInfoAdapter = new ArrayAdapter<>(dialog.getContext(),
                R.layout.hearing_devices_preset_spinner_selected,
                R.id.hearing_devices_preset_option_text);
        mPresetInfoAdapter.setDropDownViewResource(
@@ -350,7 +358,8 @@ public class HearingDevicesDialogDelegate implements SystemUIDialog.Delegate,
        final int activePresetIndex = mPresetsController.getActivePresetIndex();
        refreshPresetInfoAdapter(presetInfos, activePresetIndex);
        mPresetSpinner.setVisibility(
                (activeHearingDevice != null && !mPresetInfoAdapter.isEmpty()) ? VISIBLE : GONE);
                (activeHearingDevice != null && activeHearingDevice.isConnectedHapClientDevice()
                        && !mPresetInfoAdapter.isEmpty()) ? VISIBLE : GONE);
    }

    private void setupPairNewDeviceButton(SystemUIDialog dialog, @Visibility int visibility) {
+100 −33
Original line number Diff line number Diff line
@@ -16,8 +16,9 @@

package com.android.systemui.accessibility.hearingaid;

import static android.bluetooth.BluetoothHapClient.PRESET_INDEX_UNAVAILABLE;

import static com.android.systemui.accessibility.hearingaid.HearingDevicesDialogDelegate.LIVE_CAPTION_INTENT;
import static com.android.systemui.statusbar.phone.SystemUIDialog.DEFAULT_DISMISS_ON_DEVICE_LOCK;

import static com.google.common.truth.Truth.assertThat;

@@ -26,9 +27,13 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHapPresetInfo;
import android.bluetooth.BluetoothProfile;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
@@ -42,6 +47,7 @@ import android.provider.Settings;
import android.testing.TestableLooper;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
@@ -86,13 +92,14 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    public MockitoRule mockito = MockitoJUnit.rule();

    private static final String DEVICE_ADDRESS = "AA:BB:CC:DD:EE:FF";
    private static final String DEVICE_NAME = "test_name";
    private static final String TEST_PKG = "pkg";
    private static final String TEST_CLS = "cls";
    private static final ComponentName TEST_COMPONENT = new ComponentName(TEST_PKG, TEST_CLS);
    private static final String TEST_LABEL = "label";
    private static final int TEST_PRESET_INDEX = 1;
    private static final String TEST_PRESET_NAME = "test_preset";

    @Mock
    private SystemUIDialog.Factory mSystemUIDialogFactory;
    @Mock
    private SystemUIDialogManager mSystemUIDialogManager;
    @Mock
@@ -118,6 +125,8 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    @Mock
    private CachedBluetoothDevice mCachedDevice;
    @Mock
    private BluetoothDevice mDevice;
    @Mock
    private DeviceItem mHearingDeviceItem;
    @Mock
    private PackageManager mPackageManager;
@@ -125,7 +134,10 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    private ActivityInfo mActivityInfo;
    @Mock
    private Drawable mDrawable;
    @Mock
    private HearingDevicesPresetsController mPresetsController;
    private SystemUIDialog mDialog;
    private SystemUIDialog.Factory mDialogFactory;
    private HearingDevicesDialogDelegate mDialogDelegate;
    private TestableLooper mTestableLooper;
    private final List<CachedBluetoothDevice> mDevices = new ArrayList<>();
@@ -141,23 +153,23 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mDevices);
        when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
        when(mSysUiState.setFlag(anyLong(), anyBoolean())).thenReturn(mSysUiState);
        when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice.isConnected()).thenReturn(true);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
        when(mCachedDevice.getAddress()).thenReturn(DEVICE_ADDRESS);
        when(mCachedDevice.getName()).thenReturn(DEVICE_NAME);
        when(mCachedDevice.isActiveDevice(BluetoothProfile.HEARING_AID)).thenReturn(true);
        when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true);
        when(mCachedDevice.isConnectedHapClientDevice()).thenReturn(true);
        when(mHearingDeviceItem.getCachedBluetoothDevice()).thenReturn(mCachedDevice);
        mContext.setMockPackageManager(mPackageManager);

        setUpPairNewDeviceDialog();

        when(mSystemUIDialogFactory.create(any(SystemUIDialog.Delegate.class)))
                .thenReturn(mDialog);
    }

    @Test
    public void createDialog_dialogShown() {
        assertThat(mDialogDelegate.createDialog()).isEqualTo(mDialog);
        mContext.setMockPackageManager(mPackageManager);
        mDevices.add(mCachedDevice);
    }

    @Test
    public void clickPairNewDeviceButton_intentActionMatch() {
        setUpPairNewDeviceDialog();
        mDialog.show();

        getPairNewDeviceButton(mDialog).performClick();
@@ -185,6 +197,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

    @Test
    public void onDeviceItemOnClicked_connectedDevice_disconnect() {
        setUpDeviceListDialog();
        when(mHearingDeviceItem.getType()).thenReturn(DeviceItemType.CONNECTED_BLUETOOTH_DEVICE);

        mDialogDelegate.onDeviceItemOnClicked(mHearingDeviceItem, new View(mContext));
@@ -231,50 +244,100 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        assertThat(relatedToolsView.getChildCount()).isEqualTo(2);
    }

    @Test
    public void showDialog_noPreset_presetGone() {
        when(mPresetsController.getAllPresetInfo()).thenReturn(new ArrayList<>());
        when(mPresetsController.getActivePresetIndex()).thenReturn(PRESET_INDEX_UNAVAILABLE);

        setUpDeviceListDialog();
        mDialog.show();

        Spinner spinner = (Spinner) getPresetSpinner(mDialog);
        assertThat(spinner.getVisibility()).isEqualTo(View.GONE);
    }

    @Test
    public void showDialog_presetExist_presetSelected() {
        BluetoothHapPresetInfo info = getTestPresetInfo();
        when(mPresetsController.getAllPresetInfo()).thenReturn(List.of(info));
        when(mPresetsController.getActivePresetIndex()).thenReturn(TEST_PRESET_INDEX);

        setUpDeviceListDialog();
        mDialog.show();

        Spinner spinner = (Spinner) getPresetSpinner(mDialog);
        assertThat(spinner.getVisibility()).isEqualTo(View.VISIBLE);
        assertThat(spinner.getSelectedItemPosition()).isEqualTo(0);
    }

    @Test
    public void onActiveDeviceChanged_presetExist_presetSelected() {
        setUpDeviceListDialog();
        mDialog.show();
        BluetoothHapPresetInfo info = getTestPresetInfo();
        when(mPresetsController.getAllPresetInfo()).thenReturn(List.of(info));
        when(mPresetsController.getActivePresetIndex()).thenReturn(TEST_PRESET_INDEX);

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

        Spinner spinner = (Spinner) getPresetSpinner(mDialog);
        assertThat(spinner.getVisibility()).isEqualTo(View.VISIBLE);
        assertThat(spinner.getSelectedItemPosition()).isEqualTo(0);
    }



    private void setUpPairNewDeviceDialog() {
        mDialogFactory = new SystemUIDialog.Factory(
                mContext,
                mSystemUIDialogManager,
                mSysUiState,
                getFakeBroadcastDispatcher(),
                mDialogTransitionAnimator
        );
        mDialogDelegate = new HearingDevicesDialogDelegate(
                mContext,
                true,
                mSystemUIDialogFactory,
                mDialogFactory,
                mActivityStarter,
                mDialogTransitionAnimator,
                mLocalBluetoothManager,
                new Handler(mTestableLooper.getLooper()),
                mAudioManager
        );
        mDialog = new SystemUIDialog(

        mDialog = mDialogDelegate.createDialog();
    }

    private void setUpDeviceListDialog() {
        mDialogFactory = new SystemUIDialog.Factory(
                mContext,
                0,
                DEFAULT_DISMISS_ON_DEVICE_LOCK,
                mSystemUIDialogManager,
                mSysUiState,
                getFakeBroadcastDispatcher(),
                mDialogTransitionAnimator,
                mDialogDelegate
                mDialogTransitionAnimator
        );
    }

    private void setUpDeviceListDialog() {
        mDialogDelegate = new HearingDevicesDialogDelegate(
                mContext,
                false,
                mSystemUIDialogFactory,
                mDialogFactory,
                mActivityStarter,
                mDialogTransitionAnimator,
                mLocalBluetoothManager,
                new Handler(mTestableLooper.getLooper()),
                mAudioManager
        );
        mDialog = new SystemUIDialog(
                mContext,
                0,
                DEFAULT_DISMISS_ON_DEVICE_LOCK,
                mSystemUIDialogManager,
                mSysUiState,
                getFakeBroadcastDispatcher(),
                mDialogTransitionAnimator,
                mDialogDelegate
        );

        mDialog = mDialogDelegate.createDialog();
        mDialogDelegate.setHearingDevicesPresetsController(mPresetsController);
    }

    private BluetoothHapPresetInfo getTestPresetInfo() {
        BluetoothHapPresetInfo info = mock(BluetoothHapPresetInfo.class);
        when(info.getName()).thenReturn(TEST_PRESET_NAME);
        when(info.getIndex()).thenReturn(TEST_PRESET_INDEX);
        return info;
    }

    private View getPairNewDeviceButton(SystemUIDialog dialog) {
@@ -285,6 +348,10 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        return dialog.requireViewById(R.id.related_tools_container);
    }

    private View getPresetSpinner(SystemUIDialog dialog) {
        return dialog.requireViewById(R.id.preset_spinner);
    }

    @After
    public void reset() {
        if (mDialogDelegate != null) {