blob: 2795c7af6c828f1a0039c937c146db258493d139 [file] [log] [blame]
Matt Pietala635bd12020-02-03 13:36:18 -05001/*
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
17package com.android.systemui.controls.ui
18
Matt Pietala635bd12020-02-03 13:36:18 -050019import android.graphics.drawable.Drawable
20import android.graphics.drawable.LayerDrawable
21import android.service.controls.Control
Matt Pietalcf516132020-05-07 16:50:04 -040022import android.service.controls.templates.ControlTemplate
Matt Pietala635bd12020-02-03 13:36:18 -050023import android.service.controls.templates.TemperatureControlTemplate
Matt Pietala635bd12020-02-03 13:36:18 -050024
25import com.android.systemui.R
Matt Pietal677981d2020-04-24 14:38:32 -040026import com.android.systemui.controls.ui.ControlViewHolder.Companion.MIN_LEVEL
27import com.android.systemui.controls.ui.ControlViewHolder.Companion.MAX_LEVEL
Matt Pietala635bd12020-02-03 13:36:18 -050028
29class TemperatureControlBehavior : Behavior {
30 lateinit var clipLayer: Drawable
31 lateinit var control: Control
32 lateinit var cvh: ControlViewHolder
Matt Pietalcf516132020-05-07 16:50:04 -040033 var subBehavior: Behavior? = null
Matt Pietala635bd12020-02-03 13:36:18 -050034
Matt Pietalb582b692020-02-14 19:37:57 -050035 override fun initialize(cvh: ControlViewHolder) {
Matt Pietala635bd12020-02-03 13:36:18 -050036 this.cvh = cvh
Matt Pietalb582b692020-02-14 19:37:57 -050037 }
Matt Pietala635bd12020-02-03 13:36:18 -050038
Matt Pietalcf516132020-05-07 16:50:04 -040039 override fun bind(cws: ControlWithState, colorOffset: Int) {
Matt Pietalb582b692020-02-14 19:37:57 -050040 this.control = cws.control!!
41
42 cvh.status.setText(control.getStatusText())
Matt Pietala635bd12020-02-03 13:36:18 -050043
44 val ld = cvh.layout.getBackground() as LayerDrawable
45 clipLayer = ld.findDrawableByLayerId(R.id.clip_layer)
46
Matt Pietalcf516132020-05-07 16:50:04 -040047 val template = control.getControlTemplate() as TemperatureControlTemplate
Matt Pietala635bd12020-02-03 13:36:18 -050048 val activeMode = template.getCurrentActiveMode()
Matt Pietalcf516132020-05-07 16:50:04 -040049 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 Pietala635bd12020-02-03 13:36:18 -050074 }
75}