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.view.View 22 import android.service.controls.Control 23 import android.service.controls.templates.ControlTemplate 24 import android.service.controls.templates.StatelessTemplate 25 26 import com.android.systemui.res.R 27 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MAX_LEVEL 28 import com.android.systemui.controls.ui.ControlViewHolder.Companion.MIN_LEVEL 29 30 /** 31 * Supports touch events, but has no notion of state as the {@link ToggleBehavior} does. Must be 32 * used with {@link StatelessTemplate}. 33 */ 34 class TouchBehavior : Behavior { 35 lateinit var clipLayer: Drawable 36 lateinit var template: ControlTemplate 37 lateinit var control: Control 38 lateinit var cvh: ControlViewHolder 39 private var statelessTouch = false 40 private var lastColorOffset = 0 41 private val enabled: Boolean 42 get() = if (lastColorOffset > 0 || statelessTouch) true else false 43 44 companion object { 45 const val STATELESS_ENABLE_TIMEOUT_IN_MILLIS = 3000L 46 } 47 initializenull48 override fun initialize(cvh: ControlViewHolder) { 49 this.cvh = cvh 50 51 cvh.layout.setOnClickListener(View.OnClickListener() { 52 cvh.controlActionCoordinator.touch(cvh, template.getTemplateId(), control) 53 54 // StatelessTemplates have no state, with no way to discern between enabled and 55 // disabled. Render an enabled state for a few moments to let the user know the 56 // action is in progress. 57 if (template is StatelessTemplate) { 58 statelessTouch = true 59 cvh.applyRenderInfo(enabled, lastColorOffset) 60 cvh.uiExecutor.executeDelayed({ 61 statelessTouch = false 62 cvh.applyRenderInfo(enabled, lastColorOffset) 63 }, STATELESS_ENABLE_TIMEOUT_IN_MILLIS) 64 } 65 }) 66 } 67 bindnull68 override fun bind(cws: ControlWithState, colorOffset: Int) { 69 this.control = cws.control!! 70 lastColorOffset = colorOffset 71 cvh.setStatusText(control.getStatusText()) 72 template = control.getControlTemplate() 73 74 val ld = cvh.layout.getBackground() as LayerDrawable 75 clipLayer = ld.findDrawableByLayerId(R.id.clip_layer) 76 77 clipLayer.setLevel(if (enabled) MAX_LEVEL else MIN_LEVEL) 78 cvh.applyRenderInfo(enabled, colorOffset) 79 } 80 } 81