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 
19 import android.graphics.drawable.Drawable
20 import android.graphics.drawable.LayerDrawable
21 import android.service.controls.Control
22 import android.service.controls.templates.TemperatureControlTemplate
23 import android.service.controls.templates.ToggleTemplate
24 import android.util.Log
25 import android.view.View
26 import com.android.systemui.res.R
27 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MAX_LEVEL
28 
29 class ToggleBehavior : Behavior {
30     lateinit var clipLayer: Drawable
31     lateinit var template: ToggleTemplate
32     lateinit var control: Control
33     lateinit var cvh: ControlViewHolder
34 
initializenull35     override fun initialize(cvh: ControlViewHolder) {
36         this.cvh = cvh
37 
38         cvh.layout.setOnClickListener(View.OnClickListener() {
39             cvh.controlActionCoordinator.toggle(cvh, template.getTemplateId(), template.isChecked())
40         })
41     }
42 
bindnull43     override fun bind(cws: ControlWithState, colorOffset: Int) {
44         this.control = cws.control!!
45 
46         cvh.setStatusText(control.getStatusText())
47         val controlTemplate = control.getControlTemplate()
48         template = when (controlTemplate) {
49             is ToggleTemplate -> controlTemplate
50             is TemperatureControlTemplate -> controlTemplate.getTemplate() as ToggleTemplate
51             else -> {
52                 Log.e(ControlsUiController.TAG, "Unsupported template type: $controlTemplate")
53                 return
54             }
55         }
56 
57         val ld = cvh.layout.getBackground() as LayerDrawable
58         clipLayer = ld.findDrawableByLayerId(R.id.clip_layer)
59         clipLayer.level = MAX_LEVEL
60 
61         val checked = template.isChecked()
62         cvh.applyRenderInfo(checked, colorOffset)
63     }
64 }
65