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

Commit c7fe79b7 authored by chelseahao's avatar chelseahao Committed by Chelsea Hao
Browse files

Display the lowest battery level of the main device and it's members.

Note: this function is public for BluetoothTile.java.

Test: atest CachedBluetoothDeviceTest && atest com.android.systemui.qs
Bug: 293390436
Change-Id: Ie17e15a4351b3565e0e3930dba8b6544debf2785
parent 90d61f14
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;

/**
 * CachedBluetoothDevice represents a remote Bluetooth device. It contains
@@ -652,6 +653,20 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        return mDevice.getBatteryLevel();
    }

    /**
     * Get the lowest battery level from remote device and its member devices
     * @return battery level in percentage [0-100] or
     * {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN}
     */
    public int getMinBatteryLevelWithMemberDevices() {
        return Stream.concat(Stream.of(this), mMemberDevices.stream())
                .mapToInt(cachedDevice -> cachedDevice.getBatteryLevel())
                .filter(batteryLevel -> batteryLevel > BluetoothDevice.BATTERY_LEVEL_UNKNOWN)
                .min()
                .orElse(BluetoothDevice.BATTERY_LEVEL_UNKNOWN);
    }


    void refresh() {
        ThreadUtils.postOnBackgroundThread(() -> {
            if (BluetoothUtils.isAdvancedDetailsHeader(mDevice)) {
@@ -1147,7 +1162,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        // BluetoothDevice.BATTERY_LEVEL_BLUETOOTH_OFF, or BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
        // any other value should be a framework bug. Thus assume here that if value is greater
        // than BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must be valid
        final int batteryLevel = getBatteryLevel();
        final int batteryLevel = getMinBatteryLevelWithMemberDevices();
        if (batteryLevel > BluetoothDevice.BATTERY_LEVEL_UNKNOWN) {
            // TODO: name com.android.settingslib.bluetooth.Utils something different
            batteryLevelPercentageString =
@@ -1322,7 +1337,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        // BluetoothDevice.BATTERY_LEVEL_BLUETOOTH_OFF, or BluetoothDevice.BATTERY_LEVEL_UNKNOWN,
        // any other value should be a framework bug. Thus assume here that if value is greater
        // than BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must be valid
        final int batteryLevel = getBatteryLevel();
        final int batteryLevel = getMinBatteryLevelWithMemberDevices();
        if (batteryLevel > BluetoothDevice.BATTERY_LEVEL_UNKNOWN) {
            // TODO: name com.android.settingslib.bluetooth.Utils something different
            batteryLevelPercentageString =
+45 −0
Original line number Diff line number Diff line
@@ -528,6 +528,51 @@ public class CachedBluetoothDeviceTest {
        assertThat(mCachedDevice.getConnectionSummary()).isNull();
    }

    @Test
    public void getConnectionSummary_testMemberDevicesExist_returnMinBattery() {
        // One device is active with battery level 70.
        mBatteryLevel = 70;
        updateProfileStatus(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);


        // Add a member device with battery level 30.
        int lowerBatteryLevel = 30;
        mCachedDevice.addMemberDevice(mSubCachedDevice);
        doAnswer((invocation) -> lowerBatteryLevel).when(mSubCachedDevice).getBatteryLevel();

        assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active, 30% battery");
    }

    @Test
    public void getConnectionSummary_testMemberDevicesBatteryUnknown_returnMinBattery() {
        // One device is active with battery level 70.
        mBatteryLevel = 70;
        updateProfileStatus(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);

        // Add a member device with battery level unknown.
        mCachedDevice.addMemberDevice(mSubCachedDevice);
        doAnswer((invocation) -> BluetoothDevice.BATTERY_LEVEL_UNKNOWN).when(
                mSubCachedDevice).getBatteryLevel();

        assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active, 70% battery");
    }

    @Test
    public void getConnectionSummary_testAllDevicesBatteryUnknown_returnNoBattery() {
        // One device is active with battery level unknown.
        updateProfileStatus(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);

        // Add a member device with battery level unknown.
        mCachedDevice.addMemberDevice(mSubCachedDevice);
        doAnswer((invocation) -> BluetoothDevice.BATTERY_LEVEL_UNKNOWN).when(
                mSubCachedDevice).getBatteryLevel();

        assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active");
    }

    @Test
    public void getConnectionSummary_testMultipleProfilesActiveDevice() {
        // Test without battery level
+1 −1
Original line number Diff line number Diff line
@@ -226,7 +226,7 @@ public class BluetoothTile extends QSTileImpl<BooleanState> {
                listenToMetadata(device);
            } else {
                stopListeningToStaleDeviceMetadata();
                batteryLevel = device.getBatteryLevel();
                batteryLevel = device.getMinBatteryLevelWithMemberDevices();
            }

            if (batteryLevel > BluetoothDevice.BATTERY_LEVEL_UNKNOWN) {
+1 −1
Original line number Diff line number Diff line
@@ -168,7 +168,7 @@ class BluetoothTileTest : SysuiTestCase() {
        val btDevice = mock<BluetoothDevice>()
        whenever(cachedDevice2.device).thenReturn(btDevice)
        whenever(btDevice.getMetadata(BluetoothDevice.METADATA_MAIN_BATTERY)).thenReturn(null)
        whenever(cachedDevice2.batteryLevel).thenReturn(25)
        whenever(cachedDevice2.minBatteryLevelWithMemberDevices).thenReturn(25)
        addConnectedDevice(cachedDevice2)

        tile.handleUpdateState(state, /* arg= */ null)