Avoid locking profile task when it is already lock

WorkLockActivity was started repeatedly on top of the
task that contains work apps when turning screen on
and off over and over. So, lots of the WorkLockActivity
instances were created and added in the task, which
caused system sluggish.

Bug: 168527567
Bug: 177457096
Bug: 191385314
Test: manually test work challenges
Test: RootWindowContainerTests
Change-Id: Iac345471ef3badad6b9e5c0cc2873c60938663eb
diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java
index ea80b8b..c510603 100644
--- a/services/core/java/com/android/server/wm/RootWindowContainer.java
+++ b/services/core/java/com/android/server/wm/RootWindowContainer.java
@@ -17,6 +17,7 @@
 package com.android.server.wm;
 
 import static android.app.ActivityTaskManager.INVALID_TASK_ID;
+import static android.app.KeyguardManager.ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER;
 import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
 import static android.app.WindowConfiguration.ACTIVITY_TYPE_DREAM;
 import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
@@ -3383,6 +3384,15 @@
      */
     void lockAllProfileTasks(@UserIdInt int userId) {
         forAllLeafTasks(task -> {
+            final ActivityRecord top = task.topRunningActivity();
+            if (top != null && !top.finishing
+                    && ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER.equals(top.intent.getAction())
+                    && top.packageName.equals(
+                            mService.getSysUiServiceComponentLocked().getPackageName())) {
+                // Do nothing since the task is already secure by sysui.
+                return;
+            }
+
             if (task.getActivity(activity -> !activity.finishing && activity.mUserId == userId)
                     != null) {
                 mService.getTaskChangeNotificationController().notifyTaskProfileLocked(
diff --git a/services/tests/wmtests/src/com/android/server/wm/RootWindowContainerTests.java b/services/tests/wmtests/src/com/android/server/wm/RootWindowContainerTests.java
index 9267285..9cf29d4 100644
--- a/services/tests/wmtests/src/com/android/server/wm/RootWindowContainerTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/RootWindowContainerTests.java
@@ -16,6 +16,7 @@
 
 package com.android.server.wm;
 
+import static android.app.KeyguardManager.ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER;
 import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
 import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS;
 import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
@@ -53,6 +54,7 @@
 import static org.mockito.ArgumentMatchers.contains;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.refEq;
+import static org.mockito.Mockito.clearInvocations;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.reset;
@@ -1012,12 +1014,26 @@
         // Create another activity on top and the user id is 1
         final ActivityRecord topActivity = new ActivityBuilder(mAtm).setTask(task)
                 .setUid(UserHandle.PER_USER_RANGE + 1).build();
+        doReturn(true).when(topActivity).okToShowLocked();
+        topActivity.intent.setAction(Intent.ACTION_MAIN);
 
         // Make sure the listeners will be notified for putting the task to locked state
         TaskChangeNotificationController controller = mAtm.getTaskChangeNotificationController();
         spyOn(controller);
         mWm.mRoot.lockAllProfileTasks(0);
         verify(controller).notifyTaskProfileLocked(eq(taskId), eq(0));
+
+        // Create the work lock activity on top of the task
+        final ActivityRecord workLockActivity = new ActivityBuilder(mAtm).setTask(task)
+                .setUid(UserHandle.PER_USER_RANGE + 1).build();
+        doReturn(true).when(workLockActivity).okToShowLocked();
+        workLockActivity.intent.setAction(ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER);
+        doReturn(workLockActivity.mActivityComponent).when(mAtm).getSysUiServiceComponentLocked();
+
+        // Make sure the listener won't be notified again.
+        clearInvocations(controller);
+        mWm.mRoot.lockAllProfileTasks(0);
+        verify(controller, never()).notifyTaskProfileLocked(anyInt(), anyInt());
     }
 
     /**