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

Commit eb1109d0 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov Committed by Android (Google) Code Review
Browse files

Merge "[SB][Battery] Make an interface for BatteryRepository." into main

parents bcfa0d64 009311ad
Loading
Loading
Loading
Loading
+44 −27
Original line number Diff line number Diff line
@@ -41,12 +41,45 @@ import kotlinx.coroutines.flow.scan
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.suspendCancellableCoroutine

/** Repository-style state for battery information. */
interface BatteryRepository {
    /**
 * Repository-style state for battery information. Currently we just use the [BatteryController] as
 * our source of truth, but we could (should?) migrate away from that eventually.
     * True if the phone is plugged in. Note that this does not always mean the device is charging
     */
    val isPluggedIn: Flow<Boolean>

    /** Is power saver enabled */
    val isPowerSaveEnabled: Flow<Boolean>

    /** Battery defender means the device is plugged in but not charging to protect the battery */
    val isBatteryDefenderEnabled: Flow<Boolean>

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

    /** State unknown means that we can't detect a battery */
    val isStateUnknown: Flow<Boolean>

    /**
     * [Settings.System.SHOW_BATTERY_PERCENT]. A user setting to indicate whether we should show the
     * battery percentage in the home screen status bar
     */
    val isShowBatteryPercentSettingEnabled: StateFlow<Boolean>

    /**
     * If available, this flow yields a string that describes the approximate time remaining for the
     * current battery charge and usage information. While subscribed, the estimate is updated every
     * 2 minutes.
     */
    val batteryTimeRemainingEstimate: Flow<String?>
}

/**
 * Currently we just use the [BatteryController] as our source of truth, but we could (should?)
 * migrate away from that eventually.
 */
@SysUISingleton
class BatteryRepository
class BatteryRepositoryImpl
@Inject
constructor(
    @Application context: Context,
@@ -54,7 +87,7 @@ constructor(
    @Background bgDispatcher: CoroutineDispatcher,
    private val controller: BatteryController,
    settingsRepository: SystemSettingsRepository,
) {
) : BatteryRepository {
    private val batteryState: StateFlow<BatteryCallbackState> =
        conflatedCallbackFlow<(BatteryCallbackState) -> BatteryCallbackState> {
                val callback =
@@ -97,28 +130,17 @@ constructor(
            .flowOn(bgDispatcher)
            .stateIn(scope, SharingStarted.Lazily, BatteryCallbackState())

    /**
     * True if the phone is plugged in. Note that this does not always mean the device is charging
     */
    val isPluggedIn = batteryState.map { it.isPluggedIn }
    override val isPluggedIn = batteryState.map { it.isPluggedIn }

    /** Is power saver enabled */
    val isPowerSaveEnabled = batteryState.map { it.isPowerSaveEnabled }
    override val isPowerSaveEnabled = batteryState.map { it.isPowerSaveEnabled }

    /** Battery defender means the device is plugged in but not charging to protect the battery */
    val isBatteryDefenderEnabled = batteryState.map { it.isBatteryDefenderEnabled }
    override val isBatteryDefenderEnabled = batteryState.map { it.isBatteryDefenderEnabled }

    /** The current level [0-100] */
    val level = batteryState.map { it.level }
    override val level = batteryState.map { it.level }

    /** State unknown means that we can't detect a battery */
    val isStateUnknown = batteryState.map { it.isStateUnknown }
    override val isStateUnknown = batteryState.map { it.isStateUnknown }

    /**
     * [Settings.System.SHOW_BATTERY_PERCENT]. A user setting to indicate whether we should show the
     * battery percentage in the home screen status bar
     */
    val isShowBatteryPercentSettingEnabled = run {
    override val isShowBatteryPercentSettingEnabled = run {
        val default =
            context.resources.getBoolean(
                com.android.internal.R.bool.config_defaultBatteryPercentageSetting
@@ -138,12 +160,7 @@ constructor(
        }
    }

    /**
     * If available, this flow yields a string that describes the approximate time remaining for the
     * current battery charge and usage information. While subscribed, the estimate is updated every
     * 2 minutes.
     */
    val batteryTimeRemainingEstimate: Flow<String?> = estimate.flowOn(bgDispatcher)
    override val batteryTimeRemainingEstimate: Flow<String?> = estimate.flowOn(bgDispatcher)

    private suspend fun fetchEstimate() = suspendCancellableCoroutine { continuation ->
        val callback =
+1 −1
Original line number Diff line number Diff line
@@ -265,7 +265,7 @@ constructor(interactor: BatteryInteractor, @Application context: Context) :
        )

    @AssistedFactory
    interface Factory {
    fun interface Factory {
        fun create(): UnifiedBatteryViewModel
    }

+4 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ import com.android.systemui.statusbar.pipeline.airplane.data.repository.Airplane
import com.android.systemui.statusbar.pipeline.airplane.data.repository.AirplaneModeRepositoryImpl
import com.android.systemui.statusbar.pipeline.airplane.ui.viewmodel.AirplaneModeViewModel
import com.android.systemui.statusbar.pipeline.airplane.ui.viewmodel.AirplaneModeViewModelImpl
import com.android.systemui.statusbar.pipeline.battery.data.repository.BatteryRepository
import com.android.systemui.statusbar.pipeline.battery.data.repository.BatteryRepositoryImpl
import com.android.systemui.statusbar.pipeline.icons.shared.BindableIconsRegistry
import com.android.systemui.statusbar.pipeline.icons.shared.BindableIconsRegistryImpl
import com.android.systemui.statusbar.pipeline.mobile.data.repository.CarrierConfigCoreStartable
@@ -118,6 +120,8 @@ abstract class StatusBarPipelineModule {
    @Binds
    abstract fun connectivityRepository(impl: ConnectivityRepositoryImpl): ConnectivityRepository

    @Binds abstract fun batteryRepository(impl: BatteryRepositoryImpl): BatteryRepository

    @Binds
    abstract fun systemStatusEventAnimationRepository(
        impl: SystemStatusEventAnimationRepositoryImpl
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ class BatteryRepositoryTest : SysuiTestCase() {

    val Kosmos.underTest by
        Kosmos.Fixture {
            BatteryRepository(
            BatteryRepositoryImpl(
                testableContext,
                backgroundScope,
                testDispatcher,
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ import com.android.systemui.statusbar.policy.batteryController
/** Use [Kosmos.batteryController.fake] to make the repo have the state you want */
val Kosmos.batteryRepository by
    Kosmos.Fixture {
        BatteryRepository(
        BatteryRepositoryImpl(
            testableContext,
            testScope.backgroundScope,
            testDispatcher,