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.BlendMode
20 import android.graphics.BlendModeColorFilter
21 import android.graphics.drawable.ClipDrawable
22 import android.graphics.drawable.LayerDrawable
23 import android.view.View
24 import android.service.controls.Control
25 import android.service.controls.templates.ThumbnailTemplate
26 import android.util.TypedValue
27 
28 import com.android.systemui.R
29 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MAX_LEVEL
30 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MIN_LEVEL
31 
32 /**
33  * Supports display of static images on the background of the tile. When marked active, the title
34  * and subtitle will not be visible. To be used with {@link Thumbnailtemplate} only.
35  */
36 class ThumbnailBehavior : Behavior {
37     lateinit var template: ThumbnailTemplate
38     lateinit var control: Control
39     lateinit var cvh: ControlViewHolder
40     private var shadowOffsetX: Float = 0f
41     private var shadowOffsetY: Float = 0f
42     private var shadowRadius: Float = 0f
43     private var shadowColor: Int = 0
44 
45     private val enabled: Boolean
46         get() = template.isActive()
47 
48     override fun initialize(cvh: ControlViewHolder) {
49         this.cvh = cvh
50 
51         val outValue = TypedValue()
52         cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_x, outValue, true)
53         shadowOffsetX = outValue.getFloat()
54 
55         cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_y, outValue, true)
56         shadowOffsetY = outValue.getFloat()
57 
58         cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_radius, outValue, true)
59         shadowRadius = outValue.getFloat()
60 
61         shadowColor = cvh.context.resources.getColor(R.color.control_thumbnail_shadow_color)
62         cvh.layout.setOnClickListener(View.OnClickListener() {
63             cvh.controlActionCoordinator.touch(cvh, template.getTemplateId(), control)
64         })
65     }
66 
67     override fun bind(cws: ControlWithState, colorOffset: Int) {
68         this.control = cws.control!!
69         cvh.setStatusText(control.getStatusText())
70         template = control.getControlTemplate() as ThumbnailTemplate
71 
72         val ld = cvh.layout.getBackground() as LayerDrawable
73         val clipLayer = ld.findDrawableByLayerId(R.id.clip_layer) as ClipDrawable
74 
75         clipLayer.setLevel(if (enabled) MAX_LEVEL else MIN_LEVEL)
76 
77         if (template.isActive()) {
78             cvh.title.visibility = View.INVISIBLE
79             cvh.subtitle.visibility = View.INVISIBLE
80             cvh.status.setShadowLayer(shadowOffsetX, shadowOffsetY, shadowRadius, shadowColor)
81 
82             cvh.bgExecutor.execute {
83                 val drawable = template.getThumbnail().loadDrawable(cvh.context)
84                 cvh.uiExecutor.execute {
85                     val radius = cvh.context.getResources()
86                         .getDimensionPixelSize(R.dimen.control_corner_radius).toFloat()
87                     clipLayer.setDrawable(CornerDrawable(drawable, radius))
88                     clipLayer.setColorFilter(BlendModeColorFilter(cvh.context.resources
89                         .getColor(R.color.control_thumbnail_tint), BlendMode.LUMINOSITY))
90                     cvh.applyRenderInfo(enabled, colorOffset)
91                 }
92             }
93         } else {
94             cvh.title.visibility = View.VISIBLE
95             cvh.subtitle.visibility = View.VISIBLE
96             cvh.status.setShadowLayer(0f, 0f, 0f, shadowColor)
97         }
98 
99         cvh.applyRenderInfo(enabled, colorOffset)
100     }
101 }
102