LauncherModel.java revision d4844c3e731b00547a31f23a00f8bd4a271e2b62
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
1968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport java.lang.ref.WeakReference;
2068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport java.net.URISyntaxException;
2168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport java.text.Collator;
2268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport java.util.ArrayList;
2368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport java.util.Collections;
2468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport java.util.Comparator;
2568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport java.util.HashMap;
2668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson 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;
4717a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onoratoimport android.os.Environment;
483611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.Handler;
493611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.HandlerThread;
500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.os.Parcelable;
5131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.os.Process;
52aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.os.RemoteException;
539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.os.SystemClock;
54aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.util.Log;
5531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
5668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport com.android.launcher.R;
5768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
58edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
5931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
6031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Maintains in-memory state of the Launcher. It is expected that there should be only one
6131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * LauncherModel object held in a static. Also provide APIs for updating the database state
62bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project * for the Launcher.
6331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
64f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratopublic class LauncherModel extends BroadcastReceiver {
65a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final boolean DEBUG_LOADERS = false;
669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final String TAG = "Launcher.Model";
679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
6917a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato    private final boolean mAppsCanBeOnExternalStorage;
70d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato    private int mBatchSize; // 0 is all apps at once
712ff10b3b0137d258569ce5471808842e88696133Daniel Sandler    private int mAllAppsLoadDelay; // milliseconds between batches
72dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
73f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    private final LauncherApplication mApp;
749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
78700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
79700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    static {
80700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorkerThread.start();
81700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    }
82700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
83700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick
84cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
85cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
86cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
87cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
88cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
89cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private AllAppsList mAllAppsList; // only access in worker thread
930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
980589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
100d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountX;
101d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountY;
102aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
104ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        public boolean setLoadOnResume();
1059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
1069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
1079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
108ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
1099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void finishBindingItems();
1109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
1119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAllApplications(ArrayList<ApplicationInfo> apps);
11264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
11364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
1143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
11580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung        public void bindPackagesUpdated();
116843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        public boolean isAllAppsVisible();
1179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
11831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    LauncherModel(LauncherApplication app, IconCache iconCache) {
12017a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
121f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        mApp = app;
1220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mAllAppsList = new AllAppsList(iconCache);
1230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
1240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mDefaultIcon = Utilities.createIconBitmap(
126c9a961952d1a057029874f8426b90181f6876034Michael Jurka                mIconCache.getFullResDefaultActivityIcon(), app);
1272ff10b3b0137d258569ce5471808842e88696133Daniel Sandler
1282ff10b3b0137d258569ce5471808842e88696133Daniel Sandler        mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
129d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
130d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato        mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
1310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
13356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
1340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
135f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
13631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
1399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
1409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
1429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY) {
1439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
1449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
1459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            addItemToDatabase(context, item, container, screen, cellX, cellY, false);
1469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
1479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
1489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            moveItemInDatabase(context, item, container, screen, cellX, cellY);
1499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
15131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
1549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
1569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int cellX, int cellY) {
157aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
1599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
1609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
1619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
16231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
163ade2f8122f6b1a9b64d2d4a81348db619db4d5b2Brad Fitzpatrick        final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
1649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
1659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
166bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
1679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
168d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, cellX);
169d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, cellY);
1709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.SCREEN, item.screen);
171ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
172700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(new Runnable() {
173700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                public void run() {
174700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                    cr.update(uri, values, null, null);
175700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                }
176700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick            });
17731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
17831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
180d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen     * Resize an item in the DB to a new <spanX, spanY>
181d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen     */
182d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    static void resizeItemInDatabase(Context context, ItemInfo item, int cellX, int cellY,
183d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen            int spanX, int spanY) {
184d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.spanX = spanX;
185d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.spanY = spanY;
186d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellX = cellX;
187d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellY = cellY;
188d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
189d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
190d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        final ContentValues values = new ContentValues();
191d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        final ContentResolver cr = context.getContentResolver();
192d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
193d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
194d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.SPANX, spanX);
195d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.SPANY, spanY);
196d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, cellX);
197d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, cellY);
198d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
199d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        sWorker.post(new Runnable() {
200d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                public void run() {
201d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                    cr.update(uri, values, null, null);
202d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                }
203d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen            });
204d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    }
205d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
206d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    /**
2079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
2089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
209bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
2109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
2119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
2139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
2149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
2159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
2169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
2179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
2189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
2199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
2209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
2219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
222bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
223bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
224bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
225aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
226aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
227aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
228aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
229aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
230aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
231aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
232aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
233aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
234aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
235aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
236aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
237aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
238aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
239aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
240aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
241aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
242aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
243aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
244aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
245aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
246aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                ItemInfo item = new ItemInfo();
247aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
248aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
249aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanX = c.getInt(spanXIndex);
250aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanY = c.getInt(spanYIndex);
251aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
252aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
253aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.screen = c.getInt(screenIndex);
254aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
255aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
256aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
257aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
258aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
259aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
260aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
261aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
262aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
263aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
264aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
265aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
266aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
2679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
26831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
2709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
2729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
2739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
2749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
2759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
276f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
2789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
2799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
2809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
2819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
2829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
2839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
2849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
285f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
2879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
2889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeUserFolder(folderList, id);
2909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeLiveFolder(folderList, id);
2939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
29531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
2979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
2989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
2999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.screen = c.getInt(screenIndex);
300d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
301d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
302ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
3039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
3049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
3059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
3069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
3072fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
308ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
3099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
3109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
31131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
3149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
3159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addItemToDatabase(Context context, ItemInfo item, long container,
3179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY, boolean notify) {
3189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
3199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
3209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
3219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
322ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
3239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
3249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
3259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
326ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
327d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, cellX, cellY);
328aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
3309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
331ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
3329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (result != null) {
3339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            item.id = Integer.parseInt(result.getPathSegments().get(1));
33431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
33531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
33631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
338aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
339aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
340845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka    static int getCellLayoutChildId(
341845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka            int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
342845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka        return ((cellId & 0xFF) << 24)
343845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka                | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
344aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
345aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
346d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountX() {
347d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountX;
348aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
349aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
350d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountY() {
351d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountY;
352aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
353aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
354aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
355aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Updates the model orientation helper to take into account the current layout dimensions
356aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * when performing local/canonical coordinate transformations.
357aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
358aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
359d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountX = shortAxisCellCount;
360d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountY = longAxisCellCount;
361aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
362aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
363aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
3649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Update an item to the database in a specified container.
3659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void updateItemInDatabase(Context context, ItemInfo item) {
3679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
3689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
369f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
371d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
372aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
37431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
37531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Removes the specified item from the database
3789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param context
3799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param item
3809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteItemFromDatabase(Context context, ItemInfo item) {
3829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
38373013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick        final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
384700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(new Runnable() {
385700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                public void run() {
386700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                    cr.delete(uriToDelete, null, null);
387700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                }
388700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick            });
3899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
390f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
3939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
3959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
396f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
3989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.CONTENT_URI,
3999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
4009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
401f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
4039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
4049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
4059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
4069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
4079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
408f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
409f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
410f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4111d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
4129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
4139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
4149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
415f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
4163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
417aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
4183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
4199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
4203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
4213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
4223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
4233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
4243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
425f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
42764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
4283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
4293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
4303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
4313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
43256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
4343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
4353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
4363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
43856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
4403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
4413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
4423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
4443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
4453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
44656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
44856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
4503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
451f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
452f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
454cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // First, schedule to add these apps back in.
455cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
456cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
457cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // Then, rebind everything.
458e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            startLoaderFromBackground();
4593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
4603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
4613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(
4623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        PackageUpdatedTask.OP_UNAVAILABLE, packages));
463e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
464e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // If we have changed locale we need to clear out the labels in all apps.
465e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // Do this here because if the launcher activity is running it will be restarted.
466e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // If it's not running startLoaderFromBackground will merely tell it that it needs
467e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // to reload.  Either way, mAllAppsLoaded will be cleared so it re-reads everything
468e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // next time.
469e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            mAllAppsLoaded = false;
470e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            startLoaderFromBackground();
471e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
472e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    }
473e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato
474e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    /**
475e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * When the launcher is in the background, it's possible for it to miss paired
476e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * configuration changes.  So whenever we trigger the loader from the background
477e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * tell the launcher that it needs to re-run the loader when it comes back instead
478e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * of doing it now.
479e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     */
480e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    public void startLoaderFromBackground() {
481e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        boolean runLoader = false;
482e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (mCallbacks != null) {
483e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            Callbacks callbacks = mCallbacks.get();
484e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            if (callbacks != null) {
485e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                // Only actually run the loader if they're not paused.
486e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                if (!callbacks.setLoadOnResume()) {
487e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                    runLoader = true;
488e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                }
489e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            }
490e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
491e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (runLoader) {
492e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            startLoader(mApp, false);
493f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
495f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void startLoader(Context context, boolean isLaunching) {
4973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
4993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
5003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
501843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
5033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
5043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
5053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LoaderTask oldTask = mLoaderTask;
5063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (oldTask != null) {
5073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (oldTask.isLaunching()) {
5083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // don't downgrade isLaunching if we're already running
5093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        isLaunching = true;
5109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    oldTask.stopLocked();
5129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
5133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask = new LoaderTask(context, isLaunching);
514700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                sWorker.post(mLoaderTask);
515b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
516b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
5173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
518b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
5193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
5203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
5213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
5223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
5239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
524f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
5252313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mItems.clear();
5262313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mAppWidgets.clear();
5272313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mFolders.clear();
5283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
529b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
5303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
5313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
5323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
5333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
5343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
5353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
5363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
5373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
5383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Thread mWaitThread;
5393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
5403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
5413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
5423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        LoaderTask(Context context, boolean isLaunching) {
5443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
5453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
5463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
547f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
5493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
5503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
551f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindWorkspace() {
5533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
554cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
5553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // For now, just always reload the workspace.  It's ~100 ms vs. the
5563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // binding which takes many hundreds of ms.
5573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // We can reconsider.
5583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
5593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
5603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
5613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (true || !mWorkspaceLoaded) {
5623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadWorkspace();
5633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
5643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
5659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
5663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorkspaceLoaded = true;
5673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
568cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
5693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
5703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            bindWorkspace();
5713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
572f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
5743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
5753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
5763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
5773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
5783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
579843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
5813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
5823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
5833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
5843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
5853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
5869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
5873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
5889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
5899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
5903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
5913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && !mLoadAndBindStepFinished) {
5933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
5943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        this.wait();
5953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
5963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
5979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
6003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
601aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
6023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
6039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
604843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
6053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
606f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
6083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
6093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
6103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
6113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks cbk = mCallbacks.get();
6123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
613843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
6143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
615843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
616843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
617dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
618843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
619843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
620dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
621843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
622843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
623843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
624843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
625843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
626cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
6279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
628843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
6293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
6303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
6313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
6323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
6343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
6359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
636843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
637843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
6389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
6399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
6403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
641f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
642843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
643843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
644843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
645cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
646843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
647843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
648843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
649843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
6503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
651843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
6523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
6533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
6543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
655f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
6563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
6573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
6583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
6593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
660843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
6613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
662843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
6633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Trigger a gc to try to clean up after the stuff is done, since the
6643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // renderscript allocations aren't charged to the java heap.
6653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mStopped) {
666f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                mHandler.post(new Runnable() {
667f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        public void run() {
668f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                            System.gc();
669f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        }
670f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                    });
6713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
6723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
6733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
6743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            System.gc();
6753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
6763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
677f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
679f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
6813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
6823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
6833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
684f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
686f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
6883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
6893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
6903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
6913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
6923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
6933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
6943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
6953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
6963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
6973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
699ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
7003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
7013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
7023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
703f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
7053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
7063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
7073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
7083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
7093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
7103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
7119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
7123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
714f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
7153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
716f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
7183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
7193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
7203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
7213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
7223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
7233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
7243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (occupied[item.screen][x][y] != null) {
7253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
726aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + " into cell (" + item.screen + ":"
7273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
728aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
7293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + occupied[item.screen][x][y]);
7303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
7318802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
7328802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
7333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
7343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
7353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
7363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    occupied[item.screen][x][y] = item;
7378802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
7388802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
7393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
7403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
7418802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadWorkspace() {
7433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
744f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
7463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
7473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
7483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
7493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
750f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mItems.clear();
7523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mAppWidgets.clear();
7533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mFolders.clear();
7543c2f7e105d8daab439bff2c362a727ad348d8a44Joe Onorato
7553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
7565c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
7573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Cursor c = contentResolver.query(
7583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
759f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
760d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen            final ItemInfo occupied[][][] =
761d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                    new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
7628802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            try {
7643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
7653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int intentIndex = c.getColumnIndexOrThrow
7663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.INTENT);
7673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int titleIndex = c.getColumnIndexOrThrow
7683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.TITLE);
7693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconTypeIndex = c.getColumnIndexOrThrow(
7703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_TYPE);
7713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
7723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconPackageIndex = c.getColumnIndexOrThrow(
7733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_PACKAGE);
7743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconResourceIndex = c.getColumnIndexOrThrow(
7753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_RESOURCE);
7763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int containerIndex = c.getColumnIndexOrThrow(
7773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.CONTAINER);
7783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(
7793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ITEM_TYPE);
7803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int appWidgetIdIndex = c.getColumnIndexOrThrow(
7813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.APPWIDGET_ID);
7823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int screenIndex = c.getColumnIndexOrThrow(
7833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SCREEN);
7843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellXIndex = c.getColumnIndexOrThrow
7853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLX);
7863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellYIndex = c.getColumnIndexOrThrow
7873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLY);
7883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanXIndex = c.getColumnIndexOrThrow
7893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.SPANX);
7903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanYIndex = c.getColumnIndexOrThrow(
7913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SPANY);
7923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
7933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int displayModeIndex = c.getColumnIndexOrThrow(
7943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.DISPLAY_MODE);
7953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ShortcutInfo info;
7973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                String intentDescription;
7983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LauncherAppWidgetInfo appWidgetInfo;
7993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                int container;
8003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                long id;
8013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Intent intent;
8023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && c.moveToNext()) {
8043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
8053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        int itemType = c.getInt(itemTypeIndex);
8063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        switch (itemType) {
8083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
8093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
8103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            intentDescription = c.getString(intentIndex);
8113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            try {
8123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = Intent.parseUri(intentDescription, 0);
8133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } catch (URISyntaxException e) {
8143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                continue;
8153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
8183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(manager, intent, context, c, iconIndex,
8193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
8203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(c, context, iconTypeIndex,
8223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconPackageIndex, iconResourceIndex, iconIndex,
8233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
8243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (info != null) {
8273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.intent = intent;
8283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.id = c.getLong(idIndex);
8293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
8303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.container = container;
8313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.screen = c.getInt(screenIndex);
832d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellX = c.getInt(cellXIndex);
833d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellY = c.getInt(cellYIndex);
8343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
8363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, info)) {
8373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
839f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
8413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(info);
8433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                default:
8453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    // Item is in a user folder
8463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    UserFolderInfo folderInfo =
8473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            findOrMakeUserFolder(mFolders, container);
8483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    folderInfo.add(info);
8493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
85117a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato
85217a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato                                // now that we've loaded everthing re-save it with the
85317a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato                                // icon in case it disappears somehow.
85417a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato                                updateSavedIcon(context, info, c, iconIndex);
8553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Failed to load the shortcut, probably because the
8573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // activity manager couldn't resolve it (maybe the app
8583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // was uninstalled), or the db row was somehow screwed up.
8593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Delete it.
8603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
8613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Error loading shortcut " + id + ", removing it");
8623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                contentResolver.delete(LauncherSettings.Favorites.getContentUri(
8633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            id, false), null, null);
8643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
866f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
8683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
8709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
871aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            folderInfo.title = c.getString(titleIndex);
8723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.id = id;
8733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            container = c.getInt(containerIndex);
8743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.container = container;
8753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.screen = c.getInt(screenIndex);
876d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellX = c.getInt(cellXIndex);
877d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellY = c.getInt(cellYIndex);
8788802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // check & update map of what's occupied
8803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!checkItemPlacement(occupied, folderInfo)) {
8819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
8823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            switch (container) {
8843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(folderInfo);
8863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            mFolders.put(folderInfo.id, folderInfo);
8903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
8933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Uri uri = Uri.parse(c.getString(uriIndex));
895f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Make sure the live folder exists
8973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final ProviderInfo providerInfo =
8983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    context.getPackageManager().resolveContentProvider(
8993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            uri.getAuthority(), 0);
9003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (providerInfo == null && !isSafeMode) {
9023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
9033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
9043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
9053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intentDescription = c.getString(intentIndex);
9063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = null;
9073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (intentDescription != null) {
9083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    try {
9093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        intent = Intent.parseUri(intentDescription, 0);
9103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    } catch (URISyntaxException e) {
9113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        // Ignore, a live folder might not have a base intent
9123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    }
9133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                }
914f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
9153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.title = c.getString(titleIndex);
9163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.id = id;
9173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.uri = uri;
9189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                container = c.getInt(containerIndex);
9193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.container = container;
9203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.screen = c.getInt(screenIndex);
921d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellX = c.getInt(cellXIndex);
922d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellY = c.getInt(cellYIndex);
9233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.baseIntent = intent;
9243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.displayMode = c.getInt(displayModeIndex);
925f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
9268802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                // check & update map of what's occupied
9273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, liveFolderInfo)) {
9288802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                    break;
9298802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                }
9308802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
9313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
9323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconResourceIndex, liveFolderInfo);
9333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                switch (container) {
9359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
9363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        mItems.add(liveFolderInfo);
9379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                        break;
9389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mFolders.put(liveFolderInfo.id, liveFolderInfo);
9403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
9413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
9423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
9443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Read all Launcher-specific widget details
9453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            int appWidgetId = c.getInt(appWidgetIdIndex);
9463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
9473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final AppWidgetProviderInfo provider =
9493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    widgets.getAppWidgetInfo(appWidgetId);
950aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
9513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!isSafeMode && (provider == null || provider.provider == null ||
9523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    provider.provider.getPackageName() == null)) {
9533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Deleting widget that isn't installed anymore: id="
9543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        + id + " appWidgetId=" + appWidgetId);
9553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
9563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
9573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
9583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.id = id;
9593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.screen = c.getInt(screenIndex);
960d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellX = c.getInt(cellXIndex);
961d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellY = c.getInt(cellYIndex);
962d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanX = c.getInt(spanXIndex);
963d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanY = c.getInt(spanYIndex);
964ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
9653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
9663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
9673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.e(TAG, "Widget found where container "
9683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            + "!= CONTAINER_DESKTOP -- ignoring!");
9693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    continue;
9709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.container = c.getInt(containerIndex);
9728802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
9733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
9743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, appWidgetInfo)) {
9753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
9769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mAppWidgets.add(appWidgetInfo);
9799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
9803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
9819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (Exception e) {
9833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Desktop items loading interrupted:", e);
9849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
9863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } finally {
9873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                c.close();
9883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9895c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (itemsToRemove.size() > 0) {
9913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ContentProviderClient client = contentResolver.acquireContentProviderClient(
9923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LauncherSettings.Favorites.CONTENT_URI);
9933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Remove dead items
9943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (long id : itemsToRemove) {
9953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
9963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Removed id = " + id);
9973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // Don't notify content observers
9993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
10003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        client.delete(LauncherSettings.Favorites.getContentUri(id, false),
10013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                null, null);
10023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (RemoteException e) {
10033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Could not remove id = " + id);
10045c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
10055c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
10063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10075c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
10083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
10093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
10103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "workspace layout: ");
1011d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                for (int y = 0; y < mCellCountY; y++) {
10123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    String line = "";
10133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
10143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (s > 0) {
10153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += " | ";
10163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
1017d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                        for (int x = 0; x < mCellCountX; x++) {
10183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += ((occupied[s][x][y] != null) ? "#" : ".");
10198802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                        }
10208802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
10213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "[ " + line + " ]");
1022a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
10239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
10243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
102531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
10273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Read everything out of our database.
10283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
10293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void bindWorkspace() {
10303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = SystemClock.uptimeMillis();
10313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
10333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
10343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
10353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
10363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
10373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher");
10383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
10393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
104031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N;
10423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're about to start firing items at it
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.startBinding();
10483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Add the items to the workspace.
10523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mItems.size();
10533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i+=ITEMS_CHUNK) {
10543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
10553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
10569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                mHandler.post(new Runnable() {
10579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
1058c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
10603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindItems(mItems, start, start+chunkSize);
10619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
10629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                });
10643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindFolders(mFolders);
10703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the queue goes empty.
10743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Going to start binding widgets soon.");
10783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the widgets, one at a time.
10823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // WARNING: this is calling into the workspace from the background thread,
10833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // but since getCurrentScreen() just returns the int, we should be okay.  This
10843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // is just a hint for the order, and if it's wrong, we'll be okay.
10853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // TODO: instead, we should have that push the current screen into here.
10863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
10873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mAppWidgets.size();
10883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the current screen
10893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen == currentScreen) {
10929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    mHandler.post(new Runnable() {
10939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        public void run() {
1094c131b74922e6323331c374856bda830010d761c8Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            if (callbacks != null) {
10963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
10979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
10999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    });
11009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
11013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the other screens
11033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
11043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
11053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen != currentScreen) {
11063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mHandler.post(new Runnable() {
11073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
11083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
11093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (callbacks != null) {
11103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
11119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
11123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
11133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
11143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're done.
11173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
11183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
11193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
11203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
11213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.finishBindingItems();
11229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
11239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
11243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
11253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // If we're profiling, this is the last thing in the queue.
11263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
11273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
11283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
11303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
11319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
11329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
11333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
11343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
11373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
11383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
11393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
11413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadAllAppsByBatch();
11423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
11433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
11443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsLoaded = true;
11463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
11473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
11483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
11523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
11533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
11543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
11553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
11563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
11603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<ApplicationInfo> list
11613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
11623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
11633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
11643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
11653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
11663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
11673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
11689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
11693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1171a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
11729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
11733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
11753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAllAppsByBatch() {
11793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
11823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
11833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
11843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
11853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
11863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
11873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
118931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
11913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1192cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager packageManager = mContext.getPackageManager();
11943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            List<ResolveInfo> apps = null;
11953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N = Integer.MAX_VALUE;
11973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int startIndex;
11993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int i=0;
12003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int batchSize = -1;
12013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            while (i < N && !mStopped) {
12023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (i == 0) {
12033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mAllAppsList.clear();
12043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
12053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    apps = packageManager.queryIntentActivities(mainIntent, 0);
12063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
12073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities took "
12083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-qiaTime) + "ms");
12093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (apps == null) {
1211cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        return;
1212cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
12133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    N = apps.size();
12143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
12153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities got " + N + " apps");
12163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (N == 0) {
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // There are no apps?!?
12193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return;
12203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (mBatchSize == 0) {
12223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = N;
12233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } else {
12243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = mBatchSize;
12253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
12283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Collections.sort(apps,
12293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            new ResolveInfo.DisplayNameComparator(packageManager));
12303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
12313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "sort took "
12323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-sortTime) + "ms");
12333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
1234cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1235cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
12373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                startIndex = i;
12393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int j=0; i<N && j<batchSize; j++) {
12403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // This builds the icon bitmaps.
12413d605d5bbef35e3b8aded44c5ef7fe3948f8f7d5Patrick Dubroy                    mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
12423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    i++;
1243cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1244cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean first = i <= batchSize;
12463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
12473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> added = mAllAppsList.added;
12483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
12493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1250cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                mHandler.post(new Runnable() {
1251cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    public void run() {
1252cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        final long t = SystemClock.uptimeMillis();
1253cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        if (callbacks != null) {
12543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (first) {
12553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAllApplications(added);
12563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
12573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppsAdded(added);
12583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
12593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (DEBUG_LOADERS) {
12603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.d(TAG, "bound " + added.size() + " apps in "
12613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    + (SystemClock.uptimeMillis() - t) + "ms");
12623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
12633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        } else {
12643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.i(TAG, "not binding apps: no Launcher activity");
1265cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        }
1266cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
1267cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                });
1268cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
12703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
12713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t2) + "ms");
12723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
12733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mAllAppsLoadDelay > 0 && i < N) {
12753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
12763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
12773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
12783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
12793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Thread.sleep(mAllAppsLoadDelay);
12803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException exc) { }
12813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1282cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato            }
1283cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
12853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "cached all " + N + " apps in "
12863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (SystemClock.uptimeMillis()-t) + "ms"
12873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
12883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1290dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
12923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mContext=" + mContext);
12933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
12943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
12953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
12963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
12973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
12983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
129931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
1301700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(task);
13023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1303d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
13043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
13053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
13063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
1307d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
13083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
13093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
13103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
13113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
13123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
131331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1314d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
13153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
13163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
13173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
13183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1319d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
13203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
13213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mApp;
1322dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
13233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
13243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
13253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
13263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
13273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
13283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
13293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.addPackage(context, packages[i]);
1330dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
13313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
13323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
13333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
13343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
13353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.updatePackage(context, packages[i]);
13363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
13383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
13393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
13403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
13413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
13423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.removePackage(packages[i]);
13433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
13453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1346dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
13473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> added = null;
13483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> removed = null;
13493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> modified = null;
13503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
13513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.added.size() > 0) {
13523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                added = mAllAppsList.added;
13533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
13543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.removed.size() > 0) {
13563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                removed = mAllAppsList.removed;
13573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.removed = new ArrayList<ApplicationInfo>();
13583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (ApplicationInfo info: removed) {
13593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mIconCache.remove(info.intent.getComponent());
136031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
136131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
13623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.modified.size() > 0) {
13633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                modified = mAllAppsList.modified;
13643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.modified = new ArrayList<ApplicationInfo>();
13653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
136631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
13683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
13693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
13703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
1371be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
1372be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
13733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
13743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> addedFinal = added;
13753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsAdded(addedFinal);
13793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
13843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> modifiedFinal = modified;
13853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
13893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (removed != null) {
13943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean permanent = mOp != OP_UNAVAILABLE;
13953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> removedFinal = removed;
13963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsRemoved(removedFinal, permanent);
14003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
14013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
14023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
1403be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
140480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
140580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
140680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
140780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
140880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    if (callbacks == mCallbacks.get()) {
140980baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                        callbacks.bindPackagesUpdated();
141080baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
141180baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
141280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
141331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
141431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
141531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
141756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
141856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
14199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
142056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
1421e74daedc3b41d958315f7d5bf69deb232ca89468Joe Onorato        return getShortcutInfo(manager, intent, context, null, -1, -1);
142256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
142356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
142456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
142556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
142656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
142756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
142856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
142956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
143056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Cursor c, int iconIndex, int titleIndex) {
143156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
143256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
143331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
143456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
143556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (componentName == null) {
14369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
143731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
143831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14398ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
14408ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
14418ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
144256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
144356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
144456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
144556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
144656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
144756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
144856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = mIconCache.getIcon(componentName, resolveInfo);
144956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
145056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
145156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
145256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
145356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
145456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
14559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
145656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
145756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
145856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
145956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
146056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
146156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
146256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
146356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
146456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
146556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = resolveInfo.activityInfo.loadLabel(manager);
146656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
146756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
146856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
146956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
147056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
147156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
147256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
147356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
14749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
147556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
14769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
14779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
14789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
14799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
148031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
14820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
14839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
14840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
148556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
148656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
148731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
148856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
14890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = new ShortcutInfo();
14909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
14917376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
14928ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
14938ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
149456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
149556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
14979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
14989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
14999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
15009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
15019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
150256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
150356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
15049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
15059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
150656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
150756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
1508c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
1509c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
151056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
15119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
151256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
151356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
151456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
151556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
151656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
151756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
151856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
151956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
152056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
152156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
15227376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
15239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
15249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
152556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getIconFromCursor(c, iconIndex);
152656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
152756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
152856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
152956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
153056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
153156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
15329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
15339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
15349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
1535d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
153656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
15379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
15389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
153931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1540d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
15419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
1542f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
154331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
154456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    Bitmap getIconFromCursor(Cursor c, int iconIndex) {
154556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (false) {
154656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
154756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
154856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
154956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
155056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
155156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return BitmapFactory.decodeByteArray(data, 0, data.length);
155256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
155356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
155456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
155556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
155656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    ShortcutInfo addShortcut(Context context, Intent data,
15580280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            int screen, int cellX, int cellY, boolean notify) {
15590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1560a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
15610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
15620280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                screen, cellX, cellY, notify);
15630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
15650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
15660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1567a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    /**
156855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     * Attempts to find an AppWidgetProviderInfo that matches the given component.
156955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     */
157055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
157155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            ComponentName component) {
157255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        List<AppWidgetProviderInfo> widgets =
157355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
157455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        for (AppWidgetProviderInfo info : widgets) {
157555cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            if (info.provider.equals(component)) {
157655cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                return info;
157755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            }
157855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        }
157955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        return null;
1580a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
1581a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
158268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    /**
158368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     * Returns a list of all the widgets that can handle configuration with a particular mimeType.
158468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     */
158568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
158668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final PackageManager packageManager = context.getPackageManager();
158768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
158868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new ArrayList<WidgetMimeTypeHandlerData>();
158968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
159068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final Intent supportsIntent =
159168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
159268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        supportsIntent.setType(mimeType);
159368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
159468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Create a set of widget configuration components that we can test against
159568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<AppWidgetProviderInfo> widgets =
159668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
159768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
159868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new HashMap<ComponentName, AppWidgetProviderInfo>();
159968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (AppWidgetProviderInfo info : widgets) {
160068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            configurationComponentToWidget.put(info.configure, info);
160168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
160268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
160368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Run through each of the intents that can handle this type of clip data, and cross
160468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // reference them with the components that are actual configuration components
160568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
160668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                PackageManager.MATCH_DEFAULT_ONLY);
160768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (ResolveInfo info : activities) {
160868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ActivityInfo activityInfo = info.activityInfo;
160968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
161068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                    activityInfo.name);
161168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            if (configurationComponentToWidget.containsKey(infoComponent)) {
161268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                supportedConfigurationActivities.add(
161368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
161468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                                configurationComponentToWidget.get(infoComponent)));
161568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            }
161668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
161768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        return supportedConfigurationActivities;
161868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    }
161968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
1620a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
16210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
16220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
16230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
16240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
16250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
16260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean filtered = false;
16270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
16280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
16290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
16300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
16310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
16320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            filtered = true;
16330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
16340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
16350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
16360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
16370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
16380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
16390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
16400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
16410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
16420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1643c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
1644c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
16450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
16460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
16470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
16480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
16490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
16500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
165156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
165256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
16530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
1654a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            if (fallbackIcon != null) {
1655a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = fallbackIcon;
1656a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            } else {
1657a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = getFallbackIcon();
1658a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                info.usingFallbackIcon = true;
1659a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            }
16600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
16610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
166256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
16630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
16640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
16650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
16660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
16670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
16680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
16690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
16700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1671c9a961952d1a057029874f8426b90181f6876034Michael Jurka    private void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
16729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
167331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
16759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
16769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
16779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
16789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
16799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
16809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
1681c9a961952d1a057029874f8426b90181f6876034Michael Jurka                Resources appResources = packageManager.getResourcesForApplication(packageName);
1682c9a961952d1a057029874f8426b90181f6876034Michael Jurka                final int id = appResources.getIdentifier(resourceName, null, null);
1683c9a961952d1a057029874f8426b90181f6876034Michael Jurka                liveFolderInfo.icon = Utilities.createIconBitmap(
1684c9a961952d1a057029874f8426b90181f6876034Michael Jurka                        mIconCache.getFullResIcon(appResources, id), context);
16859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
1686c9a961952d1a057029874f8426b90181f6876034Michael Jurka                Resources resources = context.getResources();
16870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(
1688c9a961952d1a057029874f8426b90181f6876034Michael Jurka                        mIconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder),
16890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
16909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
16919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
16929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.packageName = packageName;
16939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.resourceName = resourceName;
16949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
16959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
1696c9a961952d1a057029874f8426b90181f6876034Michael Jurka            Resources resources = context.getResources();
16970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            liveFolderInfo.icon = Utilities.createIconBitmap(
1698c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    mIconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder),
16990589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context);
17009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
170131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1702bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
170356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
170417a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        // If apps can't be on SD, don't even bother.
170517a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        if (!mAppsCanBeOnExternalStorage) {
170617a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato            return;
170717a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        }
170856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
170956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
171056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
171156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
171256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
171356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
1714ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
171556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            boolean needSave;
171656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            byte[] data = c.getBlob(iconIndex);
171756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            try {
171856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (data != null) {
171956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
172056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap loaded = info.getIcon(mIconCache);
172156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = !saved.sameAs(loaded);
172256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                } else {
172356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = true;
172456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
172556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } catch (Exception e) {
172656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
172756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
172856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (needSave) {
172956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                Log.d(TAG, "going to save icon bitmap for info=" + info);
173017a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato                // This is slower than is ideal, but this only happens once
173117a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato                // or when the app is updated with a new icon.
173256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                updateItemInDatabase(context, info);
173356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
173456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
173556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
173656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
173731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
17389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously,
17399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
174031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
17419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
17429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
17439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
17449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
17459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
17469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new UserFolderInfo();
17479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
174831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
17499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (UserFolderInfo) folderInfo;
17509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
175131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
17539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
17549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * new one.
17559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
17569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
17579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
17589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
17599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
17609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
17619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new LiveFolderInfo();
17629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
176331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
17649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (LiveFolderInfo) folderInfo;
176531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
176631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
176731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
176831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        String label = activityInfo.loadLabel(manager).toString();
176931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (label == null) {
177031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
177131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (label == null) {
177231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                label = activityInfo.name;
177331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
177431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
177531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return label;
177631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
177731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static final Collator sCollator = Collator.getInstance();
1779b0c27f254a9929be208d5e04554f438076c833bcJoe Onorato    public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
17809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            = new Comparator<ApplicationInfo>() {
17819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public final int compare(ApplicationInfo a, ApplicationInfo b) {
17829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return sCollator.compare(a.title.toString(), b.title.toString());
178331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
17849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    };
178578403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung    public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
178678403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            = new Comparator<ApplicationInfo>() {
178778403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung        public final int compare(ApplicationInfo a, ApplicationInfo b) {
178878403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime < b.firstInstallTime) return 1;
178978403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime > b.firstInstallTime) return -1;
179078403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            return 0;
179178403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung        }
179278403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung    };
1793be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
1794be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
1795be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
1796be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1797be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1798be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1799be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
18003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Log.d(TAG, "mItems size=" + mItems.size());
18013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
18023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
18033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
18043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
18053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1806be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
180731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
1808