From fa1b2ba983bfa084089d035f498015036d31c727 Mon Sep 17 00:00:00 2001 From: Stefan Andonian Date: Tue, 3 Dec 2024 16:57:42 -0800 Subject: [PATCH] If Pinned Shortcuts aren't available, then check for pinned shortcuts by id/package name Bug: 356096502 Test: Verified locally that if you hard-code the PINNED shortcut query to not return data, the backup query still allows the deep shortcuts to show up on the home screen. Flag: EXEMPT bug fix Change-Id: I7a9b2d2c3be2ecce38a5af71951f1a12bc1c20ee --- .../android/launcher3/model/LoaderTask.java | 3 ++ .../launcher3/model/WorkspaceItemProcessor.kt | 24 +++++++++++++- .../model/WorkspaceItemProcessorTest.kt | 33 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/com/android/launcher3/model/LoaderTask.java b/src/com/android/launcher3/model/LoaderTask.java index f96e9591e6..88830398ce 100644 --- a/src/com/android/launcher3/model/LoaderTask.java +++ b/src/com/android/launcher3/model/LoaderTask.java @@ -261,6 +261,9 @@ public class LoaderTask implements Runnable { // sanitizeData should not be invoked if the workspace is loaded from a db different // from the main db as defined in the invariant device profile. // (e.g. both grid preview and minimal device mode uses a different db) + // TODO(b/384731096): Write Unit Test to make sure sanitizeWidgetsShortcutsAndPackages + // actually re-pins shortcuts that are in model but not in ShortcutManager, if possible + // after a simulated restore. if (Objects.equals(mApp.getInvariantDeviceProfile().dbFile, mDbName)) { verifyNotStopped(); sanitizeFolders(mItemsDeleted); diff --git a/src/com/android/launcher3/model/WorkspaceItemProcessor.kt b/src/com/android/launcher3/model/WorkspaceItemProcessor.kt index e86b5926a6..5b6ee60257 100644 --- a/src/com/android/launcher3/model/WorkspaceItemProcessor.kt +++ b/src/com/android/launcher3/model/WorkspaceItemProcessor.kt @@ -20,6 +20,7 @@ import android.appwidget.AppWidgetProviderInfo import android.content.ComponentName import android.content.Intent import android.content.pm.LauncherApps +import android.content.pm.LauncherApps.ShortcutQuery import android.content.pm.PackageInstaller import android.content.pm.ShortcutInfo import android.graphics.Point @@ -44,6 +45,7 @@ import com.android.launcher3.model.data.WorkspaceItemInfo import com.android.launcher3.pm.PackageInstallInfo import com.android.launcher3.pm.UserCache import com.android.launcher3.shortcuts.ShortcutKey +import com.android.launcher3.shortcuts.ShortcutRequest import com.android.launcher3.util.ApiWrapper import com.android.launcher3.util.ApplicationInfoWrapper import com.android.launcher3.util.ComponentKey @@ -270,7 +272,8 @@ class WorkspaceItemProcessor( c.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT -> { val key = ShortcutKey.fromIntent(intent, c.user) if (unlockedUsers[c.serialNumber]) { - val pinnedShortcut = shortcutKeyToPinnedShortcuts[key] + val pinnedShortcut = + shortcutKeyToPinnedShortcuts[key] ?: retryDeepShortcutById(key) if (pinnedShortcut == null) { // The shortcut is no longer valid. c.markDeleted( @@ -375,6 +378,25 @@ class WorkspaceItemProcessor( } } + /** + * It is possible that the data was cleared from ShortcutManager after it was restored. In that + * instance, the Launcher would have a valid Shortcut id, but ShortcutManager wouldn't recognize + * it as valid. Here we retry by querying ShortcutManager by package name and shortcut id. + */ + private fun retryDeepShortcutById(key: ShortcutKey): ShortcutInfo? { + FileLog.d(TAG, "retryDeepShortcutById: package=${key.packageName}, shortcutId=${key.id}") + return launcherApps + .getShortcuts( + ShortcutQuery().apply { + setPackage(key.packageName) + setShortcutIds(listOf(key.id)) + setQueryFlags(ShortcutRequest.ALL) + }, + key.user, + ) + ?.firstOrNull() + } + /** * Loads CollectionInfo information from the database and formats it. This function runs while * LoaderTask is still active; some of the processing for folder content items is done after all diff --git a/tests/multivalentTests/src/com/android/launcher3/model/WorkspaceItemProcessorTest.kt b/tests/multivalentTests/src/com/android/launcher3/model/WorkspaceItemProcessorTest.kt index 7099d38254..d699eeea02 100644 --- a/tests/multivalentTests/src/com/android/launcher3/model/WorkspaceItemProcessorTest.kt +++ b/tests/multivalentTests/src/com/android/launcher3/model/WorkspaceItemProcessorTest.kt @@ -392,6 +392,39 @@ class WorkspaceItemProcessorTest { verify(mockCursor).checkAndAddItem(any(), any(), anyOrNull()) } + @Test + fun `When Pinned Deep Shortcut is not stored in ShortcutManager re-query by Shortcut ID`() { + // Given + mockCursor.itemType = ITEM_TYPE_DEEP_SHORTCUT + val si = + mock().apply { + whenever(id).thenReturn("") + whenever(`package`).thenReturn("") + whenever(activity).thenReturn(mock()) + whenever(longLabel).thenReturn("") + whenever(isEnabled).thenReturn(true) + whenever(disabledMessage).thenReturn("") + whenever(disabledReason).thenReturn(0) + whenever(persons).thenReturn(EMPTY_PERSON_ARRAY) + } + whenever(mockLauncherApps.getShortcuts(any(), any())).thenReturn(listOf(si)) + mKeyToPinnedShortcutsMap.clear() + mIconRequestInfos = mutableListOf() + + // When + itemProcessorUnderTest = + createWorkspaceItemProcessorUnderTest(allDeepShortcuts = mAllDeepShortcuts) + itemProcessorUnderTest.processItem() + + // Then + verify(mockLauncherApps).getShortcuts(any(), any()) + assertWithMessage("item restoreFlag should be set to 0") + .that(mockCursor.restoreFlag) + .isEqualTo(0) + verify(mockCursor).markRestored() + verify(mockCursor).checkAndAddItem(any(), any(), anyOrNull()) + } + @Test fun `When Pinned Deep Shortcut not found then mark deleted`() {