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

Commit 5378acc3 authored by William Xiao's avatar William Xiao
Browse files

Check availability of hub when receiving EXTRA_TRIGGER_HUB lock request

If the glanceable hub isn't available when the power button behavior in
PhoneWindowManager sends the EXTRA_TRIGGER_HUB extra, the device will
just lock but not go to sleep or show the hub. This can happen for a
variety of reasons as PhoneWindowManager only does some basic checks
for the hub's availability.

We instead check directly if the hub is enabled and if the "when to
show hub automatically setting" is enabled. If either is false, we
request that the device go to sleep instead.

Bug: 401079452
Bug: 396662135
Fixes: 396662135
Test: atest KeyguardViewMediatorTestKt
Flag: com.android.systemui.glanceable_hub_v2
Change-Id: I8c195826cf8904689342306be94f6d0f3f39206e
parent 05136c4c
Loading
Loading
Loading
Loading
+16 −3
Original line number Diff line number Diff line
@@ -140,6 +140,7 @@ import com.android.systemui.animation.TransitionAnimator;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.classifier.FalsingCollector;
import com.android.systemui.communal.domain.interactor.CommunalSceneInteractor;
import com.android.systemui.communal.domain.interactor.CommunalSettingsInteractor;
import com.android.systemui.communal.ui.viewmodel.CommunalTransitionViewModel;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.dagger.qualifiers.UiBackground;
@@ -364,6 +365,7 @@ public class KeyguardViewMediator implements CoreStartable,
    private final Lazy<NotificationShadeDepthController> mNotificationShadeDepthController;
    private final Lazy<ShadeController> mShadeController;
    private final Lazy<CommunalSceneInteractor> mCommunalSceneInteractor;
    private final Lazy<CommunalSettingsInteractor> mCommunalSettingsInteractor;
    /*
     * Records the user id on request to go away, for validation when WM calls back to start the
     * exit animation.
@@ -1567,6 +1569,7 @@ public class KeyguardViewMediator implements CoreStartable,
            KeyguardInteractor keyguardInteractor,
            KeyguardTransitionBootInteractor transitionBootInteractor,
            Lazy<CommunalSceneInteractor> communalSceneInteractor,
            Lazy<CommunalSettingsInteractor> communalSettingsInteractor,
            WindowManagerOcclusionManager wmOcclusionManager) {
        mContext = context;
        mUserTracker = userTracker;
@@ -1609,6 +1612,7 @@ public class KeyguardViewMediator implements CoreStartable,
        mKeyguardInteractor = keyguardInteractor;
        mTransitionBootInteractor = transitionBootInteractor;
        mCommunalSceneInteractor = communalSceneInteractor;
        mCommunalSettingsInteractor = communalSettingsInteractor;

        mStatusBarStateController = statusBarStateController;
        statusBarStateController.addCallback(this);
@@ -2426,9 +2430,18 @@ public class KeyguardViewMediator implements CoreStartable,
    private void doKeyguardLocked(Bundle options) {
        // If the power button behavior requests to open the glanceable hub.
        if (options != null && options.getBoolean(EXTRA_TRIGGER_HUB)) {
            // Set the hub to show immediately when the SysUI window shows, then continue to lock
            // the device.
            if (mCommunalSettingsInteractor.get().getAutoOpenEnabled().getValue()) {
                // Set the hub to show immediately when the SysUI window shows, then continue to
                // lock the device.
                mCommunalSceneInteractor.get().showHubFromPowerButton();
            } else {
                // If the hub is not available, go to sleep instead of locking. This can happen
                // because the power button behavior does not check all possible reasons the hub
                // might be disabled.
                mPM.goToSleep(android.os.SystemClock.uptimeMillis(),
                        PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON, 0);
                return;
            }
        }

        int currentUserId = mSelectedUserInteractor.getSelectedUserId();
+3 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.classifier.FalsingCollector;
import com.android.systemui.classifier.FalsingModule;
import com.android.systemui.communal.domain.interactor.CommunalSceneInteractor;
import com.android.systemui.communal.domain.interactor.CommunalSettingsInteractor;
import com.android.systemui.communal.ui.viewmodel.CommunalTransitionViewModel;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Main;
@@ -182,6 +183,7 @@ public interface KeyguardModule {
            KeyguardInteractor keyguardInteractor,
            KeyguardTransitionBootInteractor transitionBootInteractor,
            Lazy<CommunalSceneInteractor> communalSceneInteractor,
            Lazy<CommunalSettingsInteractor> communalSettingsInteractor,
            WindowManagerOcclusionManager windowManagerOcclusionManager) {
        return new KeyguardViewMediator(
                context,
@@ -234,6 +236,7 @@ public interface KeyguardModule {
                keyguardInteractor,
                transitionBootInteractor,
                communalSceneInteractor,
                communalSettingsInteractor,
                windowManagerOcclusionManager);
    }

+1 −0
Original line number Diff line number Diff line
@@ -1502,6 +1502,7 @@ public class KeyguardViewMediatorTest extends SysuiTestCase {
                mKeyguardInteractor,
                mKeyguardTransitionBootInteractor,
                mKosmos::getCommunalSceneInteractor,
                mKosmos::getCommunalSettingsInteractor,
                mock(WindowManagerOcclusionManager.class));
        mViewMediator.mUserChangedCallback = mUserTrackerCallback;
        mViewMediator.start();
+66 −0
Original line number Diff line number Diff line
@@ -35,8 +35,14 @@ import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.activityTransitionAnimator
import com.android.systemui.broadcast.broadcastDispatcher
import com.android.systemui.classifier.falsingCollector
import com.android.systemui.common.data.repository.batteryRepository
import com.android.systemui.common.data.repository.fake
import com.android.systemui.communal.data.model.FEATURE_AUTO_OPEN
import com.android.systemui.communal.data.model.SuppressionReason
import com.android.systemui.communal.data.repository.communalSceneRepository
import com.android.systemui.communal.domain.interactor.communalSceneInteractor
import com.android.systemui.communal.domain.interactor.communalSettingsInteractor
import com.android.systemui.communal.domain.interactor.setCommunalV2Enabled
import com.android.systemui.communal.shared.model.CommunalScenes
import com.android.systemui.communal.ui.viewmodel.communalTransitionViewModel
import com.android.systemui.concurrency.fakeExecutor
@@ -81,8 +87,11 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify

/** Kotlin version of KeyguardViewMediatorTest to allow for coroutine testing. */
@SmallTest
@@ -152,6 +161,7 @@ class KeyguardViewMediatorTestKt : SysuiTestCase() {
                keyguardInteractor,
                keyguardTransitionBootInteractor,
                { communalSceneInteractor },
                { communalSettingsInteractor },
                mock<WindowManagerOcclusionManager>(),
            )
        }
@@ -164,6 +174,10 @@ class KeyguardViewMediatorTestKt : SysuiTestCase() {
    @Test
    fun doKeyguardTimeout_changesCommunalScene() =
        kosmos.runTest {
            // Hub is enabled and hub condition is active.
            setCommunalV2Enabled(true)
            enableHubOnCharging()

            // doKeyguardTimeout message received.
            val timeoutOptions = Bundle()
            timeoutOptions.putBoolean(KeyguardViewMediator.EXTRA_TRIGGER_HUB, true)
@@ -174,4 +188,56 @@ class KeyguardViewMediatorTestKt : SysuiTestCase() {
            assertThat(communalSceneRepository.currentScene.value)
                .isEqualTo(CommunalScenes.Communal)
        }

    @Test
    fun doKeyguardTimeout_communalNotAvailable_sleeps() =
        kosmos.runTest {
            // Hub disabled.
            setCommunalV2Enabled(false)

            // doKeyguardTimeout message received.
            val timeoutOptions = Bundle()
            timeoutOptions.putBoolean(KeyguardViewMediator.EXTRA_TRIGGER_HUB, true)
            underTest.doKeyguardTimeout(timeoutOptions)
            testableLooper.processAllMessages()

            // Sleep is requested.
            verify(powerManager)
                .goToSleep(anyOrNull(), eq(PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON), eq(0))

            // Hub scene is not changed.
            assertThat(communalSceneRepository.currentScene.value).isEqualTo(CommunalScenes.Blank)
        }

    @Test
    fun doKeyguardTimeout_hubConditionNotActive_sleeps() =
        kosmos.runTest {
            // Communal enabled, but hub condition set to never.
            setCommunalV2Enabled(true)
            disableHubShowingAutomatically()

            // doKeyguardTimeout message received.
            val timeoutOptions = Bundle()
            timeoutOptions.putBoolean(KeyguardViewMediator.EXTRA_TRIGGER_HUB, true)
            underTest.doKeyguardTimeout(timeoutOptions)
            testableLooper.processAllMessages()

            // Sleep is requested.
            verify(powerManager)
                .goToSleep(anyOrNull(), eq(PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON), eq(0))

            // Hub scene is not changed.
            assertThat(communalSceneRepository.currentScene.value).isEqualTo(CommunalScenes.Blank)
        }

    private fun Kosmos.enableHubOnCharging() {
        communalSettingsInteractor.setSuppressionReasons(emptyList())
        batteryRepository.fake.setDevicePluggedIn(true)
    }

    private fun Kosmos.disableHubShowingAutomatically() {
        communalSettingsInteractor.setSuppressionReasons(
            listOf(SuppressionReason.ReasonUnknown(FEATURE_AUTO_OPEN))
        )
    }
}