summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Ze Li <lze@google.com> 2025-03-17 20:56:05 +0800
committer Ze Li <lze@google.com> 2025-03-17 20:56:48 +0800
commit68c384d54d985ea73319331f14e7eb8838bababd (patch)
tree00deb64262e3a7014f4119a299ab0101517f59ae
parent68e111b1585d0511e1b976901ae3077d23d5b871 (diff)
[Battery refactor] Function to get battery information from bluetooth serve for le audio components.
Test: com.android.settingslib.bluetooth.CachedBluetoothDeviceTest Bug: 397847825 Flag: EXEMPT utils function Change-Id: I9da7890cf40e960e32c0c271b4a33d70ed0d22ce
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java51
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java21
-rw-r--r--packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java26
3 files changed, 96 insertions, 2 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index 6b3814fbf9d6..aafc6837230f 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -75,6 +75,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
+import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@@ -1780,9 +1781,55 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
@Nullable
private BatteryLevelsInfo getBatteryOfLeAudioDeviceComponents() {
- // TODO(b/397847825): Implement the logic to get battery of LE audio device components.
- return null;
+ LeAudioProfile leAudio = mProfileManager.getLeAudioProfile();
+ if (leAudio == null) {
+ return null;
+ }
+ int leftBattery = BluetoothDevice.BATTERY_LEVEL_UNKNOWN;
+ int rightBattery = BluetoothDevice.BATTERY_LEVEL_UNKNOWN;
+ int overallBattery = BluetoothDevice.BATTERY_LEVEL_UNKNOWN;
+
+ Set<BluetoothDevice> allDevices =
+ Stream.concat(
+ mMemberDevices.stream().map(CachedBluetoothDevice::getDevice),
+ Stream.of(mDevice))
+ .collect(Collectors.toSet());
+ for (BluetoothDevice device : allDevices) {
+ int battery = device.getBatteryLevel();
+ if (battery <= BluetoothDevice.BATTERY_LEVEL_UNKNOWN) {
+ continue;
+ }
+ int deviceId = leAudio.getAudioLocation(device);
+ boolean isLeft = (deviceId & LeAudioProfile.LEFT_DEVICE_ID) != 0;
+ boolean isRight = (deviceId & LeAudioProfile.RIGHT_DEVICE_ID) != 0;
+ boolean isLeftRight = isLeft && isRight;
+ // We should expect only one device assign to one side, but if it happens,
+ // we don't care which one.
+ if (isLeftRight) {
+ overallBattery = battery;
+ } else if (isLeft) {
+ leftBattery = battery;
+ } else if (isRight) {
+ rightBattery = battery;
+ }
+ }
+ overallBattery = getMinBatteryLevels(
+ Arrays.stream(new int[]{leftBattery, rightBattery, overallBattery}));
+
+ Log.d(TAG, "Acquired battery info from Bluetooth service for le audio device "
+ + mDevice.getAnonymizedAddress()
+ + " left battery: " + leftBattery
+ + " right battery: " + rightBattery
+ + " overall battery: " + overallBattery);
+ return overallBattery > BluetoothDevice.BATTERY_LEVEL_UNKNOWN
+ ? new BatteryLevelsInfo(
+ leftBattery,
+ rightBattery,
+ BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
+ overallBattery)
+ : null;
}
+
private CharSequence getTvBatterySummary(int mainBattery, int leftBattery, int rightBattery,
int lowBatteryColorRes) {
// Since there doesn't seem to be a way to use format strings to add the
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java
index 155c7e6530aa..7b516f9de08e 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java
@@ -43,6 +43,27 @@ import java.util.List;
import java.util.concurrent.Executor;
public class LeAudioProfile implements LocalBluetoothProfile {
+ public static final int LEFT_DEVICE_ID = BluetoothLeAudio.AUDIO_LOCATION_FRONT_LEFT
+ | BluetoothLeAudio.AUDIO_LOCATION_BACK_LEFT
+ | BluetoothLeAudio.AUDIO_LOCATION_FRONT_LEFT_OF_CENTER
+ | BluetoothLeAudio.AUDIO_LOCATION_SIDE_LEFT
+ | BluetoothLeAudio.AUDIO_LOCATION_TOP_FRONT_LEFT
+ | BluetoothLeAudio.AUDIO_LOCATION_TOP_BACK_LEFT
+ | BluetoothLeAudio.AUDIO_LOCATION_TOP_SIDE_LEFT
+ | BluetoothLeAudio.AUDIO_LOCATION_BOTTOM_FRONT_LEFT
+ | BluetoothLeAudio.AUDIO_LOCATION_FRONT_LEFT_WIDE
+ | BluetoothLeAudio.AUDIO_LOCATION_LEFT_SURROUND;
+ public static final int RIGHT_DEVICE_ID = BluetoothLeAudio.AUDIO_LOCATION_FRONT_RIGHT
+ | BluetoothLeAudio.AUDIO_LOCATION_BACK_RIGHT
+ | BluetoothLeAudio.AUDIO_LOCATION_FRONT_RIGHT_OF_CENTER
+ | BluetoothLeAudio.AUDIO_LOCATION_SIDE_RIGHT
+ | BluetoothLeAudio.AUDIO_LOCATION_TOP_FRONT_RIGHT
+ | BluetoothLeAudio.AUDIO_LOCATION_TOP_BACK_RIGHT
+ | BluetoothLeAudio.AUDIO_LOCATION_TOP_SIDE_RIGHT
+ | BluetoothLeAudio.AUDIO_LOCATION_BOTTOM_FRONT_RIGHT
+ | BluetoothLeAudio.AUDIO_LOCATION_FRONT_RIGHT_WIDE
+ | BluetoothLeAudio.AUDIO_LOCATION_RIGHT_SURROUND;
+
private static final String TAG = "LeAudioProfile";
private static boolean DEBUG = true;
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java
index abe40c4f4b95..ff53682013c7 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java
@@ -2290,6 +2290,32 @@ public class CachedBluetoothDeviceTest {
Integer.parseInt(TWS_BATTERY_LEFT));
}
+ @Test
+ public void getBatteryLevelsInfo_leAudioDeviceWithBattery_returnBatteryLevelsInfo() {
+ when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn(
+ "false".getBytes());
+ when(mProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile);
+ updateProfileStatus(mLeAudioProfile, BluetoothProfile.STATE_CONNECTED);
+ mCachedDevice.addMemberDevice(mSubCachedDevice);
+ when(mLeAudioProfile.getAudioLocation(mSubDevice)).thenReturn(
+ BluetoothLeAudio.AUDIO_LOCATION_BACK_LEFT);
+ when(mSubDevice.getBatteryLevel()).thenReturn(Integer.parseInt(TWS_BATTERY_LEFT));
+ when(mLeAudioProfile.getAudioLocation(mDevice)).thenReturn(
+ BluetoothLeAudio.AUDIO_LOCATION_SIDE_RIGHT);
+ when(mDevice.getBatteryLevel()).thenReturn(Integer.parseInt(TWS_BATTERY_RIGHT));
+
+ BatteryLevelsInfo batteryLevelsInfo = mCachedDevice.getBatteryLevelsInfo();
+
+ assertThat(batteryLevelsInfo.getLeftBatteryLevel()).isEqualTo(
+ Integer.parseInt(TWS_BATTERY_LEFT));
+ assertThat(batteryLevelsInfo.getRightBatteryLevel()).isEqualTo(
+ Integer.parseInt(TWS_BATTERY_RIGHT));
+ assertThat(batteryLevelsInfo.getCaseBatteryLevel()).isEqualTo(
+ BluetoothDevice.BATTERY_LEVEL_UNKNOWN);
+ assertThat(batteryLevelsInfo.getOverallBatteryLevel()).isEqualTo(
+ Integer.parseInt(TWS_BATTERY_LEFT));
+ }
+
private void updateProfileStatus(LocalBluetoothProfile profile, int status) {
doReturn(status).when(profile).getConnectionStatus(mDevice);
mCachedDevice.onProfileStateChanged(profile, status);