diff options
| author | 2024-03-05 16:56:22 -0500 | |
|---|---|---|
| committer | 2024-03-06 09:52:16 -0500 | |
| commit | 0daea65bf49fc1b40cab9b34a8536d32b9d26f49 (patch) | |
| tree | 79b47df5c222b7dc3f3ef88e437314b0939aeb6b | |
| parent | 58e0af70731dd2964fe0cc4a2e2743985f178003 (diff) | |
[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
5 files changed, 34 insertions, 10 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryAttributionDrawable.kt b/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryAttributionDrawable.kt index 1b8495ace243..f3652b89cc50 100644 --- a/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryAttributionDrawable.kt +++ b/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryAttributionDrawable.kt @@ -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() ) } } diff --git a/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryFillDrawable.kt b/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryFillDrawable.kt index 6d3206767d2b..5e34d2909d81 100644 --- a/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryFillDrawable.kt +++ b/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryFillDrawable.kt @@ -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 diff --git a/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryLayersDrawable.kt b/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryLayersDrawable.kt index 21738ab8adbd..706b9ec563c9 100644 --- a/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryLayersDrawable.kt +++ b/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryLayersDrawable.kt @@ -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) diff --git a/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryPercentTextOnlyDrawable.kt b/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryPercentTextOnlyDrawable.kt index 123d6ba57900..aa0e37348f1e 100644 --- a/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryPercentTextOnlyDrawable.kt +++ b/packages/SystemUI/src/com/android/systemui/battery/unified/BatteryPercentTextOnlyDrawable.kt @@ -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 ) diff --git a/packages/SystemUI/src/com/android/systemui/battery/unified/BatterySpaceSharingPercentTextDrawable.kt b/packages/SystemUI/src/com/android/systemui/battery/unified/BatterySpaceSharingPercentTextDrawable.kt index 0c418b9caa7d..3b4c77936cff 100644 --- a/packages/SystemUI/src/com/android/systemui/battery/unified/BatterySpaceSharingPercentTextDrawable.kt +++ b/packages/SystemUI/src/com/android/systemui/battery/unified/BatterySpaceSharingPercentTextDrawable.kt @@ -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 |