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

Commit 12bdcf39 authored by Jorge Gil's avatar Jorge Gil
Browse files

Desks: Let DesktopWallpaperActivity be focusable with empty desk

When there are no desktop windows on top of the
DesktopWallpaperActivities, input events (such as keyboard shortcuts)
could result in ANRs because the focused app (the desktop wallpaper) did
not have any windows to send the input to. See go/anr-book.

With this change, the activity is allowed to be focusable only when it
is the top-resumed activity in the system, which is equivalent to it
being visible with an empty desk.

Flag: com.android.window.flags.enable_multiple_desktops_backend
Fix: 402270860
Test: with 2 desks (1 non empty, 1 empty), switch back and forth with
keyboard shortcuts a few times and verify there is no ANR.

Change-Id: Idc7ac2c374bee72bb54bd7b3a5daca915262af25
parent 1ff45114
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import android.app.Activity
import android.app.TaskInfo
import android.content.ComponentName
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import android.window.DesktopExperienceFlags

/**
 * A transparent activity used in the desktop mode to show the wallpaper under the freeform windows.
@@ -35,11 +37,29 @@ class DesktopWallpaperActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        updateFocusableFlag(focusable = true)
    }

    override fun onTopResumedActivityChanged(isTopResumedActivity: Boolean) {
        if (!DesktopExperienceFlags.ENABLE_MULTIPLE_DESKTOPS_BACKEND.isTrue) return
        Log.d(TAG, "onTopResumedActivityChanged: $isTopResumedActivity")
        // Let the activity be focusable when it is top-resumed (e.g. empty desk), otherwise input
        // events will result in an ANR because the focused app would have no focusable window.
        updateFocusableFlag(focusable = isTopResumedActivity)
    }

    private fun updateFocusableFlag(focusable: Boolean) {
        if (focusable) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        }
    }

    companion object {
        private const val TAG = "DesktopWallpaperActivity"
        private const val SYSTEM_UI_PACKAGE_NAME = "com.android.systemui"

        @JvmStatic
        val wallpaperActivityComponent =
            ComponentName(SYSTEM_UI_PACKAGE_NAME, DesktopWallpaperActivity::class.java.name)