Merge "Keeping folders synced when applications are uninstalled"
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index 60c7ed4..0454430 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -76,7 +76,6 @@
     private final LayoutInflater mInflater;
     private final IconCache mIconCache;
     private int mState = STATE_NONE;
-    private int[] mDragItemPosition = new int[2];
     private static final int FULL_GROW = 0;
     private static final int PARTIAL_GROW = 1;
     private static final int REORDER_ANIMATION_DURATION = 230;
@@ -213,8 +212,6 @@
 
             mLauncher.getWorkspace().onDragStartedWithItem(v);
             mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY);
-            mDragItemPosition[0] = item.cellX;
-            mDragItemPosition[1] = item.cellY;
             mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
 
             mCurrentDragInfo = item;
@@ -865,7 +862,7 @@
     public void onRemove(ShortcutInfo item) {
         mItemsInvalidated = true;
         if (item == mCurrentDragInfo) return;
-        View v = mContent.getChildAt(mDragItemPosition[0], mDragItemPosition[1]);
+        View v = getViewForInfo(item);
         mContent.removeView(v);
         if (mState == STATE_ANIMATING) {
             mRearrangeOnClose = true;
@@ -877,6 +874,18 @@
         }
     }
 
+    private View getViewForInfo(ShortcutInfo item) {
+        for (int j = 0; j < mContent.getCountY(); j++) {
+            for (int i = 0; i < mContent.getCountX(); i++) {
+                View v = mContent.getChildAt(i, j);
+                if (v.getTag() == item) {
+                    return v;
+                }
+            }
+        }
+        return null;
+    }
+
     public void onItemsChanged() {
     }
     public void onTitleChanged(CharSequence title) {
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index fcf8d66..7fa96fb 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -3409,9 +3409,9 @@
                         } else if (tag instanceof FolderInfo) {
                             final FolderInfo info = (FolderInfo) tag;
                             final ArrayList<ShortcutInfo> contents = info.contents;
-                            final ArrayList<ShortcutInfo> toRemove = new ArrayList<ShortcutInfo>(1);
                             final int contentsCount = contents.size();
-                            boolean removedFromFolder = false;
+                            final ArrayList<ShortcutInfo> appsToRemoveFromFolder =
+                                    new ArrayList<ShortcutInfo>();
 
                             for (int k = 0; k < contentsCount; k++) {
                                 final ShortcutInfo appInfo = contents.get(k);
@@ -3421,19 +3421,14 @@
                                 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
                                     for (String packageName: packageNames) {
                                         if (packageName.equals(name.getPackageName())) {
-                                            toRemove.add(appInfo);
-                                            LauncherModel.deleteItemFromDatabase(mLauncher, appInfo);
-                                            removedFromFolder = true;
+                                            appsToRemoveFromFolder.add(appInfo);
                                         }
                                     }
                                 }
                             }
-
-                            contents.removeAll(toRemove);
-                            if (removedFromFolder) {
-                                final Folder folder = getOpenFolder();
-                                if (folder != null)
-                                    folder.notifyDataSetChanged();
+                            for (ShortcutInfo item: appsToRemoveFromFolder) {
+                                info.remove(item);
+                                LauncherModel.deleteItemFromDatabase(mLauncher, item);
                             }
                         } else if (tag instanceof LauncherAppWidgetInfo) {
                             final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;