From b07754d178ee569344e4f6d00a4595e8edcbe298 Mon Sep 17 00:00:00 2001 From: jasonwshsu Date: Wed, 3 Aug 2022 13:20:20 +0800 Subject: [PATCH] Add pair button in bluetooth details page for hearing aid device Root Cause: Users can not connect another ear again after they cancel the pairing dialog in Accessibility -> hearing aids entry Solution: Add pair button in bluetooth details page for hearing aid device Bug: 233038449 Test: make RunSettingsRoboTests ROBOTEST_FILTER=BluetoothDetailsPairOtherControllerTest Change-Id: I6a7af1c2c2263476b040233edb072cc64a2927b0 --- res/values/strings.xml | 5 + res/xml/bluetooth_device_details_fragment.xml | 6 + .../BluetoothDetailsPairOtherController.java | 102 +++++++++++++++ .../BluetoothDeviceDetailsFragment.java | 2 + ...uetoothDetailsPairOtherControllerTest.java | 121 ++++++++++++++++++ 5 files changed, 236 insertions(+) create mode 100644 src/com/android/settings/bluetooth/BluetoothDetailsPairOtherController.java create mode 100644 tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherControllerTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index f397b478562..dc3d92ae626 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -157,6 +157,11 @@ bluetooth + + + Pair right ear + + Pair left ear Pair your other ear diff --git a/res/xml/bluetooth_device_details_fragment.xml b/res/xml/bluetooth_device_details_fragment.xml index f330b19e2a1..efb2bf7036b 100644 --- a/res/xml/bluetooth_device_details_fragment.xml +++ b/res/xml/bluetooth_device_details_fragment.xml @@ -42,6 +42,12 @@ settings:searchable="false" settings:controller="com.android.settings.bluetooth.LeAudioBluetoothDetailsHeaderController"/> + + + diff --git a/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherController.java b/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherController.java new file mode 100644 index 00000000000..d14a9b1144a --- /dev/null +++ b/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherController.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2022 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.bluetooth; + +import android.content.Context; + +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.PreferenceScreen; + +import com.android.settings.R; +import com.android.settings.core.SubSettingLauncher; +import com.android.settingslib.bluetooth.CachedBluetoothDevice; +import com.android.settingslib.bluetooth.HearingAidProfile; +import com.android.settingslib.core.lifecycle.Lifecycle; +import com.android.settingslib.widget.ButtonPreference; + +/** + * This class handles button preference logic to display for hearing aid device. + */ +public class BluetoothDetailsPairOtherController extends BluetoothDetailsController { + private static final String KEY_PAIR_OTHER = "hearing_aid_pair_other_button"; + + private ButtonPreference mPreference; + + public BluetoothDetailsPairOtherController(Context context, + PreferenceFragmentCompat fragment, + CachedBluetoothDevice device, + Lifecycle lifecycle) { + super(context, fragment, device, lifecycle); + lifecycle.addObserver(this); + } + + @Override + public boolean isAvailable() { + return getButtonPreferenceVisibility(mCachedDevice); + } + + @Override + public String getPreferenceKey() { + return KEY_PAIR_OTHER; + } + + @Override + protected void init(PreferenceScreen screen) { + final int side = mCachedDevice.getDeviceSide(); + final int stringRes = (side == HearingAidProfile.DeviceSide.SIDE_LEFT) + ? R.string.bluetooth_pair_right_ear_button + : R.string.bluetooth_pair_left_ear_button; + + mPreference = screen.findPreference(getPreferenceKey()); + mPreference.setTitle(stringRes); + mPreference.setOnClickListener(v -> launchPairingDetail()); + } + + @Override + protected void refresh() { + mPreference.setVisible(getButtonPreferenceVisibility(mCachedDevice)); + } + + private boolean getButtonPreferenceVisibility(CachedBluetoothDevice cachedDevice) { + return isBinauralMode(cachedDevice) && isOnlyOneSideConnected(cachedDevice); + } + + private void launchPairingDetail() { + new SubSettingLauncher(mContext) + .setDestination(BluetoothPairingDetail.class.getName()) + .setSourceMetricsCategory( + ((BluetoothDeviceDetailsFragment) mFragment).getMetricsCategory()) + .launch(); + } + + private boolean isBinauralMode(CachedBluetoothDevice cachedDevice) { + return cachedDevice.getDeviceMode() == HearingAidProfile.DeviceMode.MODE_BINAURAL; + } + + private boolean isOnlyOneSideConnected(CachedBluetoothDevice cachedDevice) { + if (!cachedDevice.isConnectedHearingAidDevice()) { + return false; + } + + final CachedBluetoothDevice subDevice = cachedDevice.getSubDevice(); + if (subDevice != null && subDevice.isConnectedHearingAidDevice()) { + return false; + } + + return true; + } +} diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java index 1c2b27fa799..f8914c310f6 100644 --- a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java +++ b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java @@ -268,6 +268,8 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment lifecycle)); controllers.add(new BluetoothDetailsRelatedToolsController(context, this, mCachedDevice, lifecycle)); + controllers.add(new BluetoothDetailsPairOtherController(context, this, mCachedDevice, + lifecycle)); } return controllers; } diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherControllerTest.java new file mode 100644 index 00000000000..cfa6d41e0d7 --- /dev/null +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherControllerTest.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2022 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.bluetooth; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.when; + +import com.android.settings.R; +import com.android.settingslib.bluetooth.CachedBluetoothDevice; +import com.android.settingslib.bluetooth.HearingAidProfile; +import com.android.settingslib.widget.ButtonPreference; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.robolectric.RobolectricTestRunner; + +/** Tests for {@link BluetoothDetailsPairOtherController}. */ +@RunWith(RobolectricTestRunner.class) +public class BluetoothDetailsPairOtherControllerTest extends BluetoothDetailsControllerTestBase { + @Rule + public final MockitoRule mockito = MockitoJUnit.rule(); + + @Mock + private CachedBluetoothDevice mSubCachedDevice; + private BluetoothDetailsPairOtherController mController; + private ButtonPreference mPreference; + + @Override + public void setUp() { + super.setUp(); + + mController = new BluetoothDetailsPairOtherController(mContext, mFragment, mCachedDevice, + mLifecycle); + mPreference = new ButtonPreference(mContext); + mPreference.setKey(mController.getPreferenceKey()); + mScreen.addPreference(mPreference); + } + + @Test + public void init_leftSideDevice_expectedTitle() { + when(mCachedDevice.getDeviceSide()).thenReturn(HearingAidProfile.DeviceSide.SIDE_LEFT); + + mController.init(mScreen); + + assertThat(mPreference.getTitle().toString()).isEqualTo( + mContext.getString(R.string.bluetooth_pair_right_ear_button)); + } + + @Test + public void init_rightSideDevice_expectedTitle() { + when(mCachedDevice.getDeviceSide()).thenReturn(HearingAidProfile.DeviceSide.SIDE_RIGHT); + + mController.init(mScreen); + + assertThat(mPreference.getTitle().toString()).isEqualTo( + mContext.getString(R.string.bluetooth_pair_left_ear_button)); + } + + @Test + public void isAvailable_isConnectedHearingAidDevice_available() { + when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(false); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + public void isAvailable_notConnectedHearingAidDevice_notAvailable() { + when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); + when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidProfile.DeviceMode.MODE_MONAURAL); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + public void isAvailable_subDeviceIsConnectedHearingAidDevice_notAvailable() { + when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); + when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidProfile.DeviceMode.MODE_BINAURAL); + when(mSubCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); + when(mCachedDevice.getSubDevice()).thenReturn(mSubCachedDevice); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + public void isAvailable_subDeviceNotConnectedHearingAidDevice_available() { + when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); + when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidProfile.DeviceMode.MODE_BINAURAL); + when(mSubCachedDevice.isConnectedHearingAidDevice()).thenReturn(false); + when(mCachedDevice.getSubDevice()).thenReturn(mSubCachedDevice); + + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + public void isAvailable_subDeviceNotExist_available() { + when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true); + when(mCachedDevice.getDeviceMode()).thenReturn(HearingAidProfile.DeviceMode.MODE_BINAURAL); + when(mCachedDevice.getSubDevice()).thenReturn(null); + + assertThat(mController.isAvailable()).isTrue(); + } +}