Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ade3b59b authored by Ze Li's avatar Ze Li Committed by Android (Google) Code Review
Browse files

Merge "[Battery refactor] Function to get battery information from bluetooth...

Merge "[Battery refactor] Function to get battery information from bluetooth serve for le audio components." into main
parents be3f79a8 68c384d5
Loading
Loading
Loading
Loading
+49 −2
Original line number Diff line number Diff line
@@ -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;

@@ -1778,9 +1779,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.
        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
+21 −0
Original line number Diff line number Diff line
@@ -45,6 +45,27 @@ import java.util.concurrent.ConcurrentHashMap;
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;

+26 −0
Original line number Diff line number Diff line
@@ -2357,6 +2357,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);