LauncherModel.java revision e9ad59eba6d8ffd2cbf28520c237ccefd291a33c
131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.lang.ref.WeakReference;
20aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.net.URISyntaxException;
21aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.text.Collator;
22aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.ArrayList;
23aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.Collections;
24aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.Comparator;
25aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.HashMap;
26aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.List;
27aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
28629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetManager;
29629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetProviderInfo;
30f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratoimport android.content.BroadcastReceiver;
3131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ComponentName;
325c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.ContentProviderClient;
3331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentResolver;
3431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentValues;
35aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.content.Context;
3631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Intent;
370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent.ShortcutIconResource;
3831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ActivityInfo;
3931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.PackageManager;
405c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.pm.ProviderInfo;
4131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ResolveInfo;
4231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.res.Resources;
4331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.database.Cursor;
4431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Bitmap;
4531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.BitmapFactory;
4631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.net.Uri;
473611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.Handler;
483611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.HandlerThread;
490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.os.Parcelable;
5031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.os.Process;
51aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.os.RemoteException;
529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.os.SystemClock;
53aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.util.Log;
5431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
55edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guyimport com.android.launcher.R;
56edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
5731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
5831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Maintains in-memory state of the Launcher. It is expected that there should be only one
5931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * LauncherModel object held in a static. Also provide APIs for updating the database state
60bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project * for the Launcher.
6131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
62f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratopublic class LauncherModel extends BroadcastReceiver {
63a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final boolean DEBUG_LOADERS = false;
649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final String TAG = "Launcher.Model";
659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
67d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato    private int mBatchSize; // 0 is all apps at once
682ff10b3b0137d258569ce5471808842e88696133Daniel Sandler    private int mAllAppsLoadDelay; // milliseconds between batches
69dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
70f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    private final LauncherApplication mApp;
719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
75700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
76700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    static {
77700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorkerThread.start();
78700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    }
79700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
80700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick
81cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
82cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
83cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
84cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
85cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
86cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private AllAppsList mAllAppsList; // only access in worker thread
900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
97d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountX;
98d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountY;
99aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
101ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        public boolean setLoadOnResume();
1029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
1039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
1049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
105ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
1069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void finishBindingItems();
1079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
1089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAllApplications(ArrayList<ApplicationInfo> apps);
10964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
11064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
1113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
11280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung        public void bindPackagesUpdated();
113843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        public boolean isAllAppsVisible();
1149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
11531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    LauncherModel(LauncherApplication app, IconCache iconCache) {
117f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        mApp = app;
1180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mAllAppsList = new AllAppsList(iconCache);
1190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
1200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mDefaultIcon = Utilities.createIconBitmap(
1220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                app.getPackageManager().getDefaultActivityIcon(), app);
1232ff10b3b0137d258569ce5471808842e88696133Daniel Sandler
1242ff10b3b0137d258569ce5471808842e88696133Daniel Sandler        mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
125d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
126d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato        mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
12956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
1300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
131f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
13231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
1359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
1369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
1389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY) {
1399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
1409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
1419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            addItemToDatabase(context, item, container, screen, cellX, cellY, false);
1429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
1439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
1449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            moveItemInDatabase(context, item, container, screen, cellX, cellY);
1459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
14731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
1509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
1529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int cellX, int cellY) {
153aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
1559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
1569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
1579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
15831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
159ade2f8122f6b1a9b64d2d4a81348db619db4d5b2Brad Fitzpatrick        final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
1609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
1619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
162bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
1639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
164d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, cellX);
165d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, cellY);
1669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.SCREEN, item.screen);
167ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
168700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(new Runnable() {
169700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                public void run() {
170700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                    cr.update(uri, values, null, null);
171700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                }
172700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick            });
17331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
17431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
1769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
1779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
178bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
1799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
1809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
1829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
1839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
1849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
1859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
1869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
1879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
1889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
1899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
191bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
192bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
193bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
194aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
195aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
196aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
197aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
198aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
199aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
200aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
201aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
202aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
203aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
204aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
205aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
206aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
207aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
208aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
209aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
210aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
211aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
212aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
213aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
214aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
215aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                ItemInfo item = new ItemInfo();
216aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
217aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
218aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanX = c.getInt(spanXIndex);
219aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanY = c.getInt(spanYIndex);
220aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
221aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
222aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.screen = c.getInt(screenIndex);
223aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
224aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
225aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
226aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
227aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
228aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
229aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
230aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
231aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
232aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
233aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
234aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
235aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
2369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
23731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
2399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
2419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
2429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
2439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
2449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
245f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
2479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
2489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
2499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
2509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
2519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
2529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
2539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
254f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
2569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
2579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeUserFolder(folderList, id);
2599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeLiveFolder(folderList, id);
2629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
26431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
2669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
2679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
2689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.screen = c.getInt(screenIndex);
269d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
270d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
271ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
2739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
2749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
2759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
2762fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
277ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
2799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
28031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
2839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
2849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addItemToDatabase(Context context, ItemInfo item, long container,
2869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY, boolean notify) {
2879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
2889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
2899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
2909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
291ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
2939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
295ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
296d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, cellX, cellY);
297aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
2989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
2999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
300ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
3019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (result != null) {
3029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            item.id = Integer.parseInt(result.getPathSegments().get(1));
30331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
30431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
30531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
307aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
308aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
309845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka    static int getCellLayoutChildId(
310845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka            int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
311845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka        return ((cellId & 0xFF) << 24)
312845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka                | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
313aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
314aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
315d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountX() {
316d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountX;
317aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
318aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
319d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountY() {
320d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountY;
321aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
322aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
323aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
324aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Updates the model orientation helper to take into account the current layout dimensions
325aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * when performing local/canonical coordinate transformations.
326aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
327aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
328d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountX = shortAxisCellCount;
329d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountY = longAxisCellCount;
330aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
331aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
332aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
3339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Update an item to the database in a specified container.
3349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void updateItemInDatabase(Context context, ItemInfo item) {
3369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
3379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
338f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
340d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
341aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
34331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
34431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Removes the specified item from the database
3479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param context
3489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param item
3499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteItemFromDatabase(Context context, ItemInfo item) {
3519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
35273013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick        final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
353700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(new Runnable() {
354700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                public void run() {
355700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                    cr.delete(uriToDelete, null, null);
356700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                }
357700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick            });
3589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
359f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
3629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
3649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
365f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
3679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.CONTENT_URI,
3689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
3699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
370f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
3739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
3759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
3769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
377f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
378f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
379f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3801d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
3819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
3829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
3839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
384f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
3853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
386aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
3889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
3893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
3903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
3913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
3923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
3933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
394f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
39664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
3973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
3983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
3993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
4003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
40156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
4033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
4043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
4053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
40756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
4093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
4103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
4113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
4133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
4143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
41556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
41756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
4193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
420f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
421f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
423cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // First, schedule to add these apps back in.
424cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
425cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
426cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // Then, rebind everything.
427e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            startLoaderFromBackground();
4283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
4293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
4303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(
4313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        PackageUpdatedTask.OP_UNAVAILABLE, packages));
432e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
433e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // If we have changed locale we need to clear out the labels in all apps.
434e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // Do this here because if the launcher activity is running it will be restarted.
435e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // If it's not running startLoaderFromBackground will merely tell it that it needs
436e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // to reload.  Either way, mAllAppsLoaded will be cleared so it re-reads everything
437e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            // next time.
438e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            mAllAppsLoaded = false;
439e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            startLoaderFromBackground();
440e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
441e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    }
442e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato
443e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    /**
444e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * When the launcher is in the background, it's possible for it to miss paired
445e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * configuration changes.  So whenever we trigger the loader from the background
446e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * tell the launcher that it needs to re-run the loader when it comes back instead
447e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * of doing it now.
448e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     */
449e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    public void startLoaderFromBackground() {
450e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        boolean runLoader = false;
451e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (mCallbacks != null) {
452e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            Callbacks callbacks = mCallbacks.get();
453e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            if (callbacks != null) {
454e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                // Only actually run the loader if they're not paused.
455e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                if (!callbacks.setLoadOnResume()) {
456e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                    runLoader = true;
457e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                }
458e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            }
459e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
460e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (runLoader) {
461e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            startLoader(mApp, false);
462f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
464f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void startLoader(Context context, boolean isLaunching) {
4663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
4683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
4693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
470843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
4713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
4723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
4733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
4743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LoaderTask oldTask = mLoaderTask;
4753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (oldTask != null) {
4763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (oldTask.isLaunching()) {
4773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // don't downgrade isLaunching if we're already running
4783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        isLaunching = true;
4799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
4803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    oldTask.stopLocked();
4819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
4823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask = new LoaderTask(context, isLaunching);
483700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                sWorker.post(mLoaderTask);
484b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
485b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
4863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
487b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
4883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
4893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
4913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
4929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
493f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
495b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
4963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
4973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
4983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
4993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
5003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
5013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
5023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
5033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
5043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Thread mWaitThread;
5053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
5063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
5073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
5083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        LoaderTask(Context context, boolean isLaunching) {
5103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
5113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
5123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
513f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
5153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
5163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
517f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindWorkspace() {
5193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
520cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
5213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // For now, just always reload the workspace.  It's ~100 ms vs. the
5223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // binding which takes many hundreds of ms.
5233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // We can reconsider.
5243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
5253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
5263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
5273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (true || !mWorkspaceLoaded) {
5283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadWorkspace();
5293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
5303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
5319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
5323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorkspaceLoaded = true;
5333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
534cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
5353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
5363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            bindWorkspace();
5373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
538f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
5403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
5413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
5423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
5433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
5443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
545843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
5473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
5483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
5493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
5503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
5513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
5529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
5533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
5549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
5559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
5563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
5573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && !mLoadAndBindStepFinished) {
5593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
5603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        this.wait();
5613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
5623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
5639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
5663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
567aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
5683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
5699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
570843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
5713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
572f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
5743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
5753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
5763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
5773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks cbk = mCallbacks.get();
5783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
579843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
581843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
582843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
583dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
584843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
585843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
586dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
587843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
588843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
589843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
590843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
591843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
592cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
5939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
594843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
5963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
5973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
6003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
6019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
602843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
603843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
6049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
6059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
6063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
607f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
608843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
609843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
610843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
611cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
612843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
613843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
614843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
615843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
6163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
617843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
6183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
6193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
6203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
621f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
6223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
6233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
6243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
6253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
626843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
6273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
628843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
6293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Trigger a gc to try to clean up after the stuff is done, since the
6303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // renderscript allocations aren't charged to the java heap.
6313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mStopped) {
632f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                mHandler.post(new Runnable() {
633f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        public void run() {
634f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                            System.gc();
635f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        }
636f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                    });
6373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
6383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
6393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
6403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            System.gc();
6413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
6423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
643f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
645f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
6473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
6483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
6493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
650f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
652f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
6543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
6553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
6563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
6573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
6583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
6593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
6603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
6613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
6623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
6633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
665ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
6663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
6673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
669f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
6713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
6723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
6743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
6753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
6763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
6783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
680f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
682f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
6843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
6853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
6863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
6873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
6883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
6893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
6903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (occupied[item.screen][x][y] != null) {
6913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
692aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + " into cell (" + item.screen + ":"
6933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
694aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
6953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + occupied[item.screen][x][y]);
6963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
6978802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
6988802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
6993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
7003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
7013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
7023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    occupied[item.screen][x][y] = item;
7038802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
7048802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
7053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
7063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
7078802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadWorkspace() {
7093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
710f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
7123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
7133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
7143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
7153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
716f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mItems.clear();
7183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mAppWidgets.clear();
7193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mFolders.clear();
7203c2f7e105d8daab439bff2c362a727ad348d8a44Joe Onorato
7213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
7225c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
7233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Cursor c = contentResolver.query(
7243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
725f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
726d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen            final ItemInfo occupied[][][] =
727d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                    new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
7288802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            try {
7303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
7313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int intentIndex = c.getColumnIndexOrThrow
7323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.INTENT);
7333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int titleIndex = c.getColumnIndexOrThrow
7343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.TITLE);
7353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconTypeIndex = c.getColumnIndexOrThrow(
7363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_TYPE);
7373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
7383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconPackageIndex = c.getColumnIndexOrThrow(
7393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_PACKAGE);
7403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconResourceIndex = c.getColumnIndexOrThrow(
7413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_RESOURCE);
7423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int containerIndex = c.getColumnIndexOrThrow(
7433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.CONTAINER);
7443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(
7453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ITEM_TYPE);
7463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int appWidgetIdIndex = c.getColumnIndexOrThrow(
7473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.APPWIDGET_ID);
7483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int screenIndex = c.getColumnIndexOrThrow(
7493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SCREEN);
7503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellXIndex = c.getColumnIndexOrThrow
7513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLX);
7523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellYIndex = c.getColumnIndexOrThrow
7533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLY);
7543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanXIndex = c.getColumnIndexOrThrow
7553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.SPANX);
7563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanYIndex = c.getColumnIndexOrThrow(
7573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SPANY);
7583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
7593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int displayModeIndex = c.getColumnIndexOrThrow(
7603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.DISPLAY_MODE);
7613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ShortcutInfo info;
7633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                String intentDescription;
7643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LauncherAppWidgetInfo appWidgetInfo;
7653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                int container;
7663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                long id;
7673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Intent intent;
7683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && c.moveToNext()) {
7703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
7713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        int itemType = c.getInt(itemTypeIndex);
7723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        switch (itemType) {
7743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
7753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
7763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            intentDescription = c.getString(intentIndex);
7773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            try {
7783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = Intent.parseUri(intentDescription, 0);
7793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } catch (URISyntaxException e) {
7803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                continue;
7813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
7843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(manager, intent, context, c, iconIndex,
7853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
7863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
7873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(c, context, iconTypeIndex,
7883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconPackageIndex, iconResourceIndex, iconIndex,
7893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
7903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (info != null) {
7933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                updateSavedIcon(context, info, c, iconIndex);
7943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.intent = intent;
7963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.id = c.getLong(idIndex);
7973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
7983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.container = container;
7993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.screen = c.getInt(screenIndex);
800d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellX = c.getInt(cellXIndex);
801d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellY = c.getInt(cellYIndex);
8023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
8043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, info)) {
8053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
807f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
8093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(info);
8113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                default:
8133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    // Item is in a user folder
8143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    UserFolderInfo folderInfo =
8153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            findOrMakeUserFolder(mFolders, container);
8163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    folderInfo.add(info);
8173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Failed to load the shortcut, probably because the
8213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // activity manager couldn't resolve it (maybe the app
8223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // was uninstalled), or the db row was somehow screwed up.
8233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Delete it.
8243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
8253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Error loading shortcut " + id + ", removing it");
8263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                contentResolver.delete(LauncherSettings.Favorites.getContentUri(
8273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            id, false), null, null);
8283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
830f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
8323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
8349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
835aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            folderInfo.title = c.getString(titleIndex);
8363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.id = id;
8373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            container = c.getInt(containerIndex);
8383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.container = container;
8393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.screen = c.getInt(screenIndex);
840d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellX = c.getInt(cellXIndex);
841d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellY = c.getInt(cellYIndex);
8428802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // check & update map of what's occupied
8443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!checkItemPlacement(occupied, folderInfo)) {
8459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
8463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            switch (container) {
8483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(folderInfo);
8503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            mFolders.put(folderInfo.id, folderInfo);
8543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
8573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Uri uri = Uri.parse(c.getString(uriIndex));
859f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Make sure the live folder exists
8613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final ProviderInfo providerInfo =
8623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    context.getPackageManager().resolveContentProvider(
8633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            uri.getAuthority(), 0);
8643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (providerInfo == null && !isSafeMode) {
8663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
8673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
8693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intentDescription = c.getString(intentIndex);
8703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = null;
8713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (intentDescription != null) {
8723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    try {
8733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        intent = Intent.parseUri(intentDescription, 0);
8743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    } catch (URISyntaxException e) {
8753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        // Ignore, a live folder might not have a base intent
8763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    }
8773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                }
878f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.title = c.getString(titleIndex);
8803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.id = id;
8813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.uri = uri;
8829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                container = c.getInt(containerIndex);
8833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.container = container;
8843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.screen = c.getInt(screenIndex);
885d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellX = c.getInt(cellXIndex);
886d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellY = c.getInt(cellYIndex);
8873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.baseIntent = intent;
8883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.displayMode = c.getInt(displayModeIndex);
889f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8908802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                // check & update map of what's occupied
8913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, liveFolderInfo)) {
8928802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                    break;
8938802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                }
8948802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
8963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconResourceIndex, liveFolderInfo);
8973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                switch (container) {
8999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
9003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        mItems.add(liveFolderInfo);
9019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                        break;
9029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mFolders.put(liveFolderInfo.id, liveFolderInfo);
9043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
9053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
9063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
9083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Read all Launcher-specific widget details
9093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            int appWidgetId = c.getInt(appWidgetIdIndex);
9103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
9113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final AppWidgetProviderInfo provider =
9133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    widgets.getAppWidgetInfo(appWidgetId);
914aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
9153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!isSafeMode && (provider == null || provider.provider == null ||
9163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    provider.provider.getPackageName() == null)) {
9173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Deleting widget that isn't installed anymore: id="
9183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        + id + " appWidgetId=" + appWidgetId);
9193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
9203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
9213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
9223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.id = id;
9233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.screen = c.getInt(screenIndex);
924d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellX = c.getInt(cellXIndex);
925d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellY = c.getInt(cellYIndex);
926d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanX = c.getInt(spanXIndex);
927d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanY = c.getInt(spanYIndex);
928ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
9293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
9303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
9313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.e(TAG, "Widget found where container "
9323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            + "!= CONTAINER_DESKTOP -- ignoring!");
9333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    continue;
9349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.container = c.getInt(containerIndex);
9368802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
9373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
9383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, appWidgetInfo)) {
9393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
9409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mAppWidgets.add(appWidgetInfo);
9439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
9443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
9459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (Exception e) {
9473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Desktop items loading interrupted:", e);
9489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
9503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } finally {
9513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                c.close();
9523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9535c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (itemsToRemove.size() > 0) {
9553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ContentProviderClient client = contentResolver.acquireContentProviderClient(
9563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LauncherSettings.Favorites.CONTENT_URI);
9573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Remove dead items
9583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (long id : itemsToRemove) {
9593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
9603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Removed id = " + id);
9613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // Don't notify content observers
9633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
9643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        client.delete(LauncherSettings.Favorites.getContentUri(id, false),
9653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                null, null);
9663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (RemoteException e) {
9673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Could not remove id = " + id);
9685c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
9695c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
9703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9715c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
9733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
9743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "workspace layout: ");
975d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                for (int y = 0; y < mCellCountY; y++) {
9763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    String line = "";
9773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
9783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (s > 0) {
9793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += " | ";
9803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
981d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                        for (int x = 0; x < mCellCountX; x++) {
9823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += ((occupied[s][x][y] != null) ? "#" : ".");
9838802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                        }
9848802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
9853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "[ " + line + " ]");
986a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
9879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
9883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
98931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
9913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Read everything out of our database.
9923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
9933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void bindWorkspace() {
9943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = SystemClock.uptimeMillis();
9953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
9973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
9983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
9993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
10003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
10013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher");
10023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
10033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
100431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N;
10063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're about to start firing items at it
10073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.startBinding();
10123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Add the items to the workspace.
10163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mItems.size();
10173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i+=ITEMS_CHUNK) {
10183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
10193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
10209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                mHandler.post(new Runnable() {
10219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
1022c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
10243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindItems(mItems, start, start+chunkSize);
10259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
10269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                });
10283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindFolders(mFolders);
10343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the queue goes empty.
10383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Going to start binding widgets soon.");
10423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the widgets, one at a time.
10463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // WARNING: this is calling into the workspace from the background thread,
10473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // but since getCurrentScreen() just returns the int, we should be okay.  This
10483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // is just a hint for the order, and if it's wrong, we'll be okay.
10493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // TODO: instead, we should have that push the current screen into here.
10503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
10513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mAppWidgets.size();
10523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the current screen
10533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen == currentScreen) {
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.bindAppWidget(widget);
10619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
10639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    });
10649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the other screens
10673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen != currentScreen) {
10703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mHandler.post(new Runnable() {
10713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
10723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (callbacks != null) {
10743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
10759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
10773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
10783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're done.
10813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.finishBindingItems();
10869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // If we're profiling, this is the last thing in the queue.
10903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
10943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
10959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
11013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
11023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
11033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
11053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadAllAppsByBatch();
11063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
11073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
11083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsLoaded = true;
11103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
11113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
11123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
11163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
11173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
11183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
11193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
11203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
11243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<ApplicationInfo> list
11253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
11263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
11273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
11283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
11293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
11303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
11313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
11329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
11333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1135a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
11369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
11373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
11393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAllAppsByBatch() {
11433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
11463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
11473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
11483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
11493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
11503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
11513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
115331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
11553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1156cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager packageManager = mContext.getPackageManager();
11583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            List<ResolveInfo> apps = null;
11593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N = Integer.MAX_VALUE;
11613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int startIndex;
11633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int i=0;
11643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int batchSize = -1;
11653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            while (i < N && !mStopped) {
11663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (i == 0) {
11673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mAllAppsList.clear();
11683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    apps = packageManager.queryIntentActivities(mainIntent, 0);
11703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities took "
11723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-qiaTime) + "ms");
11733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (apps == null) {
1175cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        return;
1176cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
11773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    N = apps.size();
11783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities got " + N + " apps");
11803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (N == 0) {
11823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // There are no apps?!?
11833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return;
11843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (mBatchSize == 0) {
11863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = N;
11873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } else {
11883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = mBatchSize;
11893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Collections.sort(apps,
11933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            new ResolveInfo.DisplayNameComparator(packageManager));
11943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "sort took "
11963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-sortTime) + "ms");
11973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
1198cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1199cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
12013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                startIndex = i;
12033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int j=0; i<N && j<batchSize; j++) {
12043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // This builds the icon bitmaps.
12053d605d5bbef35e3b8aded44c5ef7fe3948f8f7d5Patrick Dubroy                    mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
12063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    i++;
1207cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1208cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean first = i <= batchSize;
12103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
12113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> added = mAllAppsList.added;
12123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
12133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1214cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                mHandler.post(new Runnable() {
1215cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    public void run() {
1216cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        final long t = SystemClock.uptimeMillis();
1217cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        if (callbacks != null) {
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (first) {
12193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAllApplications(added);
12203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
12213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppsAdded(added);
12223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
12233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (DEBUG_LOADERS) {
12243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.d(TAG, "bound " + added.size() + " apps in "
12253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    + (SystemClock.uptimeMillis() - t) + "ms");
12263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
12273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        } else {
12283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.i(TAG, "not binding apps: no Launcher activity");
1229cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        }
1230cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
1231cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                });
1232cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
12343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
12353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t2) + "ms");
12363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
12373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mAllAppsLoadDelay > 0 && i < N) {
12393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
12403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
12413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
12423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
12433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Thread.sleep(mAllAppsLoadDelay);
12443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException exc) { }
12453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1246cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato            }
1247cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
12493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "cached all " + N + " apps in "
12503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (SystemClock.uptimeMillis()-t) + "ms"
12513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
12523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1254dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
12563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mContext=" + mContext);
12573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
12583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
12593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
12603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
12613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
12623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
126331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
1265700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(task);
12663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1267d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
12693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
12703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
1271d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
12733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
12743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
12753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
12763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
127731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1278d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
12803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
12813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
12823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1283d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
12853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mApp;
1286dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
12883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
12893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
12903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
12913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
12933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.addPackage(context, packages[i]);
1294dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
12953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
12973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
12993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.updatePackage(context, packages[i]);
13003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
13023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
13033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
13043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
13053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
13063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.removePackage(packages[i]);
13073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
13093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1310dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
13113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> added = null;
13123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> removed = null;
13133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> modified = null;
13143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
13153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.added.size() > 0) {
13163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                added = mAllAppsList.added;
13173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
13183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.removed.size() > 0) {
13203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                removed = mAllAppsList.removed;
13213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.removed = new ArrayList<ApplicationInfo>();
13223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (ApplicationInfo info: removed) {
13233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mIconCache.remove(info.intent.getComponent());
132431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
132531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
13263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.modified.size() > 0) {
13273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                modified = mAllAppsList.modified;
13283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.modified = new ArrayList<ApplicationInfo>();
13293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
133031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
13323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
13333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
13343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
1335be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
1336be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
13373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
13383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> addedFinal = added;
13393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsAdded(addedFinal);
13433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
13483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> modifiedFinal = modified;
13493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
13533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (removed != null) {
13583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean permanent = mOp != OP_UNAVAILABLE;
13593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> removedFinal = removed;
13603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsRemoved(removedFinal, permanent);
13643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
1367be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
136880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
136980baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
137080baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
137180baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
137280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    if (callbacks == mCallbacks.get()) {
137380baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                        callbacks.bindPackagesUpdated();
137480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
137580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
137680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
137731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
137831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
137931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
138156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
138256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
13839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
138456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
1385e74daedc3b41d958315f7d5bf69deb232ca89468Joe Onorato        return getShortcutInfo(manager, intent, context, null, -1, -1);
138656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
138756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
138856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
138956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
139056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
139156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
139256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
139356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
139456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Cursor c, int iconIndex, int titleIndex) {
139556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
139656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
139731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
139856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
139956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (componentName == null) {
14009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
140131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
140231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14038ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
14048ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
14058ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
140656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
140756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
140856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
140956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
141056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
141156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
141256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = mIconCache.getIcon(componentName, resolveInfo);
141356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
141456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
141556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
141656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
141756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
141856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
14199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
142056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
142156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
142256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
142356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
142456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
142556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
142656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
142756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
142856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
142956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = resolveInfo.activityInfo.loadLabel(manager);
143056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
143156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
143256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
143356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
143456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
143556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
143656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
143756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
14389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
143956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
14409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
14419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
14429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
14439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
144431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
14460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
14479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
14480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
144956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
145056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
145131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
145256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
14530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = new ShortcutInfo();
14549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
14557376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
14568ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
14578ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
145856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
145956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
14619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
14629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
14639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
14649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
14659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
146656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
146756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
14689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
14699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
147056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
147156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
147256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
147356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
14749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
147556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
147656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
147756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
147856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
147956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
148056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
148156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
148256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
148356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
148456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
14857376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
14869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
148856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getIconFromCursor(c, iconIndex);
148956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
149056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
149156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
149256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
149356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
149456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
14959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
14969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
1498d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
149956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
15009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
15019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
150231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1503d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
15049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
1505f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
150631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
150756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    Bitmap getIconFromCursor(Cursor c, int iconIndex) {
150856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (false) {
150956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
151056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
151156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
151256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
151356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
151456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return BitmapFactory.decodeByteArray(data, 0, data.length);
151556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
151656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
151756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
151856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
151956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    ShortcutInfo addShortcut(Context context, Intent data,
15210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            int screen, int cellX, int cellY, boolean notify) {
15220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1523a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
15240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
15250280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                screen, cellX, cellY, notify);
15260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
15280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
15290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1530a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    /**
1531a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     * Ensures that a given shortcut intent actually has all the fields that we need to create a
1532a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     * proper ShortcutInfo.
1533a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     */
1534a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    boolean validateShortcutIntent(Intent data) {
1535a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        // We don't require Intent.EXTRA_SHORTCUT_ICON, since we can pull a default fallback icon
1536a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        return InstallShortcutReceiver.ACTION_INSTALL_SHORTCUT.equals(data.getAction()) &&
153755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                (data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME) != null) &&
153855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                (data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT) != null);
153955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    }
154055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung
154155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    /**
154255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     * Ensures that a given widget intent actually has all the fields that we need to create a
154355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     * proper widget.
154455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     */
154555cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    boolean validateWidgetIntent(Intent data) {
154655cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        // We don't require Intent.EXTRA_APPWIDGET_CONFIGURATION_DATA, since that will just be
154755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        // forwarded onto the widget's configuration activity if it exists
154855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        return InstallWidgetReceiver.ACTION_INSTALL_WIDGET.equals(data.getAction()) &&
154955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                (data.getStringExtra(InstallWidgetReceiver.EXTRA_APPWIDGET_COMPONENT) != null);
155055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    }
155155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung
155255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    /**
155355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     * Attempts to find an AppWidgetProviderInfo that matches the given component.
155455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     */
155555cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
155655cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            ComponentName component) {
155755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        List<AppWidgetProviderInfo> widgets =
155855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
155955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        for (AppWidgetProviderInfo info : widgets) {
156055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            if (info.provider.equals(component)) {
156155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                return info;
156255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            }
156355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        }
156455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        return null;
1565a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
1566a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
1567a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
15680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
15690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
15700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
15710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
15730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean filtered = false;
15740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
15750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
15760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
15780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
15790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            filtered = true;
15800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
15810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
15820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
15830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
15840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
15850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
15860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
15870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
15880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
15890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
15900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
15910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
15920589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
15930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
15940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
15950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
15960589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
159756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
159856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15990589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
1600a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            if (fallbackIcon != null) {
1601a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = fallbackIcon;
1602a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            } else {
1603a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = getFallbackIcon();
1604a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                info.usingFallbackIcon = true;
1605a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            }
16060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
16070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
160856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
16090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
16100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
16110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
16120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
16130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
16140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
16150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
16160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
16179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
16189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
161931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
16219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
16229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
16239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
16249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
16259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
16269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
16279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
16289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int id = resources.getIdentifier(resourceName, null, null);
16290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
16300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
16319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
16320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(
16330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context.getResources().getDrawable(R.drawable.ic_launcher_folder),
16340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
16359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
16369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
16379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.packageName = packageName;
16389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.resourceName = resourceName;
16399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
16409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
16410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            liveFolderInfo.icon = Utilities.createIconBitmap(
16420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context.getResources().getDrawable(R.drawable.ic_launcher_folder),
16430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context);
16449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
164531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1646bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
164756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
164856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
164956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
165056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
165156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
165256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
165356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
1654ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
165556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            boolean needSave;
165656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            byte[] data = c.getBlob(iconIndex);
165756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            try {
165856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (data != null) {
165956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
166056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap loaded = info.getIcon(mIconCache);
166156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = !saved.sameAs(loaded);
166256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                } else {
166356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = true;
166456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
166556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } catch (Exception e) {
166656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
166756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
166856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (needSave) {
166956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                Log.d(TAG, "going to save icon bitmap for info=" + info);
167056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // This is slower than is ideal, but this only happens either
167156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // after the froyo OTA or when the app is updated with a new
167256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // icon.
167356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                updateItemInDatabase(context, info);
167456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
167556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
167656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
167756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
167831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
16799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously,
16809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
168131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
16829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
16839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
16849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
16859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
16869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
16879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new UserFolderInfo();
16889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
168931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (UserFolderInfo) folderInfo;
16919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
169231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
16949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
16959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * new one.
16969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
16979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
16989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
16999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
17009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
17019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
17029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new LiveFolderInfo();
17039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
170431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
17059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (LiveFolderInfo) folderInfo;
170631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
170731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
170831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
170931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        String label = activityInfo.loadLabel(manager).toString();
171031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (label == null) {
171131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
171231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (label == null) {
171331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                label = activityInfo.name;
171431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
171531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
171631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return label;
171731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
171831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static final Collator sCollator = Collator.getInstance();
1720b0c27f254a9929be208d5e04554f438076c833bcJoe Onorato    public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
17219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            = new Comparator<ApplicationInfo>() {
17229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public final int compare(ApplicationInfo a, ApplicationInfo b) {
17239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return sCollator.compare(a.title.toString(), b.title.toString());
172431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
17259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    };
1726be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
1727be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
1728be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
1729be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1730be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1731be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1732be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
17333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Log.d(TAG, "mItems size=" + mItems.size());
17343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
17353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
17363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
17373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
17383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1739be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
174031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
1741