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

Commit 73756d92 authored by Michal Brzezinski's avatar Michal Brzezinski Committed by Michał Brzeziński
Browse files

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
parent 3cb27206
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -1730,15 +1730,15 @@
    <!-- Keyboard backlight indicator-->
    <dimen name="backlight_indicator_root_corner_radius">48dp</dimen>
    <dimen name="backlight_indicator_root_vertical_padding">8dp</dimen>
    <dimen name="backlight_indicator_root_horizontal_padding">4dp</dimen>
    <dimen name="backlight_indicator_root_horizontal_padding">6dp</dimen>
    <dimen name="backlight_indicator_icon_width">22dp</dimen>
    <dimen name="backlight_indicator_icon_height">11dp</dimen>
    <dimen name="backlight_indicator_icon_left_margin">2dp</dimen>
    <dimen name="backlight_indicator_icon_padding">10dp</dimen>
    <dimen name="backlight_indicator_step_width">52dp</dimen>
    <dimen name="backlight_indicator_step_height">40dp</dimen>
    <dimen name="backlight_indicator_step_horizontal_margin">4dp</dimen>
    <dimen name="backlight_indicator_step_horizontal_margin">2dp</dimen>
    <dimen name="backlight_indicator_step_small_radius">4dp</dimen>
    <dimen name="backlight_indicator_step_large_radius">48dp</dimen>
    <dimen name="backlight_indicator_step_large_radius">28dp</dimen>

    <!-- Broadcast dialog -->
    <dimen name="broadcast_dialog_title_img_margin_top">18dp</dimen>
+3 −0
Original line number Diff line number Diff line
@@ -203,5 +203,8 @@

    <item type="id" name="log_access_dialog_allow_button" />
    <item type="id" name="log_access_dialog_deny_button" />

    <!-- keyboard backlight indicator-->
    <item type="id" name="backlight_icon" />
</resources>
+91 −56
Original line number Diff line number Diff line
@@ -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,24 +152,35 @@ 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)
    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 {
                    updateColor(drawable, emptyRectangleColor)
            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) {
        if (drawable.paint.color != color) {
@@ -192,9 +216,33 @@ class KeyboardBacklightDialog(
    }

    private fun buildStepViews(): List<FrameLayout> {
        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)
    }
}