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

Commit 0daea65b authored by Evan Laird's avatar Evan Laird
Browse files

[Battery] RTL support

Apply the following layoutDirection-dependent changes in the unified
battery icon:

1. Fill layer now reverses the canvas
2. Frame and frame-bg are auto-mirrored
3. Attributions have checks for RTL mode layouts and adjust their
   offsets as such

Numerical text does not have an LTR rendering mode, so the foreground
attributions of the battery do not mirror.

Test: manual setting RTL mode
Bug: 314812750
Flag: ACONFIG com.android.settingslib.flags.new_status_bar_icons DEVELOPMENT
Change-Id: I581ec376e6004a76dbc96e1ec0be5e14dafa8fb7
parent 58e0af70
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.graphics.drawable.DrawableWrapper
import android.view.Gravity
import kotlin.math.ceil
import kotlin.math.min
import kotlin.math.roundToInt

@@ -36,7 +37,7 @@ import kotlin.math.roundToInt
 */
@Suppress("RtlHardcoded")
class BatteryAttributionDrawable(dr: Drawable?) : DrawableWrapper(dr) {
    /** One of [CENTER, LEFT]. Note that RTL is handled in the parent */
    /** One of [CENTER, LEFT]. Note that number text does not RTL. */
    var gravity = Gravity.CENTER
        set(value) {
            field = value
@@ -67,8 +68,8 @@ class BatteryAttributionDrawable(dr: Drawable?) : DrawableWrapper(dr) {
            dr.setBounds(
                bounds.left,
                bounds.top,
                (bounds.left + dw).roundToInt(),
                (bounds.top + dh).roundToInt()
                ceil(bounds.left + dw).toInt(),
                ceil(bounds.top + dh).toInt()
            )
        }
    }
+6 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.view.View
import com.android.systemui.battery.unified.BatteryLayersDrawable.Companion.Metrics
import kotlin.math.floor
import kotlin.math.roundToInt
@@ -103,6 +104,11 @@ class BatteryFillDrawable(private val framePath: Path) : Drawable() {
        // saveLayer is needed here so we don't clip the other layers of our drawable
        canvas.saveLayer(null, null)

        // Fill from the opposite direction in rtl mode
        if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
            canvas.scale(-1f, 1f, bounds.width() / 2f, bounds.height() / 2f)
        }

        // We need to use 3 draw commands:
        // 1. Clip to the current level
        // 2. Clip anything outside of the path
+16 −5
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import android.util.PathParser
import android.view.Gravity
import android.view.View
import com.android.systemui.res.R
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.roundToInt

/**
@@ -93,6 +95,7 @@ class BatteryLayersDrawable(
        }

    init {
        isAutoMirrored = true
        // Initialize the canvas rects since they are not static
        setAttrRects(layoutDirection == View.LAYOUT_DIRECTION_RTL)
    }
@@ -156,6 +159,13 @@ class BatteryLayersDrawable(
        scaleAttributionBounds()
    }

    override fun onLayoutDirectionChanged(layoutDirection: Int): Boolean {
        setAttrRects(layoutDirection == View.LAYOUT_DIRECTION_RTL)
        scaleAttributionBounds()

        return super.onLayoutDirectionChanged(layoutDirection)
    }

    private fun setAttrRects(rtl: Boolean) {
        // Local refs make the math easier to parse
        val full = Metrics.AttrFullCanvasInsets
@@ -198,13 +208,14 @@ class BatteryLayersDrawable(
        if (batteryState.showPercent && batteryState.attribution != null) {
            // 4a. percent & attribution. Implies space-sharing

            // Configure the attribute to draw in a smaller bounding box and align left
            // Configure the attribute to draw in a smaller bounding box and align left and use
            // floor/ceil math to make sure we get every available pixel
            attribution.gravity = Gravity.LEFT
            attribution.setBounds(
                scaledAttrRightCanvas.left.roundToInt(),
                scaledAttrRightCanvas.top.roundToInt(),
                scaledAttrRightCanvas.right.roundToInt(),
                scaledAttrRightCanvas.bottom.roundToInt(),
                floor(scaledAttrRightCanvas.left).toInt(),
                floor(scaledAttrRightCanvas.top).toInt(),
                ceil(scaledAttrRightCanvas.right).toInt(),
                ceil(scaledAttrRightCanvas.bottom).toInt(),
            )
            attribution.draw(canvas)

+4 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.view.View
import com.android.systemui.battery.unified.BatteryLayersDrawable.Companion.Metrics

/**
@@ -71,6 +72,7 @@ class BatteryPercentTextOnlyDrawable(font: Typeface) : Drawable() {
    }

    override fun draw(canvas: Canvas) {
        val rtl = layoutDirection == View.LAYOUT_DIRECTION_RTL
        val totalAvailableHeight = CanvasHeight * vScale

        // Distribute the vertical whitespace around the text. This is a simplified version of
@@ -81,11 +83,12 @@ class BatteryPercentTextOnlyDrawable(font: Typeface) : Drawable() {
        val totalAvailableWidth = CanvasWidth * hScale
        val textWidth = textPaint.measureText(percentText)
        val offsetX = (totalAvailableWidth - textWidth) / 2
        val startOffset = if (rtl) ViewportInsetRight else ViewportInsetLeft

        // Draw the text centered in the available area
        canvas.drawText(
            percentText,
            (ViewportInsetLeft * hScale) + offsetX,
            (startOffset * hScale) + offsetX,
            (ViewportInsetTop * vScale) + offsetY,
            textPaint
        )
+4 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.view.View
import com.android.systemui.battery.unified.BatteryLayersDrawable.Companion.Metrics

/**
@@ -94,6 +95,7 @@ class BatterySpaceSharingPercentTextDrawable(font: Typeface) : Drawable() {
    }

    override fun draw(canvas: Canvas) {
        val rtl = layoutDirection == View.LAYOUT_DIRECTION_RTL
        val totalAvailableHeight = CanvasHeight * vScale

        // Distribute the vertical whitespace around the text. This is a simplified version of
@@ -107,7 +109,7 @@ class BatterySpaceSharingPercentTextDrawable(font: Typeface) : Drawable() {

        canvas.drawText(
            percentText,
            (ViewportInsetLeft * hScale) + offsetX,
            ((if (rtl) ViewportInsetLeftRtl else ViewportInsetLeft) * hScale) + offsetX,
            (ViewportInsetTop * vScale) + offsetY,
            textPaint
        )
@@ -128,6 +130,7 @@ class BatterySpaceSharingPercentTextDrawable(font: Typeface) : Drawable() {

    companion object {
        private const val ViewportInsetLeft = 4f
        private const val ViewportInsetLeftRtl = 2f
        private const val ViewportInsetTop = 2f

        private const val CanvasWidth = 12f