From 73756d9218a536511243cf28f2bbe7cd84d6af47 Mon Sep 17 00:00:00 2001 From: Michal Brzezinski Date: Tue, 2 May 2023 16:17:40 +0100 Subject: [PATCH] Implementing new visd for backlight indicator With new design first tile with backlight icon has different shape than rest of the tiles. Also it changes color when backlight it turned off (level set to 0). Besides there are a few small changes with paddings/corners. Flag: KEYBOARD_BACKLIGHT_INDICATOR Test: KeyboardBacklightDialogScreenshotTest Fixes: 270374251 Change-Id: I8bfbafc2e637a5df6e8df536784187146ddc10ca --- packages/SystemUI/res/values/dimens.xml | 8 +- packages/SystemUI/res/values/ids.xml | 3 + .../ui/view/KeyboardBacklightDialog.kt | 147 +++++++++++------- 3 files changed, 98 insertions(+), 60 deletions(-) diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml index 271fab17e0af6..a62dead7834b5 100644 --- a/packages/SystemUI/res/values/dimens.xml +++ b/packages/SystemUI/res/values/dimens.xml @@ -1730,15 +1730,15 @@ 48dp 8dp - 4dp + 6dp 22dp 11dp - 2dp + 10dp 52dp 40dp - 4dp + 2dp 4dp - 48dp + 28dp 18dp diff --git a/packages/SystemUI/res/values/ids.xml b/packages/SystemUI/res/values/ids.xml index eaeaabe8779a9..e5c946156e463 100644 --- a/packages/SystemUI/res/values/ids.xml +++ b/packages/SystemUI/res/values/ids.xml @@ -203,5 +203,8 @@ + + + diff --git a/packages/SystemUI/src/com/android/systemui/keyboard/backlight/ui/view/KeyboardBacklightDialog.kt b/packages/SystemUI/src/com/android/systemui/keyboard/backlight/ui/view/KeyboardBacklightDialog.kt index d3678b5a2a4e9..70783418ddb45 100644 --- a/packages/SystemUI/src/com/android/systemui/keyboard/backlight/ui/view/KeyboardBacklightDialog.kt +++ b/packages/SystemUI/src/com/android/systemui/keyboard/backlight/ui/view/KeyboardBacklightDialog.kt @@ -22,9 +22,12 @@ import android.annotation.ColorInt import android.app.Dialog import android.content.Context import android.graphics.drawable.ShapeDrawable +import android.graphics.drawable.shapes.OvalShape import android.graphics.drawable.shapes.RoundRectShape import android.os.Bundle import android.view.Gravity +import android.view.View +import android.view.ViewGroup.MarginLayoutParams import android.view.Window import android.view.WindowManager import android.widget.FrameLayout @@ -32,9 +35,10 @@ import android.widget.ImageView import android.widget.LinearLayout import android.widget.LinearLayout.LayoutParams import android.widget.LinearLayout.LayoutParams.WRAP_CONTENT +import androidx.annotation.IdRes +import androidx.core.view.setPadding import com.android.settingslib.Utils import com.android.systemui.R -import com.android.systemui.util.children class KeyboardBacklightDialog( context: Context, @@ -51,7 +55,7 @@ class KeyboardBacklightDialog( private data class BacklightIconProperties( val width: Int, val height: Int, - val leftMargin: Int, + val padding: Int, ) private data class StepViewProperties( @@ -71,6 +75,7 @@ class KeyboardBacklightDialog( private lateinit var rootProperties: RootProperties private lateinit var iconProperties: BacklightIconProperties private lateinit var stepProperties: StepViewProperties + @ColorInt var filledRectangleColor = getColorFromStyle(com.android.internal.R.attr.materialColorPrimary) @ColorInt @@ -78,7 +83,16 @@ class KeyboardBacklightDialog( getColorFromStyle(com.android.internal.R.attr.materialColorOutlineVariant) @ColorInt var backgroundColor = getColorFromStyle(com.android.internal.R.attr.materialColorSurfaceBright) - @ColorInt var iconColor = getColorFromStyle(com.android.internal.R.attr.materialColorOnPrimary) + @ColorInt + var defaultIconColor = getColorFromStyle(com.android.internal.R.attr.materialColorOnPrimary) + @ColorInt + var defaultIconBackgroundColor = + getColorFromStyle(com.android.internal.R.attr.materialColorPrimary) + @ColorInt + var dimmedIconColor = getColorFromStyle(com.android.internal.R.attr.materialColorOnSurface) + @ColorInt + var dimmedIconBackgroundColor = + getColorFromStyle(com.android.internal.R.attr.materialColorSurfaceDim) init { currentLevel = initialCurrentLevel @@ -111,8 +125,7 @@ class KeyboardBacklightDialog( BacklightIconProperties( width = getDimensionPixelSize(R.dimen.backlight_indicator_icon_width), height = getDimensionPixelSize(R.dimen.backlight_indicator_icon_height), - leftMargin = - getDimensionPixelSize(R.dimen.backlight_indicator_icon_left_margin), + padding = getDimensionPixelSize(R.dimen.backlight_indicator_icon_padding), ) stepProperties = StepViewProperties( @@ -139,23 +152,34 @@ class KeyboardBacklightDialog( if (maxLevel != max || forceRefresh) { maxLevel = max rootView.removeAllViews() + rootView.addView(buildIconTile()) buildStepViews().forEach { rootView.addView(it) } } currentLevel = current - updateLevel() + updateIconTile() + updateStepColors() } - private fun updateLevel() { - rootView.children.forEachIndexed( - action = { index, v -> - val drawable = v.background as ShapeDrawable - if (index <= currentLevel) { - updateColor(drawable, filledRectangleColor) - } else { - updateColor(drawable, emptyRectangleColor) - } - } - ) + private fun updateIconTile() { + val iconTile = rootView.findViewById(BACKLIGHT_ICON_ID) as ImageView + val backgroundDrawable = iconTile.background as ShapeDrawable + if (currentLevel == 0) { + iconTile.setColorFilter(dimmedIconColor) + updateColor(backgroundDrawable, dimmedIconBackgroundColor) + } else { + iconTile.setColorFilter(defaultIconColor) + updateColor(backgroundDrawable, defaultIconBackgroundColor) + } + } + + private fun updateStepColors() { + (1 until rootView.childCount).forEach { index -> + val drawable = rootView.getChildAt(index).background as ShapeDrawable + updateColor( + drawable, + if (index <= currentLevel) filledRectangleColor else emptyRectangleColor, + ) + } } private fun updateColor(drawable: ShapeDrawable, @ColorInt color: Int) { @@ -192,9 +216,33 @@ class KeyboardBacklightDialog( } private fun buildStepViews(): List { - val stepViews = (0..maxLevel).map { i -> createStepViewAt(i) } - stepViews[0].addView(createBacklightIconView()) - return stepViews + return (1..maxLevel).map { i -> createStepViewAt(i) } + } + + private fun buildIconTile(): View { + val diameter = stepProperties.height + val circleDrawable = + ShapeDrawable(OvalShape()).apply { + intrinsicHeight = diameter + intrinsicWidth = diameter + } + + return ImageView(context).apply { + setImageResource(R.drawable.ic_keyboard_backlight) + id = BACKLIGHT_ICON_ID + setColorFilter(defaultIconColor) + setPadding(iconProperties.padding) + layoutParams = + MarginLayoutParams(diameter, diameter).apply { + setMargins( + /* left= */ stepProperties.horizontalMargin, + /* top= */ 0, + /* right= */ stepProperties.horizontalMargin, + /* bottom= */ 0 + ) + } + background = circleDrawable + } } private fun createStepViewAt(i: Int): FrameLayout { @@ -221,18 +269,6 @@ class KeyboardBacklightDialog( } } - private fun createBacklightIconView(): ImageView { - return ImageView(context).apply { - setImageResource(R.drawable.ic_keyboard_backlight) - setColorFilter(iconColor) - layoutParams = - FrameLayout.LayoutParams(iconProperties.width, iconProperties.height).apply { - gravity = Gravity.CENTER - leftMargin = iconProperties.leftMargin - } - } - } - private fun setWindowPosition() { window?.apply { setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL) @@ -262,30 +298,29 @@ class KeyboardBacklightDialog( private fun radiiForIndex(i: Int, last: Int): FloatArray { val smallRadius = stepProperties.smallRadius val largeRadius = stepProperties.largeRadius - return when (i) { - 0 -> // left radii bigger - floatArrayOf( - largeRadius, - largeRadius, - smallRadius, - smallRadius, - smallRadius, - smallRadius, - largeRadius, - largeRadius - ) - last -> // right radii bigger - floatArrayOf( - smallRadius, - smallRadius, - largeRadius, - largeRadius, - largeRadius, - largeRadius, - smallRadius, - smallRadius - ) - else -> FloatArray(8) { smallRadius } // all radii equal + val radii = FloatArray(8) { smallRadius } + if (i == 1) { + radii.setLeftCorners(largeRadius) } + // note "first" and "last" might be the same tile + if (i == last) { + radii.setRightCorners(largeRadius) + } + return radii + } + + private fun FloatArray.setLeftCorners(radius: Float) { + LEFT_CORNERS_INDICES.forEach { this[it] = radius } + } + private fun FloatArray.setRightCorners(radius: Float) { + RIGHT_CORNERS_INDICES.forEach { this[it] = radius } + } + + private companion object { + @IdRes val BACKLIGHT_ICON_ID = R.id.backlight_icon + + // indices used to define corners radii in ShapeDrawable + val LEFT_CORNERS_INDICES: IntArray = intArrayOf(0, 1, 6, 7) + val RIGHT_CORNERS_INDICES: IntArray = intArrayOf(2, 3, 4, 5) } }