From 6820d70823930954b723ca39fbf89f17aa0109c6 Mon Sep 17 00:00:00 2001 From: Louis Chang Date: Thu, 15 Apr 2021 12:11:07 +0800 Subject: [PATCH] Detects all activities for whether showing work challenge Work challenge did not show when a work activity is not on top, but still visible after screen turns on. Also show work challenge even if the work activity is behind a top fullscreen activity of another profile because the user can still navigate back to the work activity when top activity finishes. Bug: 177457096 Test: ActivityStackSupervisorTests Change-Id: I5e09b09be547d04fdfd709cb9cd4bcd4a94bbf21 Merged-In: I5e09b09be547d04fdfd709cb9cd4bcd4a94bbf21 --- .../server/am/ActivityStackSupervisor.java | 37 ++++------------ .../am/ActivityStackSupervisorTests.java | 42 ++++++++++++++++--- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java index 87e596f870abc..814d580749182 100644 --- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java +++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java @@ -852,27 +852,7 @@ public class ActivityStackSupervisor extends ConfigurationContainer implements D } /** - * Detects whether we should show a lock screen in front of this task for a locked user. - *

- * We'll do this if either of the following holds: - *

- * - * @return {@code true} if the top activity looks like it belongs to {@param userId}. - */ - private boolean taskTopActivityIsUser(TaskRecord task, @UserIdInt int userId) { - // To handle the case that work app is in the task but just is not the top one. - final ActivityRecord activityRecord = task.getTopActivity(); - final ActivityRecord resultTo = (activityRecord != null ? activityRecord.resultTo : null); - - return (activityRecord != null && activityRecord.userId == userId) - || (resultTo != null && resultTo.userId == userId); - } - - /** - * Find all visible task stacks containing {@param userId} and intercept them with an activity + * Find all task stacks containing {@param userId} and intercept them with an activity * to block out the contents and possibly start a credential-confirming intent. * * @param userId user handle for the locked managed profile. @@ -885,13 +865,14 @@ public class ActivityStackSupervisor extends ConfigurationContainer implements D final List tasks = stacks.get(stackNdx).getAllTasks(); for (int taskNdx = tasks.size() - 1; taskNdx >= 0; taskNdx--) { final TaskRecord task = tasks.get(taskNdx); - - // Check the task for a top activity belonging to userId, or returning a result - // to an activity belonging to userId. Example case: a document picker for - // personal files, opened by a work app, should still get locked. - if (taskTopActivityIsUser(task, userId)) { - mService.mTaskChangeNotificationController.notifyTaskProfileLocked( - task.taskId, userId); + for (int activityNdx = task.mActivities.size() - 1; activityNdx >= 0; + activityNdx--) { + final ActivityRecord activity = task.mActivities.get(activityNdx); + if (!activity.finishing && activity.userId == userId) { + mService.mTaskChangeNotificationController.notifyTaskProfileLocked( + task.taskId, userId); + break; + } } } } 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 661dd4fc828c8..986037ed0c8a9 100644 --- a/services/tests/servicestests/src/com/android/server/am/ActivityStackSupervisorTests.java +++ b/services/tests/servicestests/src/com/android/server/am/ActivityStackSupervisorTests.java @@ -18,25 +18,26 @@ package com.android.server.am; import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID; import static android.app.ActivityManager.StackId.PINNED_STACK_ID; + +import static com.android.server.am.ActivityStackSupervisor.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; import android.content.ComponentName; import android.graphics.Rect; +import android.os.RemoteException; +import android.os.UserHandle; import android.platform.test.annotations.Presubmit; import android.support.test.filters.MediumTest; import android.support.test.runner.AndroidJUnit4; -import org.junit.runner.RunWith; import org.junit.Test; +import org.junit.runner.RunWith; import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static com.android.server.am.ActivityStackSupervisor.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS_AND_RESTORE; /** * Tests for the {@link ActivityStackSupervisor} class. @@ -135,4 +136,33 @@ public class ActivityStackSupervisorTests extends ActivityTestsBase { assertFalse(service.mStackSupervisor.mStoppingActivities.contains(firstActivity)); } + + @Test + public void testLockAllProfileTasks() throws Exception { + // Make an activity visible with the user id set to 1 + final ActivityManagerService service = createActivityManagerService(); + final TaskRecord task = createTask(service, testActivityComponent, + FULLSCREEN_WORKSPACE_STACK_ID); + final ActivityRecord activity = createActivity(service, testActivityComponent, task, 1); + + // Create another activity on top and the user id is 2 + final ActivityRecord topActivity = createActivity(service, testActivityComponent, task, 2); + + // Make sure the listeners will be notified for putting the task to locked state + LocalTaskStackListener listener = new LocalTaskStackListener(); + service.registerTaskStackListener(listener); + service.mStackSupervisor.lockAllProfileTasks(1); + assertTrue(listener.mTaskProfileLocked); + service.unregisterTaskStackListener(listener); + } + + private class LocalTaskStackListener extends android.app.TaskStackListener { + boolean mTaskProfileLocked; + + @Override + public void onTaskProfileLocked(int taskId, int userId) { + super.onTaskProfileLocked(taskId, userId); + mTaskProfileLocked = true; + } + } }