Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f78246d authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Controls UI - Reuse behaviors"

parents 5ff35689 b582b693
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -16,6 +16,20 @@

package com.android.systemui.controls.ui

/**
 * All controls need to respond to changes in state and handle user-generated events.
 * Implementations of this interface provide these different means by adding their own
 * event handlers, and will update the control ui as they see fit.
 */
interface Behavior {
    fun apply(cvh: ControlViewHolder, cws: ControlWithState)

    /**
     * Only called once per instance
     */
    fun initialize(cvh: ControlViewHolder)

    /**
     * Will be invoked on every update provided to the Control
     */
    fun bind(cws: ControlWithState)
}
+23 −17
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ import com.android.systemui.controls.controller.ControlsController
import com.android.systemui.util.concurrency.DelayableExecutor
import com.android.systemui.R

import kotlin.reflect.KClass

private const val UPDATE_DELAY_IN_MILLIS = 3000L

class ControlViewHolder(
@@ -54,6 +56,7 @@ class ControlViewHolder(
    val clipLayer: ClipDrawable
    lateinit var cws: ControlWithState
    var cancelUpdate: Runnable? = null
    var behavior: Behavior? = null

    init {
        val ld = layout.getBackground() as LayerDrawable
@@ -83,7 +86,14 @@ class ControlViewHolder(
            })
        }

        findBehavior(status, template).apply(this, cws)
        val clazz = findBehavior(status, template)
        if (behavior == null || behavior!!::class != clazz) {
            // Behavior changes can signal a change in template from the app or
            // first time setup
            behavior = clazz.java.newInstance()
            behavior?.initialize(this)
        }
        behavior?.bind(cws)
    }

    fun actionResponse(@ControlAction.ResponseResult response: Int) {
@@ -115,21 +125,14 @@ class ControlViewHolder(
        controlsController.action(cws.ci, action)
    }

    private fun findBehavior(status: Int, template: ControlTemplate): Behavior {
    private fun findBehavior(status: Int, template: ControlTemplate): KClass<out Behavior> {
        return when {
            status == Control.STATUS_UNKNOWN -> UnknownBehavior()
            template is ToggleTemplate -> ToggleBehavior()
            template is ToggleRangeTemplate -> ToggleRangeBehavior()
            template is TemperatureControlTemplate -> TemperatureControlBehavior()
            template is ThumbnailTemplate -> StaticBehavior(uiExecutor, bgExecutor)
            else -> {
                object : Behavior {
                    override fun apply(cvh: ControlViewHolder, cws: ControlWithState) {
                        cvh.status.setText(cws.control?.getStatusText())
                        cvh.applyRenderInfo(RenderInfo.lookup(cws.ci.deviceType, false))
                    }
                }
            }
            status == Control.STATUS_UNKNOWN -> UnknownBehavior::class
            template is ToggleTemplate -> ToggleBehavior::class
            template is ToggleRangeTemplate -> ToggleRangeBehavior::class
            template is TemperatureControlTemplate -> TemperatureControlBehavior::class
            template is ThumbnailTemplate -> StaticBehavior::class
            else -> DefaultBehavior::class
        }
    }

@@ -142,9 +145,12 @@ class ControlViewHolder(
        icon.setImageIcon(Icon.createWithResource(context, ri.iconResourceId))
        icon.setImageTintList(fg)

        clipLayer.getDrawable().setTintBlendMode(BlendMode.HUE)
        clipLayer.getDrawable().setTintList(bg)
        clipLayer.getDrawable().apply {
            setTintBlendMode(BlendMode.HUE)
            setTintList(bg)
        }
    }

    fun setEnabled(enabled: Boolean) {
        status.setEnabled(enabled)
        icon.setEnabled(enabled)
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.controls.ui

class DefaultBehavior : Behavior {
    lateinit var cvh: ControlViewHolder

    override fun initialize(cvh: ControlViewHolder) {
        this.cvh = cvh
    }

    override fun bind(cws: ControlWithState) {
        cvh.status.setText(cws.control?.getStatusText() ?: "")
        cvh.setEnabled(false)
        cvh.applyRenderInfo(RenderInfo.lookup(cws.ci.deviceType, false))
    }
}
+9 −9
Original line number Diff line number Diff line
@@ -24,20 +24,20 @@ import android.service.controls.templates.ThumbnailTemplate
import com.android.systemui.R
import com.android.systemui.controls.ui.ControlActionCoordinator.MAX_LEVEL

import java.util.concurrent.Executor

/**
 * Used for controls that cannot be interacted with. Information is presented to the user
 * but no actions can be taken. If using a ThumbnailTemplate, the background image will
 * be changed.
 */
class StaticBehavior(
    val uiExecutor: Executor,
    val bgExecutor: Executor
) : Behavior {
class StaticBehavior() : Behavior {
    lateinit var control: Control
    lateinit var cvh: ControlViewHolder

    override fun initialize(cvh: ControlViewHolder) {
        this.cvh = cvh
    }

    override fun apply(cvh: ControlViewHolder, cws: ControlWithState) {
    override fun bind(cws: ControlWithState) {
        this.control = cws.control!!

        cvh.status.setText(control.getStatusText())
@@ -51,12 +51,12 @@ class StaticBehavior(

        val template = control.getControlTemplate()
        if (template is ThumbnailTemplate) {
            bgExecutor.execute {
            cvh.bgExecutor.execute {
                // clear the default tinting in favor of only using alpha
                val drawable = template.getThumbnail().loadDrawable(cvh.context)
                drawable.setTintList(null)
                drawable.setAlpha((0.45 * 255).toInt())
                uiExecutor.execute {
                cvh.uiExecutor.execute {
                    val radius = cvh.context.getResources()
                            .getDimensionPixelSize(R.dimen.control_corner_radius).toFloat()
                    clipLayer.setDrawable(CornerDrawable(drawable, radius))
+6 −8
Original line number Diff line number Diff line
@@ -16,12 +16,10 @@

package com.android.systemui.controls.ui

import android.content.Context
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.service.controls.Control
import android.service.controls.templates.TemperatureControlTemplate
import android.widget.TextView

import com.android.systemui.R
import com.android.systemui.controls.ui.ControlActionCoordinator.MIN_LEVEL
@@ -32,15 +30,15 @@ class TemperatureControlBehavior : Behavior {
    lateinit var control: Control
    lateinit var cvh: ControlViewHolder
    lateinit var template: TemperatureControlTemplate
    lateinit var status: TextView
    lateinit var context: Context

    override fun apply(cvh: ControlViewHolder, cws: ControlWithState) {
        this.control = cws.control!!
    override fun initialize(cvh: ControlViewHolder) {
        this.cvh = cvh
        status = cvh.status
    }

    override fun bind(cws: ControlWithState) {
        this.control = cws.control!!

        status.setText(control.getStatusText())
        cvh.status.setText(control.getStatusText())

        val ld = cvh.layout.getBackground() as LayerDrawable
        clipLayer = ld.findDrawableByLayerId(R.id.clip_layer)
Loading