LauncherModel.java revision 845ba3b17b83a2b11d79c6fb076cf96ab4a737df
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
19aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.lang.ref.WeakReference;
20aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.net.URISyntaxException;
21aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.text.Collator;
22aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.ArrayList;
23aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.Collections;
24aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.Comparator;
25aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.HashMap;
26aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.List;
27aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
28629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetManager;
29629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetProviderInfo;
30f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratoimport android.content.BroadcastReceiver;
3131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ComponentName;
325c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.ContentProviderClient;
3331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentResolver;
3431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentValues;
35aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.content.Context;
3631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Intent;
370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent.ShortcutIconResource;
3831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ActivityInfo;
3931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.PackageManager;
405c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.pm.ProviderInfo;
4131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ResolveInfo;
4231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.res.Resources;
4331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.database.Cursor;
4431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Bitmap;
4531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.BitmapFactory;
4631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.net.Uri;
473611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.Handler;
483611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.HandlerThread;
490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.os.Parcelable;
5031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.os.Process;
51aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.os.RemoteException;
529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.os.SystemClock;
53aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.util.Log;
5431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
55edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guyimport com.android.launcher.R;
56edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
5731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
5831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Maintains in-memory state of the Launcher. It is expected that there should be only one
5931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * LauncherModel object held in a static. Also provide APIs for updating the database state
60bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project * for the Launcher.
6131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
62f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratopublic class LauncherModel extends BroadcastReceiver {
63a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final boolean DEBUG_LOADERS = false;
649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final String TAG = "Launcher.Model";
659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
67d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato    private int mBatchSize; // 0 is all apps at once
682ff10b3b0137d258569ce5471808842e88696133Daniel Sandler    private int mAllAppsLoadDelay; // milliseconds between batches
69dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
70f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    private final LauncherApplication mApp;
719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private HandlerThread mWorkerThread;
743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private Handler mWorker;
753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
77cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
78cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
79cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
80cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
81cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
82cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private AllAppsList mAllAppsList; // only access in worker thread
860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
93d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountX;
94d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountY;
95aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
100ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
1019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void finishBindingItems();
1029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
1039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAllApplications(ArrayList<ApplicationInfo> apps);
10464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
10564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
1063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
10780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung        public void bindPackagesUpdated();
108843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        public boolean isAllAppsVisible();
1099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
11031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    LauncherModel(LauncherApplication app, IconCache iconCache) {
112f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        mApp = app;
1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mAllAppsList = new AllAppsList(iconCache);
1140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
1150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mDefaultIcon = Utilities.createIconBitmap(
1170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                app.getPackageManager().getDefaultActivityIcon(), app);
1182ff10b3b0137d258569ce5471808842e88696133Daniel Sandler
1192ff10b3b0137d258569ce5471808842e88696133Daniel Sandler        mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
120d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
121d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato        mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
1223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorkerThread = new HandlerThread("launcher-loader");
1243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorkerThread.start();
1253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorker = new Handler(mWorkerThread.getLooper());
1260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
12856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
1290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
130f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
13131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
1349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
1359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
1379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY) {
1389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
1399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
1409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            addItemToDatabase(context, item, container, screen, cellX, cellY, false);
1419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
1429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
1439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            moveItemInDatabase(context, item, container, screen, cellX, cellY);
1449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
14631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
1499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
1519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int cellX, int cellY) {
152aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
1549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
1559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
1569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
15731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
1599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
160bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
1619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
162d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, cellX);
163d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, cellY);
1649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.SCREEN, item.screen);
165ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
1669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
16731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
16831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
1709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
1719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
172bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
1739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
1749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
1769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
1779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
1789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
1799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
1809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
1819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
1829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
1839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
185bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
186bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
187bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
188aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
189aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
190aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
191aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
192aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
193aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
194aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
195aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
196aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
197aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
198aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
199aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
200aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
201aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
202aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
203aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
204aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
205aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
206aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
207aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
208aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
209aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                ItemInfo item = new ItemInfo();
210aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
211aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
212aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanX = c.getInt(spanXIndex);
213aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanY = c.getInt(spanYIndex);
214aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
215aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
216aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.screen = c.getInt(screenIndex);
217aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
218aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
219aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
220aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
221aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
222aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
223aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
224aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
225aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
226aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
227aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
228aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
229aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
2309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
23131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
2339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
2359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
2369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
2379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
2389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
239f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
2419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
2429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
2439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
2449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
2459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
2469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
2479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
248f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
2509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
2519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeUserFolder(folderList, id);
2539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeLiveFolder(folderList, id);
2569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
25831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
2609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
2619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
2629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.screen = c.getInt(screenIndex);
263d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
264d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
265ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
2679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
2689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
2699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
2702fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
271ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
2739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
27431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
2779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
2789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addItemToDatabase(Context context, ItemInfo item, long container,
2809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY, boolean notify) {
2819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
2829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
2839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
2849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
285ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
2879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
289ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
290d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, cellX, cellY);
291aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
2929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
2939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
294ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (result != null) {
2969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            item.id = Integer.parseInt(result.getPathSegments().get(1));
29731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
29831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
29931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
301aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
302aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
303845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka    static int getCellLayoutChildId(
304845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka            int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
305845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka        return ((cellId & 0xFF) << 24)
306845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka                | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
307aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
308aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
309d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountX() {
310d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountX;
311aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
312aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
313d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountY() {
314d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountY;
315aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
316aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
317aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
318aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Updates the model orientation helper to take into account the current layout dimensions
319aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * when performing local/canonical coordinate transformations.
320aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
321aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
322d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountX = shortAxisCellCount;
323d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountY = longAxisCellCount;
324aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
325aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
326aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
3279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Update an item to the database in a specified container.
3289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void updateItemInDatabase(Context context, ItemInfo item) {
3309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
3319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
332f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
334d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
335aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
33731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
33831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Removes the specified item from the database
3419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param context
3429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param item
3439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteItemFromDatabase(Context context, ItemInfo item) {
3459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
34673013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick        final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
34773013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick        new Thread("deleteItemFromDatabase") {
34873013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick            public void run() {
34973013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick                cr.delete(uriToDelete, null, null);
35073013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick            }
35173013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick        }.start();
3529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
353f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
3569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
3589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
359f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
3619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.CONTENT_URI,
3629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
3639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
364f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
3679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
3699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
3709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
371f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
372f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
373f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3741d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
3759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
3769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
3779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
378f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
3793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
380aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
3829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
3833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
3843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
3853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
3863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
3873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
388f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
39064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
3913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
3923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
3933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
3943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
39556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
3963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
3973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
3983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
3993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
40156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
4033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
4043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
4053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
4073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
4083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
40956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
41156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
4133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
414f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
415f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
4173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
4183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
419f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
4213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
4223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(
4233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        PackageUpdatedTask.OP_UNAVAILABLE, packages));
424f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
426f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void startLoader(Context context, boolean isLaunching) {
4283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
4303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
4313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
432843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
4333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
4343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
4353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
4363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LoaderTask oldTask = mLoaderTask;
4373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (oldTask != null) {
4383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (oldTask.isLaunching()) {
4393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // don't downgrade isLaunching if we're already running
4403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        isLaunching = true;
4419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
4423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    oldTask.stopLocked();
4439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
4443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask = new LoaderTask(context, isLaunching);
4453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorker.post(mLoaderTask);
446b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
447b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
4483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
449b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
4503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
4513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
4533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
4549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
455f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
457b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
4583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
4593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
4603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
4613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
4623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
4633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
4643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
4653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
4663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Thread mWaitThread;
4673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
4683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
4693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
4703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
4713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        LoaderTask(Context context, boolean isLaunching) {
4723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
4733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
4743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
475f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
4773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
4783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
479f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindWorkspace() {
4813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
482cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
4833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // For now, just always reload the workspace.  It's ~100 ms vs. the
4843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // binding which takes many hundreds of ms.
4853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // We can reconsider.
4863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
4873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
4883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
4893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (true || !mWorkspaceLoaded) {
4903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadWorkspace();
4913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
4923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
4939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
4943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorkspaceLoaded = true;
4953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
496cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
4973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
4983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            bindWorkspace();
4993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
500f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
5023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
5033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
5043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
5053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
5063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
507843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
5093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
5103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
5113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
5123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
5133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
5149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
5153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
5169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
5179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
5183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
5193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && !mLoadAndBindStepFinished) {
5213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
5223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        this.wait();
5233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
5243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
5259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
5283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
529aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
5303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
5319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
532843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
5333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
534f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
5363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
5373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
5383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
5393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks cbk = mCallbacks.get();
5403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
541843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
543843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
544843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
545dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
546843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
547843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
548dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
549843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
550843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
551843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
552843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
553843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
554cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
5559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
556843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
5583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
5593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
5623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
5639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
564843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
565843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
5669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
5683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
569f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
570843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
571843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
572843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
573cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
574843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
575843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
576843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
577843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
5783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
579843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
5813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
5823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
583f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
5843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
5853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
5863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
5873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
588843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
5893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
590843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Trigger a gc to try to clean up after the stuff is done, since the
5923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // renderscript allocations aren't charged to the java heap.
5933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mStopped) {
594f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                mHandler.post(new Runnable() {
595f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        public void run() {
596f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                            System.gc();
597f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        }
598f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                    });
5993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
6003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
6013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
6023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            System.gc();
6033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
6043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
605f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
607f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
6093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
6103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
6113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
612f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
614f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
6163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
6173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
6183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
6193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
6203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
6213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
6223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
6233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
6243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
6253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
627ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
6283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
6293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
631f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
6333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
6343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
6363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
6373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
6383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
6403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
642f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
644f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
6463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
6473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
6483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
6493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
6503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
6513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
6523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (occupied[item.screen][x][y] != null) {
6533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
654aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + " into cell (" + item.screen + ":"
6553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
656aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
6573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + occupied[item.screen][x][y]);
6583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
6598802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
6608802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
6613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
6623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
6633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
6643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    occupied[item.screen][x][y] = item;
6658802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
6668802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
6673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
6683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
6698802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
6703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadWorkspace() {
6713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
672f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
6743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
6753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
6763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
6773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
678f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mItems.clear();
6803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mAppWidgets.clear();
6813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mFolders.clear();
6823c2f7e105d8daab439bff2c362a727ad348d8a44Joe Onorato
6833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
6845c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
6853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Cursor c = contentResolver.query(
6863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
687f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
688d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen            final ItemInfo occupied[][][] =
689d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                    new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
6908802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
6913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            try {
6923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
6933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int intentIndex = c.getColumnIndexOrThrow
6943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.INTENT);
6953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int titleIndex = c.getColumnIndexOrThrow
6963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.TITLE);
6973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconTypeIndex = c.getColumnIndexOrThrow(
6983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_TYPE);
6993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
7003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconPackageIndex = c.getColumnIndexOrThrow(
7013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_PACKAGE);
7023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconResourceIndex = c.getColumnIndexOrThrow(
7033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_RESOURCE);
7043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int containerIndex = c.getColumnIndexOrThrow(
7053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.CONTAINER);
7063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(
7073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ITEM_TYPE);
7083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int appWidgetIdIndex = c.getColumnIndexOrThrow(
7093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.APPWIDGET_ID);
7103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int screenIndex = c.getColumnIndexOrThrow(
7113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SCREEN);
7123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellXIndex = c.getColumnIndexOrThrow
7133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLX);
7143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellYIndex = c.getColumnIndexOrThrow
7153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLY);
7163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanXIndex = c.getColumnIndexOrThrow
7173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.SPANX);
7183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanYIndex = c.getColumnIndexOrThrow(
7193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SPANY);
7203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
7213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int displayModeIndex = c.getColumnIndexOrThrow(
7223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.DISPLAY_MODE);
7233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ShortcutInfo info;
7253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                String intentDescription;
7263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LauncherAppWidgetInfo appWidgetInfo;
7273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                int container;
7283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                long id;
7293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Intent intent;
7303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && c.moveToNext()) {
7323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
7333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        int itemType = c.getInt(itemTypeIndex);
7343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        switch (itemType) {
7363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
7373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
7383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            intentDescription = c.getString(intentIndex);
7393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            try {
7403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = Intent.parseUri(intentDescription, 0);
7413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } catch (URISyntaxException e) {
7423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                continue;
7433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
7463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(manager, intent, context, c, iconIndex,
7473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
7483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
7493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(c, context, iconTypeIndex,
7503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconPackageIndex, iconResourceIndex, iconIndex,
7513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
7523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (info != null) {
7553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                updateSavedIcon(context, info, c, iconIndex);
7563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.intent = intent;
7583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.id = c.getLong(idIndex);
7593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
7603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.container = container;
7613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.screen = c.getInt(screenIndex);
762d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellX = c.getInt(cellXIndex);
763d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellY = c.getInt(cellYIndex);
7643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
7663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, info)) {
7673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
769f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
7713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
7723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(info);
7733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                default:
7753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    // Item is in a user folder
7763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    UserFolderInfo folderInfo =
7773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            findOrMakeUserFolder(mFolders, container);
7783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    folderInfo.add(info);
7793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
7813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
7823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Failed to load the shortcut, probably because the
7833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // activity manager couldn't resolve it (maybe the app
7843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // was uninstalled), or the db row was somehow screwed up.
7853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Delete it.
7863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
7873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Error loading shortcut " + id + ", removing it");
7883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                contentResolver.delete(LauncherSettings.Favorites.getContentUri(
7893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            id, false), null, null);
7903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
792f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
7943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
7953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
7969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
797aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            folderInfo.title = c.getString(titleIndex);
7983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.id = id;
7993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            container = c.getInt(containerIndex);
8003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.container = container;
8013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.screen = c.getInt(screenIndex);
802d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellX = c.getInt(cellXIndex);
803d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellY = c.getInt(cellYIndex);
8048802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // check & update map of what's occupied
8063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!checkItemPlacement(occupied, folderInfo)) {
8079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
8083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            switch (container) {
8103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(folderInfo);
8123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            mFolders.put(folderInfo.id, folderInfo);
8163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
8193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Uri uri = Uri.parse(c.getString(uriIndex));
821f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Make sure the live folder exists
8233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final ProviderInfo providerInfo =
8243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    context.getPackageManager().resolveContentProvider(
8253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            uri.getAuthority(), 0);
8263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (providerInfo == null && !isSafeMode) {
8283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
8293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
8313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intentDescription = c.getString(intentIndex);
8323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = null;
8333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (intentDescription != null) {
8343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    try {
8353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        intent = Intent.parseUri(intentDescription, 0);
8363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    } catch (URISyntaxException e) {
8373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        // Ignore, a live folder might not have a base intent
8383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    }
8393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                }
840f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.title = c.getString(titleIndex);
8423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.id = id;
8433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.uri = uri;
8449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                container = c.getInt(containerIndex);
8453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.container = container;
8463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.screen = c.getInt(screenIndex);
847d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellX = c.getInt(cellXIndex);
848d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellY = c.getInt(cellYIndex);
8493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.baseIntent = intent;
8503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.displayMode = c.getInt(displayModeIndex);
851f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8528802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                // check & update map of what's occupied
8533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, liveFolderInfo)) {
8548802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                    break;
8558802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                }
8568802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
8583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconResourceIndex, liveFolderInfo);
8593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                switch (container) {
8619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        mItems.add(liveFolderInfo);
8639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                        break;
8649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mFolders.put(liveFolderInfo.id, liveFolderInfo);
8663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
8703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Read all Launcher-specific widget details
8713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            int appWidgetId = c.getInt(appWidgetIdIndex);
8723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final AppWidgetProviderInfo provider =
8753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    widgets.getAppWidgetInfo(appWidgetId);
876aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
8773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!isSafeMode && (provider == null || provider.provider == null ||
8783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    provider.provider.getPackageName() == null)) {
8793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Deleting widget that isn't installed anymore: id="
8803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        + id + " appWidgetId=" + appWidgetId);
8813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
8823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
8843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.id = id;
8853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.screen = c.getInt(screenIndex);
886d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellX = c.getInt(cellXIndex);
887d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellY = c.getInt(cellYIndex);
888d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanX = c.getInt(spanXIndex);
889d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanY = c.getInt(spanYIndex);
890ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
8913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
8923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
8933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.e(TAG, "Widget found where container "
8943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            + "!= CONTAINER_DESKTOP -- ignoring!");
8953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    continue;
8969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.container = c.getInt(containerIndex);
8988802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
9003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, appWidgetInfo)) {
9013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
9029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mAppWidgets.add(appWidgetInfo);
9059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
9063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
9079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (Exception e) {
9093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Desktop items loading interrupted:", e);
9109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
9123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } finally {
9133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                c.close();
9143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9155c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (itemsToRemove.size() > 0) {
9173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ContentProviderClient client = contentResolver.acquireContentProviderClient(
9183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LauncherSettings.Favorites.CONTENT_URI);
9193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Remove dead items
9203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (long id : itemsToRemove) {
9213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
9223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Removed id = " + id);
9233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // Don't notify content observers
9253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
9263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        client.delete(LauncherSettings.Favorites.getContentUri(id, false),
9273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                null, null);
9283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (RemoteException e) {
9293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Could not remove id = " + id);
9305c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
9315c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
9323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9335c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
9353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
9363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "workspace layout: ");
937d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                for (int y = 0; y < mCellCountY; y++) {
9383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    String line = "";
9393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
9403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (s > 0) {
9413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += " | ";
9423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
943d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                        for (int x = 0; x < mCellCountX; x++) {
9443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += ((occupied[s][x][y] != null) ? "#" : ".");
9458802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                        }
9468802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
9473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "[ " + line + " ]");
948a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
9499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
9503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
95131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
9533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Read everything out of our database.
9543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
9553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void bindWorkspace() {
9563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = SystemClock.uptimeMillis();
9573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
9593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
9603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
9613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
9623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
9633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher");
9643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
9653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
96631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N;
9683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're about to start firing items at it
9693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
9733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.startBinding();
9743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Add the items to the workspace.
9783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mItems.size();
9793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i+=ITEMS_CHUNK) {
9803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
9813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
9829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                mHandler.post(new Runnable() {
9839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
984c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
9863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindItems(mItems, start, start+chunkSize);
9879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                });
9903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
9953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindFolders(mFolders);
9963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the queue goes empty.
10003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Going to start binding widgets soon.");
10043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the widgets, one at a time.
10083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // WARNING: this is calling into the workspace from the background thread,
10093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // but since getCurrentScreen() just returns the int, we should be okay.  This
10103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // is just a hint for the order, and if it's wrong, we'll be okay.
10113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // TODO: instead, we should have that push the current screen into here.
10123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
10133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mAppWidgets.size();
10143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the current screen
10153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen == currentScreen) {
10189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    mHandler.post(new Runnable() {
10199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        public void run() {
1020c131b74922e6323331c374856bda830010d761c8Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            if (callbacks != null) {
10223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
10239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
10259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    });
10269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the other screens
10293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen != currentScreen) {
10323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mHandler.post(new Runnable() {
10333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
10343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (callbacks != null) {
10363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
10379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
10393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
10403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're done.
10433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.finishBindingItems();
10489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // If we're profiling, this is the last thing in the queue.
10523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
10563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
10579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
10633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
10643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
10653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
10673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadAllAppsByBatch();
10683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
10693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
10703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsLoaded = true;
10723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
10733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
10743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
10783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
10793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
10803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
10813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
10823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
10833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
10863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<ApplicationInfo> list
10873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
10883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
10913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
10949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1097a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
10989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
11013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAllAppsByBatch() {
11053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
11083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
11093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
11103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
11113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
11123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
11133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
111531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
11173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1118cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager packageManager = mContext.getPackageManager();
11203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            List<ResolveInfo> apps = null;
11213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N = Integer.MAX_VALUE;
11233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int startIndex;
11253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int i=0;
11263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int batchSize = -1;
11273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            while (i < N && !mStopped) {
11283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (i == 0) {
11293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mAllAppsList.clear();
11303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    apps = packageManager.queryIntentActivities(mainIntent, 0);
11323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities took "
11343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-qiaTime) + "ms");
11353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (apps == null) {
1137cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        return;
1138cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
11393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    N = apps.size();
11403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities got " + N + " apps");
11423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (N == 0) {
11443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // There are no apps?!?
11453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return;
11463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (mBatchSize == 0) {
11483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = N;
11493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } else {
11503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = mBatchSize;
11513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Collections.sort(apps,
11553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            new ResolveInfo.DisplayNameComparator(packageManager));
11563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "sort took "
11583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-sortTime) + "ms");
11593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
1160cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1161cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                startIndex = i;
11653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int j=0; i<N && j<batchSize; j++) {
11663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // This builds the icon bitmaps.
11673d605d5bbef35e3b8aded44c5ef7fe3948f8f7d5Patrick Dubroy                    mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
11683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    i++;
1169cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1170cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean first = i <= batchSize;
11723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
11733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> added = mAllAppsList.added;
11743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
11753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1176cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                mHandler.post(new Runnable() {
1177cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    public void run() {
1178cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        final long t = SystemClock.uptimeMillis();
1179cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        if (callbacks != null) {
11803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (first) {
11813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAllApplications(added);
11823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
11833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppsAdded(added);
11843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
11853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (DEBUG_LOADERS) {
11863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.d(TAG, "bound " + added.size() + " apps in "
11873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    + (SystemClock.uptimeMillis() - t) + "ms");
11883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
11893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        } else {
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.i(TAG, "not binding apps: no Launcher activity");
1191cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        }
1192cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
1193cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                });
1194cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
11963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
11973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t2) + "ms");
11983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mAllAppsLoadDelay > 0 && i < N) {
12013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
12023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
12033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
12043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
12053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Thread.sleep(mAllAppsLoadDelay);
12063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException exc) { }
12073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1208cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato            }
1209cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
12113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "cached all " + N + " apps in "
12123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (SystemClock.uptimeMillis()-t) + "ms"
12133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
12143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1216dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mContext=" + mContext);
12193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
12203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
12213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
12223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
12233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
12243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
122531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
12273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorker.post(task);
12283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1229d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
12313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
12323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
1233d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
12353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
12363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
12373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
12383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
123931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1240d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
12423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
12433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
12443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1245d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
12473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mApp;
1248dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
12503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
12513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
12523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
12533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
12553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.addPackage(context, packages[i]);
1256dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
12573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
12593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
12613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.updatePackage(context, packages[i]);
12623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
12653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
12663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
12683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.removePackage(packages[i]);
12693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1272dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> added = null;
12743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> removed = null;
12753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> modified = null;
12763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.added.size() > 0) {
12783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                added = mAllAppsList.added;
12793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
12803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.removed.size() > 0) {
12823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                removed = mAllAppsList.removed;
12833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.removed = new ArrayList<ApplicationInfo>();
12843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (ApplicationInfo info: removed) {
12853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mIconCache.remove(info.intent.getComponent());
128631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
128731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
12883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.modified.size() > 0) {
12893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                modified = mAllAppsList.modified;
12903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.modified = new ArrayList<ApplicationInfo>();
12913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
129231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
12943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
12953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
12963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
1297be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
1298be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
12993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
13003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> addedFinal = added;
13013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsAdded(addedFinal);
13053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
13103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> modifiedFinal = modified;
13113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
13153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (removed != null) {
13203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean permanent = mOp != OP_UNAVAILABLE;
13213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> removedFinal = removed;
13223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsRemoved(removedFinal, permanent);
13263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
1329be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
133080baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
133180baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
133280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
133380baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
133480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    if (callbacks == mCallbacks.get()) {
133580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                        callbacks.bindPackagesUpdated();
133680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
133780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
133880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
133931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
134031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
134131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
134356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
134456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
13459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
134656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
1347e74daedc3b41d958315f7d5bf69deb232ca89468Joe Onorato        return getShortcutInfo(manager, intent, context, null, -1, -1);
134856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
134956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
135056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
135156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
135256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
135356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
135456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
135556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
135656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Cursor c, int iconIndex, int titleIndex) {
135756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
135856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
135931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
136056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
136156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (componentName == null) {
13629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
136331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
136431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13658ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
13668ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
13678ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
136856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
136956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
137056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
137156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
137256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
137356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
137456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = mIconCache.getIcon(componentName, resolveInfo);
137556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
137656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
137756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
137856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
137956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
138056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
13819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
138256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
138356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
138456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
138556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
138656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
138756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
138856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
138956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
139056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
139156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = resolveInfo.activityInfo.loadLabel(manager);
139256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
139356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
139456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
139556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
139656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
139756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
139856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
139956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
14009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
140156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
14029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
14039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
14049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
14059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
140631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
14080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
14099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
14100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
141156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
141256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
141331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
141456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
14150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = new ShortcutInfo();
14169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
14177376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
14188ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
14198ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
142056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
142156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
14239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
14249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
14259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
14269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
14279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
142856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
142956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
14309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
14319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
143256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
143356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
143456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
143556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
14369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
143756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
143856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
143956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
144056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
144156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
144256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
144356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
144456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
144556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
144656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
14477376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
14489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
145056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getIconFromCursor(c, iconIndex);
145156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
145256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
145356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
145456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
145556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
145656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
14579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
14589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
1460d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
146156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
14629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
14639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
146431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1465d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
14669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
1467f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
146831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
146956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    Bitmap getIconFromCursor(Cursor c, int iconIndex) {
147056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (false) {
147156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
147256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
147356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
147456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
147556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
147656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return BitmapFactory.decodeByteArray(data, 0, data.length);
147756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
147856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
147956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
148056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
148156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    ShortcutInfo addShortcut(Context context, Intent data,
14830280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            int screen, int cellX, int cellY, boolean notify) {
14840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = infoFromShortcutIntent(context, data);
14860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
14870280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                screen, cellX, cellY, notify);
14880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
14900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
14910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14920589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
14930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
14940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
14950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
14960589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
14980589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean filtered = false;
14990589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
15000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
15010589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
15030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
15040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            filtered = true;
15050589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
15060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
15070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
15080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
15090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
15100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
15110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
15120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
15130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
15140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
15150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
15160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
15170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
15180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
15190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
15200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
15210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
152256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
152356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
152556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
152656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
15270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
15280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
152956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
15310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
15320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
15330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
15340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
15360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
15370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
15399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
154031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
15419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
15429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
15439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
15449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
15459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
15469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
15479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
15489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
15499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int id = resources.getIdentifier(resourceName, null, null);
15500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
15510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
15529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
15530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(
15540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context.getResources().getDrawable(R.drawable.ic_launcher_folder),
15550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
15569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
15579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
15589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.packageName = packageName;
15599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.resourceName = resourceName;
15609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
15619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
15620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            liveFolderInfo.icon = Utilities.createIconBitmap(
15630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context.getResources().getDrawable(R.drawable.ic_launcher_folder),
15640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context);
15659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
156631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1567bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
156856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
156956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
157056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
157156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
157256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
157356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
157456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
1575ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
157656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            boolean needSave;
157756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            byte[] data = c.getBlob(iconIndex);
157856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            try {
157956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (data != null) {
158056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
158156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap loaded = info.getIcon(mIconCache);
158256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = !saved.sameAs(loaded);
158356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                } else {
158456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = true;
158556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
158656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } catch (Exception e) {
158756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
158856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
158956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (needSave) {
159056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                Log.d(TAG, "going to save icon bitmap for info=" + info);
159156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // This is slower than is ideal, but this only happens either
159256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // after the froyo OTA or when the app is updated with a new
159356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // icon.
159456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                updateItemInDatabase(context, info);
159556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
159656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
159756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
159856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
159931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
16009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously,
16019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
160231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
16039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
16049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
16059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
16069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
16079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
16089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new UserFolderInfo();
16099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
161031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (UserFolderInfo) folderInfo;
16129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
161331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
16159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
16169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * new one.
16179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
16189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
16199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
16209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
16219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
16229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
16239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new LiveFolderInfo();
16249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
162531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (LiveFolderInfo) folderInfo;
162731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
162831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
162931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
163031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        String label = activityInfo.loadLabel(manager).toString();
163131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (label == null) {
163231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
163331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (label == null) {
163431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                label = activityInfo.name;
163531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
163631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
163731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return label;
163831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
163931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static final Collator sCollator = Collator.getInstance();
1641b0c27f254a9929be208d5e04554f438076c833bcJoe Onorato    public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
16429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            = new Comparator<ApplicationInfo>() {
16439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public final int compare(ApplicationInfo a, ApplicationInfo b) {
16449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return sCollator.compare(a.title.toString(), b.title.toString());
164531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    };
1647be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
1648be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
1649be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
1650be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1651be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1652be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1653be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
16543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Log.d(TAG, "mItems size=" + mItems.size());
16553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
16563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
16573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
16583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
16593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1660be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
166131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
1662