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

Commit 0d458e28 authored by LuK1337's avatar LuK1337 Committed by Łukasz Patron
Browse files

SystemUI: Bring back good ol' circle battery style once again

Change-Id: I0357c8d71f15f71d86bf0e4fab8b7f4487e6efb3
parent ce05f32c
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -74,7 +74,11 @@ class BGImageView(context: Context) : ImageView(context), BackgroundAnimatableVi
    }
}

class BatteryEvent(@IntRange(from = 0, to = 100) val batteryLevel: Int) : StatusEvent {
class BatteryEvent(
    @IntRange(from = 0, to = 100) val batteryLevel: Int,
    val batteryIconStyle: Int,
    val showPercentNextToIcon: Boolean,
) : StatusEvent {
    override val priority = 50
    override var forceVisible = false
    override val showAnimation = true
@@ -83,7 +87,12 @@ class BatteryEvent(@IntRange(from = 0, to = 100) val batteryLevel: Int) : Status

    override val viewCreator: ViewCreator = { context ->
        if (NewStatusBarIcons.isEnabled) {
            BatteryStatusEventComposeChip(batteryLevel, context)
            BatteryStatusEventComposeChip(
                batteryLevel,
                batteryIconStyle,
                showPercentNextToIcon,
                context,
            )
        } else {
            BatteryStatusChip(context).apply { setBatteryLevel(batteryLevel) }
        }
+9 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.android.systemui.privacy.PrivacyItemController
import com.android.systemui.privacy.PrivacyType
import com.android.systemui.res.R
import com.android.systemui.statusbar.featurepods.vc.domain.interactor.AvControlsChipInteractor
import com.android.systemui.statusbar.pipeline.battery.domain.interactor.BatteryInteractor
import com.android.systemui.statusbar.policy.BatteryController
import com.android.systemui.util.time.SystemClock
import javax.inject.Inject
@@ -51,6 +52,7 @@ class SystemEventCoordinator
constructor(
    private val systemClock: SystemClock,
    private val batteryController: BatteryController,
    private val batteryInteractor: BatteryInteractor,
    private val privacyController: PrivacyItemController,
    private val avControlsChipInteractor: AvControlsChipInteractor,
    private val context: Context,
@@ -82,7 +84,13 @@ constructor(
    }

    fun notifyPluggedIn(@IntRange(from = 0, to = 100) batteryLevel: Int) {
        scheduler.onStatusEvent(BatteryEvent(batteryLevel))
        scheduler.onStatusEvent(
            BatteryEvent(
                batteryLevel,
                batteryInteractor.batteryIconStyle.value,
                batteryInteractor.showPercentNextToIcon.value,
            )
        )
    }

    fun notifyPrivacyItemsEmpty() {
+42 −27
Original line number Diff line number Diff line
@@ -22,10 +22,9 @@ import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.MaterialTheme
@@ -39,8 +38,8 @@ import androidx.compose.ui.unit.dp
import com.android.compose.theme.PlatformTheme
import com.android.systemui.res.R
import com.android.systemui.statusbar.core.NewStatusBarIcons
import com.android.systemui.statusbar.core.RudimentaryBattery
import com.android.systemui.statusbar.events.BackgroundAnimatableView
import com.android.systemui.statusbar.pipeline.battery.data.repository.BatteryRepository
import com.android.systemui.statusbar.pipeline.battery.domain.interactor.BatteryInteractor
import com.android.systemui.statusbar.pipeline.battery.shared.ui.BatteryColors
import com.android.systemui.statusbar.pipeline.battery.shared.ui.BatteryGlyph
@@ -58,8 +57,13 @@ import java.text.NumberFormat
@SuppressLint("ViewConstructor")
class BatteryStatusEventComposeChip
@JvmOverloads
constructor(level: Int, context: Context, attrs: AttributeSet? = null) :
    FrameLayout(context, attrs), BackgroundAnimatableView {
constructor(
    level: Int,
    batteryIconStyle: Int,
    showPercentNextToIcon: Boolean,
    context: Context,
    attrs: AttributeSet? = null,
) : FrameLayout(context, attrs), BackgroundAnimatableView {
    private val roundedContainer: LinearLayout
    private val composeInner: ComposeView
    override val contentView: View
@@ -74,11 +78,7 @@ constructor(level: Int, context: Context, attrs: AttributeSet? = null) :
        composeInner.apply {
            setContent {
                PlatformTheme {
                    if (RudimentaryBattery.isEnabled) {
                        BatteryAndPercentChip(level)
                    } else {
                        UnifiedBatteryChip(level)
                    }
                    BatteryAndPercentChip(level, batteryIconStyle, showPercentNextToIcon)
                }
            }
        }
@@ -106,7 +106,9 @@ private fun UnifiedBatteryChip(level: Int) {
    val height = with(LocalDensity.current) { BatteryViewModel.STATUS_BAR_BATTERY_HEIGHT.toDp() }
    BatteryLayout(
        attribution = BatteryGlyph.Bolt, // Always charging
        iconStyleProvider = { BatteryRepository.ICON_STYLE_DEFAULT },
        levelProvider = { level },
        showLevelProvider = { false },
        isFullProvider = { isFull },
        glyphsProvider = { level.glyphRepresentation() },
        colorsProvider = { BatteryColors.DarkTheme.Charging },
@@ -118,20 +120,32 @@ private fun UnifiedBatteryChip(level: Int) {

@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
private fun BatteryAndPercentChip(level: Int) {
private fun BatteryAndPercentChip(
    level: Int,
    batteryIconStyle: Int,
    showPercentNextToIcon: Boolean,
) {
    val isFull = BatteryInteractor.isBatteryFull(level)
    val height = with(LocalDensity.current) { BatteryViewModel.STATUS_BAR_BATTERY_HEIGHT.toDp() }
    Row(verticalAlignment = Alignment.CenterVertically) {
    Row(
        horizontalArrangement = Arrangement.spacedBy(4.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        val isText = batteryIconStyle == BatteryRepository.ICON_STYLE_TEXT
        if (!isText) {
            BatteryLayout(
                attribution = BatteryGlyph.Bolt, // Always charging
                iconStyleProvider = { batteryIconStyle },
                levelProvider = { level },
                showLevelProvider = { false },
                isFullProvider = { isFull },
                glyphsProvider = { emptyList() },
                colorsProvider = { BatteryColors.DarkTheme.Charging },
                modifier = Modifier.height(height).wrapContentWidth(),
                contentDescription = "",
            )
        Spacer(modifier = Modifier.width(4.dp))
        }
        if (isText || showPercentNextToIcon) {
            Text(
                text = NumberFormat.getPercentInstance().format(level / 100f),
                color = BatteryColors.DarkTheme.Default.fill,
@@ -139,3 +153,4 @@ private fun BatteryAndPercentChip(level: Int) {
            )
        }
    }
}
+76 −4
Original line number Diff line number Diff line
@@ -69,6 +69,12 @@ interface BatteryRepository {
    /** State unknown means that we can't detect a battery */
    val isStateUnknown: Flow<Boolean>

    /**
     * [LineageSettings.System.STATUS_BAR_BATTERY_STYLE]. A user setting to indicate the battery
     * style in the home screen status bar
     */
    val batteryIconStyle: StateFlow<Int>

    /**
     * [LineageSettings.System.STATUS_BAR_SHOW_BATTERY_PERCENT]. A user setting to indicate whether
     * we should show the battery percentage in the home screen status bar
@@ -76,6 +82,10 @@ interface BatteryRepository {
    val showBatteryPercentMode: StateFlow<Int>

    companion object {
        const val ICON_STYLE_DEFAULT = 0
        const val ICON_STYLE_CIRCLE = 1
        const val ICON_STYLE_TEXT = 2
        const val ICON_STYLE_CIRCLE_DOTTED = 3
        const val SHOW_PERCENT_HIDDEN = 0
        const val SHOW_PERCENT_INSIDE = 1
        const val SHOW_PERCENT_NEXT_TO = 2
@@ -165,22 +175,82 @@ constructor(

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

    override val showBatteryPercentMode =
    override val batteryIconStyle =
        callbackFlow {
                val resolver = context.contentResolver
                val uri =
                    LineageSettings.System.getUriFor(
                        LineageSettings.System.STATUS_BAR_SHOW_BATTERY_PERCENT
                        LineageSettings.System.STATUS_BAR_BATTERY_STYLE
                    )

                fun readMode(): Int {
                    return LineageSettings.System.getIntForUser(
                        resolver,
                        LineageSettings.System.STATUS_BAR_BATTERY_STYLE,
                        BatteryRepository.ICON_STYLE_DEFAULT,
                        UserHandle.USER_CURRENT,
                    )
                }

                val observer =
                    object : ContentObserver(Handler(Looper.getMainLooper())) {
                        override fun onChange(selfChange: Boolean) {
                            trySend(readMode())
                        }
                    }

                resolver.registerContentObserver(
                    uri,
                    /* notifyForDescendants = */ false,
                    observer,
                    UserHandle.USER_ALL,
                )

                // Emit current value immediately
                trySend(readMode())

                awaitClose { resolver.unregisterContentObserver(observer) }
            }
            .distinctUntilChanged()
            .flowOn(bgDispatcher)
            .stateIn(
                scope = scope,
                started = SharingStarted.Lazily,
                initialValue = BatteryRepository.ICON_STYLE_DEFAULT,
            )

    override val showBatteryPercentMode =
        callbackFlow {
                val resolver = context.contentResolver
                val uris =
                    listOf(
                        LineageSettings.System.getUriFor(
                            LineageSettings.System.STATUS_BAR_BATTERY_STYLE
                        ),
                        LineageSettings.System.getUriFor(
                            LineageSettings.System.STATUS_BAR_SHOW_BATTERY_PERCENT
                        ),
                    )

                fun readMode(): Int {
                    val iconStyle =
                        LineageSettings.System.getIntForUser(
                            resolver,
                            LineageSettings.System.STATUS_BAR_BATTERY_STYLE,
                            BatteryRepository.ICON_STYLE_DEFAULT,
                            UserHandle.USER_CURRENT,
                        )
                    return if (iconStyle == BatteryRepository.ICON_STYLE_TEXT) {
                        BatteryRepository.SHOW_PERCENT_NEXT_TO
                    } else {
                        LineageSettings.System.getIntForUser(
                            resolver,
                            LineageSettings.System.STATUS_BAR_SHOW_BATTERY_PERCENT,
                            BatteryRepository.SHOW_PERCENT_HIDDEN,
                            UserHandle.USER_CURRENT,
                        )
                    }
                }

                val observer =
                    object : ContentObserver(Handler(Looper.getMainLooper())) {
@@ -189,12 +259,14 @@ constructor(
                        }
                    }

                for (uri in uris) {
                    resolver.registerContentObserver(
                        uri,
                        /* notifyForDescendants = */ false,
                        observer,
                        UserHandle.USER_ALL,
                    )
                }

                // Emit current value immediately
                trySend(readMode())
+3 −0
Original line number Diff line number Diff line
@@ -74,6 +74,9 @@ class BatteryInteractor @Inject constructor(
    /** @see [BatteryRepository.isPowerSaveEnabled] */
    val powerSave = repo.isPowerSaveEnabled

    /** @see [BatteryRepository.batteryIconStyle] */
    val batteryIconStyle: StateFlow<Int> = repo.batteryIconStyle

    /** @see [BatteryRepository.showBatteryPercentMode] */
    val showBatteryPercentMode: StateFlow<Int> = repo.showBatteryPercentMode

Loading