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

Commit 97472af7 authored by Matt Sziklay's avatar Matt Sziklay
Browse files

Have DesktopWallpaperActivity finish on disconnect.

DesktopWallpaperActivity should never be able to change displays, and in some situations, the removal in DesktopTasksController#addOnDisplayDisconnectChanges is unsuccessful, leading to default disconnect logic moving the activity to the default display.

Fix this by having the activity finish when its display is removed or when attempting to move it to another display.

Bug: 434198011
Bug: 432494930
Test: Manual, disconnect display on phone, no wallpaper activity remaining
Flag: com.android.window.flags.enable_display_disconnect_interaction
Change-Id: Ifb26c32ee7135b1def4f92d6d5a6a74fa4f2003f
parent 7f37e37d
Loading
Loading
Loading
Loading
+27 −17
Original line number Diff line number Diff line
@@ -16,9 +16,9 @@

package com.android.wm.shell.desktopmode

import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.SetFlagsRule
import android.view.Display.DEFAULT_DISPLAY
import android.view.WindowManager
import androidx.activity.BackEventCompat
import androidx.fragment.app.FragmentActivity
@@ -34,42 +34,39 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/** Tests for [DesktopWallpaperActivity].
/**
 * Tests for [DesktopWallpaperActivity].
 *
 * Build/Install/Run:
 *  atest WMShellRobolectricTests:DesktopWallpaperActivityTest (on host)
 *  atest WMShellMultivalentTestsOnDevice:DesktopWallpaperActivityTest (on device)
 * Build/Install/Run: atest WMShellRobolectricTests:DesktopWallpaperActivityTest (on host) atest
 * WMShellMultivalentTestsOnDevice:DesktopWallpaperActivityTest (on device)
 */
@SmallTest
@RunWith(AndroidJUnit4::class)
class DesktopWallpaperActivityTest() {

    @get:Rule
    val setFlagsRule = SetFlagsRule()
    @get:Rule val setFlagsRule = SetFlagsRule()
    @Rule
    @JvmField
    val activityScenarioRule =
        ActivityScenarioRule(DesktopWallpaperActivity::class.java)
    val activityScenarioRule = ActivityScenarioRule(DesktopWallpaperActivity::class.java)

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE,
    )
    fun onTopResumedActivityChanged_whenTrue_setsWindowFocusable() {
        activityScenarioRule.scenario.onActivity { activity ->
            activity.onTopResumedActivityChanged(true)

            val windowFlags = activity.window.attributes.flags
            assertThat(windowFlags and WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
                .isEqualTo(0)
            assertThat(windowFlags and WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE).isEqualTo(0)
        }
    }

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE,
    )
    fun onTopResumedActivityChanged_whenFalse_setsWindowNotFocusable() {
        activityScenarioRule.scenario.onActivity { activity ->
@@ -91,7 +88,7 @@ class DesktopWallpaperActivityTest() {
    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE,
    )
    fun onBackPressed_movesTaskToBack() {
        var wallpaperActivity: FragmentActivity? = null
@@ -105,6 +102,19 @@ class DesktopWallpaperActivityTest() {
        assertThat(wallpaperActivity?.isFinishing).isFalse()
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND)
    fun onMovedToDisplay_finishesActivity() {
        var wallpaperActivity: FragmentActivity? = null

        activityScenarioRule.scenario.onActivity { activity ->
            wallpaperActivity = activity
            activity.onMovedToDisplay(DEFAULT_DISPLAY, null)
        }

        assertThat(wallpaperActivity?.isFinishing).isTrue()
    }

    private fun backEvent(progress: Float = 0f): BackEventCompat {
        return BackEventCompat(
            touchX = 0f,
+0 −1
Original line number Diff line number Diff line
@@ -767,7 +767,6 @@ class DesktopTasksController(
            )
        }
        snapEventHandler.onDisplayDisconnected(disconnectedDisplayId)
        removeWallpaperTask(wct, disconnectedDisplayId)
        removeHomeTask(wct, disconnectedDisplayId)
        userRepositories.forAllRepositories { desktopRepository ->
            val userId = desktopRepository.userId
+36 −1
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.app.TaskInfo
import android.app.WallpaperColors
import android.app.WallpaperManager
import android.content.ComponentName
import android.content.res.Configuration
import android.hardware.display.DisplayManager
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
@@ -42,8 +44,10 @@ import androidx.fragment.app.FragmentActivity
class DesktopWallpaperActivity : FragmentActivity() {

    private var wallpaperManager: WallpaperManager? = null
    private var displayManager: DisplayManager? = null
    // TODO(b/432710419): Refresh current user on user change if needed
    private var currentUser: Int = getCurrentUser()
    private var initialDisplayId: Int? = null

    private val wallpaperColorsListener =
        object : WallpaperManager.OnColorsChangedListener {
@@ -56,6 +60,25 @@ class DesktopWallpaperActivity : FragmentActivity() {
            }
        }

    private val displayRemovedListener =
        object : DisplayManager.DisplayListener {
            override fun onDisplayAdded(displayId: Int) {
                // No-op
            }

            override fun onDisplayRemoved(displayId: Int) {
                // DesktopWallpaperActivity should never move to another display; if this
                // activity's display is removed, finish the activity.
                if (displayId == initialDisplayId) {
                    finish()
                }
            }

            override fun onDisplayChanged(displayId: Int) {
                // No-op
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate")
@@ -80,13 +103,25 @@ class DesktopWallpaperActivity : FragmentActivity() {
        wallpaperManager = getSystemService(WallpaperManager::class.java)
        wallpaperManager?.addOnColorsChangedListener(wallpaperColorsListener, mainThreadHandler)

        // Handle self-removal on display disconnect
        displayManager = getSystemService(DisplayManager::class.java)
        displayManager?.registerDisplayListener(displayRemovedListener, mainThreadHandler)

        // Set the initial color of status bar icons on activity creation.
        updateStatusBarIconColors(wallpaperManager?.getWallpaperColors(WallpaperManager.FLAG_SYSTEM, currentUser))
        updateStatusBarIconColors(
            wallpaperManager?.getWallpaperColors(WallpaperManager.FLAG_SYSTEM, currentUser)
        )
        initialDisplayId = displayId
    }

    override fun onDestroy() {
        super.onDestroy()
        wallpaperManager?.removeOnColorsChangedListener(wallpaperColorsListener)
        displayManager?.unregisterDisplayListener(displayRemovedListener)
    }

    override fun onMovedToDisplay(displayId: Int, config: Configuration?) {
        finish()
    }

    override fun onTopResumedActivityChanged(isTopResumedActivity: Boolean) {