From 5a108c225a81cedacb1cec9b5b1986f2f3eff75c Mon Sep 17 00:00:00 2001 From: Jorim Jaggi Date: Thu, 13 Oct 2016 14:33:27 +0200 Subject: [PATCH] The big keyguard transition refactor (2/n) Introduce UnknownVisibilityController, which keeps track of apps that may or may not be visible when launching an activity behind Keyguard. When Keyguard is occluded and we launch another activity, we don't know whether we still have to keep Keyguard occluded until the app has gone through the resume path and issued a relayout call to update the Keyguard flags. This class keeps track of that state and defers the app transition until the unknown visibility of all apps is resolved. Test: 1) Have an occluding activity that starts another occluding activity, ensure that there is no flicker. 2) Have an occluding activity while the Keyguard is insecure, start a DISMISS_KEYGUARD activity, ensure there is no flicker. 3) runtest frameworks-services -c com.android.server.wm.UnknownVisibilityControllerTest Bug: 32057734 Change-Id: I5145b272722ab8c31dd7c5383286f5c9473e26a4 --- .../android/server/am/ActivityManagerService.java | 1 + .../android/server/am/ActivityStackSupervisor.java | 4 + .../com/android/server/wm/RootWindowContainer.java | 1 + .../server/wm/UnknownAppVisibilityController.java | 136 +++++++++++++++++++++ .../android/server/wm/WindowManagerService.java | 29 +++++ .../com/android/server/wm/WindowSurfacePlacer.java | 9 ++ .../wm/UnknownAppVisibilityControllerTest.java | 120 ++++++++++++++++++ 7 files changed, 300 insertions(+) create mode 100644 services/core/java/com/android/server/wm/UnknownAppVisibilityController.java create mode 100644 services/tests/servicestests/src/com/android/server/wm/UnknownAppVisibilityControllerTest.java diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index d9c641f3df53..5d41d36a3e9a 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -6758,6 +6758,7 @@ public class ActivityManagerService extends ActivityManagerNative final long origId = Binder.clearCallingIdentity(); synchronized(this) { ActivityRecord.activityResumedLocked(token); + mWindowManager.notifyAppResumedFinished(token); } Binder.restoreCallingIdentity(origId); } diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java index 834a4de268ee..00e65d2770da 100644 --- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java +++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java @@ -1222,6 +1222,10 @@ public class ActivityStackSupervisor extends ConfigurationContainer displayId); } + if (mKeyguardController.isKeyguardLocked()) { + mWindowManager.notifyUnknownAppVisibilityLaunched(r.appToken); + } + r.app = app; app.waitingToKill = null; r.launchCount++; diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java index a28dc10df088..26f79a97401b 100644 --- a/services/core/java/com/android/server/wm/RootWindowContainer.java +++ b/services/core/java/com/android/server/wm/RootWindowContainer.java @@ -483,6 +483,7 @@ class RootWindowContainer extends WindowContainer { appToken.voiceInteraction); mService.mOpeningApps.remove(appToken); + mService.mUnknownAppVisibilityController.appRemoved(appToken); appToken.waitingToShow = false; if (mService.mClosingApps.contains(appToken)) { delayed = true; diff --git a/services/core/java/com/android/server/wm/UnknownAppVisibilityController.java b/services/core/java/com/android/server/wm/UnknownAppVisibilityController.java new file mode 100644 index 000000000000..ebe0e0a5da34 --- /dev/null +++ b/services/core/java/com/android/server/wm/UnknownAppVisibilityController.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.server.wm; + +import android.annotation.NonNull; +import android.util.ArrayMap; + +import com.android.server.wm.WindowManagerService.H; + +import java.io.PrintWriter; + +/** + * Manages the set of {@link AppWindowToken}s for which we don't know yet whether it's visible or + * not. This happens when starting an activity while the lockscreen is showing. In that case, the + * keyguard flags an app might set influence it's visibility, so we wait until this is resolved to + * start the transition to avoid flickers. + */ +class UnknownAppVisibilityController { + + /** + * We are currently waiting until the app is done resuming. + */ + private static final int UNKNOWN_STATE_WAITING_RESUME = 1; + + /** + * The activity has finished resuming, and we are waiting on the next relayout. + */ + private static final int UNKNOWN_STATE_WAITING_RELAYOUT = 2; + + /** + * The client called {@link Session#relayout} with the appropriate Keyguard flags and we are + * waiting until activity manager has updated the visibilities of all the apps. + */ + private static final int UNKNOWN_STATE_WAITING_VISIBILITY_UPDATE = 3; + + // Set of apps for which we don't know yet whether it's visible or not, depending on what kind + // of lockscreen flags the app might set during its first relayout. + private final ArrayMap mUnknownApps = new ArrayMap<>(); + + private final WindowManagerService mService; + + UnknownAppVisibilityController(WindowManagerService service) { + mService = service; + } + + boolean allResolved() { + return mUnknownApps.isEmpty(); + } + + void clear() { + mUnknownApps.clear(); + } + + String getDebugMessage() { + final StringBuilder builder = new StringBuilder(); + for (int i = mUnknownApps.size() - 1; i >= 0; i--) { + builder.append("app=").append(mUnknownApps.keyAt(i)) + .append(" state=").append(mUnknownApps.valueAt(i)); + if (i != 0) { + builder.append(' '); + } + } + return builder.toString(); + } + + void appRemoved(@NonNull AppWindowToken appWindow) { + mUnknownApps.remove(appWindow); + } + + /** + * Notifies that {@param appWindow} has been launched behind Keyguard, and we need to wait until + * it is resumed and relaid out to resolve the visibility. + */ + void notifyLaunched(@NonNull AppWindowToken appWindow) { + mUnknownApps.put(appWindow, UNKNOWN_STATE_WAITING_RESUME); + } + + /** + * Notifies that {@param appWindow} has finished resuming. + */ + void notifyAppResumedFinished(@NonNull AppWindowToken appWindow) { + if (mUnknownApps.containsKey(appWindow) + && mUnknownApps.get(appWindow) == UNKNOWN_STATE_WAITING_RESUME) { + mUnknownApps.put(appWindow, UNKNOWN_STATE_WAITING_RELAYOUT); + } + } + + /** + * Notifies that {@param appWindow} has relaid out. + */ + void notifyRelayouted(@NonNull AppWindowToken appWindow) { + if (!mUnknownApps.containsKey(appWindow)) { + return; + } + int state = mUnknownApps.get(appWindow); + if (state == UNKNOWN_STATE_WAITING_RELAYOUT) { + mUnknownApps.put(appWindow, UNKNOWN_STATE_WAITING_VISIBILITY_UPDATE); + mService.notifyKeyguardFlagsChanged(this::notifyVisibilitiesUpdated); + } + } + + private void notifyVisibilitiesUpdated() { + boolean changed = false; + for (int i = mUnknownApps.size() - 1; i >= 0; i--) { + if (mUnknownApps.valueAt(i) == UNKNOWN_STATE_WAITING_VISIBILITY_UPDATE) { + mUnknownApps.removeAt(i); + changed = true; + } + } + if (changed) { + mService.mWindowPlacerLocked.performSurfacePlacement(); + } + } + + void dump(PrintWriter pw, String prefix) { + pw.println(prefix + "Unknown visibilities"); + for (int i = mUnknownApps.size() - 1; i >= 0; i--) { + pw.println(prefix + " app=" + mUnknownApps.keyAt(i) + + " state=" + mUnknownApps.valueAt(i)); + } + } +} diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java index 41dd7b5a8c36..d2605a154277 100644 --- a/services/core/java/com/android/server/wm/WindowManagerService.java +++ b/services/core/java/com/android/server/wm/WindowManagerService.java @@ -583,6 +583,9 @@ public class WindowManagerService extends IWindowManager.Stub final ArraySet mOpeningApps = new ArraySet<>(); final ArraySet mClosingApps = new ArraySet<>(); + final UnknownAppVisibilityController mUnknownAppVisibilityController = + new UnknownAppVisibilityController(this); + boolean mIsTouchDevice; final DisplayMetrics mDisplayMetrics = new DisplayMetrics(); @@ -2069,6 +2072,10 @@ public class WindowManagerService extends IWindowManager.Stub WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER; } + if (win.mAppToken != null) { + mUnknownAppVisibilityController.notifyRelayouted(win.mAppToken); + } + win.setDisplayLayoutNeeded(); win.mGivenInsetsPending = (flags&WindowManagerGlobal.RELAYOUT_INSETS_PENDING) != 0; configChanged = updateOrientationFromAppTokensLocked(false, displayId); @@ -3196,6 +3203,19 @@ public class WindowManagerService extends IWindowManager.Stub } } + /** + * Notifies that we launched an app that might be visible or not visible depending on what kind + * of Keyguard flags it's going to set on its windows. + */ + public void notifyUnknownAppVisibilityLaunched(IBinder token) { + synchronized(mWindowMap) { + AppWindowToken appWindow = mRoot.getAppWindowToken(token); + if (appWindow != null) { + mUnknownAppVisibilityController.notifyLaunched(appWindow); + } + } + } + @Override public void startAppFreezingScreen(IBinder token, int configChanges) { if (!checkCallingPermission(MANAGE_APP_TOKENS, "setAppFreezingScreen()")) { @@ -7696,6 +7716,15 @@ public class WindowManagerService extends IWindowManager.Stub } } + public void notifyAppResumedFinished(IBinder token) { + synchronized (mWindowMap) { + final AppWindowToken appWindow = mRoot.getAppWindowToken(token); + if (appWindow != null) { + mUnknownAppVisibilityController.notifyAppResumedFinished(appWindow); + } + } + } + @Override public int getDockedDividerInsetsLw() { return getDefaultDisplayContentLocked().getDockedDividerController().getContentInsets(); diff --git a/services/core/java/com/android/server/wm/WindowSurfacePlacer.java b/services/core/java/com/android/server/wm/WindowSurfacePlacer.java index 35d90711bc90..f2682baea614 100644 --- a/services/core/java/com/android/server/wm/WindowSurfacePlacer.java +++ b/services/core/java/com/android/server/wm/WindowSurfacePlacer.java @@ -361,6 +361,7 @@ class WindowSurfacePlacer { mService.mOpeningApps.clear(); mService.mClosingApps.clear(); + mService.mUnknownAppVisibilityController.clear(); // This has changed the visibility of windows, so perform // a new layout to get them all up-to-date. @@ -555,6 +556,14 @@ class WindowSurfacePlacer { return false; } + if (!mService.mUnknownAppVisibilityController.allResolved()) { + if (DEBUG_APP_TRANSITIONS) { + Slog.v(TAG, "unknownApps is not empty: " + + mService.mUnknownAppVisibilityController.getDebugMessage()); + } + return false; + } + // If the wallpaper is visible, we need to check it's ready too. boolean wallpaperReady = !mWallpaperControllerLocked.isWallpaperVisible() || mWallpaperControllerLocked.wallpaperTransitionReady(); diff --git a/services/tests/servicestests/src/com/android/server/wm/UnknownAppVisibilityControllerTest.java b/services/tests/servicestests/src/com/android/server/wm/UnknownAppVisibilityControllerTest.java new file mode 100644 index 000000000000..36bd13af7ea9 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/wm/UnknownAppVisibilityControllerTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.server.wm; + +import static junit.framework.Assert.assertTrue; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; + +import android.app.ActivityManagerInternal; +import android.content.Context; +import android.platform.test.annotations.Presubmit; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; +import android.view.IApplicationToken; + +import com.android.server.LocalServices; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; + +/** + * Test class for {@link AppTransition}. + * + * runtest frameworks-services -c com.android.server.wm.UnknownVisibilityControllerTest + */ +@SmallTest +@Presubmit +@RunWith(AndroidJUnit4.class) +public class UnknownAppVisibilityControllerTest { + + private WindowManagerService mWm; + private @Mock ActivityManagerInternal mAm; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + final Context context = InstrumentationRegistry.getTargetContext(); + LocalServices.addService(ActivityManagerInternal.class, mAm); + doAnswer((InvocationOnMock invocationOnMock) -> { + invocationOnMock.getArgumentAt(0, Runnable.class).run(); + return null; + }).when(mAm).notifyKeyguardFlagsChanged(any()); + mWm = TestWindowManagerPolicy.getWindowManagerService(context); + } + + @After + public void tearDown() throws Exception { + LocalServices.removeServiceForTest(ActivityManagerInternal.class); + } + + @Test + public void testFlow() throws Exception { + AppWindowToken token = createAppToken(); + mWm.mUnknownAppVisibilityController.notifyLaunched(token); + mWm.mUnknownAppVisibilityController.notifyAppResumedFinished(token); + mWm.mUnknownAppVisibilityController.notifyRelayouted(token); + + // Make sure our handler processed the message. + Thread.sleep(100); + assertTrue(mWm.mUnknownAppVisibilityController.allResolved()); + } + + @Test + public void testMultiple() throws Exception { + AppWindowToken token1 = createAppToken(); + AppWindowToken token2 = createAppToken(); + mWm.mUnknownAppVisibilityController.notifyLaunched(token1); + mWm.mUnknownAppVisibilityController.notifyAppResumedFinished(token1); + mWm.mUnknownAppVisibilityController.notifyLaunched(token2); + mWm.mUnknownAppVisibilityController.notifyRelayouted(token1); + mWm.mUnknownAppVisibilityController.notifyAppResumedFinished(token2); + mWm.mUnknownAppVisibilityController.notifyRelayouted(token2); + + // Make sure our handler processed the message. + Thread.sleep(100); + assertTrue(mWm.mUnknownAppVisibilityController.allResolved()); + } + + @Test + public void testClear() throws Exception { + AppWindowToken token = createAppToken(); + mWm.mUnknownAppVisibilityController.notifyLaunched(token); + mWm.mUnknownAppVisibilityController.clear();; + assertTrue(mWm.mUnknownAppVisibilityController.allResolved()); + } + + @Test + public void testAppRemoved() throws Exception { + AppWindowToken token = createAppToken(); + mWm.mUnknownAppVisibilityController.notifyLaunched(token); + mWm.mUnknownAppVisibilityController.appRemoved(token); + assertTrue(mWm.mUnknownAppVisibilityController.allResolved()); + } + + private AppWindowToken createAppToken() { + return new AppWindowToken(mWm, mock(IApplicationToken.class), false, + mWm.getDefaultDisplayContentLocked()); + } +} -- 2.11.0