Merge "Reset stashed in Taskbar All Apps for gestures instead of IME insets." into 24D1-dev
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index 99fca62..be01d63 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -20,6 +20,7 @@
 
 import static com.android.launcher3.LauncherAppState.ACTION_FORCE_ROLOAD;
 import static com.android.launcher3.config.FeatureFlags.IS_STUDIO_BUILD;
+import static com.android.launcher3.icons.cache.BaseIconCache.EMPTY_CLASS_NAME;
 import static com.android.launcher3.model.PackageUpdatedTask.OP_UPDATE;
 import static com.android.launcher3.pm.UserCache.ACTION_PROFILE_AVAILABLE;
 import static com.android.launcher3.pm.UserCache.ACTION_PROFILE_UNAVAILABLE;
@@ -27,6 +28,7 @@
 import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
 import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
 
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageInstaller;
@@ -70,6 +72,7 @@
 import com.android.launcher3.shortcuts.ShortcutRequest;
 import com.android.launcher3.util.IntSet;
 import com.android.launcher3.util.ItemInfoMatcher;
+import com.android.launcher3.util.PackageManagerHelper;
 import com.android.launcher3.util.PackageUserKey;
 import com.android.launcher3.util.Preconditions;
 
@@ -443,9 +446,18 @@
                     @NonNull final BgDataModel dataModel, @NonNull final AllAppsList apps) {
                 IconCache iconCache = app.getIconCache();
                 final IntSet removedIds = new IntSet();
-                HashSet<WorkspaceItemInfo> archivedItemsToCacheRefresh = new HashSet<>();
-                HashSet<String> archivedPackagesToCacheRefresh = new HashSet<>();
+                HashSet<WorkspaceItemInfo> archivedWorkspaceItemsToCacheRefresh = new HashSet<>();
+                boolean isAppArchived = new PackageManagerHelper(
+                        mApp.getContext()).isAppArchivedForUser(packageName, user);
                 synchronized (dataModel) {
+                    if (isAppArchived) {
+                        // Remove package icon cache entry for archived app in case of a session
+                        // failure.
+                        mApp.getIconCache().remove(
+                                new ComponentName(packageName, packageName + EMPTY_CLASS_NAME),
+                                user);
+                    }
+
                     for (ItemInfo info : dataModel.itemsIdMap) {
                         if (info instanceof WorkspaceItemInfo
                                 && ((WorkspaceItemInfo) info).hasPromiseIconUi()
@@ -456,19 +468,16 @@
                             }
                             if (((WorkspaceItemInfo) info).isArchived()) {
                                 WorkspaceItemInfo workspaceItem = (WorkspaceItemInfo) info;
-                                // Remove package cache icon for archived app in case of a session
-                                // failure.
-                                mApp.getIconCache().removeIconsForPkg(packageName, user);
                                 // Refresh icons on the workspace for archived apps.
                                 iconCache.getTitleAndIcon(workspaceItem,
                                         workspaceItem.usingLowResIcon());
-                                archivedPackagesToCacheRefresh.add(packageName);
-                                archivedItemsToCacheRefresh.add(workspaceItem);
+                                archivedWorkspaceItemsToCacheRefresh.add(workspaceItem);
                             }
                         }
                     }
-                    if (!archivedPackagesToCacheRefresh.isEmpty()) {
-                        apps.updateIconsAndLabels(archivedPackagesToCacheRefresh, user);
+
+                    if (isAppArchived) {
+                        apps.updateIconsAndLabels(new HashSet<>(List.of(packageName)), user);
                     }
                 }
 
@@ -477,8 +486,11 @@
                             ItemInfoMatcher.ofItemIds(removedIds),
                             "removed because install session failed");
                 }
-                if (!archivedItemsToCacheRefresh.isEmpty()) {
-                    bindUpdatedWorkspaceItems(archivedItemsToCacheRefresh.stream().toList());
+                if (!archivedWorkspaceItemsToCacheRefresh.isEmpty()) {
+                    bindUpdatedWorkspaceItems(
+                            archivedWorkspaceItemsToCacheRefresh.stream().toList());
+                }
+                if (isAppArchived) {
                     bindApplicationsIfNeeded();
                 }
             }
diff --git a/src/com/android/launcher3/allapps/AppInfoComparator.java b/src/com/android/launcher3/allapps/AppInfoComparator.java
index a0867db..bbf8e5a 100644
--- a/src/com/android/launcher3/allapps/AppInfoComparator.java
+++ b/src/com/android/launcher3/allapps/AppInfoComparator.java
@@ -18,6 +18,7 @@
 import android.content.Context;
 import android.os.Process;
 import android.os.UserHandle;
+import android.text.TextUtils;
 
 import com.android.launcher3.model.data.AppInfo;
 import com.android.launcher3.pm.UserCache;
@@ -64,7 +65,7 @@
     }
 
     private String getSortingTitle(AppInfo info) {
-        if (info.appTitle != null) {
+        if (!TextUtils.isEmpty(info.appTitle)) {
             return info.appTitle.toString();
         }
         if (info.title != null) {
diff --git a/src/com/android/launcher3/folder/Folder.java b/src/com/android/launcher3/folder/Folder.java
index ec9c27d..ecb5c8f 100644
--- a/src/com/android/launcher3/folder/Folder.java
+++ b/src/com/android/launcher3/folder/Folder.java
@@ -1039,6 +1039,9 @@
     public void onDropCompleted(final View target, final DragObject d,
             final boolean success) {
         if (success) {
+            if (getItemCount() <= 1) {
+                mDeleteFolderOnDropCompleted = true;
+            }
             if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
                 replaceFolderWithFinalItem();
             }
diff --git a/src/com/android/launcher3/icons/IconCache.java b/src/com/android/launcher3/icons/IconCache.java
index 41e3ef0..1633eba 100644
--- a/src/com/android/launcher3/icons/IconCache.java
+++ b/src/com/android/launcher3/icons/IconCache.java
@@ -222,6 +222,7 @@
      * Updates {@param application} only if a valid entry is found.
      */
     public synchronized void updateTitleAndIcon(AppInfo application) {
+        boolean preferPackageIcon = application.isArchived();
         CacheEntry entry = cacheLocked(application.componentName,
                 application.user, () -> null, mLauncherActivityInfoCachingLogic,
                 false, application.usingLowResIcon());
@@ -229,13 +230,12 @@
             return;
         }
 
-        boolean preferPackageIcon = application.isArchived();
         if (preferPackageIcon) {
             String packageName = application.getTargetPackage();
             CacheEntry packageEntry =
                     cacheLocked(new ComponentName(packageName, packageName + EMPTY_CLASS_NAME),
                             application.user, () -> null, mLauncherActivityInfoCachingLogic,
-                            false, application.usingLowResIcon());
+                            true, application.usingLowResIcon());
             applyPackageEntry(packageEntry, application, entry);
         } else {
             applyCacheEntry(entry, application);
diff --git a/tests/src/com/android/launcher3/dragging/TaplDragTest.java b/tests/src/com/android/launcher3/dragging/TaplDragTest.java
index d1227d8..bf1ba39 100644
--- a/tests/src/com/android/launcher3/dragging/TaplDragTest.java
+++ b/tests/src/com/android/launcher3/dragging/TaplDragTest.java
@@ -96,6 +96,21 @@
                 MAPS_APP_NAME);
     }
 
+    /**
+     * Adds two icons to the Workspace and combines them into a folder, then makes sure we are able
+     * to remove an icon from the folder and that the folder ceases to exist since it only has one
+     * icon left.
+     */
+    @Test
+    public void testDragOutOfFolder() {
+        final HomeAppIcon playStoreIcon = createShortcutIfNotExist(STORE_APP_NAME, 0, 1);
+        final HomeAppIcon photosIcon = createShortcutInCenterIfNotExist(PHOTOS_APP_NAME);
+        FolderIcon folderIcon = photosIcon.dragToIcon(playStoreIcon);
+        Folder folder = folderIcon.open();
+        folder.getAppIcon(STORE_APP_NAME).internalDragToWorkspace(false, false);
+        assertNotNull(mLauncher.getWorkspace().tryGetWorkspaceAppIcon(STORE_APP_NAME));
+        assertNotNull(mLauncher.getWorkspace().tryGetWorkspaceAppIcon(PHOTOS_APP_NAME));
+    }
 
     /** Drags a shortcut from a long press menu into the workspace.
      * 1. Open all apps and wait for load complete.