From bb0710089a1ee99b5a65d79ec37610254dfd28b3 Mon Sep 17 00:00:00 2001 From: Michal Brzezinski Date: Tue, 28 Feb 2023 17:26:43 +0000 Subject: [PATCH] Implementing keyboard indicator dialog Dialog is not using xml resource as currently number of steps is flexible and comes from API call. That means everything in the view is built dynamically. Flag: KEYBOARD_BACKLIGHT_INDICATOR Fixes: 268650355 Test: manual + will add unit tests for the whole path in future CLs Change-Id: I35179c73dd1bba9962e3881d74209760d1d7dac5 --- .../res/drawable/ic_keyboard_backlight.xml | 12 + packages/SystemUI/res/values/colors.xml | 5 + packages/SystemUI/res/values/dimens.xml | 13 + .../ui/KeyboardBacklightDialogCoordinator.kt | 11 +- .../ui/view/KeyboardBacklightDialog.kt | 252 +++++++++++++++++- 5 files changed, 287 insertions(+), 6 deletions(-) create mode 100644 packages/SystemUI/res/drawable/ic_keyboard_backlight.xml diff --git a/packages/SystemUI/res/drawable/ic_keyboard_backlight.xml b/packages/SystemUI/res/drawable/ic_keyboard_backlight.xml new file mode 100644 index 0000000000000..d123caf82ed5f --- /dev/null +++ b/packages/SystemUI/res/drawable/ic_keyboard_backlight.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + diff --git a/packages/SystemUI/res/values/colors.xml b/packages/SystemUI/res/values/colors.xml index ca4217f64b600..ac4c4929bab2a 100644 --- a/packages/SystemUI/res/values/colors.xml +++ b/packages/SystemUI/res/values/colors.xml @@ -207,6 +207,11 @@ @*android:color/black #CC191C1D + + #F6E388 + #494740 + #32302A + #F28B82 diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml index 3b8f1a7d1e4d9..22706b342da1c 100644 --- a/packages/SystemUI/res/values/dimens.xml +++ b/packages/SystemUI/res/values/dimens.xml @@ -1653,6 +1653,19 @@ 20dp 18dp + + 48dp + 8dp + 4dp + 22dp + 11dp + 2dp + 52dp + 40dp + 4dp + 4dp + 48dp + 18dp 24sp diff --git a/packages/SystemUI/src/com/android/systemui/keyboard/backlight/ui/KeyboardBacklightDialogCoordinator.kt b/packages/SystemUI/src/com/android/systemui/keyboard/backlight/ui/KeyboardBacklightDialogCoordinator.kt index 85d0379a77db8..5e806b6128055 100644 --- a/packages/SystemUI/src/com/android/systemui/keyboard/backlight/ui/KeyboardBacklightDialogCoordinator.kt +++ b/packages/SystemUI/src/com/android/systemui/keyboard/backlight/ui/KeyboardBacklightDialogCoordinator.kt @@ -46,8 +46,15 @@ constructor( viewModel.dialogContent.collect { dialogViewModel -> if (dialogViewModel != null) { if (dialog == null) { - dialog = KeyboardBacklightDialog(context, dialogViewModel) - // pass viewModel and show + dialog = + KeyboardBacklightDialog( + context, + initialCurrentLevel = dialogViewModel.currentValue, + initialMaxLevel = dialogViewModel.maxValue + ) + dialog?.show() + } else { + dialog?.updateState(dialogViewModel.currentValue, dialogViewModel.maxValue) } } else { dialog?.dismiss() 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 b68a2a84b5d13..a173f8b914db0 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 @@ -17,16 +17,260 @@ package com.android.systemui.keyboard.backlight.ui.view +import android.annotation.ColorInt import android.app.Dialog import android.content.Context +import android.graphics.drawable.ShapeDrawable +import android.graphics.drawable.shapes.RoundRectShape import android.os.Bundle -import com.android.systemui.keyboard.backlight.ui.viewmodel.BacklightDialogContentViewModel +import android.view.Gravity +import android.view.Window +import android.view.WindowManager +import android.widget.FrameLayout +import android.widget.ImageView +import android.widget.LinearLayout +import android.widget.LinearLayout.LayoutParams +import android.widget.LinearLayout.LayoutParams.WRAP_CONTENT +import com.android.systemui.R +import com.android.systemui.util.children -class KeyboardBacklightDialog(context: Context, val viewModel: BacklightDialogContentViewModel) : - Dialog(context) { +class KeyboardBacklightDialog( + context: Context, + initialCurrentLevel: Int, + initialMaxLevel: Int, +) : Dialog(context) { + + private data class RootProperties( + val cornerRadius: Float, + val verticalPadding: Int, + val horizontalPadding: Int, + ) + + private data class BacklightIconProperties( + val width: Int, + val height: Int, + val leftMargin: Int, + ) + + private data class StepViewProperties( + val width: Int, + val height: Int, + val horizontalMargin: Int, + val smallRadius: Float, + val largeRadius: Float, + ) + + private var currentLevel: Int = 0 + private var maxLevel: Int = 0 + + private lateinit var rootView: LinearLayout + + private var dialogBottomMargin = 208 + private lateinit var rootProperties: RootProperties + private lateinit var iconProperties: BacklightIconProperties + private lateinit var stepProperties: StepViewProperties + @ColorInt var filledRectangleColor: Int = 0 + @ColorInt var emptyRectangleColor: Int = 0 + @ColorInt var backgroundColor: Int = 0 + + init { + currentLevel = initialCurrentLevel + maxLevel = initialMaxLevel + } override fun onCreate(savedInstanceState: Bundle?) { + setUpWindowProperties(this) + setWindowTitle() + updateResources() + rootView = buildRootView() + setContentView(rootView) super.onCreate(savedInstanceState) - // TODO(b/268650355) Implement the dialog + updateState(currentLevel, maxLevel, forceRefresh = true) + } + + private fun updateResources() { + context.resources.apply { + filledRectangleColor = getColor(R.color.backlight_indicator_step_filled) + emptyRectangleColor = getColor(R.color.backlight_indicator_step_empty) + backgroundColor = getColor(R.color.backlight_indicator_background) + rootProperties = + RootProperties( + cornerRadius = + getDimensionPixelSize(R.dimen.backlight_indicator_root_corner_radius) + .toFloat(), + verticalPadding = + getDimensionPixelSize(R.dimen.backlight_indicator_root_vertical_padding), + horizontalPadding = + getDimensionPixelSize(R.dimen.backlight_indicator_root_horizontal_padding) + ) + iconProperties = + 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), + ) + stepProperties = + StepViewProperties( + width = getDimensionPixelSize(R.dimen.backlight_indicator_step_width), + height = getDimensionPixelSize(R.dimen.backlight_indicator_step_height), + horizontalMargin = + getDimensionPixelSize(R.dimen.backlight_indicator_step_horizontal_margin), + smallRadius = + getDimensionPixelSize(R.dimen.backlight_indicator_step_small_radius) + .toFloat(), + largeRadius = + getDimensionPixelSize(R.dimen.backlight_indicator_step_large_radius) + .toFloat(), + ) + } + } + + fun updateState(current: Int, max: Int, forceRefresh: Boolean = false) { + if (maxLevel != max || forceRefresh) { + maxLevel = max + rootView.removeAllViews() + buildStepViews().forEach { rootView.addView(it) } + } + currentLevel = current + updateLevel() + } + + 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 updateColor(drawable: ShapeDrawable, @ColorInt color: Int) { + if (drawable.paint.color != color) { + drawable.paint.color = color + drawable.invalidateSelf() + } + } + + private fun buildRootView(): LinearLayout { + val linearLayout = + LinearLayout(context).apply { + orientation = LinearLayout.HORIZONTAL + layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT) + setPadding( + /* left= */ rootProperties.horizontalPadding, + /* top= */ rootProperties.verticalPadding, + /* right= */ rootProperties.horizontalPadding, + /* bottom= */ rootProperties.verticalPadding + ) + } + val drawable = + ShapeDrawable( + RoundRectShape( + /* outerRadii= */ FloatArray(8) { rootProperties.cornerRadius }, + /* inset= */ null, + /* innerRadii= */ null + ) + ) + drawable.paint.color = backgroundColor + linearLayout.background = drawable + return linearLayout + } + + private fun buildStepViews(): List { + val stepViews = (0..maxLevel).map { i -> createStepViewAt(i) } + stepViews[0].addView(createBacklightIconView()) + return stepViews + } + + private fun createStepViewAt(i: Int): FrameLayout { + return FrameLayout(context).apply { + layoutParams = + FrameLayout.LayoutParams(stepProperties.width, stepProperties.height).apply { + setMargins( + /* left= */ stepProperties.horizontalMargin, + /* top= */ 0, + /* right= */ stepProperties.horizontalMargin, + /* bottom= */ 0 + ) + } + val drawable = + ShapeDrawable( + RoundRectShape( + /* outerRadii= */ radiiForIndex(i, maxLevel), + /* inset= */ null, + /* innerRadii= */ null + ) + ) + drawable.paint.color = emptyRectangleColor + background = drawable + } + } + + private fun createBacklightIconView(): ImageView { + return ImageView(context).apply { + setImageResource(R.drawable.ic_keyboard_backlight) + layoutParams = + FrameLayout.LayoutParams(iconProperties.width, iconProperties.height).apply { + gravity = Gravity.CENTER + leftMargin = iconProperties.leftMargin + } + } + } + + private fun setWindowTitle() { + val attrs = window.attributes + // TODO(b/271796169): check if title needs to be a translatable resource. + attrs.title = "KeyboardBacklightDialog" + attrs?.y = dialogBottomMargin + window.attributes = attrs + } + + private fun setUpWindowProperties(dialog: Dialog) { + val window = dialog.window + window.requestFeature(Window.FEATURE_NO_TITLE) // otherwise fails while creating actionBar + window.setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL) + window.addFlags( + WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM or + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + ) + window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) + window.setBackgroundDrawableResource(android.R.color.transparent) + window.setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL) + setCanceledOnTouchOutside(true) + } + + 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 + } } }