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.TemperatureControlTemplate 26 import android.service.controls.templates.ThumbnailTemplate 27 import android.util.TypedValue 28 29 import com.android.systemui.res.R 30 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MAX_LEVEL 31 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MIN_LEVEL 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) : 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(View.OnClickListener() { 65 cvh.controlActionCoordinator.touch(cvh, template.getTemplateId(), control) 66 }) 67 } 68 bindnull69 override fun bind(cws: ControlWithState, colorOffset: Int) { 70 this.control = cws.control!! 71 cvh.setStatusText(control.getStatusText()) 72 template = control.controlTemplate as? ThumbnailTemplate 73 ?: (control.controlTemplate as TemperatureControlTemplate).template 74 as ThumbnailTemplate 75 76 val ld = cvh.layout.getBackground() as LayerDrawable 77 val clipLayer = ld.findDrawableByLayerId(R.id.clip_layer) as ClipDrawable 78 79 clipLayer.setLevel(if (enabled) MAX_LEVEL else MIN_LEVEL) 80 81 if (template.isActive()) { 82 cvh.title.visibility = View.INVISIBLE 83 cvh.subtitle.visibility = View.INVISIBLE 84 cvh.status.setShadowLayer(shadowOffsetX, shadowOffsetY, shadowRadius, shadowColor) 85 86 cvh.bgExecutor.execute { 87 val drawable = template.thumbnail 88 ?.takeIf(canUseIconPredicate) 89 ?.loadDrawable(cvh.context) 90 cvh.uiExecutor.execute { 91 val radius = cvh.context.getResources() 92 .getDimensionPixelSize(R.dimen.control_corner_radius).toFloat() 93 // TODO(b/290037843): Add a placeholder 94 drawable?.let { 95 clipLayer.drawable = CornerDrawable(it, radius) 96 } 97 clipLayer.setColorFilter(BlendModeColorFilter(cvh.context.resources 98 .getColor(R.color.control_thumbnail_tint), BlendMode.LUMINOSITY)) 99 cvh.applyRenderInfo(enabled, colorOffset) 100 } 101 } 102 } else { 103 cvh.title.visibility = View.VISIBLE 104 cvh.subtitle.visibility = View.VISIBLE 105 cvh.status.setShadowLayer(0f, 0f, 0f, shadowColor) 106 } 107 108 cvh.applyRenderInfo(enabled, colorOffset) 109 } 110 } 111