LauncherModel.java revision 3611578579b4bfb25616085dafdb1a45207394f9
131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetManager;
20629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetProviderInfo;
21f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratoimport android.content.BroadcastReceiver;
2231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ComponentName;
235c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.ContentProviderClient;
2431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentResolver;
2531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentValues;
2631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Intent;
270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent.ShortcutIconResource;
2831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Context;
2931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ActivityInfo;
3031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.PackageManager;
315c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.pm.ProviderInfo;
3231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ResolveInfo;
3331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.res.Resources;
3431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.database.Cursor;
3531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Bitmap;
3631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.BitmapFactory;
3731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.net.Uri;
383611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.Handler;
393611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.HandlerThread;
400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.os.Parcelable;
415c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.os.RemoteException;
429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.util.Log;
4331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.os.Process;
449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.os.SystemClock;
4531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.lang.ref.WeakReference;
479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.net.URISyntaxException;
489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.text.Collator;
4931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport java.util.ArrayList;
5056d8291af6a28c6ba64113120efdf84a785e816cJoe Onoratoimport java.util.Arrays;
519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.util.Comparator;
529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.util.Collections;
5331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport java.util.HashMap;
5431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport java.util.List;
5531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
56edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guyimport com.android.launcher.R;
57edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
5831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
5931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Maintains in-memory state of the Launcher. It is expected that there should be only one
6031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * LauncherModel object held in a static. Also provide APIs for updating the database state
61bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project * for the Launcher.
6231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
63f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratopublic class LauncherModel extends BroadcastReceiver {
64a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final boolean DEBUG_LOADERS = false;
659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final String TAG = "Launcher.Model";
669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
68d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato    private int mBatchSize; // 0 is all apps at once
692ff10b3b0137d258569ce5471808842e88696133Daniel Sandler    private int mAllAppsLoadDelay; // milliseconds between batches
70dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
71f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    private final LauncherApplication mApp;
729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private HandlerThread mWorkerThread;
753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private Handler mWorker;
763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
78cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
79cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
80cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
81cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
82cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
83cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private AllAppsList mAllAppsList; // only access in worker thread
870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
920589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
98ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void finishBindingItems();
1009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
1019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAllApplications(ArrayList<ApplicationInfo> apps);
10264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
10364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
1043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
105843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        public boolean isAllAppsVisible();
1069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
10731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    LauncherModel(LauncherApplication app, IconCache iconCache) {
109f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        mApp = app;
1100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mAllAppsList = new AllAppsList(iconCache);
1110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
1120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mDefaultIcon = Utilities.createIconBitmap(
1140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                app.getPackageManager().getDefaultActivityIcon(), app);
1152ff10b3b0137d258569ce5471808842e88696133Daniel Sandler
1162ff10b3b0137d258569ce5471808842e88696133Daniel Sandler        mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
117d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
118d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato        mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
1193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorkerThread = new HandlerThread("launcher-loader");
1213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorkerThread.start();
1223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorker = new Handler(mWorkerThread.getLooper());
1230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
12556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
1260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
127f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
12831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
1319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
1329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
1349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY) {
1359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
1369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
1379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            addItemToDatabase(context, item, container, screen, cellX, cellY, false);
1389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
1399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
1409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            moveItemInDatabase(context, item, container, screen, cellX, cellY);
1419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
14331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
1469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
1489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int cellX, int cellY) {
1499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
1509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
1519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
1529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
15331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
1559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
156bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
1579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
1589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
1599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
1609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.SCREEN, item.screen);
161ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
1629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
16331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
16431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
1669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
1679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
168bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
1699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
1709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
1729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
1739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
1749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
1759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
1769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
1779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
1789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
1799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
181bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
182bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
183bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
1849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
18531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
1869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
1879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
1899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
1909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
1919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
1929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
193f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
1959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
1969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
2009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
2019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
202f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
2049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
2059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeUserFolder(folderList, id);
2079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeLiveFolder(folderList, id);
2109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
21231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
2149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
2159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
2169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.screen = c.getInt(screenIndex);
2179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.cellX = c.getInt(cellXIndex);
2189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.cellY = c.getInt(cellYIndex);
219ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
2219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
2229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
2239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
2242fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
225ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
2279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
22831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
2319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
2329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addItemToDatabase(Context context, ItemInfo item, long container,
2349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY, boolean notify) {
2359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
2369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
2379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
2389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
239ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
2419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
242ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
244ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
2469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
247ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (result != null) {
2499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            item.id = Integer.parseInt(result.getPathSegments().get(1));
25031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
25131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
25231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Update an item to the database in a specified container.
2559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void updateItemInDatabase(Context context, ItemInfo item) {
2579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
2589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
259f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
261ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
26331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
26431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Removes the specified item from the database
2679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param context
2689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param item
2699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteItemFromDatabase(Context context, ItemInfo item) {
2719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
272f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
2749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
275f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
2789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
2809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
281f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
2839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.CONTENT_URI,
2849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
2859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
286f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
2899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
2919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
2929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
293f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
294f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
295f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2961d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
2979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
2989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
2999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
300f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
3013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
3023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
3033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
3049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
3053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
3063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
3073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
3083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
3093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
310f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
31264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
3133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
3143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
3153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
3163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
31756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
3183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
3193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
3203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
3213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
3223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
32356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
3243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
3253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
3263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
3273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
3283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
3293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
3303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
33156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
3323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
33356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
3343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
3353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
336f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
337f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
3393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
3403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
341f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
3433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
3443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(
3453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        PackageUpdatedTask.OP_UNAVAILABLE, packages));
346f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
347f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
3483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
349f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void startLoader(Context context, boolean isLaunching) {
3513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
3523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
3533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
3543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
355843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
3563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
3573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
3583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
3593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LoaderTask oldTask = mLoaderTask;
3603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (oldTask != null) {
3613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (oldTask.isLaunching()) {
3623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // don't downgrade isLaunching if we're already running
3633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        isLaunching = true;
3649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
3653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    oldTask.stopLocked();
3669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
3673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask = new LoaderTask(context, isLaunching);
3683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorker.post(mLoaderTask);
369b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
370b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
3713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
372b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
3733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
3743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
3753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
3763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
3779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
378f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
3793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
380b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
3813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
3823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
3833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
3843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
3853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
3863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
3873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
3883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
3893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Thread mWaitThread;
3903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
3913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
3923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
3933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
3943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        LoaderTask(Context context, boolean isLaunching) {
3953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
3963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
3973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
398f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
4003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
4013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
402f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindWorkspace() {
4043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
405cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
4063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // For now, just always reload the workspace.  It's ~100 ms vs. the
4073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // binding which takes many hundreds of ms.
4083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // We can reconsider.
4093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
4103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
4113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
4123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (true || !mWorkspaceLoaded) {
4133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadWorkspace();
4143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
4153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
4169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
4173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorkspaceLoaded = true;
4183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
419cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
4203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
4213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            bindWorkspace();
4223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
423f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
4253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
4263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
4273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
4283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
4293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
430843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
4313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
4323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
4333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
4343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
4353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
4363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
4379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
4383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
4399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
4409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
4413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
4423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
4433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && !mLoadAndBindStepFinished) {
4443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
4453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        this.wait();
4463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
4473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
4489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
4493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
4503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
4513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
4523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
4533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
4549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
455843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
4563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
457f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
4593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
4603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
4613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
4623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks cbk = mCallbacks.get();
4633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
464843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
4653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
466843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
467843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
468dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
469843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
470843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
471dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
472dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
473843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
474843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
475843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
476843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
477843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
478cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
4799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
480843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
4813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
4823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
4833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
4843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
4853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
4863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
4879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
488843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
489843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
4909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
4919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
4923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
493f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
494843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
495843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
496843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
497cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
498843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
499843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
500843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
501843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
5023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
503843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
5053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
5063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
507f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
5083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
5093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
5103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
5113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
512843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
5133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
514843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Trigger a gc to try to clean up after the stuff is done, since the
5163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // renderscript allocations aren't charged to the java heap.
5173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mStopped) {
518f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                mHandler.post(new Runnable() {
519f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        public void run() {
520f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                            System.gc();
521f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        }
522f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                    });
5233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
5243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
5253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
5263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            System.gc();
5273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
5283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
529f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
5303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
531f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
5333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
5343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
5353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
536f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
5373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
538f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
5403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
5413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
5423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
5433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
5443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
5453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
5463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
5473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
5483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
5493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
5503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
551ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
5523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
5533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
5543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
555f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
5573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
5583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
5593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
5613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
5623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
5639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
5643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
566f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
5673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
568f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
5703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
5713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
5723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
5733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
574774b327991dc9e1c140d929c32c87dff36695bfcJoe Onorato
5753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
5763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
5773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (occupied[item.screen][x][y] != null) {
5783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
5793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + " into cell (" + item.screen + ":"
5803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
5813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + ") occupied by "
5823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + occupied[item.screen][x][y]);
5833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
5848802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
5858802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
5863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
5873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
5883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
5893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    occupied[item.screen][x][y] = item;
5908802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
5918802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
5923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
5933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
5948802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
5953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadWorkspace() {
5963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
597f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
5993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
6003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
6013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
6023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
603f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mItems.clear();
6053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mAppWidgets.clear();
6063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mFolders.clear();
6073c2f7e105d8daab439bff2c362a727ad348d8a44Joe Onorato
6083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
6095c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
6103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Cursor c = contentResolver.query(
6113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
612f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ItemInfo occupied[][][] = new ItemInfo[Launcher.SCREEN_COUNT][Launcher.NUMBER_CELLS_X][Launcher.NUMBER_CELLS_Y];
6148802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
6153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            try {
6163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
6173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int intentIndex = c.getColumnIndexOrThrow
6183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.INTENT);
6193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int titleIndex = c.getColumnIndexOrThrow
6203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.TITLE);
6213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconTypeIndex = c.getColumnIndexOrThrow(
6223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_TYPE);
6233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
6243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconPackageIndex = c.getColumnIndexOrThrow(
6253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_PACKAGE);
6263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconResourceIndex = c.getColumnIndexOrThrow(
6273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_RESOURCE);
6283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int containerIndex = c.getColumnIndexOrThrow(
6293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.CONTAINER);
6303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(
6313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ITEM_TYPE);
6323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int appWidgetIdIndex = c.getColumnIndexOrThrow(
6333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.APPWIDGET_ID);
6343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int screenIndex = c.getColumnIndexOrThrow(
6353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SCREEN);
6363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellXIndex = c.getColumnIndexOrThrow
6373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLX);
6383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellYIndex = c.getColumnIndexOrThrow
6393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLY);
6403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanXIndex = c.getColumnIndexOrThrow
6413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.SPANX);
6423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanYIndex = c.getColumnIndexOrThrow(
6433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SPANY);
6443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
6453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int displayModeIndex = c.getColumnIndexOrThrow(
6463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.DISPLAY_MODE);
6473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ShortcutInfo info;
6493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                String intentDescription;
6503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LauncherAppWidgetInfo appWidgetInfo;
6513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                int container;
6523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                long id;
6533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Intent intent;
6543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && c.moveToNext()) {
6563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
6573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        int itemType = c.getInt(itemTypeIndex);
6583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        switch (itemType) {
6603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
6613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
6623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            intentDescription = c.getString(intentIndex);
6633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            try {
6643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = Intent.parseUri(intentDescription, 0);
6653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } catch (URISyntaxException e) {
6663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                continue;
6673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
6683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
6703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(manager, intent, context, c, iconIndex,
6713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
6723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
6733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(c, context, iconTypeIndex,
6743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconPackageIndex, iconResourceIndex, iconIndex,
6753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
6763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
6773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (info != null) {
6793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                updateSavedIcon(context, info, c, iconIndex);
6803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.intent = intent;
6823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.id = c.getLong(idIndex);
6833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
6843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.container = container;
6853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.screen = c.getInt(screenIndex);
6863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.cellX = c.getInt(cellXIndex);
6873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.cellY = c.getInt(cellYIndex);
6883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
6903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, info)) {
6913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
6929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
693f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
6953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
6963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(info);
6973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
6983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                default:
6993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    // Item is in a user folder
7003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    UserFolderInfo folderInfo =
7013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            findOrMakeUserFolder(mFolders, container);
7023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    folderInfo.add(info);
7033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
7053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
7063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Failed to load the shortcut, probably because the
7073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // activity manager couldn't resolve it (maybe the app
7083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // was uninstalled), or the db row was somehow screwed up.
7093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Delete it.
7103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
7113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Error loading shortcut " + id + ", removing it");
7123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                contentResolver.delete(LauncherSettings.Favorites.getContentUri(
7133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            id, false), null, null);
7143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
716f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
7183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
7193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
7209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
7213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.title = c.getString(titleIndex);
7229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
7233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.id = id;
7243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            container = c.getInt(containerIndex);
7253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.container = container;
7263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.screen = c.getInt(screenIndex);
7273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.cellX = c.getInt(cellXIndex);
7283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.cellY = c.getInt(cellYIndex);
7298802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // check & update map of what's occupied
7313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!checkItemPlacement(occupied, folderInfo)) {
7329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
7333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
734f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            switch (container) {
7363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
7373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(folderInfo);
7383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            mFolders.put(folderInfo.id, folderInfo);
7423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
7433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
7453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
7463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Uri uri = Uri.parse(c.getString(uriIndex));
747f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Make sure the live folder exists
7493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final ProviderInfo providerInfo =
7503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    context.getPackageManager().resolveContentProvider(
7513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            uri.getAuthority(), 0);
7523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (providerInfo == null && !isSafeMode) {
7543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
7553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
7563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
7573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intentDescription = c.getString(intentIndex);
7593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = null;
7603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (intentDescription != null) {
7613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    try {
7623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        intent = Intent.parseUri(intentDescription, 0);
7633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    } catch (URISyntaxException e) {
7643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        // Ignore, a live folder might not have a base intent
7653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    }
7663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                }
767f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.title = c.getString(titleIndex);
7693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.id = id;
7703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.uri = uri;
7719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                container = c.getInt(containerIndex);
7723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.container = container;
7733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.screen = c.getInt(screenIndex);
7743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.cellX = c.getInt(cellXIndex);
7753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.cellY = c.getInt(cellYIndex);
7763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.baseIntent = intent;
7773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.displayMode = c.getInt(displayModeIndex);
778f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7798802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                // check & update map of what's occupied
7803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, liveFolderInfo)) {
7818802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                    break;
7828802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                }
7838802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
7853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconResourceIndex, liveFolderInfo);
7863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                switch (container) {
7889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
7893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        mItems.add(liveFolderInfo);
7909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                        break;
7919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
7923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mFolders.put(liveFolderInfo.id, liveFolderInfo);
7933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
7953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
7973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Read all Launcher-specific widget details
7983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            int appWidgetId = c.getInt(appWidgetIdIndex);
7993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final AppWidgetProviderInfo provider =
8023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    widgets.getAppWidgetInfo(appWidgetId);
8033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!isSafeMode && (provider == null || provider.provider == null ||
8053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    provider.provider.getPackageName() == null)) {
8063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Deleting widget that isn't installed anymore: id="
8073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        + id + " appWidgetId=" + appWidgetId);
8083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
8093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
8113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.id = id;
8123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.screen = c.getInt(screenIndex);
8133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.cellX = c.getInt(cellXIndex);
8143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.cellY = c.getInt(cellYIndex);
8153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.spanX = c.getInt(spanXIndex);
8163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.spanY = c.getInt(spanYIndex);
817ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
8183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
8193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
8203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.e(TAG, "Widget found where container "
8213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            + "!= CONTAINER_DESKTOP -- ignoring!");
8223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    continue;
8239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.container = c.getInt(containerIndex);
8258802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
8273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, appWidgetInfo)) {
8283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mAppWidgets.add(appWidgetInfo);
8329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
8333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
8353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (Exception e) {
8363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Desktop items loading interrupted:", e);
8379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
8389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
8393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } finally {
8403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                c.close();
8413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
8425c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
8433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (itemsToRemove.size() > 0) {
8443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ContentProviderClient client = contentResolver.acquireContentProviderClient(
8453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LauncherSettings.Favorites.CONTENT_URI);
8463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Remove dead items
8473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (long id : itemsToRemove) {
8483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
8493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Removed id = " + id);
8503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
8513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // Don't notify content observers
8523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
8533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        client.delete(LauncherSettings.Favorites.getContentUri(id, false),
8543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                null, null);
8553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (RemoteException e) {
8563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Could not remove id = " + id);
8575c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
8585c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
8593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
8605c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
8613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
8623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
8633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "workspace layout: ");
8643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = 0; y < Launcher.NUMBER_CELLS_Y; y++) {
8653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    String line = "";
8663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
8673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (s > 0) {
8683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += " | ";
8693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
8703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        for (int x = 0; x < Launcher.NUMBER_CELLS_X; x++) {
8713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += ((occupied[s][x][y] != null) ? "#" : ".");
8728802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                        }
8738802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
8743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "[ " + line + " ]");
875a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
8769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
8773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
87831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
8803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Read everything out of our database.
8813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
8823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void bindWorkspace() {
8833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = SystemClock.uptimeMillis();
8843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
8863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
8873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
8883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
8893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
8903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher");
8913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
8923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
89331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N;
8953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're about to start firing items at it
8963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
8973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
8983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
8993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
9003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.startBinding();
9013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Add the items to the workspace.
9053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mItems.size();
9063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i+=ITEMS_CHUNK) {
9073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
9083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
9099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                mHandler.post(new Runnable() {
9109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
911c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
9133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindItems(mItems, start, start+chunkSize);
9149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                });
9173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
9223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindFolders(mFolders);
9233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the queue goes empty.
9273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
9303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Going to start binding widgets soon.");
9313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the widgets, one at a time.
9353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // WARNING: this is calling into the workspace from the background thread,
9363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // but since getCurrentScreen() just returns the int, we should be okay.  This
9373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // is just a hint for the order, and if it's wrong, we'll be okay.
9383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // TODO: instead, we should have that push the current screen into here.
9393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
9403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mAppWidgets.size();
9413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the current screen
9423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
9433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
9443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen == currentScreen) {
9459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    mHandler.post(new Runnable() {
9469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        public void run() {
947c131b74922e6323331c374856bda830010d761c8Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            if (callbacks != null) {
9493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
9509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
9519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    });
9539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
9543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the other screens
9563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
9573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
9583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen != currentScreen) {
9593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mHandler.post(new Runnable() {
9603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
9613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (callbacks != null) {
9633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
9649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
9653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
9663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
9673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're done.
9703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
9743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.finishBindingItems();
9759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
9773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // If we're profiling, this is the last thing in the queue.
9793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
9823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
9833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
9849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
9863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
9883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
9903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
9913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
9923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
9943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadAllAppsByBatch();
9953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
9963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
9973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsLoaded = true;
9993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
10003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
10013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
10053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
10063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
10073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
10083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
10093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
10103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
10133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<ApplicationInfo> list
10143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
10153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
10183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
10219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1024a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
10259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAllAppsByBatch() {
10323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
10333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
10353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
10363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
10373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
10383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
10393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
10403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
10419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
104231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
10443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1045cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
10463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager packageManager = mContext.getPackageManager();
10473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            List<ResolveInfo> apps = null;
10483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N = Integer.MAX_VALUE;
10503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int startIndex;
10523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int i=0;
10533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int batchSize = -1;
10543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            while (i < N && !mStopped) {
10553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (i == 0) {
10563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mAllAppsList.clear();
10573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
10583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    apps = packageManager.queryIntentActivities(mainIntent, 0);
10593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities took "
10613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-qiaTime) + "ms");
10623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (apps == null) {
1064cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        return;
1065cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
10663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    N = apps.size();
10673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities got " + N + " apps");
10693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (N == 0) {
10713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // There are no apps?!?
10723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return;
10733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (mBatchSize == 0) {
10753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = N;
10763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } else {
10773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = mBatchSize;
10783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
10813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Collections.sort(apps,
10823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            new ResolveInfo.DisplayNameComparator(packageManager));
10833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "sort took "
10853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-sortTime) + "ms");
10863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
1087cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1088cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
10893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
10903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                startIndex = i;
10923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int j=0; i<N && j<batchSize; j++) {
10933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // This builds the icon bitmaps.
10943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache));
10953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    i++;
1096cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1097cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
10983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean first = i <= batchSize;
10993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
11003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> added = mAllAppsList.added;
11013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
11023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1103cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                mHandler.post(new Runnable() {
1104cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    public void run() {
1105cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        final long t = SystemClock.uptimeMillis();
1106cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        if (callbacks != null) {
11073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (first) {
11083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAllApplications(added);
11093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
11103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppsAdded(added);
11113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
11123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (DEBUG_LOADERS) {
11133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.d(TAG, "bound " + added.size() + " apps in "
11143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    + (SystemClock.uptimeMillis() - t) + "ms");
11153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
11163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        } else {
11173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.i(TAG, "not binding apps: no Launcher activity");
1118cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        }
1119cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
1120cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                });
1121cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
11233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
11243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t2) + "ms");
11253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mAllAppsLoadDelay > 0 && i < N) {
11283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
11293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
11303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
11313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
11323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Thread.sleep(mAllAppsLoadDelay);
11333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException exc) { }
11343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1135cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato            }
1136cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
11383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "cached all " + N + " apps in "
11393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (SystemClock.uptimeMillis()-t) + "ms"
11403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
11413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1143dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
11443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
11453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mContext=" + mContext);
11463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
11473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
11483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
11493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
11503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
115231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
11543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorker.post(task);
11553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1156d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
11573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
11583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
11593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
1160d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
11613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
11623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
11633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
11643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
11653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
116631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1167d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
11683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
11693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
11703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
11713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1172d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
11733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
11743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mApp;
1175dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
11763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
11773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
11783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
11793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
11803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
11813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
11823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.addPackage(context, packages[i]);
1183dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
11843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
11853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
11863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
11873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
11883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.updatePackage(context, packages[i]);
11893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
11913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
11923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
11933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
11943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
11953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.removePackage(packages[i]);
11963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
11983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1199dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> added = null;
12013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> removed = null;
12023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> modified = null;
12033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.added.size() > 0) {
12053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                added = mAllAppsList.added;
12063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
12073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.removed.size() > 0) {
12093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                removed = mAllAppsList.removed;
12103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.removed = new ArrayList<ApplicationInfo>();
12113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (ApplicationInfo info: removed) {
12123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mIconCache.remove(info.intent.getComponent());
121331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
121431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
12153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.modified.size() > 0) {
12163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                modified = mAllAppsList.modified;
12173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.modified = new ArrayList<ApplicationInfo>();
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
121931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
12213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
12223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
12233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
1224be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
1225be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
12263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
12273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> addedFinal = added;
12283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
12293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
12303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
12313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsAdded(addedFinal);
12323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
12333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
12353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
12373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> modifiedFinal = modified;
12383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
12393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
12403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
12413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
12423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
12433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
12453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (removed != null) {
12473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean permanent = mOp != OP_UNAVAILABLE;
12483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> removedFinal = removed;
12493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
12503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
12513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
12523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsRemoved(removedFinal, permanent);
12533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
12543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
1256be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
125731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
125831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
125931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
126156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
126256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
12639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
126456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
1265e74daedc3b41d958315f7d5bf69deb232ca89468Joe Onorato        return getShortcutInfo(manager, intent, context, null, -1, -1);
126656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
126756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
126856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
126956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
127056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
127156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
127256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
127356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
127456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Cursor c, int iconIndex, int titleIndex) {
127556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
127656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
127731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
127856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
127956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (componentName == null) {
12809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
128131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
128231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12838ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
12848ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
12858ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
128656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
128756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
128856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
128956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
129056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
129156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
129256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = mIconCache.getIcon(componentName, resolveInfo);
129356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
129456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
129556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
129656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
129756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
129856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
12999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
130056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
130156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
130256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
130356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
130456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
130556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
130656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
130756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
130856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
130956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = resolveInfo.activityInfo.loadLabel(manager);
131056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
131156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
131256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
131356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
131456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
131556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
131656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
131756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
13189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
131956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
13209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
13219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
13229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
13239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
132431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
13260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
13279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
13280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
132956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
133056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
133131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
133256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
13330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = new ShortcutInfo();
13349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
13357376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
13368ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
13378ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
133856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
133956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
13409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
13419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
13429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
13439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
13449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
13459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
134656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
134756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
13489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
13499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
135056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
135156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
135256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
135356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
13549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
135556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
135656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
135756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
135856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
135956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
136056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
136156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
136256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
136356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
136456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
13657376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
13669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
13679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
136856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getIconFromCursor(c, iconIndex);
136956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
137056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
137156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
137256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
137356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
137456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
13759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
13769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
13779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
1378d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
137956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
13809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
13819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
138231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1383d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
13849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
1385f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
138631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
138756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    Bitmap getIconFromCursor(Cursor c, int iconIndex) {
138856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (false) {
138956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
139056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
139156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
139256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
139356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
139456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return BitmapFactory.decodeByteArray(data, 0, data.length);
139556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
139656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
139756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
139856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
139956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    ShortcutInfo addShortcut(Context context, Intent data,
14010589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            CellLayout.CellInfo cellInfo, boolean notify) {
14020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = infoFromShortcutIntent(context, data);
14040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
14050589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
14060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
14080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
14090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
14110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
14120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
14130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
14140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
14160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean filtered = false;
14170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
14180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
14190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
14210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
14220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            filtered = true;
14230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
14240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
14250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
14260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
14270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
14280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
14290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
14300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
14310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
14320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
14330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
14340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
14350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
14360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
14370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
14380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
14390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
144056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
144156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
144356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
144456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
14450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
14460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
144756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
14490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
14500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
14510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
14520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
14540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
14550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
14579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
145831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
14609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
14619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
14629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
14639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
14649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
14659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
14669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
14679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int id = resources.getIdentifier(resourceName, null, null);
14680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
14690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
14709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
14710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(
14720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context.getResources().getDrawable(R.drawable.ic_launcher_folder),
14730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
14749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
14759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
14769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.packageName = packageName;
14779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.resourceName = resourceName;
14789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
14800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            liveFolderInfo.icon = Utilities.createIconBitmap(
14810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context.getResources().getDrawable(R.drawable.ic_launcher_folder),
14820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context);
14839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
148431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1485bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
148656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
148756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
148856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
148956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
149056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
149156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
149256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
149356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.onExternalStorage && !info.customIcon && !info.usingFallbackIcon) {
149456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            boolean needSave;
149556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            byte[] data = c.getBlob(iconIndex);
149656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            try {
149756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (data != null) {
149856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
149956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap loaded = info.getIcon(mIconCache);
150056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = !saved.sameAs(loaded);
150156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                } else {
150256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = true;
150356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
150456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } catch (Exception e) {
150556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
150656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
150756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (needSave) {
150856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                Log.d(TAG, "going to save icon bitmap for info=" + info);
150956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // This is slower than is ideal, but this only happens either
151056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // after the froyo OTA or when the app is updated with a new
151156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // icon.
151256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                updateItemInDatabase(context, info);
151356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
151456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
151556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
151656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
151731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
15189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously,
15199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
152031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
15219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
15229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
15239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
15249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
15259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
15269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new UserFolderInfo();
15279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
152831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
15299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (UserFolderInfo) folderInfo;
15309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
153131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
15329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
15339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
15349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * new one.
15359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
15369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
15379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
15389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
15399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
15409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
15419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new LiveFolderInfo();
15429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
154331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
15449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (LiveFolderInfo) folderInfo;
154531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
154631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
154731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
154831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        String label = activityInfo.loadLabel(manager).toString();
154931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (label == null) {
155031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
155131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (label == null) {
155231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                label = activityInfo.name;
155331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
155431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
155531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return label;
155631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
155731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
15589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static final Collator sCollator = Collator.getInstance();
1559b0c27f254a9929be208d5e04554f438076c833bcJoe Onorato    public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
15609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            = new Comparator<ApplicationInfo>() {
15619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public final int compare(ApplicationInfo a, ApplicationInfo b) {
15629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return sCollator.compare(a.title.toString(), b.title.toString());
156331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
15649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    };
1565be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
1566be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
1567be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
1568be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1569be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1570be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1571be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
15723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Log.d(TAG, "mItems size=" + mItems.size());
15733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
15743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
15753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
15763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
15773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1578be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
157931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
1580