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

Commit db622c1a authored by Candice's avatar Candice
Browse files

Allow connected Hearing Aid devices to be searched by names

Bug: 353847080
Test: atest AvailableHearingDevicePreferenceControllerTest
Flag: com.android.settings.accessibility.fix_a11y_settings_search
Change-Id: I634c5ce7b31ae5d7f78a006b9cedc0725794482a
parent 898d1e07
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -18,11 +18,14 @@ package com.android.settings.accessibility;

import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.dashboard.DashboardFragment;
@@ -32,6 +35,9 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.search.SearchIndexableRaw;

import java.util.List;

/**
 * Controller to update the {@link androidx.preference.PreferenceCategory} for all
@@ -44,6 +50,7 @@ public class AvailableHearingDevicePreferenceController extends
        BluetoothCallback {

    private static final String TAG = "AvailableHearingDevicePreferenceController";
    private static final String SEARCH_DATA_KEY_PREFIX = "a11y_available_hearing_device";

    private BluetoothDeviceUpdater mAvailableHearingDeviceUpdater;
    private final LocalBluetoothManager mLocalBluetoothManager;
@@ -56,6 +63,14 @@ public class AvailableHearingDevicePreferenceController extends
                context);
    }

    @VisibleForTesting
    void init(AvailableHearingDeviceUpdater availableHearingDeviceUpdater) {
        if (mAvailableHearingDeviceUpdater != null) {
            throw new IllegalStateException("Should not call init() more than 1 time.");
        }
        mAvailableHearingDeviceUpdater = availableHearingDeviceUpdater;
    }

    /**
     * Initializes objects in this controller. Need to call this before onStart().
     *
@@ -107,4 +122,34 @@ public class AvailableHearingDevicePreferenceController extends
                    getMetricsCategory());
        }
    }

    @Override
    public void updateDynamicRawDataToIndex(List<SearchIndexableRaw> rawData) {
        if (Flags.fixA11ySettingsSearch()) {
            if (mLocalBluetoothManager == null) {
                Log.d(TAG, "Bluetooth is not supported");
                return;
            }

            for (CachedBluetoothDevice cachedDevice :
                    mLocalBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()) {

                if (!AvailableHearingDeviceUpdater.isAvailableHearingDevice(cachedDevice)) {
                    continue;
                }

                SearchIndexableRaw data = new SearchIndexableRaw(mContext);
                // Include the identity address and add prefix to ensure the key is unique and
                // distinguish from Bluetooth's connected devices.
                data.key = SEARCH_DATA_KEY_PREFIX
                        + cachedDevice.getName() + cachedDevice.getIdentityAddress();
                data.title = cachedDevice.getName();
                data.summaryOn = mContext.getString(R.string.accessibility_hearingaid_title);
                data.screenTitle = mContext.getString(R.string.accessibility_hearingaid_title);
                rawData.add(data);
            }
        } else {
            super.updateDynamicRawDataToIndex(rawData);
        }
    }
}
+9 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.accessibility;

import android.bluetooth.BluetoothDevice;
import android.content.Context;

import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
@@ -34,10 +35,16 @@ public class AvailableHearingDeviceUpdater extends AvailableMediaBluetoothDevice
        super(context, devicePreferenceCallback, metricsCategory);
    }

    static boolean isAvailableHearingDevice(CachedBluetoothDevice cachedDevice) {
        final BluetoothDevice device = cachedDevice.getDevice();
        return cachedDevice.isHearingAidDevice()
                && device.getBondState() == BluetoothDevice.BOND_BONDED
                && device.isConnected();
    }

    @Override
    public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
        return cachedDevice.isHearingAidDevice()
                && isDeviceConnected(cachedDevice)
        return isAvailableHearingDevice(cachedDevice)
                && isDeviceInCachedDevicesList(cachedDevice);
    }

+118 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.accessibility;

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

import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;

import androidx.test.core.app.ApplicationProvider;

import com.android.settings.bluetooth.Utils;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.search.SearchIndexableRaw;

import com.google.common.collect.ImmutableList;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;

/** Tests for {@link AvailableHearingDevicePreferenceController} */
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothUtils.class})
public class AvailableHearingDevicePreferenceControllerTest {
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private static final String PREFERENCE_KEY = "preference_key";
    private static final String DEVICE_NAME = "device";

    private Context mContext;
    private AvailableHearingDevicePreferenceController mAvailableHearingDevicePreferenceController;
    @Mock
    private AvailableHearingDeviceUpdater mAvailableHearingDeviceUpdater;
    @Mock
    private CachedBluetoothDeviceManager mCachedDeviceManager;
    @Mock
    private LocalBluetoothManager mLocalBluetoothManager;
    @Mock
    private CachedBluetoothDevice mCachedDevice;
    @Mock
    private BluetoothDevice mDevice;


    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mContext = ApplicationProvider.getApplicationContext();
        ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager;
        mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
        when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
        when(mCachedDevice.getName()).thenReturn(DEVICE_NAME);
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(
                ImmutableList.of(mCachedDevice));

        mAvailableHearingDevicePreferenceController =
                new AvailableHearingDevicePreferenceController(mContext, PREFERENCE_KEY);
        mAvailableHearingDevicePreferenceController.init(mAvailableHearingDeviceUpdater);
    }

    @Test
    @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
    public void updateDynamicRawDataToIndex_isNotHearingAidDevice_deviceIsNotSearchable() {
        when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice.isConnected()).thenReturn(true);
        when(mCachedDevice.isHearingAidDevice()).thenReturn(false);
        List<SearchIndexableRaw> searchData = new ArrayList<>();

        mAvailableHearingDevicePreferenceController.updateDynamicRawDataToIndex(searchData);

        assertThat(searchData).isEmpty();
    }

    @Test
    @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
    public void updateDynamicRawDataToIndex_isHearingAidDevice_deviceIsSearchable() {
        when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice.isConnected()).thenReturn(true);
        when(mCachedDevice.isHearingAidDevice()).thenReturn(true);
        List<SearchIndexableRaw> searchData = new ArrayList<>();

        mAvailableHearingDevicePreferenceController.updateDynamicRawDataToIndex(searchData);

        assertThat(searchData).isNotEmpty();
        assertThat(searchData.get(0).key).contains(DEVICE_NAME);
    }
}