Do not consider overlays when finding task to reuse.

An overlay should not be considered the top activity in a task when
considering a task when launching an activity. Doing so will alter
the behavior of launch modes, such as singleTop and singleTask. In
these cases, the developer has chosen such mode with the expectation
that their task will not have activities from other tasks placed on
top, which is the case for features such as locking a work profile.

This changelist addresses the issue by not considering an overlay to
be the top activity when finding a task based on ActivityRecord.

Fixes: 64839155
Test: bit FrameworksServicesTests:com.android.server.am.ActivityStackTests#testFindTaskWithOverlay
Change-Id: I2684baf6929e5af321404e2eef597f456ff87ee8
diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityStackTests.java b/services/tests/servicestests/src/com/android/server/am/ActivityStackTests.java
index 80e241c..02fba08 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityStackTests.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityStackTests.java
@@ -24,6 +24,7 @@
 import android.app.ActivityManager;
 import android.content.ComponentName;
 import android.content.pm.ActivityInfo;
+import android.os.UserHandle;
 import android.platform.test.annotations.Presubmit;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
@@ -44,6 +45,8 @@
     private static final int TEST_STACK_ID = 100;
     private static final ComponentName testActivityComponent =
             ComponentName.unflattenFromString("com.foo/.BarActivity");
+    private static final ComponentName testOverlayComponent =
+            ComponentName.unflattenFromString("com.foo/.OverlayActivity");
 
     @Test
     public void testEmptyTaskCleanupOnRemove() throws Exception {
@@ -98,4 +101,25 @@
 
         testStack.stopActivityLocked(activityRecord);
     }
+
+    @Test
+    public void testFindTaskWithOverlay() throws Exception {
+        final ActivityManagerService service = createActivityManagerService();
+        final TaskRecord task = createTask(service, testActivityComponent, TEST_STACK_ID);
+        final ActivityRecord activityRecord = createActivity(service, testActivityComponent, task,
+                0);
+        // Overlay must be for a different user to prevent recognizing a matching top activity
+        final ActivityRecord taskOverlay = createActivity(service, testOverlayComponent, task,
+                UserHandle.PER_USER_RANGE * 2);
+        taskOverlay.mTaskOverlay = true;
+
+        final ActivityStack testStack = service.mStackSupervisor.getStack(TEST_STACK_ID);
+        final ActivityStackSupervisor.FindTaskResult result =
+                new ActivityStackSupervisor.FindTaskResult();
+        testStack.findTaskLocked(activityRecord, result);
+
+        assertEquals(task.getTopActivity(false /* includeOverlays */), activityRecord);
+        assertEquals(task.getTopActivity(true /* includeOverlays */), taskOverlay);
+        assertNotNull(result.r);
+    }
 }