From 4dedfc8ba3d8654d9cbf006101770e2ee4331837 Mon Sep 17 00:00:00 2001 From: Pinyao Ting Date: Tue, 30 Mar 2021 14:34:04 -0700 Subject: [PATCH] Fixes an issue in shortcut persistence. Currently after reboot the shortcuts previously saved in AppSearch could be lost since ShortcutService never calls AppSearchSession#close. Closing the session upon device shutdown fixes the issue. Also, removes the exception where calling AppSearchSession#close gives rise to IllegalArgumentException when the call wasn't made from a binder thread. Since ShortcutService runs in the system process, it is expected that calling AppSearchSession#close doesn't goes through binder. Bug: 183982287 Test: flash, pin shortcuts, reboot, observe Test: atest ShortcutManagerTest1 ShortcutManagerTest2 ShortcutManagerTest3 ShortcutManagerTest4 ShortcutManagerTest5 ShortcutManagerTest6 ShortcutManagerTest7 ShortcutManagerTest8 ShortcutManagerTest9 ShortcutManagerTest10 ShortcutManagerTest11 Test: atest CtsShortcutManagerTestCases Change-Id: I61bc4354735770630965ca4f81314d23671165d0 --- .../server/appsearch/AppSearchManagerService.java | 2 +- .../com/android/server/pm/ShortcutPackage.java | 14 ++++++++++++++ .../com/android/server/pm/ShortcutService.java | 5 ++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java b/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java index 991dda7cacd2d..7049d37f6fe7b 100644 --- a/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java +++ b/apex/appsearch/service/java/com/android/server/appsearch/AppSearchManagerService.java @@ -640,7 +640,7 @@ public class AppSearchManagerService extends SystemService { @Override public void persistToDisk(@UserIdInt int userId) { - int callingUid = Binder.getCallingUidOrThrow(); + int callingUid = Binder.getCallingUid(); int callingUserId = handleIncomingUser(userId, callingUid); final long callingIdentity = Binder.clearCallingIdentity(); try { diff --git a/services/core/java/com/android/server/pm/ShortcutPackage.java b/services/core/java/com/android/server/pm/ShortcutPackage.java index 2dad8ceb4add5..b02a21da3d696 100644 --- a/services/core/java/com/android/server/pm/ShortcutPackage.java +++ b/services/core/java/com/android/server/pm/ShortcutPackage.java @@ -2419,6 +2419,20 @@ class ShortcutPackage extends ShortcutPackageItem { } } + void closeAppSearchSession() { + synchronized (mLock) { + if (mAppSearchSession != null) { + final long callingIdentity = Binder.clearCallingIdentity(); + try { + mAppSearchSession.close(); + } finally { + Binder.restoreCallingIdentity(callingIdentity); + } + } + mAppSearchSession = null; + } + } + @NonNull private AndroidFuture setupSchema( @NonNull final AppSearchSession session) { diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java index d86438bb4a361..4cfa353b36f85 100644 --- a/services/core/java/com/android/server/pm/ShortcutService.java +++ b/services/core/java/com/android/server/pm/ShortcutService.java @@ -1050,7 +1050,10 @@ public class ShortcutService extends IShortcutService.Stub { file.failWrite(os); } - getUserShortcutsLocked(userId).logSharingShortcutStats(mMetricsLogger); + final ShortcutUser user = getUserShortcutsLocked(userId); + // Close AppSearchSession to flush pending changes. + user.forAllPackages(ShortcutPackage::closeAppSearchSession); + user.logSharingShortcutStats(mMetricsLogger); } @GuardedBy("mLock")