From efd686af335b918188167bdb185fc2aa7a40a9b5 Mon Sep 17 00:00:00 2001
From: Pinyao Ting <pinyaoting@google.com>
Date: Wed, 15 May 2024 18:19:55 +0000
Subject: [PATCH] Unblock the registration of shortcut change callbacks

This CL avoids blocking the registration of change listeners while
performing disk I/O in the background.

Bug: 309815010
Test: manual
Flag: EXEMPT bugfix
Change-Id: Iedfc4d86d221b35cc95c3660ab70c30dfd61546b
---
 .../com/android/server/pm/ShortcutService.java  | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java
index 9edf3b14bad7..ac16d6c1588a 100644
--- a/services/core/java/com/android/server/pm/ShortcutService.java
+++ b/services/core/java/com/android/server/pm/ShortcutService.java
@@ -320,10 +320,10 @@ public class ShortcutService extends IShortcutService.Stub {
 
     private final Handler mHandler;
 
-    @GuardedBy("mServiceLock")
+    @GuardedBy("itself")
     private final ArrayList<ShortcutChangeListener> mListeners = new ArrayList<>(1);
 
-    @GuardedBy("mServiceLock")
+    @GuardedBy("itself")
     private final ArrayList<LauncherApps.ShortcutChangeCallback> mShortcutChangeCallbacks =
             new ArrayList<>(1);
 
@@ -1847,7 +1847,9 @@ public class ShortcutService extends IShortcutService.Stub {
                         return;
                     }
 
-                    copy = new ArrayList<>(mListeners);
+                    synchronized (mListeners) {
+                        copy = new ArrayList<>(mListeners);
+                    }
                 }
                 // Note onShortcutChanged() needs to be called with the system service permissions.
                 for (int i = copy.size() - 1; i >= 0; i--) {
@@ -1872,8 +1874,9 @@ public class ShortcutService extends IShortcutService.Stub {
                     if (!isUserUnlockedL(userId)) {
                         return;
                     }
-
-                    copy = new ArrayList<>(mShortcutChangeCallbacks);
+                    synchronized (mShortcutChangeCallbacks) {
+                        copy = new ArrayList<>(mShortcutChangeCallbacks);
+                    }
                 }
                 for (int i = copy.size() - 1; i >= 0; i--) {
                     if (!CollectionUtils.isEmpty(changedList)) {
@@ -3425,7 +3428,7 @@ public class ShortcutService extends IShortcutService.Stub {
 
         @Override
         public void addListener(@NonNull ShortcutChangeListener listener) {
-            synchronized (mServiceLock) {
+            synchronized (mListeners) {
                 mListeners.add(Objects.requireNonNull(listener));
             }
         }
@@ -3433,7 +3436,7 @@ public class ShortcutService extends IShortcutService.Stub {
         @Override
         public void addShortcutChangeCallback(
                 @NonNull LauncherApps.ShortcutChangeCallback callback) {
-            synchronized (mServiceLock) {
+            synchronized (mShortcutChangeCallbacks) {
                 mShortcutChangeCallbacks.add(Objects.requireNonNull(callback));
             }
         }
-- 
GitLab