Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.controls.ui |
| 18 | |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 19 | import android.animation.Animator |
| 20 | import android.animation.AnimatorListenerAdapter |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 21 | import android.animation.AnimatorSet |
| 22 | import android.animation.ObjectAnimator |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 23 | import android.animation.ValueAnimator |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 24 | import android.annotation.ColorRes |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 25 | import android.app.Dialog |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 26 | import android.content.Context |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 27 | import android.content.res.ColorStateList |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 28 | import android.graphics.drawable.ClipDrawable |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable |
Matt Pietal | a103a94 | 2020-03-13 12:10:35 -0400 | [diff] [blame] | 30 | import android.graphics.drawable.GradientDrawable |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 31 | import android.graphics.drawable.LayerDrawable |
Matt Pietal | 798d113 | 2020-06-01 09:47:42 -0400 | [diff] [blame] | 32 | import android.graphics.drawable.StateListDrawable |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 33 | import android.service.controls.Control |
Matt Pietal | f8cc0fa | 2020-03-26 08:48:50 -0400 | [diff] [blame] | 34 | import android.service.controls.DeviceTypes |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 35 | import android.service.controls.actions.ControlAction |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 36 | import android.service.controls.templates.ControlTemplate |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 37 | import android.service.controls.templates.RangeTemplate |
Matt Pietal | 53a8bbd | 2020-03-05 16:10:34 -0500 | [diff] [blame] | 38 | import android.service.controls.templates.StatelessTemplate |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 39 | import android.service.controls.templates.TemperatureControlTemplate |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 40 | import android.service.controls.templates.ToggleRangeTemplate |
| 41 | import android.service.controls.templates.ToggleTemplate |
Lucas Dupin | d60b332 | 2020-04-15 18:06:47 -0700 | [diff] [blame] | 42 | import android.util.MathUtils |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 43 | import android.util.TypedValue |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 44 | import android.view.View |
| 45 | import android.view.ViewGroup |
| 46 | import android.widget.ImageView |
| 47 | import android.widget.TextView |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 48 | import com.android.internal.graphics.ColorUtils |
| 49 | import com.android.systemui.Interpolators |
| 50 | import com.android.systemui.R |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 51 | import com.android.systemui.controls.controller.ControlsController |
Matt Pietal | 1aac43b | 2020-02-04 15:43:31 -0500 | [diff] [blame] | 52 | import com.android.systemui.util.concurrency.DelayableExecutor |
Matt Pietal | b582b69 | 2020-02-14 19:37:57 -0500 | [diff] [blame] | 53 | import kotlin.reflect.KClass |
| 54 | |
Matt Pietal | dc78c84 | 2020-03-30 08:09:18 -0400 | [diff] [blame] | 55 | /** |
| 56 | * Wraps the widgets that make up the UI representation of a {@link Control}. Updates to the view |
| 57 | * are signaled via calls to {@link #bindData}. Similar to the ViewHolder concept used in |
| 58 | * RecyclerViews. |
| 59 | */ |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 60 | class ControlViewHolder( |
| 61 | val layout: ViewGroup, |
Matt Pietal | 1aac43b | 2020-02-04 15:43:31 -0500 | [diff] [blame] | 62 | val controlsController: ControlsController, |
Matt Pietal | 5bdfba4 | 2020-02-14 09:14:43 -0500 | [diff] [blame] | 63 | val uiExecutor: DelayableExecutor, |
Matt Pietal | 677981d | 2020-04-24 14:38:32 -0400 | [diff] [blame] | 64 | val bgExecutor: DelayableExecutor, |
| 65 | val controlActionCoordinator: ControlActionCoordinator |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 66 | ) { |
Matt Pietal | dc78c84 | 2020-03-30 08:09:18 -0400 | [diff] [blame] | 67 | |
| 68 | companion object { |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 69 | const val STATE_ANIMATION_DURATION = 700L |
Matt Pietal | d8ca2c0 | 2020-04-24 19:20:48 -0400 | [diff] [blame] | 70 | private const val ALPHA_ENABLED = 255 |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 71 | private const val ALPHA_DISABLED = 0 |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 72 | private const val STATUS_ALPHA_ENABLED = 1f |
| 73 | private const val STATUS_ALPHA_DIMMED = 0.45f |
Matt Pietal | dc78c84 | 2020-03-30 08:09:18 -0400 | [diff] [blame] | 74 | private val FORCE_PANEL_DEVICES = setOf( |
| 75 | DeviceTypes.TYPE_THERMOSTAT, |
| 76 | DeviceTypes.TYPE_CAMERA |
| 77 | ) |
Matt Pietal | 798d113 | 2020-06-01 09:47:42 -0400 | [diff] [blame] | 78 | private val ATTR_ENABLED = intArrayOf(android.R.attr.state_enabled) |
| 79 | private val ATTR_DISABLED = intArrayOf(-android.R.attr.state_enabled) |
Matt Pietal | 677981d | 2020-04-24 14:38:32 -0400 | [diff] [blame] | 80 | const val MIN_LEVEL = 0 |
| 81 | const val MAX_LEVEL = 10000 |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 82 | |
| 83 | fun findBehaviorClass( |
| 84 | status: Int, |
| 85 | template: ControlTemplate, |
| 86 | deviceType: Int |
| 87 | ): KClass<out Behavior> { |
| 88 | return when { |
Matt Pietal | 96f746b | 2020-06-08 14:40:00 -0400 | [diff] [blame] | 89 | status != Control.STATUS_OK -> StatusBehavior::class |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 90 | deviceType == DeviceTypes.TYPE_CAMERA -> TouchBehavior::class |
Matt Pietal | 733d637 | 2020-06-12 09:36:52 -0400 | [diff] [blame] | 91 | template == ControlTemplate.NO_TEMPLATE -> TouchBehavior::class |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 92 | template is ToggleTemplate -> ToggleBehavior::class |
| 93 | template is StatelessTemplate -> TouchBehavior::class |
| 94 | template is ToggleRangeTemplate -> ToggleRangeBehavior::class |
| 95 | template is RangeTemplate -> ToggleRangeBehavior::class |
| 96 | template is TemperatureControlTemplate -> TemperatureControlBehavior::class |
| 97 | else -> DefaultBehavior::class |
| 98 | } |
| 99 | } |
Matt Pietal | dc78c84 | 2020-03-30 08:09:18 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 102 | private val toggleBackgroundIntensity: Float = layout.context.resources |
| 103 | .getFraction(R.fraction.controls_toggle_bg_intensity, 1, 1) |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 104 | private var stateAnimator: ValueAnimator? = null |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 105 | private var statusAnimator: Animator? = null |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 106 | private val baseLayer: GradientDrawable |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 107 | val icon: ImageView = layout.requireViewById(R.id.icon) |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 108 | private val status: TextView = layout.requireViewById(R.id.status) |
| 109 | private var nextStatusText: CharSequence = "" |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 110 | val title: TextView = layout.requireViewById(R.id.title) |
| 111 | val subtitle: TextView = layout.requireViewById(R.id.subtitle) |
| 112 | val context: Context = layout.getContext() |
| 113 | val clipLayer: ClipDrawable |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 114 | lateinit var cws: ControlWithState |
Matt Pietal | b582b69 | 2020-02-14 19:37:57 -0500 | [diff] [blame] | 115 | var behavior: Behavior? = null |
Matt Pietal | b7da66c | 2020-03-06 10:49:31 -0500 | [diff] [blame] | 116 | var lastAction: ControlAction? = null |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 117 | var isLoading = false |
Matt Pietal | 370db87 | 2020-06-04 11:13:29 -0400 | [diff] [blame] | 118 | var visibleDialog: Dialog? = null |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 119 | private var lastChallengeDialog: Dialog? = null |
Matt Pietal | e4dda8b | 2020-05-08 09:03:57 -0400 | [diff] [blame] | 120 | private val onDialogCancel: () -> Unit = { lastChallengeDialog = null } |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 121 | |
Matt Pietal | dc78c84 | 2020-03-30 08:09:18 -0400 | [diff] [blame] | 122 | val deviceType: Int |
Matt Pietal | 96f746b | 2020-06-08 14:40:00 -0400 | [diff] [blame] | 123 | get() = cws.control?.let { it.deviceType } ?: cws.ci.deviceType |
| 124 | val controlStatus: Int |
| 125 | get() = cws.control?.let { it.status } ?: Control.STATUS_UNKNOWN |
| 126 | val controlTemplate: ControlTemplate |
| 127 | get() = cws.control?.let { it.controlTemplate } ?: ControlTemplate.NO_TEMPLATE |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 128 | |
Matt Pietal | 3b859c3 | 2020-06-18 15:27:13 -0400 | [diff] [blame] | 129 | var userInteractionInProgress = false |
| 130 | |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 131 | init { |
| 132 | val ld = layout.getBackground() as LayerDrawable |
| 133 | ld.mutate() |
| 134 | clipLayer = ld.findDrawableByLayerId(R.id.clip_layer) as ClipDrawable |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 135 | clipLayer.alpha = ALPHA_DISABLED |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 136 | baseLayer = ld.findDrawableByLayerId(R.id.background) as GradientDrawable |
Matt Pietal | 5f478c7 | 2020-04-01 15:53:54 -0400 | [diff] [blame] | 137 | // needed for marquee to start |
| 138 | status.setSelected(true) |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | fun bindData(cws: ControlWithState) { |
Matt Pietal | 3b859c3 | 2020-06-18 15:27:13 -0400 | [diff] [blame] | 142 | // If an interaction is in progress, the update may visually interfere with the action the |
| 143 | // action the user wants to make. Don't apply the update, and instead assume a new update |
| 144 | // will coming from when the user interaction is complete. |
| 145 | if (userInteractionInProgress) return |
| 146 | |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 147 | this.cws = cws |
| 148 | |
Matt Pietal | 96f746b | 2020-06-08 14:40:00 -0400 | [diff] [blame] | 149 | // For the following statuses only, assume the title/subtitle could not be set properly |
| 150 | // by the app and instead use the last known information from favorites |
| 151 | if (controlStatus == Control.STATUS_UNKNOWN || controlStatus == Control.STATUS_NOT_FOUND) { |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 152 | title.setText(cws.ci.controlTitle) |
Matt Pietal | 8587826 | 2020-03-18 15:34:46 -0400 | [diff] [blame] | 153 | subtitle.setText(cws.ci.controlSubtitle) |
Matt Pietal | 96f746b | 2020-06-08 14:40:00 -0400 | [diff] [blame] | 154 | } else { |
| 155 | cws.control?.let { |
| 156 | title.setText(it.title) |
| 157 | subtitle.setText(it.subtitle) |
| 158 | } |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Matt Pietal | 307b1ef | 2020-02-10 07:27:01 -0500 | [diff] [blame] | 161 | cws.control?.let { |
Matt Pietal | 31ec9f2 | 2020-03-12 09:02:17 -0400 | [diff] [blame] | 162 | layout.setClickable(true) |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 163 | layout.setOnLongClickListener(View.OnLongClickListener() { |
Matt Pietal | 677981d | 2020-04-24 14:38:32 -0400 | [diff] [blame] | 164 | controlActionCoordinator.longPress(this@ControlViewHolder) |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 165 | true |
| 166 | }) |
Matt Pietal | 603f8e7 | 2020-06-05 11:02:40 -0400 | [diff] [blame] | 167 | |
| 168 | controlActionCoordinator.runPendingAction(cws.ci.controlId) |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 171 | isLoading = false |
Matt Pietal | 96f746b | 2020-06-08 14:40:00 -0400 | [diff] [blame] | 172 | behavior = bindBehavior(behavior, |
| 173 | findBehaviorClass(controlStatus, controlTemplate, deviceType)) |
Matt Pietal | c879685 | 2020-05-13 15:13:30 -0400 | [diff] [blame] | 174 | updateContentDescription() |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Matt Pietal | 1aac43b | 2020-02-04 15:43:31 -0500 | [diff] [blame] | 177 | fun actionResponse(@ControlAction.ResponseResult response: Int) { |
Matt Pietal | 603f8e7 | 2020-06-05 11:02:40 -0400 | [diff] [blame] | 178 | controlActionCoordinator.enableActionOnTouch(cws.ci.controlId) |
| 179 | |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 180 | // OK responses signal normal behavior, and the app will provide control updates |
| 181 | val failedAttempt = lastChallengeDialog != null |
| 182 | when (response) { |
| 183 | ControlAction.RESPONSE_OK -> |
| 184 | lastChallengeDialog = null |
| 185 | ControlAction.RESPONSE_UNKNOWN -> { |
| 186 | lastChallengeDialog = null |
Matt Pietal | 2f63cc0 | 2020-06-19 15:21:13 -0400 | [diff] [blame] | 187 | setErrorStatus() |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 188 | } |
| 189 | ControlAction.RESPONSE_FAIL -> { |
| 190 | lastChallengeDialog = null |
Matt Pietal | 2f63cc0 | 2020-06-19 15:21:13 -0400 | [diff] [blame] | 191 | setErrorStatus() |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 192 | } |
| 193 | ControlAction.RESPONSE_CHALLENGE_PIN -> { |
Matt Pietal | e4dda8b | 2020-05-08 09:03:57 -0400 | [diff] [blame] | 194 | lastChallengeDialog = ChallengeDialogs.createPinDialog( |
Matt Pietal | 9f403a1 | 2020-06-18 08:48:24 -0400 | [diff] [blame] | 195 | this, false /* useAlphanumeric */, failedAttempt, onDialogCancel) |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 196 | lastChallengeDialog?.show() |
| 197 | } |
| 198 | ControlAction.RESPONSE_CHALLENGE_PASSPHRASE -> { |
Matt Pietal | e4dda8b | 2020-05-08 09:03:57 -0400 | [diff] [blame] | 199 | lastChallengeDialog = ChallengeDialogs.createPinDialog( |
Matt Pietal | 9f403a1 | 2020-06-18 08:48:24 -0400 | [diff] [blame] | 200 | this, true /* useAlphanumeric */, failedAttempt, onDialogCancel) |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 201 | lastChallengeDialog?.show() |
| 202 | } |
| 203 | ControlAction.RESPONSE_CHALLENGE_ACK -> { |
Matt Pietal | e4dda8b | 2020-05-08 09:03:57 -0400 | [diff] [blame] | 204 | lastChallengeDialog = ChallengeDialogs.createConfirmationDialog( |
| 205 | this, onDialogCancel) |
Matt Pietal | 94316e9 | 2020-04-22 10:58:32 -0400 | [diff] [blame] | 206 | lastChallengeDialog?.show() |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | fun dismiss() { |
| 212 | lastChallengeDialog?.dismiss() |
| 213 | lastChallengeDialog = null |
Matt Pietal | 370db87 | 2020-06-04 11:13:29 -0400 | [diff] [blame] | 214 | visibleDialog?.dismiss() |
| 215 | visibleDialog = null |
Matt Pietal | 1aac43b | 2020-02-04 15:43:31 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Matt Pietal | 2f63cc0 | 2020-06-19 15:21:13 -0400 | [diff] [blame] | 218 | fun setErrorStatus() { |
| 219 | val text = context.resources.getString(R.string.controls_error_failed) |
Matt Pietal | 370db87 | 2020-06-04 11:13:29 -0400 | [diff] [blame] | 220 | animateStatusChange(/* animated */ true, { |
Matt Pietal | 2f63cc0 | 2020-06-19 15:21:13 -0400 | [diff] [blame] | 221 | setStatusText(text, /* immediately */ true) |
Matt Pietal | 370db87 | 2020-06-04 11:13:29 -0400 | [diff] [blame] | 222 | }) |
Matt Pietal | 307b1ef | 2020-02-10 07:27:01 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Matt Pietal | c879685 | 2020-05-13 15:13:30 -0400 | [diff] [blame] | 225 | private fun updateContentDescription() = |
| 226 | layout.setContentDescription("${title.text} ${subtitle.text} ${status.text}") |
| 227 | |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 228 | fun action(action: ControlAction) { |
Matt Pietal | b7da66c | 2020-03-06 10:49:31 -0500 | [diff] [blame] | 229 | lastAction = action |
Matt Pietal | 313f37d | 2020-02-24 11:27:22 -0500 | [diff] [blame] | 230 | controlsController.action(cws.componentName, cws.ci, action) |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 231 | } |
| 232 | |
Matt Pietal | 733d637 | 2020-06-12 09:36:52 -0400 | [diff] [blame] | 233 | fun usePanel(): Boolean { |
| 234 | return deviceType in ControlViewHolder.FORCE_PANEL_DEVICES || |
| 235 | controlTemplate == ControlTemplate.NO_TEMPLATE |
| 236 | } |
Matt Pietal | dc78c84 | 2020-03-30 08:09:18 -0400 | [diff] [blame] | 237 | |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 238 | fun bindBehavior( |
| 239 | existingBehavior: Behavior?, |
| 240 | clazz: KClass<out Behavior>, |
| 241 | offset: Int = 0 |
| 242 | ): Behavior { |
| 243 | val behavior = if (existingBehavior == null || existingBehavior!!::class != clazz) { |
| 244 | // Behavior changes can signal a change in template from the app or |
| 245 | // first time setup |
| 246 | val newBehavior = clazz.java.newInstance() |
| 247 | newBehavior.initialize(this) |
| 248 | |
| 249 | // let behaviors define their own, if necessary, and clear any existing ones |
| 250 | layout.setAccessibilityDelegate(null) |
| 251 | newBehavior |
| 252 | } else { |
| 253 | existingBehavior |
| 254 | } |
| 255 | |
| 256 | return behavior.also { |
| 257 | it.bind(cws, offset) |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 261 | internal fun applyRenderInfo(enabled: Boolean, offset: Int, animated: Boolean = true) { |
Matt Pietal | 96f746b | 2020-06-08 14:40:00 -0400 | [diff] [blame] | 262 | val deviceTypeOrError = if (controlStatus == Control.STATUS_OK || |
| 263 | controlStatus == Control.STATUS_UNKNOWN) { |
| 264 | deviceType |
| 265 | } else { |
| 266 | RenderInfo.ERROR_ICON |
| 267 | } |
| 268 | val ri = RenderInfo.lookup(context, cws.componentName, deviceTypeOrError, offset) |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 269 | val fg = context.resources.getColorStateList(ri.foreground, context.theme) |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 270 | val newText = nextStatusText |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 271 | val control = cws.control |
| 272 | |
| 273 | var shouldAnimate = animated |
| 274 | if (newText == status.text) { |
| 275 | shouldAnimate = false |
| 276 | } |
| 277 | animateStatusChange(shouldAnimate) { |
| 278 | updateStatusRow(enabled, newText, ri.icon, fg, control) |
| 279 | } |
| 280 | |
| 281 | animateBackgroundChange(shouldAnimate, enabled, ri.enabledBackground) |
| 282 | } |
| 283 | |
| 284 | fun getStatusText() = status.text |
| 285 | |
| 286 | fun setStatusTextSize(textSize: Float) = |
| 287 | status.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize) |
| 288 | |
| 289 | fun setStatusText(text: CharSequence, immediately: Boolean = false) { |
| 290 | if (immediately) { |
| 291 | status.alpha = STATUS_ALPHA_ENABLED |
| 292 | status.text = text |
Matt Pietal | 395723d | 2020-06-12 13:56:48 -0400 | [diff] [blame] | 293 | updateContentDescription() |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 294 | } |
Matt Pietal | 5a5de40 | 2020-06-15 13:29:57 -0400 | [diff] [blame] | 295 | nextStatusText = text |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | private fun animateBackgroundChange( |
| 299 | animated: Boolean, |
| 300 | enabled: Boolean, |
| 301 | @ColorRes bgColor: Int |
| 302 | ) { |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 303 | val bg = context.resources.getColor(R.color.control_default_background, context.theme) |
Matt Pietal | 4f5cd67 | 2020-04-22 13:51:11 -0400 | [diff] [blame] | 304 | var (newClipColor, newAlpha) = if (enabled) { |
| 305 | // allow color overrides for the enabled state only |
| 306 | val color = cws.control?.getCustomColor()?.let { |
| 307 | val state = intArrayOf(android.R.attr.state_enabled) |
| 308 | it.getColorForState(state, it.getDefaultColor()) |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 309 | } ?: context.resources.getColor(bgColor, context.theme) |
Matt Pietal | 4f5cd67 | 2020-04-22 13:51:11 -0400 | [diff] [blame] | 310 | listOf(color, ALPHA_ENABLED) |
Matt Pietal | a103a94 | 2020-03-13 12:10:35 -0400 | [diff] [blame] | 311 | } else { |
Matt Pietal | 4f5cd67 | 2020-04-22 13:51:11 -0400 | [diff] [blame] | 312 | listOf( |
| 313 | context.resources.getColor(R.color.control_default_background, context.theme), |
| 314 | ALPHA_DISABLED |
| 315 | ) |
Matt Pietal | a103a94 | 2020-03-13 12:10:35 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Matt Pietal | a103a94 | 2020-03-13 12:10:35 -0400 | [diff] [blame] | 318 | (clipLayer.getDrawable() as GradientDrawable).apply { |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 319 | val newBaseColor = if (behavior is ToggleRangeBehavior) { |
| 320 | ColorUtils.blendARGB(bg, newClipColor, toggleBackgroundIntensity) |
| 321 | } else { |
| 322 | bg |
| 323 | } |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 324 | stateAnimator?.cancel() |
| 325 | if (animated) { |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 326 | val oldColor = color?.defaultColor ?: newClipColor |
| 327 | val oldBaseColor = baseLayer.color?.defaultColor ?: newBaseColor |
Lucas Dupin | d60b332 | 2020-04-15 18:06:47 -0700 | [diff] [blame] | 328 | val oldAlpha = layout.alpha |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 329 | stateAnimator = ValueAnimator.ofInt(clipLayer.alpha, newAlpha).apply { |
| 330 | addUpdateListener { |
| 331 | alpha = it.animatedValue as Int |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 332 | setColor(ColorUtils.blendARGB(oldColor, newClipColor, it.animatedFraction)) |
| 333 | baseLayer.setColor(ColorUtils.blendARGB(oldBaseColor, |
| 334 | newBaseColor, it.animatedFraction)) |
Matt Pietal | e27ca7c | 2020-05-01 08:29:10 -0400 | [diff] [blame] | 335 | layout.alpha = MathUtils.lerp(oldAlpha, 1f, it.animatedFraction) |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 336 | } |
| 337 | addListener(object : AnimatorListenerAdapter() { |
| 338 | override fun onAnimationEnd(animation: Animator?) { |
| 339 | stateAnimator = null |
| 340 | } |
| 341 | }) |
| 342 | duration = STATE_ANIMATION_DURATION |
| 343 | interpolator = Interpolators.CONTROL_STATE |
| 344 | start() |
| 345 | } |
| 346 | } else { |
| 347 | alpha = newAlpha |
Lucas Dupin | 92f6cca | 2020-04-14 22:30:54 -0700 | [diff] [blame] | 348 | setColor(newClipColor) |
| 349 | baseLayer.setColor(newBaseColor) |
Matt Pietal | e27ca7c | 2020-05-01 08:29:10 -0400 | [diff] [blame] | 350 | layout.alpha = 1f |
Lucas Dupin | 2ee4ec9 | 2020-04-13 19:51:14 -0700 | [diff] [blame] | 351 | } |
Matt Pietal | b582b69 | 2020-02-14 19:37:57 -0500 | [diff] [blame] | 352 | } |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 353 | } |
Matt Pietal | b582b69 | 2020-02-14 19:37:57 -0500 | [diff] [blame] | 354 | |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 355 | private fun animateStatusChange(animated: Boolean, statusRowUpdater: () -> Unit) { |
| 356 | statusAnimator?.cancel() |
| 357 | |
| 358 | if (!animated) { |
| 359 | statusRowUpdater.invoke() |
| 360 | return |
| 361 | } |
| 362 | |
| 363 | if (isLoading) { |
| 364 | statusRowUpdater.invoke() |
| 365 | statusAnimator = ObjectAnimator.ofFloat(status, "alpha", STATUS_ALPHA_DIMMED).apply { |
| 366 | repeatMode = ValueAnimator.REVERSE |
| 367 | repeatCount = ValueAnimator.INFINITE |
| 368 | duration = 500L |
| 369 | interpolator = Interpolators.LINEAR |
| 370 | startDelay = 900L |
| 371 | start() |
| 372 | } |
| 373 | } else { |
| 374 | val fadeOut = ObjectAnimator.ofFloat(status, "alpha", 0f).apply { |
| 375 | duration = 200L |
| 376 | interpolator = Interpolators.LINEAR |
| 377 | addListener(object : AnimatorListenerAdapter() { |
| 378 | override fun onAnimationEnd(animation: Animator?) { |
| 379 | statusRowUpdater.invoke() |
| 380 | } |
| 381 | }) |
| 382 | } |
| 383 | val fadeIn = ObjectAnimator.ofFloat(status, "alpha", STATUS_ALPHA_ENABLED).apply { |
| 384 | duration = 200L |
| 385 | interpolator = Interpolators.LINEAR |
| 386 | } |
| 387 | statusAnimator = AnimatorSet().apply { |
| 388 | playSequentially(fadeOut, fadeIn) |
| 389 | addListener(object : AnimatorListenerAdapter() { |
| 390 | override fun onAnimationEnd(animation: Animator?) { |
| 391 | status.alpha = STATUS_ALPHA_ENABLED |
| 392 | statusAnimator = null |
| 393 | } |
| 394 | }) |
| 395 | start() |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | private fun updateStatusRow( |
| 401 | enabled: Boolean, |
| 402 | text: CharSequence, |
| 403 | drawable: Drawable, |
| 404 | color: ColorStateList, |
| 405 | control: Control? |
| 406 | ) { |
| 407 | setEnabled(enabled) |
| 408 | |
| 409 | status.text = text |
Matt Pietal | 395723d | 2020-06-12 13:56:48 -0400 | [diff] [blame] | 410 | updateContentDescription() |
| 411 | |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 412 | status.setTextColor(color) |
| 413 | |
| 414 | control?.getCustomIcon()?.let { |
| 415 | // do not tint custom icons, assume the intended icon color is correct |
| 416 | icon.imageTintList = null |
| 417 | icon.setImageIcon(it) |
| 418 | } ?: run { |
Matt Pietal | 798d113 | 2020-06-01 09:47:42 -0400 | [diff] [blame] | 419 | if (drawable is StateListDrawable) { |
| 420 | // Only reset the drawable if it is a different resource, as it will interfere |
| 421 | // with the image state and animation. |
| 422 | if (icon.drawable == null || !(icon.drawable is StateListDrawable)) { |
| 423 | icon.setImageDrawable(drawable) |
| 424 | } |
| 425 | val state = if (enabled) ATTR_ENABLED else ATTR_DISABLED |
| 426 | icon.setImageState(state, true) |
| 427 | } else { |
| 428 | icon.setImageDrawable(drawable) |
| 429 | } |
Matt Pietal | dc6bb1d | 2020-05-14 13:33:41 -0400 | [diff] [blame] | 430 | |
| 431 | // do not color app icons |
| 432 | if (deviceType != DeviceTypes.TYPE_ROUTINE) { |
| 433 | icon.imageTintList = color |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Matt Pietal | 53a8bbd | 2020-03-05 16:10:34 -0500 | [diff] [blame] | 438 | private fun setEnabled(enabled: Boolean) { |
Matt Pietal | 2223179 | 2020-01-23 09:51:09 -0500 | [diff] [blame] | 439 | status.setEnabled(enabled) |
| 440 | icon.setEnabled(enabled) |
| 441 | } |
| 442 | } |