From afc96fda777d5615d59b92238f30b37efc130b3d Mon Sep 17 00:00:00 2001 From: Wale Ogunwale Date: Tue, 27 Jul 2021 13:18:35 -0700 Subject: [PATCH] Changed task focused to be based on focused app vs. window Task#isFocused and onWindowFocusChanged (renamed to onAppFocusChanged) where based on the current focused window which is wrong since not all windows are contained within a task. Changed to use DC#mFocusedApp instead which tracks focus changes to app windows. Also, - Start calling dispatchTaskInfoChangedIfNeeded in Task#onAppFocusChanged - Removed DC#mLastFocus that isn't used. Bug: 180525887 Test: Task focus doesn't change when notification shade is expanded. Change-Id: I1abf97b5f8d7eedd36bd429835094f6a48361bee --- .../com/android/server/wm/DisplayContent.java | 39 ++++--------------- .../core/java/com/android/server/wm/Task.java | 9 ++--- .../server/wm/DisplayContentTests.java | 13 ------- 3 files changed, 11 insertions(+), 50 deletions(-) diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java index a7c7c33cb3b78..bd4140c0dabd7 100644 --- a/services/core/java/com/android/server/wm/DisplayContent.java +++ b/services/core/java/com/android/server/wm/DisplayContent.java @@ -504,11 +504,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp */ WindowState mCurrentFocus = null; - /** - * The last focused window that we've notified the client that the focus is changed. - */ - WindowState mLastFocus = null; - /** * The foreground app of this display. Windows below this app cannot be the focused window. If * the user taps on the area outside of the task of the focused app, we will notify AM about the @@ -1244,12 +1239,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp // removing from parent. token.getParent().removeChild(token); } - if (token.hasChild(prevDc.mLastFocus)) { - // If the reparent window token contains previous display's last focus window, means - // it will end up to gain window focus on the target display, so it should not be - // notified that it lost focus from the previous display. - prevDc.mLastFocus = null; - } } addWindowToken(token.token, token); @@ -3204,9 +3193,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq); pw.print(" mCurrentFocus="); pw.println(mCurrentFocus); - if (mLastFocus != mCurrentFocus) { - pw.print(" mLastFocus="); pw.println(mLastFocus); - } pw.print(" mFocusedApp="); pw.println(mFocusedApp); if (mFixedRotationLaunchingApp != null) { pw.println(" mFixedRotationLaunchingApp=" + mFixedRotationLaunchingApp); @@ -3437,8 +3423,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp } } - onWindowFocusChanged(oldFocus, newFocus); - int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus); if (imWindowChanged && oldFocus != mInputMethodWindow) { @@ -3489,7 +3473,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp mWmService.mAccessibilityController)); } - mLastFocus = mCurrentFocus; return true; } @@ -3497,20 +3480,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp accessibilityController.onWindowFocusChangedNot(getDisplayId()); } - private static void onWindowFocusChanged(WindowState oldFocus, WindowState newFocus) { - final Task focusedTask = newFocus != null ? newFocus.getTask() : null; - final Task unfocusedTask = oldFocus != null ? oldFocus.getTask() : null; - if (focusedTask == unfocusedTask) { - return; - } - if (focusedTask != null) { - focusedTask.onWindowFocusChanged(true /* hasFocus */); - } - if (unfocusedTask != null) { - unfocusedTask.onWindowFocusChanged(false /* hasFocus */); - } - } - /** * Set the new focused app to this display. * @@ -3534,7 +3503,14 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp } ProtoLog.i(WM_DEBUG_FOCUS_LIGHT, "setFocusedApp %s displayId=%d Callers=%s", newFocus, getDisplayId(), Debug.getCallers(4)); + final Task oldTask = mFocusedApp != null ? mFocusedApp.getTask() : null; + final Task newTask = newFocus != null ? newFocus.getTask() : null; mFocusedApp = newFocus; + if (oldTask != newTask) { + if (oldTask != null) oldTask.onAppFocusChanged(false); + if (newTask != null) newTask.onAppFocusChanged(true); + } + getInputMonitor().setFocusedAppLw(newFocus); updateTouchExcludeRegion(); return true; @@ -4230,7 +4206,6 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp if (DEBUG_INPUT_METHOD) { Slog.i(TAG_WM, "Desired input method target: " + imFocus); Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId); - Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId); } if (DEBUG_INPUT_METHOD) { diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java index a4a7054cea13e..91e316ee6e16f 100644 --- a/services/core/java/com/android/server/wm/Task.java +++ b/services/core/java/com/android/server/wm/Task.java @@ -4314,10 +4314,10 @@ class Task extends TaskFragment { * @return true if the task is currently focused. */ private boolean isFocused() { - if (mDisplayContent == null || mDisplayContent.mCurrentFocus == null) { + if (mDisplayContent == null || mDisplayContent.mFocusedApp == null) { return false; } - return mDisplayContent.mCurrentFocus.getTask() == this; + return mDisplayContent.mFocusedApp.getTask() == this; } /** @@ -4374,10 +4374,9 @@ class Task extends TaskFragment { * Called on the task of a window which gained or lost focus. * @param hasFocus */ - void onWindowFocusChanged(boolean hasFocus) { + void onAppFocusChanged(boolean hasFocus) { updateShadowsRadius(hasFocus, getSyncTransaction()); - // TODO(b/180525887): Un-comment once there is resolution on the bug. - // dispatchTaskInfoChangedIfNeeded(false /* force */); + dispatchTaskInfoChangedIfNeeded(false /* force */); } void onPictureInPictureParamsChanged() { diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java index 04e2a51be0333..277048f1b6877 100644 --- a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java @@ -871,19 +871,6 @@ public class DisplayContentTests extends WindowTestsBase { .setDisplayInfoOverrideFromWindowManager(dc.getDisplayId(), null); } - @UseTestDisplay - @Test - public void testClearLastFocusWhenReparentingFocusedWindow() { - final DisplayContent defaultDisplay = mWm.getDefaultDisplayContentLocked(); - final WindowState window = createWindow(null /* parent */, TYPE_BASE_APPLICATION, - defaultDisplay, "window"); - defaultDisplay.mLastFocus = window; - mDisplayContent.mCurrentFocus = window; - mDisplayContent.reParentWindowToken(window.mToken); - - assertNull(defaultDisplay.mLastFocus); - } - @Test public void testGetPreferredOptionsPanelGravityFromDifferentDisplays() { final DisplayContent portraitDisplay = createNewDisplay();