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

Commit 54663ed2 authored by Evan Laird's avatar Evan Laird
Browse files

[battery] don't consider device to be charging if incompatible charging detected

Test: BatteryRepositoryTest
Test: BatteryInteractorTest
Test: BatteryViewModelBasedOnSettingTest
Fixes: 416698601
Flag: com.android.settingslib.flags.new_status_bar_icons
Flag: com.android.systemui.status_bar_root_modernization
Change-Id: I0f2ffe3bd75ad3548af74de9367d35988498c69c
parent d4b101a5
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -54,6 +54,9 @@ interface BatteryRepository {
    /** Battery defender means the device is plugged in but not charging to protect the battery */
    val isBatteryDefenderEnabled: Flow<Boolean>

    /** True if the system has detected an incompatible charger (and thus is not charging) */
    val isIncompatibleCharging: Flow<Boolean>

    /** The current level [0-100] */
    val level: Flow<Int?>

@@ -110,6 +113,14 @@ constructor(
                            }
                        }

                        override fun onIsIncompatibleChargingChanged(
                            isIncompatibleCharging: Boolean
                        ) {
                            trySend { prev ->
                                prev.copy(isIncompatibleCharging = isIncompatibleCharging)
                            }
                        }

                        override fun onBatteryUnknownStateChanged(isUnknown: Boolean) {
                            // If the state is unknown, then all other fields are invalid
                            trySend { prev ->
@@ -136,6 +147,8 @@ constructor(

    override val isBatteryDefenderEnabled = batteryState.map { it.isBatteryDefenderEnabled }

    override val isIncompatibleCharging = batteryState.map { it.isIncompatibleCharging }

    override val level = batteryState.map { it.level }

    override val isStateUnknown = batteryState.map { it.isStateUnknown }
@@ -177,4 +190,5 @@ private data class BatteryCallbackState(
    val isPowerSaveEnabled: Boolean = false,
    val isBatteryDefenderEnabled: Boolean = false,
    val isStateUnknown: Boolean = false,
    val isIncompatibleCharging: Boolean = false,
)
+6 −1
Original line number Diff line number Diff line
@@ -35,8 +35,13 @@ class BatteryInteractor @Inject constructor(repo: BatteryRepository) {
     * For the sake of battery views, consider it to be "charging" if plugged in. This allows users
     * to easily confirm that the device is properly plugged in, even if its' technically not
     * charging due to issues with the source.
     *
     * If an incompatible charger is detected, we don't consider the battery to be charging.
     */
    val isCharging = repo.isPluggedIn
    val isCharging =
        combine(repo.isPluggedIn, repo.isIncompatibleCharging) { pluggedIn, incompatible ->
            !incompatible && pluggedIn
        }

    /**
     * The critical level (see [CRITICAL_LEVEL]) defines the level below which we might want to
+14 −0
Original line number Diff line number Diff line
@@ -155,4 +155,18 @@ class BatteryRepositoryTest : SysuiTestCase() {

            assertThat(latest).isEqualTo("test time remaining")
        }

    @Test
    fun incompatibleCharging() =
        kosmos.runTest {
            batteryController.fake._isIncompatibleCharging = true

            val latest by collectLastValue(underTest.isIncompatibleCharging)

            assertThat(latest).isTrue()

            batteryController.fake._isIncompatibleCharging = false

            assertThat(latest).isFalse()
        }
}
+11 −0
Original line number Diff line number Diff line
@@ -109,4 +109,15 @@ class BatteryInteractorTest : SysuiTestCase() {

            assertThat(latest).isEqualTo(BatteryAttributionModel.Charging)
        }

    @Test
    fun incompatibleCharging_notConsideredCharging() =
        kosmos.runTest {
            val latest by collectLastValue(underTest.isCharging)

            batteryController.fake._isPluggedIn = true
            batteryController.fake._isIncompatibleCharging = true

            assertThat(latest).isFalse()
        }
}
+10 −0
Original line number Diff line number Diff line
@@ -219,4 +219,14 @@ class BatteryViewModelBasedOnSettingTest : SysuiTestCase() {

            assertThat(underTest.batteryTimeRemainingEstimate).isEqualTo(timeRemaining)
        }

    @Test
    fun incompatibleCharging_pluggedIn_notShowingChargingState() =
        kosmos.runTest {
            batteryController.fake._isPluggedIn = true
            batteryController.fake._level = 39
            batteryController.fake._isIncompatibleCharging = true

            assertThat(underTest.attribution).isNull()
        }
}
Loading