Merge "Fix failed test case for BatteryTip in Settings" into main
diff --git a/src/com/android/settings/bluetooth/BluetoothDetailsHearingDeviceControlsController.java b/src/com/android/settings/bluetooth/BluetoothDetailsHearingDeviceControlsController.java
index 18ad210..3d85ca2 100644
--- a/src/com/android/settings/bluetooth/BluetoothDetailsHearingDeviceControlsController.java
+++ b/src/com/android/settings/bluetooth/BluetoothDetailsHearingDeviceControlsController.java
@@ -38,7 +38,8 @@
 public class BluetoothDetailsHearingDeviceControlsController extends BluetoothDetailsController
         implements Preference.OnPreferenceClickListener {
 
-    private static final String KEY_DEVICE_CONTROLS_GENERAL_GROUP = "device_controls_general";
+    @VisibleForTesting
+    static final String KEY_DEVICE_CONTROLS_GENERAL_GROUP = "device_controls_general";
     @VisibleForTesting
     static final String KEY_HEARING_DEVICE_CONTROLS = "hearing_device_controls";
 
diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java
index 5e41a20..9c68c9c 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java
@@ -22,6 +22,7 @@
 import android.app.settings.SettingsEnums;
 import android.bluetooth.BluetoothDevice;
 import android.content.Context;
+import android.content.Intent;
 import android.content.res.TypedArray;
 import android.hardware.input.InputManager;
 import android.net.Uri;
@@ -53,6 +54,7 @@
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
 import com.android.settingslib.core.lifecycle.Lifecycle;
 
 import java.util.ArrayList;
@@ -324,8 +326,11 @@
                     lifecycle));
             controllers.add(new BluetoothDetailsPairOtherController(context, this, mCachedDevice,
                     lifecycle));
-            controllers.add(new BluetoothDetailsHearingDeviceControlsController(context, this,
-                    mCachedDevice, lifecycle));
+            // Don't need to show hearing device again when launched from the same page.
+            if (!isLaunchFromHearingDevicePage()) {
+                controllers.add(new BluetoothDetailsHearingDeviceControlsController(context, this,
+                        mCachedDevice, lifecycle));
+            }
             controllers.add(new BluetoothDetailsDataSyncController(context, this,
                     mCachedDevice, lifecycle));
             controllers.add(
@@ -348,6 +353,16 @@
         return width;
     }
 
+    private boolean isLaunchFromHearingDevicePage() {
+        final Intent intent = getIntent();
+        if (intent == null) {
+            return false;
+        }
+
+        return intent.getIntExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY,
+                SettingsEnums.PAGE_UNKNOWN) == SettingsEnums.ACCESSIBILITY_HEARING_AID_SETTINGS;
+    }
+
     @VisibleForTesting
     void setTitleForInputDevice() {
         if (StylusDevicesController.isDeviceStylus(mInputDevice, mCachedDevice)) {
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragmentTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragmentTest.java
index cfd256f..fc72c41 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragmentTest.java
@@ -18,6 +18,8 @@
 
 import static android.bluetooth.BluetoothDevice.BOND_NONE;
 
+import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceControlsController.KEY_DEVICE_CONTROLS_GENERAL_GROUP;
+
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.mockito.ArgumentMatchers.any;
@@ -29,8 +31,10 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import android.app.settings.SettingsEnums;
 import android.companion.CompanionDeviceManager;
 import android.content.Context;
+import android.content.Intent;
 import android.hardware.input.InputManager;
 import android.os.Bundle;
 import android.os.UserManager;
@@ -49,6 +53,8 @@
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
 
 import com.google.common.collect.ImmutableList;
 
@@ -65,6 +71,8 @@
 import org.robolectric.annotation.Config;
 import org.robolectric.fakes.RoboMenu;
 
+import java.util.List;
+
 @RunWith(RobolectricTestRunner.class)
 @Config(shadows = {
         com.android.settings.testutils.shadow.ShadowUserManager.class,
@@ -216,6 +224,38 @@
         verify(mFragment).finish();
     }
 
+    @Test
+    public void createPreferenceControllers_launchFromHAPage_deviceControllerNotExist() {
+        BluetoothDeviceDetailsFragment fragment = setupFragment();
+        Intent intent = fragment.getActivity().getIntent();
+        intent.putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY,
+                SettingsEnums.ACCESSIBILITY_HEARING_AID_SETTINGS);
+        fragment.onAttach(mContext);
+
+        List<AbstractPreferenceController> controllerList = fragment.createPreferenceControllers(
+                mContext);
+
+        assertThat(controllerList.stream()
+                .anyMatch(controller -> controller.getPreferenceKey().equals(
+                        KEY_DEVICE_CONTROLS_GENERAL_GROUP))).isFalse();
+    }
+
+    @Test
+    public void createPreferenceControllers_notLaunchFromHAPage_deviceControllerExist() {
+        BluetoothDeviceDetailsFragment fragment = setupFragment();
+        Intent intent = fragment.getActivity().getIntent();
+        intent.putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY,
+                SettingsEnums.PAGE_UNKNOWN);
+        fragment.onAttach(mContext);
+
+        List<AbstractPreferenceController> controllerList = fragment.createPreferenceControllers(
+                mContext);
+
+        assertThat(controllerList.stream()
+                .anyMatch(controller -> controller.getPreferenceKey().equals(
+                        KEY_DEVICE_CONTROLS_GENERAL_GROUP))).isTrue();
+    }
+
     private InputDevice createInputDeviceWithMatchingBluetoothAddress() {
         doReturn(new int[]{0}).when(mInputManager).getInputDeviceIds();
         InputDevice device = mock(InputDevice.class);