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(currentUserId: Int) : 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 canUseIconPredicate = CanUseIconPredicate(currentUserId) 46 private val enabled: Boolean 47 get() = template.isActive() 48 49 override fun initialize(cvh: ControlViewHolder) { 50 this.cvh = cvh 51 52 val outValue = TypedValue() 53 cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_x, outValue, true) 54 shadowOffsetX = outValue.getFloat() 55 56 cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_y, outValue, true) 57 shadowOffsetY = outValue.getFloat() 58 59 cvh.context.resources.getValue(R.dimen.controls_thumbnail_shadow_radius, outValue, true) 60 shadowRadius = outValue.getFloat() 61 62 shadowColor = cvh.context.resources.getColor(R.color.control_thumbnail_shadow_color) 63 cvh.layout.setOnClickListener(View.OnClickListener() { 64 cvh.controlActionCoordinator.touch(cvh, template.getTemplateId(), control) 65 }) 66 } 67 68 override fun bind(cws: ControlWithState, colorOffset: Int) { 69 this.control = cws.control!! 70 cvh.setStatusText(control.getStatusText()) 71 template = control.getControlTemplate() as ThumbnailTemplate 72 73 val ld = cvh.layout.getBackground() as LayerDrawable 74 val clipLayer = ld.findDrawableByLayerId(R.id.clip_layer) as ClipDrawable 75 76 clipLayer.setLevel(if (enabled) MAX_LEVEL else MIN_LEVEL) 77 78 if (template.isActive()) { 79 cvh.title.visibility = View.INVISIBLE 80 cvh.subtitle.visibility = View.INVISIBLE 81 cvh.status.setShadowLayer(shadowOffsetX, shadowOffsetY, shadowRadius, shadowColor) 82 83 cvh.bgExecutor.execute { 84 val drawable = template.thumbnail 85 ?.takeIf(canUseIconPredicate) 86 ?.loadDrawable(cvh.context) 87 cvh.uiExecutor.execute { 88 val radius = cvh.context.getResources() 89 .getDimensionPixelSize(R.dimen.control_corner_radius).toFloat() 90 // TODO(b/290037843): Add a placeholder 91 drawable?.let { 92 clipLayer.drawable = CornerDrawable(it, radius) 93 } 94 clipLayer.setColorFilter(BlendModeColorFilter(cvh.context.resources 95 .getColor(R.color.control_thumbnail_tint), BlendMode.LUMINOSITY)) 96 cvh.applyRenderInfo(enabled, colorOffset) 97 } 98 } 99 } else { 100 cvh.title.visibility = View.VISIBLE 101 cvh.subtitle.visibility = View.VISIBLE 102 cvh.status.setShadowLayer(0f, 0f, 0f, shadowColor) 103 } 104 105 cvh.applyRenderInfo(enabled, colorOffset) 106 } 107 } 108