• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.service.controls.Control
24 import android.service.controls.templates.TemperatureControlTemplate
25 import android.service.controls.templates.ThumbnailTemplate
26 import android.util.TypedValue
27 import android.view.View
28 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MAX_LEVEL
29 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MIN_LEVEL
30 import com.android.systemui.res.R
31 import com.android.systemui.utils.SafeIconLoader
32 
33 /**
34  * Supports display of static images on the background of the tile. When marked active, the title
35  * and subtitle will not be visible. To be used with {@link Thumbnailtemplate} only.
36  */
37 class ThumbnailBehavior(currentUserId: Int, private val safeIconLoader: SafeIconLoader) : Behavior {
38     lateinit var template: ThumbnailTemplate
39     lateinit var control: Control
40     lateinit var cvh: ControlViewHolder
41     private var shadowOffsetX: Float = 0f
42     private var shadowOffsetY: Float = 0f
43     private var shadowRadius: Float = 0f
44     private var shadowColor: Int = 0
45 
46     private val canUseIconPredicate = CanUseIconPredicate(currentUserId)
47     private val enabled: Boolean
48         get() = template.isActive()
49 
initializenull50     override fun initialize(cvh: ControlViewHolder) {
51         this.cvh = cvh
52 
53         val outValue = TypedValue()
54         cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_x, outValue, true)
55         shadowOffsetX = outValue.getFloat()
56 
57         cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_y, outValue, true)
58         shadowOffsetY = outValue.getFloat()
59 
60         cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_radius, outValue, true)
61         shadowRadius = outValue.getFloat()
62 
63         shadowColor = cvh.context.resources.getColor(R.color.control_thumbnail_shadow_color)
64         cvh.layout.setOnClickListener(
65             View.OnClickListener() {
66                 cvh.controlActionCoordinator.touch(cvh, template.getTemplateId(), control)
67             }
68         )
69     }
70 
bindnull71     override fun bind(cws: ControlWithState, colorOffset: Int) {
72         this.control = cws.control!!
73         cvh.setStatusText(control.getStatusText())
74         template =
75             control.controlTemplate as? ThumbnailTemplate
76                 ?: (control.controlTemplate as TemperatureControlTemplate).template
77                     as ThumbnailTemplate
78 
79         val ld = cvh.layout.getBackground() as LayerDrawable
80         val clipLayer = ld.findDrawableByLayerId(R.id.clip_layer) as ClipDrawable
81 
82         clipLayer.setLevel(if (enabled) MAX_LEVEL else MIN_LEVEL)
83 
84         if (template.isActive()) {
85             cvh.title.visibility = View.INVISIBLE
86             cvh.subtitle.visibility = View.INVISIBLE
87             cvh.status.setShadowLayer(shadowOffsetX, shadowOffsetY, shadowRadius, shadowColor)
88 
89             cvh.bgExecutor.execute {
90                 val drawable =
91                     template.thumbnail.takeIf(canUseIconPredicate)?.let { safeIconLoader.load(it) }
92                 cvh.uiExecutor.execute {
93                     val radius =
94                         cvh.context
95                             .getResources()
96                             .getDimensionPixelSize(R.dimen.control_corner_radius)
97                             .toFloat()
98                     // TODO(b/290037843): Add a placeholder
99                     drawable?.let { clipLayer.drawable = CornerDrawable(it, radius) }
100                     clipLayer.setColorFilter(
101                         BlendModeColorFilter(
102                             cvh.context.resources.getColor(R.color.control_thumbnail_tint),
103                             BlendMode.LUMINOSITY,
104                         )
105                     )
106                     cvh.applyRenderInfo(enabled, colorOffset)
107                 }
108             }
109         } else {
110             cvh.title.visibility = View.VISIBLE
111             cvh.subtitle.visibility = View.VISIBLE
112             cvh.status.setShadowLayer(0f, 0f, 0f, shadowColor)
113         }
114 
115         cvh.applyRenderInfo(enabled, colorOffset)
116     }
117 }
118