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

Commit 2916a864 authored by Evan Laird's avatar Evan Laird Committed by Android (Google) Code Review
Browse files

Merge changes from topic "battery-incompatible-charging" into main

* changes:
  [battery] don't consider device to be charging if incompatible charging detected
  [battery] don't show estimate if charging
parents c5aa7e50 54663ed2
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
+8 −1
Original line number Diff line number Diff line
@@ -221,7 +221,14 @@ sealed class BatteryViewModel(
        hydrator.hydratedStateOf(
            traceName = "timeRemainingEstimate",
            initialValue = null,
            source = interactor.batteryTimeRemainingEstimate,
            source =
                interactor.isCharging.flatMapLatest { charging ->
                    if (charging) {
                        flowOf(null)
                    } else {
                        interactor.batteryTimeRemainingEstimate
                    }
                },
        )

    override suspend fun onActivated(): Nothing {
+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()
        }
}
Loading