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

Commit 24667c41 authored by Andreas Agvard's avatar Andreas Agvard Committed by Android (Google) Code Review
Browse files

Merge "Implement handling of setUiHints action "set_invocation_effect_parameters"" into main

parents 5480dd20 0ea13451
Loading
Loading
Loading
Loading
+28 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.topwindoweffects.data.repository

import android.os.Bundle
import android.os.Handler
import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
@@ -23,6 +24,7 @@ import android.provider.Settings.Global.POWER_BUTTON_LONG_PRESS
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.assist.AssistManager
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
@@ -39,6 +41,12 @@ import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.MockitoAnnotations

private fun createAssistantSettingBundle(enableAssistantSetting: Boolean) =
    Bundle().apply {
        putString(AssistManager.ACTION_KEY, SET_INVOCATION_EFFECT_PARAMETERS_ACTION)
        putBoolean(IS_INVOCATION_EFFECT_ENABLED_KEY, enableAssistantSetting)
    }

@SmallTest
@RunWith(AndroidJUnit4::class)
class SqueezeEffectRepositoryTest : SysuiTestCase() {
@@ -65,8 +73,11 @@ class SqueezeEffectRepositoryTest : SysuiTestCase() {

    @DisableFlags(Flags.FLAG_ENABLE_LPP_ASSIST_INVOCATION_EFFECT)
    @Test
    fun testSqueezeEffectDisabled_WhenFlagDisabled() =
    fun testSqueezeEffectDisabled_WhenOtherwiseEnabled_FlagDisabled() =
        kosmos.runTest {
            globalSettings.putInt(POWER_BUTTON_LONG_PRESS, 5)
            underTest.tryHandleSetUiHints(createAssistantSettingBundle(true))

            val isSqueezeEffectEnabled by collectLastValue(underTest.isSqueezeEffectEnabled)

            assertThat(isSqueezeEffectEnabled).isFalse()
@@ -74,8 +85,9 @@ class SqueezeEffectRepositoryTest : SysuiTestCase() {

    @EnableFlags(Flags.FLAG_ENABLE_LPP_ASSIST_INVOCATION_EFFECT)
    @Test
    fun testSqueezeEffectDisabled_WhenFlagEnabled_GlobalSettingsDisabled() =
    fun testSqueezeEffectDisabled_WhenOtherwiseEnabled_GlobalSettingDisabled() =
        kosmos.runTest {
            underTest.tryHandleSetUiHints(createAssistantSettingBundle(true))
            globalSettings.putInt(POWER_BUTTON_LONG_PRESS, 0)

            val isSqueezeEffectEnabled by collectLastValue(underTest.isSqueezeEffectEnabled)
@@ -85,9 +97,22 @@ class SqueezeEffectRepositoryTest : SysuiTestCase() {

    @EnableFlags(Flags.FLAG_ENABLE_LPP_ASSIST_INVOCATION_EFFECT)
    @Test
    fun testSqueezeEffectEnabled_WhenFlagEnabled_GlobalSettingEnabled() =
    fun testSqueezeEffectDisabled_WhenOtherwiseEnabled_AssistantSettingDisabled() =
        kosmos.runTest {
            globalSettings.putInt(POWER_BUTTON_LONG_PRESS, 5)
            underTest.tryHandleSetUiHints(createAssistantSettingBundle(false))

            val isSqueezeEffectEnabled by collectLastValue(underTest.isSqueezeEffectEnabled)

            assertThat(isSqueezeEffectEnabled).isFalse()
        }

    @EnableFlags(Flags.FLAG_ENABLE_LPP_ASSIST_INVOCATION_EFFECT)
    @Test
    fun testSqueezeEffectEnabled_AllSettingsEnabled() =
        kosmos.runTest {
            globalSettings.putInt(POWER_BUTTON_LONG_PRESS, 5)
            underTest.tryHandleSetUiHints(createAssistantSettingBundle(true))

            val isSqueezeEffectEnabled by collectLastValue(underTest.isSqueezeEffectEnabled)

+1 −1
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ public class AssistManager {

    private static final String INVOCATION_TIME_MS_KEY = "invocation_time_ms";
    private static final String INVOCATION_PHONE_STATE_KEY = "invocation_phone_state";
    protected static final String ACTION_KEY = "action";
    public static final String ACTION_KEY = "action";
    protected static final String SET_ASSIST_GESTURE_CONSTRAINED_ACTION =
            "set_assist_gesture_constrained";
    protected static final String CONSTRAINED_KEY = "should_constrain";
+44 −13
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import android.util.DisplayUtils
import android.view.DisplayInfo
import androidx.annotation.ArrayRes
import androidx.annotation.DrawableRes
import com.android.internal.annotations.VisibleForTesting
import com.android.systemui.assist.AssistManager
import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
@@ -38,8 +40,14 @@ import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn

@VisibleForTesting
const val SET_INVOCATION_EFFECT_PARAMETERS_ACTION = "set_invocation_effect_parameters"
@VisibleForTesting const val IS_INVOCATION_EFFECT_ENABLED_KEY = "is_invocation_effect_enabled"

@SysUISingleton
class SqueezeEffectRepositoryImpl
@Inject
@@ -50,32 +58,28 @@ constructor(
    private val globalSettings: GlobalSettings,
) : SqueezeEffectRepository, InvocationEffectSetUiHintsHandler {

    override val isSqueezeEffectEnabled: Flow<Boolean> =
    private val isPowerButtonLongPressConfiguredToLaunchAssistantFlow: Flow<Boolean> =
        conflatedCallbackFlow {
                val observer =
                    object : ContentObserver(bgHandler) {
                        override fun onChange(selfChange: Boolean) {
                            trySendWithFailureLogging(
                                squeezeEffectEnabled,
                                getIsPowerButtonLongPressConfiguredToLaunchAssistant(),
                                TAG,
                                "updated isSqueezeEffectEnabled",
                                "updated isPowerButtonLongPressConfiguredToLaunchAssistantFlow",
                            )
                        }
                    }
                trySendWithFailureLogging(squeezeEffectEnabled, TAG, "init isSqueezeEffectEnabled")
                trySendWithFailureLogging(
                    getIsPowerButtonLongPressConfiguredToLaunchAssistant(),
                    TAG,
                    "init isPowerButtonLongPressConfiguredToLaunchAssistantFlow",
                )
                globalSettings.registerContentObserverAsync(POWER_BUTTON_LONG_PRESS, observer)
                awaitClose { globalSettings.unregisterContentObserverAsync(observer) }
            }
            .flowOn(bgCoroutineContext)

    private val squeezeEffectEnabled
        get() =
            Flags.enableLppAssistInvocationEffect() &&
                globalSettings.getInt(
                    POWER_BUTTON_LONG_PRESS,
                    com.android.internal.R.integer.config_longPressOnPowerBehavior,
                ) == 5 // 5 corresponds to launch assistant in config_longPressOnPowerBehavior

    override suspend fun getRoundedCornersResourceId(): SqueezeEffectCornerResourceId {
        val displayInfo = DisplayInfo()
        context.display.getDisplayInfo(displayInfo)
@@ -116,7 +120,34 @@ constructor(
        return drawableResource
    }

    override fun tryHandleSetUiHints(hints: Bundle) = false
    private val isInvocationEffectEnabledForCurrentAssistantFlow = MutableStateFlow(true)

    override val isSqueezeEffectEnabled: Flow<Boolean> =
        combine(
            isPowerButtonLongPressConfiguredToLaunchAssistantFlow,
            isInvocationEffectEnabledForCurrentAssistantFlow,
        ) { prerequisites ->
            prerequisites.all { it } && Flags.enableLppAssistInvocationEffect()
        }

    private fun getIsPowerButtonLongPressConfiguredToLaunchAssistant() =
        globalSettings.getInt(
            POWER_BUTTON_LONG_PRESS,
            com.android.internal.R.integer.config_longPressOnPowerBehavior,
        ) == 5 // 5 corresponds to launch assistant in PhoneWindowManager.java

    override fun tryHandleSetUiHints(hints: Bundle): Boolean {
        return when (hints.getString(AssistManager.ACTION_KEY)) {
            SET_INVOCATION_EFFECT_PARAMETERS_ACTION -> {
                if (hints.containsKey(IS_INVOCATION_EFFECT_ENABLED_KEY)) {
                    isInvocationEffectEnabledForCurrentAssistantFlow.value =
                        hints.getBoolean(IS_INVOCATION_EFFECT_ENABLED_KEY)
                }
                true
            }
            else -> false
        }
    }

    companion object {
        private const val TAG = "SqueezeEffectRepository"