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

Commit 024a0a8b authored by Adnan Begovic's avatar Adnan Begovic Committed by Android (Google) Code Review
Browse files

Merge changes from topic "smartspace-anr-fix" into main

* changes:
  sysui: Observe wakefulness for smartspace.
  sysui: Delegate on/off state to plugins.
parents df40cf14 618cf16a
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -128,6 +128,11 @@ public interface BcSmartspaceDataPlugin extends Plugin {
         */
        void setDozeAmount(float amount);

        /**
         * Set if the screen is on.
         */
        default void setScreenOn(boolean screenOn) {}

        /**
         * Set if dozing is true or false
         */
+18 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.util.Log
import android.view.ContextThemeWrapper
import android.view.View
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import com.android.keyguard.KeyguardUpdateMonitor
import com.android.settingslib.Utils
import com.android.systemui.Dumpable
@@ -45,6 +46,7 @@ import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.dump.DumpManager
import com.android.systemui.flags.FeatureFlags
import com.android.systemui.flags.Flags
import com.android.systemui.keyguard.WakefulnessLifecycle
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.plugins.BcSmartspaceConfigPlugin
import com.android.systemui.plugins.BcSmartspaceDataPlugin
@@ -95,6 +97,7 @@ constructor(
        private val deviceProvisionedController: DeviceProvisionedController,
        private val bypassController: KeyguardBypassController,
        private val keyguardUpdateMonitor: KeyguardUpdateMonitor,
        private val wakefulnessLifecycle: WakefulnessLifecycle,
        private val dumpManager: DumpManager,
        private val execution: Execution,
        @Main private val uiExecutor: Executor,
@@ -123,7 +126,7 @@ constructor(
    private val recentSmartspaceData: Deque<List<SmartspaceTarget>> = LinkedList()

    // Smartspace can be used on multiple displays, such as when the user casts their screen
    private var smartspaceViews = mutableSetOf<SmartspaceView>()
    @VisibleForTesting var smartspaceViews = mutableSetOf<SmartspaceView>()
    private var regionSamplers =
            mutableMapOf<SmartspaceView, RegionSampler>()

@@ -272,6 +275,18 @@ constructor(
            }
        }

    // TODO(b/331451011): Refactor to viewmodel and use interactor pattern.
    private val wakefulnessLifecycleObserver =
        object : WakefulnessLifecycle.Observer {
            override fun onStartedWakingUp() {
                smartspaceViews.forEach { it.setScreenOn(true) }
            }

            override fun onFinishedGoingToSleep() {
                smartspaceViews.forEach { it.setScreenOn(false) }
            }
        }

    init {
        deviceProvisionedController.addCallback(deviceProvisionedListener)
        dumpManager.registerDumpable(this)
@@ -451,6 +466,7 @@ constructor(
        configurationController.addCallback(configChangeListener)
        statusBarStateController.addCallback(statusBarStateListener)
        bypassController.registerOnBypassStateChangedListener(bypassStateChangedListener)
        wakefulnessLifecycle.addObserver(wakefulnessLifecycleObserver)

        datePlugin?.registerSmartspaceEventNotifier { e -> session?.notifySmartspaceEvent(e) }
        weatherPlugin?.registerSmartspaceEventNotifier { e -> session?.notifySmartspaceEvent(e) }
@@ -493,6 +509,7 @@ constructor(
        configurationController.removeCallback(configChangeListener)
        statusBarStateController.removeCallback(statusBarStateListener)
        bypassController.unregisterOnBypassStateChangedListener(bypassStateChangedListener)
        wakefulnessLifecycle.removeObserver(wakefulnessLifecycleObserver)
        session = null

        datePlugin?.registerSmartspaceEventNotifier(null)
+43 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import com.android.keyguard.KeyguardUpdateMonitor
import com.android.systemui.SysuiTestCase
import com.android.systemui.dump.DumpManager
import com.android.systemui.flags.FeatureFlags
import com.android.systemui.keyguard.WakefulnessLifecycle
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.plugins.BcSmartspaceConfigPlugin
import com.android.systemui.plugins.BcSmartspaceDataPlugin
@@ -180,6 +181,7 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() {
    private lateinit var dateSmartspaceView: SmartspaceView
    private lateinit var weatherSmartspaceView: SmartspaceView
    private lateinit var smartspaceView: SmartspaceView
    private lateinit var wakefulnessLifecycle: WakefulnessLifecycle

    private val clock = FakeSystemClock()
    private val executor = FakeExecutor(clock)
@@ -225,6 +227,14 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() {
        setAllowPrivateNotifications(userHandleSecondary, true)
        setShowNotifications(userHandlePrimary, true)

        // Use the real wakefulness lifecycle instead of a mock
        wakefulnessLifecycle = WakefulnessLifecycle(
            context,
            /* wallpaper= */ null,
            clock,
            dumpManager
        )

        controller = LockscreenSmartspaceController(
                context,
                featureFlags,
@@ -240,6 +250,7 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() {
                deviceProvisionedController,
                keyguardBypassController,
                keyguardUpdateMonitor,
                wakefulnessLifecycle,
                dumpManager,
                execution,
                executor,
@@ -773,6 +784,38 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() {
        verify(configurationController, never()).addCallback(any())
    }

    @Test
    fun testWakefulnessLifecycleDispatch_wake_setsSmartspaceScreenOnTrue() {
        // Connect session
        connectSession()

        // Add mock views
        val mockSmartspaceView = mock(SmartspaceView::class.java)
        controller.smartspaceViews.add(mockSmartspaceView)

        // Initiate wakefulness change
        wakefulnessLifecycle.dispatchStartedWakingUp(0)

        // Verify smartspace views receive screen on
        verify(mockSmartspaceView).setScreenOn(true)
    }

    @Test
    fun testWakefulnessLifecycleDispatch_sleep_setsSmartspaceScreenOnFalse() {
        // Connect session
        connectSession()

        // Add mock views
        val mockSmartspaceView = mock(SmartspaceView::class.java)
        controller.smartspaceViews.add(mockSmartspaceView)

        // Initiate wakefulness change
        wakefulnessLifecycle.dispatchFinishedGoingToSleep()

        // Verify smartspace views receive screen on
        verify(mockSmartspaceView).setScreenOn(false)
    }

    private fun connectSession() {
        val dateView = controller.buildAndConnectDateView(fakeParent)
        dateSmartspaceView = dateView as SmartspaceView