From 523f3050065010afb4a0b7d409a9a062535f6b29 Mon Sep 17 00:00:00 2001 From: Pinyao Ting Date: Sat, 22 Jan 2022 05:12:10 +0000 Subject: [PATCH] Enable Shortcut integration with AppSearch. Static shortcuts and dynamic shortcuts will now be additionally persisted in AppSearch in a background thread when they are published. Shortcuts persisted in AppSearch have a TTL of 90 days and will be removed if they are not republished (i.e. used by the user) within the timeframe. The number of shortcuts persisted in AppSearch are not bounded by the limit defined in ShortcutManager#getMaxShortcutCountPerActivity. Bug: 151359749 Test: atest CtsShortcutManagerTestCases Change-Id: I6b1474bf60529e43172a0facae5561d2d89ede46 --- .../android/server/pm/ShortcutPackage.java | 23 +++++++++++-------- .../android/server/pm/ShortcutService.java | 19 +++++++++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/services/core/java/com/android/server/pm/ShortcutPackage.java b/services/core/java/com/android/server/pm/ShortcutPackage.java index 72db2428c8c97..bda25895a4b37 100644 --- a/services/core/java/com/android/server/pm/ShortcutPackage.java +++ b/services/core/java/com/android/server/pm/ShortcutPackage.java @@ -1694,15 +1694,6 @@ class ShortcutPackage extends ShortcutPackageItem { for (int j = 0; j < shareTargetSize; j++) { mShareTargets.get(j).saveToXml(out); } - synchronized (mLock) { - final Map copy = mShortcuts; - if (!mTransientShortcuts.isEmpty()) { - copy.putAll(mTransientShortcuts); - mTransientShortcuts.clear(); - } - saveShortcutsAsync(copy.values().stream().filter(ShortcutInfo::usesQuota).collect( - Collectors.toList())); - } } out.endTag(null, TAG_ROOT); @@ -2418,6 +2409,18 @@ class ShortcutPackage extends ShortcutPackageItem { }))); } + void persistsAllShortcutsAsync() { + synchronized (mLock) { + final Map copy = mShortcuts; + if (!mTransientShortcuts.isEmpty()) { + copy.putAll(mTransientShortcuts); + mTransientShortcuts.clear(); + } + saveShortcutsAsync(copy.values().stream().filter(ShortcutInfo::usesQuota).collect( + Collectors.toList())); + } + } + private void saveShortcutsAsync( @NonNull final Collection shortcuts) { Objects.requireNonNull(shortcuts); @@ -2489,7 +2492,7 @@ class ShortcutPackage extends ShortcutPackageItem { mIsAppSearchSchemaUpToDate = true; } } catch (Exception e) { - Slog.e(TAG, "Failed to invoke app search pkg=" + Slog.e(TAG, "Failed to create app search session. pkg=" + getPackageName() + " user=" + mShortcutUser.getUserId(), e); Objects.requireNonNull(future).completeExceptionally(e); } finally { diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java index 8393deea1e7b3..27605789891e3 100644 --- a/services/core/java/com/android/server/pm/ShortcutService.java +++ b/services/core/java/com/android/server/pm/ShortcutService.java @@ -488,7 +488,7 @@ public class ShortcutService extends IShortcutService.Stub { mShortcutBitmapSaver = new ShortcutBitmapSaver(this); mShortcutDumpFiles = new ShortcutDumpFiles(this); mIsAppSearchEnabled = DeviceConfig.getBoolean(NAMESPACE_SYSTEMUI, - SystemUiDeviceConfigFlags.SHORTCUT_APPSEARCH_INTEGRATION, false); + SystemUiDeviceConfigFlags.SHORTCUT_APPSEARCH_INTEGRATION, true); if (onlyForPackageManagerApis) { return; // Don't do anything further. For unit tests only. @@ -736,7 +736,7 @@ public class ShortcutService extends IShortcutService.Stub { Slog.d(TAG, "unloadUserLocked: user=" + userId); } // Save all dirty information. - saveDirtyInfo(); + saveDirtyInfo(false); // Unload mUsers.delete(userId); @@ -1191,6 +1191,10 @@ public class ShortcutService extends IShortcutService.Stub { @VisibleForTesting void saveDirtyInfo() { + saveDirtyInfo(true); + } + + private void saveDirtyInfo(boolean saveShortcutsInAppSearch) { if (DEBUG || DEBUG_REBOOT) { Slog.d(TAG, "saveDirtyInfo"); } @@ -1205,6 +1209,10 @@ public class ShortcutService extends IShortcutService.Stub { if (userId == UserHandle.USER_NULL) { // USER_NULL for base state. saveBaseStateLocked(); } else { + if (saveShortcutsInAppSearch) { + getUserShortcutsLocked(userId).forAllPackages( + ShortcutPackage::persistsAllShortcutsAsync); + } saveUserLocked(userId); } } @@ -3719,13 +3727,16 @@ public class ShortcutService extends IShortcutService.Stub { private final BroadcastReceiver mShutdownReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { + if (DEBUG || DEBUG_REBOOT) { + Slog.d(TAG, "Shutdown broadcast received."); + } // Since it cleans up the shortcut directory and rewrite the ShortcutPackageItems // in odrder during saveToXml(), it could lead to shortcuts missing when shutdown. // We need it so that it can finish up saving before shutdown. synchronized (mLock) { if (mHandler.hasCallbacks(mSaveDirtyInfoRunner)) { mHandler.removeCallbacks(mSaveDirtyInfoRunner); - saveDirtyInfo(); + saveDirtyInfo(false); } mShutdown.set(true); } @@ -4394,7 +4405,7 @@ public class ShortcutService extends IShortcutService.Stub { // Save to the filesystem. scheduleSaveUser(userId); - saveDirtyInfo(); + saveDirtyInfo(false); // Note, in case of backup, we don't have to wait on bitmap saving, because we don't // back up bitmaps anyway.