Hearing device summary with battery info

Previously, the device summary of hearing devices will only contain
device side information. If the battery info is avialable, we should
combine it into the summary string.

Bug: 307705966
Test: manually check the UI when hearing aids are connected
Change-Id: I7722b2508f2f3339167804ada2ee5f5cd7c67755
diff --git a/packages/SettingsLib/res/values/strings.xml b/packages/SettingsLib/res/values/strings.xml
index a67839a..73c96d9 100644
--- a/packages/SettingsLib/res/values/strings.xml
+++ b/packages/SettingsLib/res/values/strings.xml
@@ -200,6 +200,10 @@
     <string name="bluetooth_active_battery_level">Active. <xliff:g id="battery_level_as_percentage">%1$s</xliff:g> battery.</string>
     <!-- Connected devices settings. Message when Bluetooth is connected and active, showing remote device status and battery level for untethered headset. [CHAR LIMIT=NONE] -->
     <string name="bluetooth_active_battery_level_untethered">Active. L: <xliff:g id="battery_level_as_percentage" example="25%">%1$s</xliff:g>, R: <xliff:g id="battery_level_as_percentage" example="25%">%2$s</xliff:g> battery.</string>
+    <!-- Connected devices settings. Message when Bluetooth is connected and active, showing remote device status and battery level for the left part of the untethered headset. [CHAR LIMIT=NONE] -->
+    <string name="bluetooth_active_battery_level_untethered_left">Active. L: <xliff:g id="battery_level_as_percentage" example="25%">%1$s</xliff:g> battery</string>
+    <!-- Connected devices settings. Message when Bluetooth is connected and active, showing remote device status and battery level  for the right part of the untethered headset. [CHAR LIMIT=NONE] -->
+    <string name="bluetooth_active_battery_level_untethered_right">Active. R: <xliff:g id="battery_level_as_percentage" example="25%">%1$s</xliff:g> battery</string>
     <!-- Connected devices settings. Message when Bluetooth is connected but not in use, showing remote device battery level. [CHAR LIMIT=NONE] -->
     <string name="bluetooth_battery_level"><xliff:g id="battery_level_as_percentage">%1$s</xliff:g> battery</string>
     <!-- Connected devices settings. Message on TV when Bluetooth is connected but not in use, showing remote device battery level. [CHAR LIMIT=NONE] -->
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index 36a9ecf..a7b7da5 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -1476,30 +1476,13 @@
                     }
                 }
 
-                // Try to show left/right information if can not get it from battery for hearing
+                // Try to show left/right information for hearing
                 // aids specifically.
                 boolean isActiveAshaHearingAid = mIsActiveDeviceHearingAid;
                 boolean isActiveLeAudioHearingAid = mIsActiveDeviceLeAudio
                         && isConnectedHapClientDevice();
                 if (isActiveAshaHearingAid || isActiveLeAudioHearingAid) {
-                    final Set<CachedBluetoothDevice> memberDevices = getMemberDevice();
-                    final CachedBluetoothDevice subDevice = getSubDevice();
-                    if (memberDevices.stream().anyMatch(m -> m.isConnected())) {
-                        stringRes = R.string.bluetooth_hearing_aid_left_and_right_active;
-                    } else if (subDevice != null && subDevice.isConnected()) {
-                        stringRes = R.string.bluetooth_hearing_aid_left_and_right_active;
-                    } else {
-                        int deviceSide = getDeviceSide();
-                        if (deviceSide == HearingAidInfo.DeviceSide.SIDE_LEFT_AND_RIGHT) {
-                            stringRes = R.string.bluetooth_hearing_aid_left_and_right_active;
-                        } else if (deviceSide == HearingAidInfo.DeviceSide.SIDE_LEFT) {
-                            stringRes = R.string.bluetooth_hearing_aid_left_active;
-                        } else if (deviceSide == HearingAidInfo.DeviceSide.SIDE_RIGHT) {
-                            stringRes = R.string.bluetooth_hearing_aid_right_active;
-                        } else {
-                            stringRes = R.string.bluetooth_active_no_battery_level;
-                        }
-                    }
+                    return getHearingDeviceSummary(leftBattery, rightBattery, shortSummary);
                 }
             }
         }
@@ -1567,6 +1550,62 @@
         return spannableBuilder;
     }
 
+    private CharSequence getHearingDeviceSummary(int leftBattery, int rightBattery,
+            boolean shortSummary) {
+
+        CachedBluetoothDevice memberDevice = getMemberDevice().stream().filter(
+                CachedBluetoothDevice::isConnected).findFirst().orElse(null);
+        if (memberDevice == null && mSubDevice != null && mSubDevice.isConnected()) {
+            memberDevice = mSubDevice;
+        }
+
+        CachedBluetoothDevice leftDevice = null;
+        CachedBluetoothDevice rightDevice = null;
+        final int deviceSide = getDeviceSide();
+        if (deviceSide == HearingAidInfo.DeviceSide.SIDE_LEFT) {
+            leftDevice = this;
+            rightDevice = memberDevice;
+        } else if (deviceSide == HearingAidInfo.DeviceSide.SIDE_RIGHT) {
+            leftDevice = memberDevice;
+            rightDevice = this;
+        } else if (deviceSide == HearingAidInfo.DeviceSide.SIDE_LEFT_AND_RIGHT) {
+            leftDevice = this;
+            rightDevice = this;
+        }
+
+        if (leftBattery < 0 && leftDevice != null) {
+            leftBattery = leftDevice.getBatteryLevel();
+        }
+        if (rightBattery < 0 && rightDevice != null) {
+            rightBattery = rightDevice.getBatteryLevel();
+        }
+
+        if (leftDevice != null && rightDevice != null) {
+            if (leftBattery >= 0 && rightBattery >= 0 && !shortSummary) {
+                return mContext.getString(R.string.bluetooth_active_battery_level_untethered,
+                        Utils.formatPercentage(leftBattery), Utils.formatPercentage(rightBattery));
+            } else {
+                return mContext.getString(R.string.bluetooth_hearing_aid_left_and_right_active);
+            }
+        } else if (leftDevice != null) {
+            if (leftBattery >= 0 && !shortSummary) {
+                return mContext.getString(R.string.bluetooth_active_battery_level_untethered_left,
+                        Utils.formatPercentage(leftBattery));
+            } else {
+                return mContext.getString(R.string.bluetooth_hearing_aid_left_active);
+            }
+        } else if (rightDevice != null) {
+            if (rightBattery >= 0 && !shortSummary) {
+                return mContext.getString(R.string.bluetooth_active_battery_level_untethered_right,
+                        Utils.formatPercentage(rightBattery));
+            } else {
+                return mContext.getString(R.string.bluetooth_hearing_aid_right_active);
+            }
+        }
+
+        return mContext.getString(R.string.bluetooth_active_no_battery_level);
+    }
+
     private void addBatterySpan(SpannableStringBuilder builder,
             String batteryString, boolean lowBattery, int lowBatteryColorRes) {
         if (lowBattery && lowBatteryColorRes != SUMMARY_NO_COLOR_FOR_LOW_BATTERY) {