Add 'Live Caption' preference for hearing aids device in Device details page

Bug: 225117933
Test: make RunSettingsRoboTests ROBOTEST_FILTER=BluetoothDetailsRelatedToolsControllerTest
Change-Id: Ic607fa7d10aa2049ab0852a86a57dd1d5d1ac7df
diff --git a/res/xml/bluetooth_device_details_fragment.xml b/res/xml/bluetooth_device_details_fragment.xml
index f6c0af6..57698b5 100644
--- a/res/xml/bluetooth_device_details_fragment.xml
+++ b/res/xml/bluetooth_device_details_fragment.xml
@@ -61,6 +61,18 @@
     <PreferenceCategory
         android:key="bluetooth_profiles"/>
 
+    <PreferenceCategory
+        android:key="bluetooth_related_tools"
+        android:title="@string/bluetooth_screen_related">
+        <Preference
+            android:key="live_caption"
+            android:icon="@drawable/ic_live_caption"
+            android:persistent="false"
+            android:summary="@string/live_caption_summary"
+            android:title="@string/live_caption_title"
+            settings:controller="com.android.settings.accessibility.LiveCaptionPreferenceController"/>
+    </PreferenceCategory>
+
     <com.android.settingslib.widget.FooterPreference
         android:key="device_details_footer"
         android:selectable="false"
diff --git a/src/com/android/settings/bluetooth/BluetoothDetailsRelatedToolsController.java b/src/com/android/settings/bluetooth/BluetoothDetailsRelatedToolsController.java
new file mode 100644
index 0000000..71d6049
--- /dev/null
+++ b/src/com/android/settings/bluetooth/BluetoothDetailsRelatedToolsController.java
@@ -0,0 +1,72 @@
+/*
+ * 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.Preference;
+import androidx.preference.PreferenceCategory;
+import androidx.preference.PreferenceFragmentCompat;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+
+/**
+ * This class adds related tools preference.
+ */
+public class BluetoothDetailsRelatedToolsController extends BluetoothDetailsController{
+    private static final String KEY_RELATED_TOOLS_GROUP = "bluetooth_related_tools";
+    private static final String KEY_LIVE_CAPTION = "live_caption";
+
+    public BluetoothDetailsRelatedToolsController(Context context,
+            PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle) {
+        super(context, fragment, device, lifecycle);
+        lifecycle.addObserver(this);
+    }
+
+    @Override
+    public boolean isAvailable() {
+        return mCachedDevice.isHearingAidDevice();
+    }
+
+    @Override
+    protected void init(PreferenceScreen screen) {
+        if (!mCachedDevice.isHearingAidDevice()) {
+            return;
+        }
+
+        final PreferenceCategory preferenceCategory = screen.findPreference(getPreferenceKey());
+        final Preference liveCaptionPreference = screen.findPreference(KEY_LIVE_CAPTION);
+        if (!liveCaptionPreference.isVisible()) {
+            preferenceCategory.removePreference(liveCaptionPreference);
+        }
+
+        if (preferenceCategory.getPreferenceCount() == 0) {
+            screen.removePreference(preferenceCategory);
+        }
+    }
+
+    @Override
+    protected void refresh() {}
+
+    @Override
+    public String getPreferenceKey() {
+        return KEY_RELATED_TOOLS_GROUP;
+    }
+}
diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java
index 6d443ee..ec31fb3 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java
@@ -194,6 +194,8 @@
                     mCachedDevice, lifecycle));
             controllers.add(new BluetoothDetailsMacAddressController(context, this, mCachedDevice,
                     lifecycle));
+            controllers.add(new BluetoothDetailsRelatedToolsController(context, this, mCachedDevice,
+                    lifecycle));
         }
         return controllers;
     }
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsRelatedToolsControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsRelatedToolsControllerTest.java
new file mode 100644
index 0000000..2035f06
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsRelatedToolsControllerTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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 org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+/** Tests for {@link BluetoothDetailsRelatedToolsController}. */
+@RunWith(RobolectricTestRunner.class)
+public class BluetoothDetailsRelatedToolsControllerTest extends BluetoothDetailsControllerTestBase {
+    private BluetoothDetailsRelatedToolsController mController;
+
+    @Override
+    public void setUp() {
+        super.setUp();
+        mController = new BluetoothDetailsRelatedToolsController(mContext, mFragment, mCachedDevice,
+                mLifecycle);
+    }
+
+    @Test
+    public void isAvailable_isHearingAidDevice_available() {
+        when(mCachedDevice.isHearingAidDevice()).thenReturn(true);
+
+        assertThat(mController.isAvailable()).isTrue();
+    }
+
+    @Test
+    public void isAvailable_notHearingAidDevice_notAvailable() {
+        when(mCachedDevice.isHearingAidDevice()).thenReturn(false);
+
+        assertThat(mController.isAvailable()).isFalse();
+    }
+}