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
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9f8df7b..7cfb966 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -184,6 +184,11 @@
     <!-- Keywords for bluetooth pairing item [CHAR LIMIT=30] -->
     <string name="keywords_add_bt_device">bluetooth</string>
 
+
+    <!-- Button to help user to pair right ear of the hearing aid device. It will show when only one of the hearing aid device set is connected. [CHAR LIMIT=20] -->
+    <string name="bluetooth_pair_right_ear_button">Pair right ear</string>
+    <!-- Button to help user to pair left ear of the hearing aid device. It will show when only one of the hearing aid device set is connected. [CHAR LIMIT=20] -->
+    <string name="bluetooth_pair_left_ear_button">Pair left ear</string>
     <!-- Connected devices settings. Title of the dialog to hint user to pair other ear of the hearing aid device. Shows when only one of the hearing aid device set is connected. [CHAR LIMIT=25] -->
     <string name="bluetooth_pair_other_ear_dialog_title">Pair your other ear</string>
     <!-- Connected devices settings. Message of the dialog to hint user to pair right ear of the hearing aid device. Shows when only left side of hearing aid device set is connected. [CHAR LIMIT=NONE] -->
diff --git a/res/xml/bluetooth_device_details_fragment.xml b/res/xml/bluetooth_device_details_fragment.xml
index f330b19..efb2bf7 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"/>
 
+    <com.android.settingslib.widget.ButtonPreference
+        android:key="hearing_aid_pair_other_button"
+        android:gravity="center" />
+    <com.android.settings.applications.SpacePreference
+        android:layout_height="8dp" />
+
     <com.android.settingslib.widget.ActionButtonsPreference
         android:key="action_buttons"
         settings:allowDividerBelow="true"/>
diff --git a/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherController.java b/src/com/android/settings/bluetooth/BluetoothDetailsPairOtherController.java
new file mode 100644
index 0000000..d14a9b1
--- /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 c118a43..999e34d 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java
@@ -251,6 +251,8 @@
                     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 0000000..cfa6d41
--- /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();
+    }
+}