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

Commit be8f2fb3 authored by Anton Potapov's avatar Anton Potapov
Browse files

Parse slice to determine if SliceAndroidView should be clickable

Flag: aconfig new_volume_panel NEXTFOOD
Fixes: 333835509
Test: manual on the phone with a compatible headset. Open the panel when
the ANC button shouldn't be clickable

Change-Id: If8368481220de7334a9f6faaf469e19754e88c1c
parent 72c37464
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -54,6 +54,13 @@ constructor(
    override fun VolumePanelComposeScope.Content(modifier: Modifier) {
        val slice by viewModel.buttonSlice.collectAsState()
        val label = stringResource(R.string.volume_panel_noise_control_title)
        val isClickable = viewModel.isClickable(slice)
        val onClick =
            if (isClickable) {
                { ancPopup.show(null) }
            } else {
                null
            }
        Column(
            modifier = modifier,
            verticalArrangement = Arrangement.spacedBy(12.dp),
@@ -69,8 +76,9 @@ constructor(
                        }
                        .clip(RoundedCornerShape(28.dp)),
                slice = slice,
                isEnabled = onClick != null,
                onWidthChanged = viewModel::onButtonSliceWidthChanged,
                onClick = { ancPopup.show(null) }
                onClick = onClick,
            )
            Text(
                modifier = Modifier.clearAndSetSemantics {},
+10 −9
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.systemui.res.R
fun SliceAndroidView(
    slice: Slice?,
    modifier: Modifier = Modifier,
    isEnabled: Boolean = true,
    onWidthChanged: ((Int) -> Unit)? = null,
    onClick: (() -> Unit)? = null,
) {
@@ -40,7 +41,6 @@ fun SliceAndroidView(
        factory = { context: Context ->
            ClickableSliceView(
                    ContextThemeWrapper(context, R.style.Widget_SliceView_VolumePanel),
                    onClick,
                )
                .apply {
                    mode = SliceView.MODE_LARGE
@@ -50,12 +50,14 @@ fun SliceAndroidView(
                    if (onWidthChanged != null) {
                        addOnLayoutChangeListener(OnWidthChangedLayoutListener(onWidthChanged))
                    }
                    if (onClick != null) {
                        setOnClickListener { onClick() }
                    }
                }
        },
        update = { sliceView: SliceView -> sliceView.slice = slice }
        update = { sliceView: ClickableSliceView ->
            sliceView.slice = slice
            sliceView.onClick = onClick
            sliceView.isEnabled = isEnabled
            sliceView.isClickable = isEnabled
        }
    )
}

@@ -86,10 +88,9 @@ class OnWidthChangedLayoutListener(private val widthChanged: (Int) -> Unit) :
 * first.
 */
@SuppressLint("ViewConstructor") // only used in this class
private class ClickableSliceView(
    context: Context,
    private val onClick: (() -> Unit)?,
) : SliceView(context) {
private class ClickableSliceView(context: Context) : SliceView(context) {

    var onClick: (() -> Unit)? = null

    init {
        if (onClick != null) {
+27 −0
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

package com.android.systemui.volume.panel.component.anc.ui.viewmodel

import android.content.Intent
import androidx.slice.Slice
import androidx.slice.SliceItem
import com.android.systemui.volume.panel.component.anc.domain.AncAvailabilityCriteria
import com.android.systemui.volume.panel.component.anc.domain.interactor.AncSliceInteractor
import com.android.systemui.volume.panel.component.anc.domain.model.AncSlices
@@ -59,6 +61,31 @@ constructor(
            .map { it.buttonSlice }
            .stateIn(coroutineScope, SharingStarted.Eagerly, null)

    fun isClickable(slice: Slice?): Boolean {
        slice ?: return false
        val slices = ArrayDeque<SliceItem>()
        slices.addAll(slice.items)
        while (slices.isNotEmpty()) {
            val item: SliceItem = slices.removeFirst()
            when (item.format) {
                android.app.slice.SliceItem.FORMAT_ACTION -> {
                    val itemActionIntent: Intent? = item.action?.intent
                    if (itemActionIntent?.hasExtra(EXTRA_ANC_ENABLED) == true) {
                        return itemActionIntent.getBooleanExtra(EXTRA_ANC_ENABLED, true)
                    }
                }
                android.app.slice.SliceItem.FORMAT_SLICE -> {
                    item.slice?.items?.let(slices::addAll)
                }
            }
        }
        return true
    }

    private companion object {
        const val EXTRA_ANC_ENABLED = "EXTRA_ANC_ENABLED"
    }

    /** Call this to update [popupSlice] width in a reaction to container size change. */
    fun onPopupSliceWidthChanged(width: Int) {
        interactor.onPopupSliceWidthChanged(width)