Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -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 | |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 19 | import android.graphics.drawable.Drawable |
| 20 | import android.graphics.drawable.LayerDrawable |
| 21 | import android.service.controls.Control |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 22 | import android.service.controls.templates.ControlTemplate |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 23 | import android.service.controls.templates.TemperatureControlTemplate |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 24 | |
| 25 | import com.android.systemui.R |
Matt Pietal | 677981d | 2020-04-24 14:38:32 -0400 | [diff] [blame] | 26 | import com.android.systemui.controls.ui.ControlViewHolder.Companion.MIN_LEVEL |
| 27 | import com.android.systemui.controls.ui.ControlViewHolder.Companion.MAX_LEVEL |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 28 | |
| 29 | class TemperatureControlBehavior : Behavior { |
| 30 | lateinit var clipLayer: Drawable |
| 31 | lateinit var control: Control |
| 32 | lateinit var cvh: ControlViewHolder |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 33 | var subBehavior: Behavior? = null |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 34 | |
Matt Pietal | b582b69 | 2020-02-14 19:37:57 -0500 | [diff] [blame] | 35 | override fun initialize(cvh: ControlViewHolder) { |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 36 | this.cvh = cvh |
Matt Pietal | b582b69 | 2020-02-14 19:37:57 -0500 | [diff] [blame] | 37 | } |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 38 | |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 39 | override fun bind(cws: ControlWithState, colorOffset: Int) { |
Matt Pietal | b582b69 | 2020-02-14 19:37:57 -0500 | [diff] [blame] | 40 | this.control = cws.control!! |
| 41 | |
| 42 | cvh.status.setText(control.getStatusText()) |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 43 | |
| 44 | val ld = cvh.layout.getBackground() as LayerDrawable |
| 45 | clipLayer = ld.findDrawableByLayerId(R.id.clip_layer) |
| 46 | |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 47 | val template = control.getControlTemplate() as TemperatureControlTemplate |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 48 | val activeMode = template.getCurrentActiveMode() |
Matt Pietal | cf51613 | 2020-05-07 16:50:04 -0400 | [diff] [blame] | 49 | val subTemplate = template.getTemplate() |
| 50 | if (subTemplate == ControlTemplate.getNoTemplateObject() || |
| 51 | subTemplate == ControlTemplate.getErrorTemplate()) { |
| 52 | // No sub template is specified, apply a default look with basic touch interaction. |
| 53 | // Treat an error as no template. |
| 54 | val enabled = activeMode != 0 && activeMode != TemperatureControlTemplate.MODE_OFF |
| 55 | clipLayer.setLevel(if (enabled) MAX_LEVEL else MIN_LEVEL) |
| 56 | cvh.applyRenderInfo(enabled, activeMode) |
| 57 | |
| 58 | cvh.layout.setOnClickListener { _ -> |
| 59 | cvh.controlActionCoordinator.touch(cvh, template.getTemplateId(), control) |
| 60 | } |
| 61 | } else { |
| 62 | // A sub template has been specified, use this as the default behavior for user |
| 63 | // interactions (touch, range) |
| 64 | subBehavior = cvh.bindBehavior( |
| 65 | subBehavior, |
| 66 | ControlViewHolder.findBehaviorClass( |
| 67 | control.status, |
| 68 | subTemplate, |
| 69 | control.deviceType |
| 70 | ), |
| 71 | activeMode |
| 72 | ) |
| 73 | } |
Matt Pietal | a635bd1 | 2020-02-03 13:36:18 -0500 | [diff] [blame] | 74 | } |
| 75 | } |