OSDN Git Service

Trebuchet: Remove stats/tracking
[android-x86/packages-apps-Trebuchet.git] / src / com / android / launcher3 / LauncherModel.java
index e90ea17..2074ad6 100644 (file)
@@ -34,6 +34,7 @@ import android.content.pm.ProviderInfo;
 import android.content.pm.ResolveInfo;
 import android.database.Cursor;
 import android.graphics.Bitmap;
+import android.graphics.Point;
 import android.net.Uri;
 import android.os.Environment;
 import android.os.Handler;
@@ -66,7 +67,6 @@ import com.android.launcher3.util.Thunk;
 import cyanogenmod.providers.CMSettings;
 
 import com.android.launcher3.settings.SettingsProvider;
-import com.android.launcher3.stats.internal.service.AggregationIntentService;
 
 import java.lang.ref.WeakReference;
 import java.net.URISyntaxException;
@@ -211,8 +211,12 @@ public class LauncherModel extends BroadcastReceiver
         public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
         public void bindComponentsRemoved(ArrayList<String> packageNames,
                         ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
+        public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
         public void bindAllPackages(WidgetsModel model);
         public void bindSearchProviderChanged();
+        public void bindComponentsUnavailable(ArrayList<String> packageNames,
+                ArrayList<AppInfo> appInfos);
+        public void bindComponentsAvailable(ArrayList<ItemInfo> itemInfos);
         public boolean isAllAppsButtonRank(int rank);
         public void onPageBoundSynchronously(int page);
         public void dumpLogsToLocalData();
@@ -1059,7 +1063,6 @@ public class LauncherModel extends BroadcastReceiver
         int widgetCount = LauncherModel.sBgAppWidgets.size();
         SharedPreferences prefs = context.getSharedPreferences(LauncherAppState
                         .getSharedPreferencesKey(), Context.MODE_PRIVATE);
-        prefs.edit().putInt(AggregationIntentService.PREF_KEY_WIDGET_COUNT, widgetCount).apply();
     }
 
     /**
@@ -1219,7 +1222,6 @@ public class LauncherModel extends BroadcastReceiver
         int pageCount = LauncherModel.sBgWorkspaceScreens.size();
         SharedPreferences prefs = context.getSharedPreferences(LauncherAppState
                         .getSharedPreferencesKey(), Context.MODE_PRIVATE);
-        prefs.edit().putInt(AggregationIntentService.PREF_KEY_PAGE_COUNT, pageCount).apply();
     }
 
     /**
@@ -1829,13 +1831,54 @@ public class LauncherModel extends BroadcastReceiver
             // Check if any workspace icons overlap with each other
             for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
                 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
-                    if (!shouldResizeAndUpdateDB && screens[x][y] != null) {
-                        Log.e(TAG, "Error loading shortcut " + item
-                            + " into cell (" + containerIndex + "-" + item.screenId + ":"
-                            + x + "," + y
-                            + ") occupied by "
-                            + screens[x][y]);
-                        return false;
+                    if (screens[x][y] != null) {
+                        if (!shouldResizeAndUpdateDB) {
+                            Log.e(TAG, "Error loading shortcut " + item
+                                    + " into cell (" + containerIndex + "-" + item.screenId + ":"
+                                    + x + "," + y
+                                    + ") occupied by "
+                                    + screens[x][y]);
+                            return false;
+                        }
+                        ItemInfo occupiedItem = screens[x][y];
+                        // If an item is overlapping another because one of them
+                        // was moved due to the size of the grid changing,
+                        // move the current item to a free spot past this one.
+                        if (occupiedItem.wasMovedDueToReducedSpace
+                                || item.wasMovedDueToReducedSpace) {
+                            // overlapping icon exists here
+                            // we must find a free space.
+                            boolean freeFound = false;
+                            int nextX = 0;
+                            int nextY = 0;
+                            while (!freeFound) {
+                                if (screens[nextX][nextY] == null) {
+                                    item.cellX = nextX;
+                                    item.cellY = nextY;
+                                    freeFound = true;
+                                } else {
+                                    if (nextX + item.spanX == countX) {
+                                        if (nextY + item.spanY == countY) {
+                                            // If we've reached the bottom of the page and are still
+                                            // searching, add a new page to place this item.
+                                            item.screenId += 1;
+                                            nextY = 0;
+                                            nextX = 0;
+                                            if (!occupied.containsKey(item.screenId)) {
+                                                ItemInfo[][] items = new ItemInfo[countX][countY];
+                                                occupied.put(item.screenId, items);
+                                            }
+                                            screens = occupied.get(item.screenId);
+                                        } else {
+                                            nextX = 0;
+                                            nextY++;
+                                        }
+                                    } else {
+                                        nextX++;
+                                    }
+                                }
+                            }
+                        }
                     }
                 }
             }
@@ -2447,13 +2490,43 @@ public class LauncherModel extends BroadcastReceiver
                 for (FolderInfo folder : sBgFolders) {
                     Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
                     int pos = 0;
+                    int needIndexing = 0;
                     for (ShortcutInfo info : folder.contents) {
-                        if (info.usingLowResIcon) {
+                        if (info.usingLowResIcon && pos < FolderIcon.NUM_ITEMS_IN_PREVIEW) {
                             info.updateIcon(mIconCache, false);
                         }
                         pos ++;
-                        if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
-                            break;
+                        needIndexing += info.screenId + info.cellX + info.cellY;
+                    }
+                    // If all screenId, cellX, and cellY are 0, then we assume they were all null.
+                    if (needIndexing == 0) {
+                        synchronized (sBgLock) {
+                            int curX = 0;
+                            int curY = 0;
+                            int folderScreenId = 0;
+                            int folderCount = folder.contents.size();
+                            Point point = Utilities
+                                    .caluclateFolderContentDimensions(folderCount, countX, countY);
+                            int maxX = point.x;
+                            int maxY = point.y;
+                            for (ShortcutInfo info : folder.contents) {
+                                ItemInfo itemInfo = sBgItemsIdMap.get(info.id);
+                                if (curY == maxY) { // New screen
+                                    curX = 0;
+                                    curY = 0;
+                                    folderScreenId++;
+                                }
+                                itemInfo.screenId = folderScreenId;
+                                itemInfo.cellX = curX;
+                                itemInfo.cellY = curY;
+                                LauncherModel.updateItemInDatabase(context, itemInfo);
+                                if (curX == maxX - 1) {
+                                    curX = 0;
+                                    curY++;
+                                } else {
+                                    curX++;
+                                }
+                            }
                         }
                     }
                 }
@@ -3290,6 +3363,7 @@ public class LauncherModel extends BroadcastReceiver
 
             final String[] packages = mPackages;
             final int N = packages.length;
+            final ArrayList<String> unavailable = new ArrayList<String>();
             switch (mOp) {
                 case OP_ADD: {
                     for (int i=0; i<N; i++) {
@@ -3328,6 +3402,9 @@ public class LauncherModel extends BroadcastReceiver
                         if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
                         mBgAllAppsList.removePackage(packages[i], mUser);
                         mApp.getWidgetCache().removePackage(packages[i], mUser);
+                        if (mOp == OP_UNAVAILABLE) {
+                            unavailable.add(packages[i]);
+                        }
                     }
                     break;
             }
@@ -3359,10 +3436,21 @@ public class LauncherModel extends BroadcastReceiver
                     new HashMap<ComponentName, AppInfo>();
 
             if (added != null) {
+                final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
                 addAppsToAllApps(context, added);
                 for (AppInfo ai : added) {
                     addedOrUpdatedApps.put(ai.componentName, ai);
                 }
+                mHandler.post(new Runnable() {
+                    public void run() {
+                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
+                        if (callbacks == cb && cb != null) {
+                            if (DEBUG_LOADERS) Log.d(TAG, "bindComponentsAvailable: " +
+                                    addedInfos.size());
+                            callbacks.bindComponentsAvailable(addedInfos);
+                        }
+                    }
+                });
             }
 
             if (modified != null) {
@@ -3536,6 +3624,17 @@ public class LauncherModel extends BroadcastReceiver
                 final int removeReason;
                 if (mOp == OP_UNAVAILABLE) {
                     removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
+                    // Call the packages-unavailable callback
+                    mHandler.post(new Runnable() {
+                        public void run() {
+                            Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
+                            if (callbacks == cb && cb != null) {
+                                if (DEBUG_LOADERS) Log.d(TAG, "bindComponentsUnavailable: " +
+                                        removedApps.size());
+                                callbacks.bindComponentsUnavailable(unavailable, removedApps);
+                            }
+                        }
+                    });
                 } else {
                     // Remove all the components associated with this package
                     for (String pn : removedPackageNames) {
@@ -3551,16 +3650,28 @@ public class LauncherModel extends BroadcastReceiver
 
                 // Remove any queued items from the install queue
                 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
-                // Call the components-removed callback
-                mHandler.post(new Runnable() {
-                    public void run() {
-                        Callbacks cb = getCallback();
-                        if (callbacks == cb && cb != null) {
-                            callbacks.bindComponentsRemoved(
+                if (mOp == OP_UNAVAILABLE) {
+                    // Call the packages-unavailable callback
+                    mHandler.post(new Runnable() {
+                        public void run() {
+                            Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
+                            if (callbacks == cb && cb != null) {
+                                 callbacks.bindComponentsUnavailable(unavailable, removedApps);
+                            }
+                        }
+                    });
+                } else {
+                    // Call the components-removed callback
+                    mHandler.post(new Runnable() {
+                        public void run() {
+                             Callbacks cb = getCallback();
+                             if (callbacks == cb && cb != null) {
+                                 callbacks.bindComponentsRemoved(
                                     removedPackageNames, removedApps, mUser, removeReason);
+                             }
                         }
-                    }
-                });
+                    });
+                }
             }
 
             // Update widgets