LauncherModel.java revision ddc9c1fb1ab426772add520d277ea9c2cd674094
131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.lang.ref.WeakReference;
20aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.net.URISyntaxException;
21aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.text.Collator;
22aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.ArrayList;
23aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.Collections;
24aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.Comparator;
25aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.HashMap;
26aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport java.util.List;
27aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
28629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetManager;
29629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetProviderInfo;
30f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratoimport android.content.BroadcastReceiver;
3131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ComponentName;
325c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.ContentProviderClient;
3331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentResolver;
3431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentValues;
35aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.content.Context;
3631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Intent;
370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent.ShortcutIconResource;
3831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ActivityInfo;
3931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.PackageManager;
405c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.pm.ProviderInfo;
4131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ResolveInfo;
4231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.res.Resources;
4331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.database.Cursor;
4431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Bitmap;
4531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.BitmapFactory;
4631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.net.Uri;
473611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.Handler;
483611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.HandlerThread;
490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.os.Parcelable;
5031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.os.Process;
51aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.os.RemoteException;
529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.os.SystemClock;
53aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.util.Log;
5431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
55edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guyimport com.android.launcher.R;
56edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
5731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
5831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Maintains in-memory state of the Launcher. It is expected that there should be only one
5931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * LauncherModel object held in a static. Also provide APIs for updating the database state
60bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project * for the Launcher.
6131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
62f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratopublic class LauncherModel extends BroadcastReceiver {
63a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final boolean DEBUG_LOADERS = false;
649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final String TAG = "Launcher.Model";
659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
67d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato    private int mBatchSize; // 0 is all apps at once
682ff10b3b0137d258569ce5471808842e88696133Daniel Sandler    private int mAllAppsLoadDelay; // milliseconds between batches
69dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
70f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    private final LauncherApplication mApp;
719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private HandlerThread mWorkerThread;
743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private Handler mWorker;
753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
77cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
78cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
79cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
80cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
81cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
82cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private AllAppsList mAllAppsList; // only access in worker thread
860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
93d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountX;
94d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountY;
95aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
100ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
1019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void finishBindingItems();
1029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
1039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAllApplications(ArrayList<ApplicationInfo> apps);
10464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
10564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
1063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
10780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung        public void bindPackagesUpdated();
108843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        public boolean isAllAppsVisible();
1099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
11031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    LauncherModel(LauncherApplication app, IconCache iconCache) {
112f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        mApp = app;
1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mAllAppsList = new AllAppsList(iconCache);
1140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
1150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mDefaultIcon = Utilities.createIconBitmap(
1170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                app.getPackageManager().getDefaultActivityIcon(), app);
1182ff10b3b0137d258569ce5471808842e88696133Daniel Sandler
1192ff10b3b0137d258569ce5471808842e88696133Daniel Sandler        mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
120d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
121d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato        mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
1223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorkerThread = new HandlerThread("launcher-loader");
1243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorkerThread.start();
1253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorker = new Handler(mWorkerThread.getLooper());
1260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
12856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
1290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
130f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
13131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
1349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
1359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
1379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY) {
1389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
1399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
1409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            addItemToDatabase(context, item, container, screen, cellX, cellY, false);
1419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
1429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
1439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            moveItemInDatabase(context, item, container, screen, cellX, cellY);
1449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
14631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
1499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
1519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int cellX, int cellY) {
152aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
1549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
1559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
1569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
15731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
1599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
160bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
1619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
162d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, cellX);
163d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, cellY);
1649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.SCREEN, item.screen);
165ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
1669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
16731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
16831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
1709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
1719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
172bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
1739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
1749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
1769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
1779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
1789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
1799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
1809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
1819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
1829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
1839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
185bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
186bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
187bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
188aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
189aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
190aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
191aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
192aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
193aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
194aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
195aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
196aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
197aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
198aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
199aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
200aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
201aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
202aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
203aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
204aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
205aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
206aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
207aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
208aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
209aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                ItemInfo item = new ItemInfo();
210aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
211aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
212aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanX = c.getInt(spanXIndex);
213aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanY = c.getInt(spanYIndex);
214aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
215aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
216aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.screen = c.getInt(screenIndex);
217aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
218aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
219aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
220aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
221aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
222aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
223aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
224aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
225aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
226aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
227aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
228aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
229aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
2309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
23131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
2339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
2359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
2369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
2379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
2389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
239f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
2419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
2429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
2439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
2449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
2459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
2469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
2479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
248f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
2509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
2519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeUserFolder(folderList, id);
2539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeLiveFolder(folderList, id);
2569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
25831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
2609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
2619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
2629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.screen = c.getInt(screenIndex);
263d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
264d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
265ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
2679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
2689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
2699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
2702fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
271ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
2739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
27431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
2779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
2789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addItemToDatabase(Context context, ItemInfo item, long container,
2809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY, boolean notify) {
2819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
2829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
2839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
2849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
285ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
2879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
289ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
290d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, cellX, cellY);
291aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
2929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
2939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
294ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (result != null) {
2969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            item.id = Integer.parseInt(result.getPathSegments().get(1));
29731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
29831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
29931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
301aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
302aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
303d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellLayoutChildId(int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
304d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return ((cellId & 0xFF) << 16) | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
305aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
306aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
307d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountX() {
308d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountX;
309aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
310aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
311d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountY() {
312d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountY;
313aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
314aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
315aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
316aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Updates the model orientation helper to take into account the current layout dimensions
317aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * when performing local/canonical coordinate transformations.
318aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
319aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
320d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountX = shortAxisCellCount;
321d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountY = longAxisCellCount;
322aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
323aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
324aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
3259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Update an item to the database in a specified container.
3269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void updateItemInDatabase(Context context, ItemInfo item) {
3289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
3299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
330f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
332d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
333aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
33531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
33631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Removes the specified item from the database
3399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param context
3409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param item
3419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteItemFromDatabase(Context context, ItemInfo item) {
3439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
344f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
3469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
347f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
3509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
3529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
353f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
3559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.CONTENT_URI,
3569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
3579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
358f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
3619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
3639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
3649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
365f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
366f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
367f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3681d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
3699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
3709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
3719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
372f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
3733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
374aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
3769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
3773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
3783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
3793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
3803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
3813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
382f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
38464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
3853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
3863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
3873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
3883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
38956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
3903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
3913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
3923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
3933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
3943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
39556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
3963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
3973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
3983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
3993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
4013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
4023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
40356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
40556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
4073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
408f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
409f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
4113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
4123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
413f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
4153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
4163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(
4173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        PackageUpdatedTask.OP_UNAVAILABLE, packages));
418f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
420f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void startLoader(Context context, boolean isLaunching) {
4223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
4243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
4253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
426843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
4273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
4283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
4293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
4303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LoaderTask oldTask = mLoaderTask;
4313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (oldTask != null) {
4323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (oldTask.isLaunching()) {
4333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // don't downgrade isLaunching if we're already running
4343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        isLaunching = true;
4359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
4363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    oldTask.stopLocked();
4379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
4383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask = new LoaderTask(context, isLaunching);
4393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorker.post(mLoaderTask);
440b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
441b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
4423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
443b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
4443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
4453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
4473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
4489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
449f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
451b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
4523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
4533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
4543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
4553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
4563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
4573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
4583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
4593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
4603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Thread mWaitThread;
4613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
4623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
4633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
4643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
4653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        LoaderTask(Context context, boolean isLaunching) {
4663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
4673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
4683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
469f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
4713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
4723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
473f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindWorkspace() {
4753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
476cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
4773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // For now, just always reload the workspace.  It's ~100 ms vs. the
4783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // binding which takes many hundreds of ms.
4793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // We can reconsider.
4803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
4813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
4823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
4833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (true || !mWorkspaceLoaded) {
4843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadWorkspace();
4853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
4863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
4879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
4883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorkspaceLoaded = true;
4893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
490cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
4913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
4923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            bindWorkspace();
4933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
494f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
4963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
4973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
4983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
4993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
5003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
501843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
5033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
5043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
5053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
5063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
5073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
5089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
5093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
5109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
5119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
5123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
5133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && !mLoadAndBindStepFinished) {
5153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
5163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        this.wait();
5173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
5183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
5199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
5223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
523aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
5243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
5259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
526843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
5273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
528f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
5303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
5313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
5323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
5333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks cbk = mCallbacks.get();
5343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
535843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
537843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
538843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
539dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
540843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
541843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
542dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
543843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
544843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
545843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
546843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
547843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
548cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
5499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
550843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
5523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
5533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
5563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
5579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
558843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
559843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
5609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
5623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
563f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
564843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
565843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
566843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
567cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
568843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
569843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
570843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
571843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
5723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
573843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
5753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
5763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
577f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
5783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
5793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
5803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
5813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
582843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
5833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
584843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Trigger a gc to try to clean up after the stuff is done, since the
5863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // renderscript allocations aren't charged to the java heap.
5873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mStopped) {
588f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                mHandler.post(new Runnable() {
589f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        public void run() {
590f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                            System.gc();
591f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        }
592f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                    });
5933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
5943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
5953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
5963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            System.gc();
5973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
5983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
599f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
601f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
6033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
6043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
6053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
606f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
608f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
6103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
6113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
6123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
6133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
6143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
6153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
6163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
6173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
6183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
6193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
621ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
6223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
6233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
625f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
6273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
6283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
6303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
6313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
6323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
6343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
636f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
638f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
6403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
6413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
6423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
6433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
6443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
6453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
6463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (occupied[item.screen][x][y] != null) {
6473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
648aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + " into cell (" + item.screen + ":"
6493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
650aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
6513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + occupied[item.screen][x][y]);
6523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
6538802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
6548802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
6553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
6563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
6573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
6583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    occupied[item.screen][x][y] = item;
6598802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
6608802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
6613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
6623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
6638802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
6643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadWorkspace() {
6653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
666f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
6683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
6693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
6703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
6713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
672f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mItems.clear();
6743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mAppWidgets.clear();
6753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mFolders.clear();
6763c2f7e105d8daab439bff2c362a727ad348d8a44Joe Onorato
6773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
6785c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
6793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Cursor c = contentResolver.query(
6803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
681f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
682d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen            final ItemInfo occupied[][][] =
683d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                    new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
6848802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
6853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            try {
6863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
6873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int intentIndex = c.getColumnIndexOrThrow
6883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.INTENT);
6893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int titleIndex = c.getColumnIndexOrThrow
6903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.TITLE);
6913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconTypeIndex = c.getColumnIndexOrThrow(
6923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_TYPE);
6933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
6943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconPackageIndex = c.getColumnIndexOrThrow(
6953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_PACKAGE);
6963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconResourceIndex = c.getColumnIndexOrThrow(
6973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_RESOURCE);
6983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int containerIndex = c.getColumnIndexOrThrow(
6993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.CONTAINER);
7003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(
7013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ITEM_TYPE);
7023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int appWidgetIdIndex = c.getColumnIndexOrThrow(
7033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.APPWIDGET_ID);
7043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int screenIndex = c.getColumnIndexOrThrow(
7053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SCREEN);
7063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellXIndex = c.getColumnIndexOrThrow
7073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLX);
7083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellYIndex = c.getColumnIndexOrThrow
7093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLY);
7103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanXIndex = c.getColumnIndexOrThrow
7113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.SPANX);
7123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanYIndex = c.getColumnIndexOrThrow(
7133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SPANY);
7143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
7153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int displayModeIndex = c.getColumnIndexOrThrow(
7163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.DISPLAY_MODE);
7173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ShortcutInfo info;
7193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                String intentDescription;
7203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LauncherAppWidgetInfo appWidgetInfo;
7213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                int container;
7223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                long id;
7233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Intent intent;
7243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && c.moveToNext()) {
7263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
7273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        int itemType = c.getInt(itemTypeIndex);
7283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        switch (itemType) {
7303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
7313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
7323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            intentDescription = c.getString(intentIndex);
7333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            try {
7343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = Intent.parseUri(intentDescription, 0);
7353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } catch (URISyntaxException e) {
7363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                continue;
7373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
7403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(manager, intent, context, c, iconIndex,
7413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
7423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
7433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(c, context, iconTypeIndex,
7443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconPackageIndex, iconResourceIndex, iconIndex,
7453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
7463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (info != null) {
7493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                updateSavedIcon(context, info, c, iconIndex);
7503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.intent = intent;
7523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.id = c.getLong(idIndex);
7533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
7543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.container = container;
7553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.screen = c.getInt(screenIndex);
756d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellX = c.getInt(cellXIndex);
757d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellY = c.getInt(cellYIndex);
7583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
7603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, info)) {
7613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
763f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
7653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
7663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(info);
7673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                default:
7693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    // Item is in a user folder
7703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    UserFolderInfo folderInfo =
7713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            findOrMakeUserFolder(mFolders, container);
7723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    folderInfo.add(info);
7733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
7753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
7763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Failed to load the shortcut, probably because the
7773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // activity manager couldn't resolve it (maybe the app
7783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // was uninstalled), or the db row was somehow screwed up.
7793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Delete it.
7803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
7813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Error loading shortcut " + id + ", removing it");
7823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                contentResolver.delete(LauncherSettings.Favorites.getContentUri(
7833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            id, false), null, null);
7843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
786f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
7883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
7893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
7909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
791aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            folderInfo.title = c.getString(titleIndex);
7923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.id = id;
7933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            container = c.getInt(containerIndex);
7943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.container = container;
7953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.screen = c.getInt(screenIndex);
796d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellX = c.getInt(cellXIndex);
797d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellY = c.getInt(cellYIndex);
7988802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // check & update map of what's occupied
8003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!checkItemPlacement(occupied, folderInfo)) {
8019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
8023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            switch (container) {
8043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(folderInfo);
8063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            mFolders.put(folderInfo.id, folderInfo);
8103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
8133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Uri uri = Uri.parse(c.getString(uriIndex));
815f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Make sure the live folder exists
8173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final ProviderInfo providerInfo =
8183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    context.getPackageManager().resolveContentProvider(
8193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            uri.getAuthority(), 0);
8203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (providerInfo == null && !isSafeMode) {
8223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
8233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
8253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intentDescription = c.getString(intentIndex);
8263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = null;
8273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (intentDescription != null) {
8283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    try {
8293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        intent = Intent.parseUri(intentDescription, 0);
8303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    } catch (URISyntaxException e) {
8313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        // Ignore, a live folder might not have a base intent
8323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    }
8333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                }
834f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.title = c.getString(titleIndex);
8363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.id = id;
8373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.uri = uri;
8389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                container = c.getInt(containerIndex);
8393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.container = container;
8403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.screen = c.getInt(screenIndex);
841d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellX = c.getInt(cellXIndex);
842d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellY = c.getInt(cellYIndex);
8433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.baseIntent = intent;
8443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.displayMode = c.getInt(displayModeIndex);
845f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8468802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                // check & update map of what's occupied
8473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, liveFolderInfo)) {
8488802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                    break;
8498802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                }
8508802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
8523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconResourceIndex, liveFolderInfo);
8533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                switch (container) {
8559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        mItems.add(liveFolderInfo);
8579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                        break;
8589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mFolders.put(liveFolderInfo.id, liveFolderInfo);
8603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
8643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Read all Launcher-specific widget details
8653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            int appWidgetId = c.getInt(appWidgetIdIndex);
8663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final AppWidgetProviderInfo provider =
8693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    widgets.getAppWidgetInfo(appWidgetId);
870aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
8713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!isSafeMode && (provider == null || provider.provider == null ||
8723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    provider.provider.getPackageName() == null)) {
8733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Deleting widget that isn't installed anymore: id="
8743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        + id + " appWidgetId=" + appWidgetId);
8753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
8763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
8783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.id = id;
8793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.screen = c.getInt(screenIndex);
880d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellX = c.getInt(cellXIndex);
881d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellY = c.getInt(cellYIndex);
882d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanX = c.getInt(spanXIndex);
883d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanY = c.getInt(spanYIndex);
884ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
8853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
8863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
8873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.e(TAG, "Widget found where container "
8883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            + "!= CONTAINER_DESKTOP -- ignoring!");
8893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    continue;
8909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.container = c.getInt(containerIndex);
8928802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
8943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, appWidgetInfo)) {
8953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mAppWidgets.add(appWidgetInfo);
8999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
9003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
9019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (Exception e) {
9033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Desktop items loading interrupted:", e);
9049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
9063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } finally {
9073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                c.close();
9083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9095c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (itemsToRemove.size() > 0) {
9113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ContentProviderClient client = contentResolver.acquireContentProviderClient(
9123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LauncherSettings.Favorites.CONTENT_URI);
9133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Remove dead items
9143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (long id : itemsToRemove) {
9153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
9163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Removed id = " + id);
9173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // Don't notify content observers
9193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
9203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        client.delete(LauncherSettings.Favorites.getContentUri(id, false),
9213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                null, null);
9223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (RemoteException e) {
9233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Could not remove id = " + id);
9245c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
9255c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
9263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9275c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
9293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
9303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "workspace layout: ");
931d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                for (int y = 0; y < mCellCountY; y++) {
9323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    String line = "";
9333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
9343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (s > 0) {
9353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += " | ";
9363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
937d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                        for (int x = 0; x < mCellCountX; x++) {
9383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += ((occupied[s][x][y] != null) ? "#" : ".");
9398802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                        }
9408802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
9413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "[ " + line + " ]");
942a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
9439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
9443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
94531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
9473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Read everything out of our database.
9483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
9493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void bindWorkspace() {
9503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = SystemClock.uptimeMillis();
9513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
9533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
9543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
9553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
9563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
9573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher");
9583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
9593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
96031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N;
9623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're about to start firing items at it
9633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
9673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.startBinding();
9683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Add the items to the workspace.
9723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mItems.size();
9733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i+=ITEMS_CHUNK) {
9743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
9753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
9769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                mHandler.post(new Runnable() {
9779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
978c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
9803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindItems(mItems, start, start+chunkSize);
9819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                });
9843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
9883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
9893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindFolders(mFolders);
9903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
9923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
9933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the queue goes empty.
9943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
9953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
9963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
9973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Going to start binding widgets soon.");
9983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the widgets, one at a time.
10023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // WARNING: this is calling into the workspace from the background thread,
10033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // but since getCurrentScreen() just returns the int, we should be okay.  This
10043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // is just a hint for the order, and if it's wrong, we'll be okay.
10053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // TODO: instead, we should have that push the current screen into here.
10063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
10073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mAppWidgets.size();
10083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the current screen
10093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen == currentScreen) {
10129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    mHandler.post(new Runnable() {
10139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        public void run() {
1014c131b74922e6323331c374856bda830010d761c8Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            if (callbacks != null) {
10163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
10179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
10199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    });
10209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the other screens
10233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen != currentScreen) {
10263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mHandler.post(new Runnable() {
10273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
10283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (callbacks != null) {
10303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
10319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
10333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
10343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're done.
10373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.finishBindingItems();
10429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // If we're profiling, this is the last thing in the queue.
10463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
10503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
10519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
10573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
10583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
10593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
10613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadAllAppsByBatch();
10623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
10633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
10643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsLoaded = true;
10663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
10673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
10683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
10723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
10733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
10743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
10753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
10763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
10773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
10803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<ApplicationInfo> list
10813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
10823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
10853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
10889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1091a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
10929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAllAppsByBatch() {
10993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
11023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
11033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
11043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
11053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
11063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
11073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
110931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
11113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1112cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager packageManager = mContext.getPackageManager();
11143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            List<ResolveInfo> apps = null;
11153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N = Integer.MAX_VALUE;
11173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int startIndex;
11193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int i=0;
11203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int batchSize = -1;
11213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            while (i < N && !mStopped) {
11223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (i == 0) {
11233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mAllAppsList.clear();
11243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    apps = packageManager.queryIntentActivities(mainIntent, 0);
11263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities took "
11283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-qiaTime) + "ms");
11293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (apps == null) {
1131cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        return;
1132cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
11333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    N = apps.size();
11343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities got " + N + " apps");
11363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (N == 0) {
11383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // There are no apps?!?
11393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return;
11403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (mBatchSize == 0) {
11423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = N;
11433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } else {
11443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = mBatchSize;
11453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Collections.sort(apps,
11493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            new ResolveInfo.DisplayNameComparator(packageManager));
11503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "sort took "
11523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-sortTime) + "ms");
11533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
1154cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1155cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                startIndex = i;
11593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int j=0; i<N && j<batchSize; j++) {
11603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // This builds the icon bitmaps.
11613d605d5bbef35e3b8aded44c5ef7fe3948f8f7d5Patrick Dubroy                    mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
11623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    i++;
1163cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1164cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean first = i <= batchSize;
11663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
11673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> added = mAllAppsList.added;
11683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
11693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1170cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                mHandler.post(new Runnable() {
1171cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    public void run() {
1172cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        final long t = SystemClock.uptimeMillis();
1173cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        if (callbacks != null) {
11743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (first) {
11753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAllApplications(added);
11763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
11773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppsAdded(added);
11783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
11793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (DEBUG_LOADERS) {
11803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.d(TAG, "bound " + added.size() + " apps in "
11813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    + (SystemClock.uptimeMillis() - t) + "ms");
11823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
11833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        } else {
11843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.i(TAG, "not binding apps: no Launcher activity");
1185cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        }
1186cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
1187cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                });
1188cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
11913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t2) + "ms");
11923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mAllAppsLoadDelay > 0 && i < N) {
11953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
11963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
11973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
11983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
11993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Thread.sleep(mAllAppsLoadDelay);
12003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException exc) { }
12013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1202cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato            }
1203cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
12053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "cached all " + N + " apps in "
12063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (SystemClock.uptimeMillis()-t) + "ms"
12073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
12083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1210dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
12123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mContext=" + mContext);
12133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
12143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
12153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
12163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
12173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
121931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
12213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        mWorker.post(task);
12223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1223d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
12253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
12263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
1227d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
12293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
12303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
12313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
12323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
123331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1234d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
12363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
12373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
12383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1239d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
12413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mApp;
1242dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
12443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
12453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
12463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
12473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
12493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.addPackage(context, packages[i]);
1250dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
12513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
12533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
12553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.updatePackage(context, packages[i]);
12563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
12593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
12603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
12623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.removePackage(packages[i]);
12633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1266dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> added = null;
12683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> removed = null;
12693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> modified = null;
12703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.added.size() > 0) {
12723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                added = mAllAppsList.added;
12733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
12743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.removed.size() > 0) {
12763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                removed = mAllAppsList.removed;
12773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.removed = new ArrayList<ApplicationInfo>();
12783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (ApplicationInfo info: removed) {
12793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mIconCache.remove(info.intent.getComponent());
128031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
128131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
12823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.modified.size() > 0) {
12833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                modified = mAllAppsList.modified;
12843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.modified = new ArrayList<ApplicationInfo>();
12853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
128631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
12883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
12893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
12903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
1291be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
1292be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
12933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
12943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> addedFinal = added;
12953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
12963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
12973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
12983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsAdded(addedFinal);
12993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
13043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> modifiedFinal = modified;
13053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
13093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (removed != null) {
13143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean permanent = mOp != OP_UNAVAILABLE;
13153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> removedFinal = removed;
13163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsRemoved(removedFinal, permanent);
13203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
1323be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
132480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
132580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
132680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
132780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
132880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    if (callbacks == mCallbacks.get()) {
132980baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                        callbacks.bindPackagesUpdated();
133080baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
133180baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
133280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
133331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
133431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
133531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
133756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
133856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
13399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
134056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
1341e74daedc3b41d958315f7d5bf69deb232ca89468Joe Onorato        return getShortcutInfo(manager, intent, context, null, -1, -1);
134256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
134356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
134456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
134556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
134656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
134756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
134856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
134956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
135056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Cursor c, int iconIndex, int titleIndex) {
135156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
135256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
135331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
135456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
135556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (componentName == null) {
13569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
135731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
135831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13598ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
13608ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
13618ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
136256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
136356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
136456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
136556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
136656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
136756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
136856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = mIconCache.getIcon(componentName, resolveInfo);
136956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
137056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
137156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
137256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
137356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
137456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
13759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
137656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
137756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
137856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
137956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
138056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
138156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
138256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
138356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
138456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
138556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = resolveInfo.activityInfo.loadLabel(manager);
138656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
138756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
138856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
138956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
139056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
139156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
139256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
139356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
13949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
139556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
13969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
13979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
13989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
13999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
140031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
14020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
14039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
14040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
140556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
140656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
140731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
140856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
14090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = new ShortcutInfo();
14109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
14117376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
14128ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
14138ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
141456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
141556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
14179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
14189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
14199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
14209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
14219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
142256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
142356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
14249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
14259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
142656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
142756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
142856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
142956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
14309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
143156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
143256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
143356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
143456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
143556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
143656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
143756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
143856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
143956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
144056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
14417376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
14429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
144456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getIconFromCursor(c, iconIndex);
144556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
144656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
144756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
144856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
144956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
145056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
14519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
14529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
1454d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
145556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
14569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
14579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
145831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1459d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
14609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
1461f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
146231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
146356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    Bitmap getIconFromCursor(Cursor c, int iconIndex) {
146456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (false) {
146556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
146656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
146756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
146856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
146956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
147056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return BitmapFactory.decodeByteArray(data, 0, data.length);
147156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
147256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
147356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
147456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
147556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    ShortcutInfo addShortcut(Context context, Intent data,
14770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            CellLayout.CellInfo cellInfo, boolean notify) {
14780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = infoFromShortcutIntent(context, data);
14800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
14810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
14820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
14840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
14850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
14870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
14880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
14890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
14900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
14920589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean filtered = false;
14930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
14940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
14950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
14960589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
14970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
14980589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            filtered = true;
14990589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
15000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
15010589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
15020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
15030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
15040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
15050589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
15060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
15070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
15080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
15090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
15100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
15110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
15120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
15130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
15140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
15150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
151656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
151756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
151956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
152056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
15210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
15220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
152356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
15250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
15260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
15270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
15280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
15300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
15310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
15339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
153431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
15359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
15369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
15379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
15389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
15399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
15409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
15419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
15429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
15439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int id = resources.getIdentifier(resourceName, null, null);
15440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
15450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
15469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
15470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(
15480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context.getResources().getDrawable(R.drawable.ic_launcher_folder),
15490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
15509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
15519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
15529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.packageName = packageName;
15539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.resourceName = resourceName;
15549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
15559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
15560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            liveFolderInfo.icon = Utilities.createIconBitmap(
15570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context.getResources().getDrawable(R.drawable.ic_launcher_folder),
15580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context);
15599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
156031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1561bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
156256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
156356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
156456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
156556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
156656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
156756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
156856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
1569ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
157056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            boolean needSave;
157156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            byte[] data = c.getBlob(iconIndex);
157256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            try {
157356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (data != null) {
157456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
157556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap loaded = info.getIcon(mIconCache);
157656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = !saved.sameAs(loaded);
157756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                } else {
157856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = true;
157956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
158056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } catch (Exception e) {
158156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
158256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
158356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (needSave) {
158456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                Log.d(TAG, "going to save icon bitmap for info=" + info);
158556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // This is slower than is ideal, but this only happens either
158656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // after the froyo OTA or when the app is updated with a new
158756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // icon.
158856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                updateItemInDatabase(context, info);
158956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
159056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
159156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
159256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
159331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
15949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously,
15959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
159631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
15979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
15989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
15999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
16009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
16019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
16029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new UserFolderInfo();
16039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
160431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (UserFolderInfo) folderInfo;
16069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
160731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
16099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
16109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * new one.
16119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
16129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
16139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
16149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
16159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
16169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
16179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new LiveFolderInfo();
16189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
161931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (LiveFolderInfo) folderInfo;
162131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
162231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
162331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
162431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        String label = activityInfo.loadLabel(manager).toString();
162531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (label == null) {
162631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
162731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (label == null) {
162831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                label = activityInfo.name;
162931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
163031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
163131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return label;
163231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
163331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static final Collator sCollator = Collator.getInstance();
1635b0c27f254a9929be208d5e04554f438076c833bcJoe Onorato    public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
16369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            = new Comparator<ApplicationInfo>() {
16379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public final int compare(ApplicationInfo a, ApplicationInfo b) {
16389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return sCollator.compare(a.title.toString(), b.title.toString());
163931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    };
1641be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
1642be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
1643be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
1644be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1645be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1646be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1647be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
16483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Log.d(TAG, "mItems size=" + mItems.size());
16493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
16503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
16513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
16523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
16533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1654be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
165531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
1656