Merge "Don't allow non-showOnLock activity to be next on finish." into pi-dev
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index a5dfd8c..d194db3 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -1224,7 +1224,7 @@
     ActivityRecord topRunningActivityLocked(boolean considerKeyguardState) {
         final ActivityStack focusedStack = mFocusedStack;
         ActivityRecord r = focusedStack.topRunningActivityLocked();
-        if (r != null) {
+        if (r != null && isValidTopRunningActivity(r, considerKeyguardState)) {
             return r;
         }
 
@@ -1257,12 +1257,11 @@
                 continue;
             }
 
-            final boolean keyguardLocked = getKeyguardController().isKeyguardLocked();
 
             // This activity can be considered the top running activity if we are not
             // considering the locked state, the keyguard isn't locked, or we can show when
             // locked.
-            if (!considerKeyguardState || !keyguardLocked || topActivity.canShowWhenLocked()) {
+            if (isValidTopRunningActivity(topActivity, considerKeyguardState)) {
                 return topActivity;
             }
         }
@@ -1270,6 +1269,25 @@
         return null;
     }
 
+    /**
+     * Verifies an {@link ActivityRecord} can be the top activity based on keyguard state and
+     * whether we are considering it.
+     */
+    private boolean isValidTopRunningActivity(ActivityRecord record,
+            boolean considerKeyguardState) {
+        if (!considerKeyguardState) {
+            return true;
+        }
+
+        final boolean keyguardLocked = getKeyguardController().isKeyguardLocked();
+
+        if (!keyguardLocked) {
+            return true;
+        }
+
+        return record.canShowWhenLocked();
+    }
+
     @VisibleForTesting
     void getRunningTasks(int maxNum, List<RunningTaskInfo> list,
             @ActivityType int ignoreActivityType, @WindowingMode int ignoreWindowingMode,
diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityStackSupervisorTests.java b/services/tests/servicestests/src/com/android/server/am/ActivityStackSupervisorTests.java
index 1415ada..1ce41a6 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityStackSupervisorTests.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityStackSupervisorTests.java
@@ -325,13 +325,12 @@
     }
 
     /**
-     * Verifies the correct activity is returned when querying the top running activity with an
-     * empty focused stack.
+     * Verifies the correct activity is returned when querying the top running activity.
      */
     @Test
-    public void testNonFocusedTopRunningActivity() throws Exception {
+    public void testTopRunningActivity() throws Exception {
         // Create stack to hold focus
-        final ActivityStack focusedStack = mService.mStackSupervisor.getDefaultDisplay()
+        final ActivityStack emptyStack = mService.mStackSupervisor.getDefaultDisplay()
                 .createStack(WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
 
         final KeyguardController keyguard = mSupervisor.getKeyguardController();
@@ -340,7 +339,7 @@
         final ActivityRecord activity = new ActivityBuilder(mService).setCreateTask(true)
                 .setStack(stack).build();
 
-        mSupervisor.mFocusedStack = focusedStack;
+        mSupervisor.mFocusedStack = emptyStack;
 
         doAnswer((InvocationOnMock invocationOnMock) -> {
             final SparseIntArray displayIds = invocationOnMock.<SparseIntArray>getArgument(0);
@@ -359,6 +358,12 @@
         assertEquals(null, mService.mStackSupervisor.topRunningActivityLocked(
                 true /* considerKeyguardState */));
 
+        // Change focus to stack with activity.
+        mSupervisor.mFocusedStack = stack;
+        assertEquals(activity, mService.mStackSupervisor.topRunningActivityLocked());
+        assertEquals(null, mService.mStackSupervisor.topRunningActivityLocked(
+                true /* considerKeyguardState */));
+
         // Add activity that should be shown on the keyguard.
         final ActivityRecord showWhenLockedActivity = new ActivityBuilder(mService)
                 .setCreateTask(true)
@@ -370,6 +375,13 @@
         assertEquals(showWhenLockedActivity, mService.mStackSupervisor.topRunningActivityLocked());
         assertEquals(showWhenLockedActivity, mService.mStackSupervisor.topRunningActivityLocked(
                 true /* considerKeyguardState */));
+
+        // Change focus back to empty stack
+        mSupervisor.mFocusedStack = emptyStack;
+        // Ensure the show when locked activity is returned when not the focused stack
+        assertEquals(showWhenLockedActivity, mService.mStackSupervisor.topRunningActivityLocked());
+        assertEquals(showWhenLockedActivity, mService.mStackSupervisor.topRunningActivityLocked(
+                true /* considerKeyguardState */));
     }
 
     /**