LauncherModel.java revision ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ec
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
559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.lang.ref.WeakReference;
569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.net.URISyntaxException;
579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.text.Collator;
5831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport java.util.ArrayList;
5956d8291af6a28c6ba64113120efdf84a785e816cJoe Onoratoimport java.util.Arrays;
609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport java.util.Collections;
61700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrickimport java.util.Comparator;
6231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport java.util.HashMap;
6331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport java.util.List;
6431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
65edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guyimport com.android.launcher.R;
66edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
6731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
6831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Maintains in-memory state of the Launcher. It is expected that there should be only one
6931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * LauncherModel object held in a static. Also provide APIs for updating the database state
70bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project * for the Launcher.
7131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
72f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratopublic class LauncherModel extends BroadcastReceiver {
73a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final boolean DEBUG_LOADERS = false;
749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final String TAG = "Launcher.Model";
759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
77d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato    private int mBatchSize; // 0 is all apps at once
782ff10b3b0137d258569ce5471808842e88696133Daniel Sandler    private int mAllAppsLoadDelay; // milliseconds between batches
79dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
80f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    private final LauncherApplication mApp;
819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
85700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
86700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    static {
87700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorkerThread.start();
88700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    }
89700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
90700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick
91cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
92cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
93cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
94cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
95cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
96cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private AllAppsList mAllAppsList; // only access in worker thread
1000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
1013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
1023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
1033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
1040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1050589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
1069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
107d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountX;
108d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountY;
109aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
111ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        public boolean setLoadOnResume();
1129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
1139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
1149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
115ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
1169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void finishBindingItems();
1179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
1189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAllApplications(ArrayList<ApplicationInfo> apps);
11964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
12064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
1213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
12280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung        public void bindPackagesUpdated();
123843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        public boolean isAllAppsVisible();
1249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
12531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    LauncherModel(LauncherApplication app, IconCache iconCache) {
127f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato        mApp = app;
1280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mAllAppsList = new AllAppsList(iconCache);
1290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
1300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mDefaultIcon = Utilities.createIconBitmap(
1320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                app.getPackageManager().getDefaultActivityIcon(), app);
1332ff10b3b0137d258569ce5471808842e88696133Daniel Sandler
1342ff10b3b0137d258569ce5471808842e88696133Daniel Sandler        mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
135d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
136d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato        mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
1370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
13956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
1400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
141f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
14231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
1459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
1469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
1489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY) {
1499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
1509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
1519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            addItemToDatabase(context, item, container, screen, cellX, cellY, false);
1529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
1539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
1549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            moveItemInDatabase(context, item, container, screen, cellX, cellY);
1559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
1569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
15731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
1609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
1629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int cellX, int cellY) {
163aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
1659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
1669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
1679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
16831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
169ade2f8122f6b1a9b64d2d4a81348db619db4d5b2Brad Fitzpatrick        final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
1709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
1719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
172bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
1739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
174d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, cellX);
175d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, cellY);
1769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.SCREEN, item.screen);
177ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
178700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(new Runnable() {
179700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                public void run() {
180700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                    cr.update(uri, values, null, null);
181700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                }
182700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick            });
18331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
18431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
18531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
1869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
1879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
188bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
1899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
1909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
1929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
1939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
1949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
1959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
1969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
1979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
1989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
1999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
2009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
201bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
202bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
203bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
204aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
205aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
206aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
207aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
208aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
209aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
210aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
211aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
212aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
213aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
214aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
215aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
216aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
217aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
218aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
219aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
220aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
221aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
222aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
223aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
224aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
225aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                ItemInfo item = new ItemInfo();
226aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
227aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
228aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanX = c.getInt(spanXIndex);
229aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanY = c.getInt(spanYIndex);
230aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
231aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
232aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.screen = c.getInt(screenIndex);
233aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
234aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
235aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
236aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
237aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
238aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
239aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
240aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
241aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
242aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
243aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
244aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
245aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
2469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
24731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
2499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
2509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
2519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
2529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
2539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
2549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
255f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
2579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
2589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
2599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
2609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
2619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
2629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
2639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
264f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
2669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
2679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeUserFolder(folderList, id);
2699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        folderInfo = findOrMakeLiveFolder(folderList, id);
2729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
2739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
27431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
2769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
2779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
2789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.screen = c.getInt(screenIndex);
279d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
280d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
281ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
2839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
2849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
2859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
2862fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
287ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
2889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
2899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
29031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
2939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
2949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
2959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addItemToDatabase(Context context, ItemInfo item, long container,
2969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int screen, int cellX, int cellY, boolean notify) {
2979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
2989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.screen = screen;
2999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
3009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
301ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
3029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
3039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
3049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
305ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
306d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, cellX, cellY);
307aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
3099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
310ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
3119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (result != null) {
3129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            item.id = Integer.parseInt(result.getPathSegments().get(1));
31331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
31431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
31531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
317aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
318aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
319845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka    static int getCellLayoutChildId(
320845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka            int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
321845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka        return ((cellId & 0xFF) << 24)
322845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka                | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
323aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
324aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
325d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountX() {
326d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountX;
327aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
328aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
329d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountY() {
330d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountY;
331aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
332aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
333aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
334aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Updates the model orientation helper to take into account the current layout dimensions
335aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * when performing local/canonical coordinate transformations.
336aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
337aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
338d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountX = shortAxisCellCount;
339d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountY = longAxisCellCount;
340aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
341aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
342aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
3439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Update an item to the database in a specified container.
3449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void updateItemInDatabase(Context context, ItemInfo item) {
3469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
3479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
348f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
350d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
351aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
35331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
35431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Removes the specified item from the database
3579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param context
3589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * @param item
3599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteItemFromDatabase(Context context, ItemInfo item) {
3619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
36273013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick        final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
363700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(new Runnable() {
364700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                public void run() {
365700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                    cr.delete(uriToDelete, null, null);
366700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                }
367700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick            });
3689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
369f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
3729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
3749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
375f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
3779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        cr.delete(LauncherSettings.Favorites.CONTENT_URI,
3789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
3799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
380f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
3829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
3839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
3859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
3869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
387f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
388f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
389f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
3901d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
3919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
3929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
3939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
394f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
3953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
396aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
3989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
3993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
4003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
4013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
4023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
4033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
404f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
40664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
4073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
4083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
4093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
4103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
41156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
4133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
4143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
4153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
41756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
4193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
4203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
4213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
4223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
4233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
4243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
42556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
4263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
42756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
4283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
4293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
430f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
431f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
433cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // First, schedule to add these apps back in.
434cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
435cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
436cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // Then, rebind everything.
437ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            boolean runLoader = true;
438ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            if (mCallbacks != null) {
439ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato                Callbacks callbacks = mCallbacks.get();
440ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato                if (callbacks != null) {
441ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato                    // If they're paused, we can skip loading, because they'll do it again anyway
442ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato                    if (callbacks.setLoadOnResume()) {
443ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato                        runLoader = false;
444ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato                    }
445ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato                }
446ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            }
447ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            if (runLoader) {
448ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato                startLoader(mApp, false);
449ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            }
450f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
4523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
4533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(
4543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        PackageUpdatedTask.OP_UNAVAILABLE, packages));
455f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
457f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void startLoader(Context context, boolean isLaunching) {
4593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
4613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
4623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
463843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
4643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
4653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
4663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
4673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LoaderTask oldTask = mLoaderTask;
4683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (oldTask != null) {
4693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (oldTask.isLaunching()) {
4703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // don't downgrade isLaunching if we're already running
4713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        isLaunching = true;
4729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
4733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    oldTask.stopLocked();
4749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
4753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask = new LoaderTask(context, isLaunching);
476700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick                sWorker.post(mLoaderTask);
477b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
478b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
4793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
480b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
4813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
4823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
4833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
4843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
4859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
486f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
4873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
488b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
4893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
4903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
4913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
4923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
4933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
4943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
4953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
4963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
4973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Thread mWaitThread;
4983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
4993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
5003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
5013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        LoaderTask(Context context, boolean isLaunching) {
5033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
5043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
5053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
506f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
5083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
5093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
510f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindWorkspace() {
5123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
513cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
5143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // For now, just always reload the workspace.  It's ~100 ms vs. the
5153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // binding which takes many hundreds of ms.
5163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // We can reconsider.
5173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
5183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
5193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
5203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (true || !mWorkspaceLoaded) {
5213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadWorkspace();
5223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
5233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
5249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
5253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mWorkspaceLoaded = true;
5263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
527cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
5283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
5293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            bindWorkspace();
5303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
531f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
5333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
5343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
5353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
5363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
5373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
538843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
5403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
5413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
5423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
5433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
5443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
5459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
5463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
5479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
5489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
5493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
5503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && !mLoadAndBindStepFinished) {
5523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
5533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        this.wait();
5543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
5553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
5569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
5593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
560aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
5613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
5629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
563843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
5643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
565f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
5663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
5673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
5683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
5693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
5703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks cbk = mCallbacks.get();
5713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
572843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
574843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
575843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
576dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
577843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
578843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
579dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
580843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
581843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
582843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
583843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
584843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
585cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
5869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
587843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
5883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
5893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
5903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
5913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
5923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
5933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
5949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
595843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
596843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
5979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
5989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
5993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
600f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
601843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
602843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                if (loadWorkspaceFirst) {
603843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
604cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    loadAndBindAllApps();
605843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                } else {
606843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
607843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    loadAndBindWorkspace();
608843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
6093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
610843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
6113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
6123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
6133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
614f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
6153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
6163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
6173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
6183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
619843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
6203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
621843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
6223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Trigger a gc to try to clean up after the stuff is done, since the
6233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // renderscript allocations aren't charged to the java heap.
6243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mStopped) {
625f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                mHandler.post(new Runnable() {
626f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        public void run() {
627f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                            System.gc();
628f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                        }
629f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato                    });
6303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
6313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
6323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
6333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            System.gc();
6343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
6353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
636f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
638f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
6403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
6413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
6423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
643f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
645f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
6473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
6483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
6493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
6503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
6513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
6523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
6533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
6543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
6553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
6563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
658ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
6593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
6603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
662f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
6643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
6653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
6673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
6683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
6693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
6709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
6713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
6723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
673f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
6743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
675f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
6763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
6773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
6783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
6793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
6803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
6813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
6823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
6833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (occupied[item.screen][x][y] != null) {
6843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
685aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + " into cell (" + item.screen + ":"
6863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
687aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
6883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + occupied[item.screen][x][y]);
6893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
6908802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
6918802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
6923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
6933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
6943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
6953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    occupied[item.screen][x][y] = item;
6968802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
6978802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
6983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
6993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
7008802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadWorkspace() {
7023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
703f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
7053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
7063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
7073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
7083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
709f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
7103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mItems.clear();
7113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mAppWidgets.clear();
7123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mFolders.clear();
7133c2f7e105d8daab439bff2c362a727ad348d8a44Joe Onorato
7143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
7155c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
7163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Cursor c = contentResolver.query(
7173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
718f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
719d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen            final ItemInfo occupied[][][] =
720d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                    new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
7218802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
7223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            try {
7233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
7243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int intentIndex = c.getColumnIndexOrThrow
7253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.INTENT);
7263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int titleIndex = c.getColumnIndexOrThrow
7273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.TITLE);
7283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconTypeIndex = c.getColumnIndexOrThrow(
7293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_TYPE);
7303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
7313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconPackageIndex = c.getColumnIndexOrThrow(
7323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_PACKAGE);
7333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int iconResourceIndex = c.getColumnIndexOrThrow(
7343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ICON_RESOURCE);
7353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int containerIndex = c.getColumnIndexOrThrow(
7363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.CONTAINER);
7373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(
7383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.ITEM_TYPE);
7393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int appWidgetIdIndex = c.getColumnIndexOrThrow(
7403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.APPWIDGET_ID);
7413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int screenIndex = c.getColumnIndexOrThrow(
7423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SCREEN);
7433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellXIndex = c.getColumnIndexOrThrow
7443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLX);
7453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int cellYIndex = c.getColumnIndexOrThrow
7463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.CELLY);
7473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanXIndex = c.getColumnIndexOrThrow
7483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        (LauncherSettings.Favorites.SPANX);
7493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int spanYIndex = c.getColumnIndexOrThrow(
7503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.SPANY);
7513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
7523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int displayModeIndex = c.getColumnIndexOrThrow(
7533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        LauncherSettings.Favorites.DISPLAY_MODE);
7543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ShortcutInfo info;
7563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                String intentDescription;
7573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                LauncherAppWidgetInfo appWidgetInfo;
7583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                int container;
7593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                long id;
7603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Intent intent;
7613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                while (!mStopped && c.moveToNext()) {
7633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
7643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        int itemType = c.getInt(itemTypeIndex);
7653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        switch (itemType) {
7673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
7683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
7693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            intentDescription = c.getString(intentIndex);
7703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            try {
7713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = Intent.parseUri(intentDescription, 0);
7723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } catch (URISyntaxException e) {
7733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                continue;
7743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
7773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(manager, intent, context, c, iconIndex,
7783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
7793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
7803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info = getShortcutInfo(c, context, iconTypeIndex,
7813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconPackageIndex, iconResourceIndex, iconIndex,
7823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        titleIndex);
7833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
7843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (info != null) {
7863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                updateSavedIcon(context, info, c, iconIndex);
7873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.intent = intent;
7893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.id = c.getLong(idIndex);
7903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
7913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.container = container;
7923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                info.screen = c.getInt(screenIndex);
793d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellX = c.getInt(cellXIndex);
794d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                info.cellY = c.getInt(cellYIndex);
7953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
7963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
7973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, info)) {
7983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
7999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
800f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
8023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(info);
8043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                default:
8063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    // Item is in a user folder
8073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    UserFolderInfo folderInfo =
8083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            findOrMakeUserFolder(mFolders, container);
8093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    folderInfo.add(info);
8103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Failed to load the shortcut, probably because the
8143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // activity manager couldn't resolve it (maybe the app
8153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // was uninstalled), or the db row was somehow screwed up.
8163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // Delete it.
8173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
8183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Error loading shortcut " + id + ", removing it");
8193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                contentResolver.delete(LauncherSettings.Favorites.getContentUri(
8203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            id, false), null, null);
8213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
823f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
8253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
8279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
828aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            folderInfo.title = c.getString(titleIndex);
8293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.id = id;
8303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            container = c.getInt(containerIndex);
8313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.container = container;
8323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            folderInfo.screen = c.getInt(screenIndex);
833d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellX = c.getInt(cellXIndex);
834d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                            folderInfo.cellY = c.getInt(cellYIndex);
8358802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // check & update map of what's occupied
8373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!checkItemPlacement(occupied, folderInfo)) {
8389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
8393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            switch (container) {
8413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    mItems.add(folderInfo);
8433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
8443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            mFolders.put(folderInfo.id, folderInfo);
8473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
8503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
8513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Uri uri = Uri.parse(c.getString(uriIndex));
852f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Make sure the live folder exists
8543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final ProviderInfo providerInfo =
8553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    context.getPackageManager().resolveContentProvider(
8563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            uri.getAuthority(), 0);
8573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (providerInfo == null && !isSafeMode) {
8593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
8603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
8613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
8623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intentDescription = c.getString(intentIndex);
8633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                intent = null;
8643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (intentDescription != null) {
8653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    try {
8663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        intent = Intent.parseUri(intentDescription, 0);
8673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    } catch (URISyntaxException e) {
8683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        // Ignore, a live folder might not have a base intent
8693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    }
8703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                }
871f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.title = c.getString(titleIndex);
8733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.id = id;
8743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.uri = uri;
8759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                container = c.getInt(containerIndex);
8763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.container = container;
8773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.screen = c.getInt(screenIndex);
878d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellX = c.getInt(cellXIndex);
879d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                liveFolderInfo.cellY = c.getInt(cellYIndex);
8803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.baseIntent = intent;
8813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                liveFolderInfo.displayMode = c.getInt(displayModeIndex);
882f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8838802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                // check & update map of what's occupied
8843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, liveFolderInfo)) {
8858802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                    break;
8868802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                                }
8878802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
8883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
8893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        iconResourceIndex, liveFolderInfo);
8903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
8919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                switch (container) {
8929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
8933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        mItems.add(liveFolderInfo);
8949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                        break;
8959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
8963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mFolders.put(liveFolderInfo.id, liveFolderInfo);
8973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
8983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
8993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
9013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            // Read all Launcher-specific widget details
9023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            int appWidgetId = c.getInt(appWidgetIdIndex);
9033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            id = c.getLong(idIndex);
9043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            final AppWidgetProviderInfo provider =
9063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    widgets.getAppWidgetInfo(appWidgetId);
907aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
9083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (!isSafeMode && (provider == null || provider.provider == null ||
9093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    provider.provider.getPackageName() == null)) {
9103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.e(TAG, "Deleting widget that isn't installed anymore: id="
9113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                        + id + " appWidgetId=" + appWidgetId);
9123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                itemsToRemove.add(id);
9133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
9143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
9153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.id = id;
9163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.screen = c.getInt(screenIndex);
917d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellX = c.getInt(cellXIndex);
918d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.cellY = c.getInt(cellYIndex);
919d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanX = c.getInt(spanXIndex);
920d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                                appWidgetInfo.spanY = c.getInt(spanYIndex);
921ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
9223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
9233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
9243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.e(TAG, "Widget found where container "
9253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                            + "!= CONTAINER_DESKTOP -- ignoring!");
9263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    continue;
9279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                appWidgetInfo.container = c.getInt(containerIndex);
9298802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
9303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
9313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (!checkItemPlacement(occupied, appWidgetInfo)) {
9323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
9339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
9343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mAppWidgets.add(appWidgetInfo);
9369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
9373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            break;
9389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
9393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (Exception e) {
9403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Desktop items loading interrupted:", e);
9419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
9429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
9433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } finally {
9443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                c.close();
9453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9465c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (itemsToRemove.size() > 0) {
9483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                ContentProviderClient client = contentResolver.acquireContentProviderClient(
9493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LauncherSettings.Favorites.CONTENT_URI);
9503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Remove dead items
9513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (long id : itemsToRemove) {
9523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
9533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Removed id = " + id);
9543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
9553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // Don't notify content observers
9563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
9573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        client.delete(LauncherSettings.Favorites.getContentUri(id, false),
9583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                null, null);
9593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (RemoteException e) {
9603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.w(TAG, "Could not remove id = " + id);
9615c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
9625c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
9633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
9645c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
9653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
9663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
9673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "workspace layout: ");
968d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                for (int y = 0; y < mCellCountY; y++) {
9693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    String line = "";
9703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
9713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (s > 0) {
9723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += " | ";
9733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
974d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                        for (int x = 0; x < mCellCountX; x++) {
9753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            line += ((occupied[s][x][y] != null) ? "#" : ".");
9768802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                        }
9778802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
9783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "[ " + line + " ]");
979a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
9809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
9813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
98231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
9843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Read everything out of our database.
9853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
9863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void bindWorkspace() {
9873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = SystemClock.uptimeMillis();
9883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
9893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
9903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
9913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
9923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
9933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
9943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher");
9953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
9963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
99731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N;
9993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're about to start firing items at it
10003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.startBinding();
10053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Add the items to the workspace.
10093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mItems.size();
10103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i+=ITEMS_CHUNK) {
10113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
10123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
10139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                mHandler.post(new Runnable() {
10149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
1015c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
10173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindItems(mItems, start, start+chunkSize);
10189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
10199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                });
10213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindFolders(mFolders);
10273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the queue goes empty.
10313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "Going to start binding widgets soon.");
10353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
10363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the widgets, one at a time.
10393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // WARNING: this is calling into the workspace from the background thread,
10403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // but since getCurrentScreen() just returns the int, we should be okay.  This
10413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // is just a hint for the order, and if it's wrong, we'll be okay.
10423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // TODO: instead, we should have that push the current screen into here.
10433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
10443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            N = mAppWidgets.size();
10453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the current screen
10463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen == currentScreen) {
10499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    mHandler.post(new Runnable() {
10509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        public void run() {
1051c131b74922e6323331c374856bda830010d761c8Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            if (callbacks != null) {
10533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
10549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
10569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    });
10579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // once for the other screens
10603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int i=0; i<N; i++) {
10613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
10623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (widget.screen != currentScreen) {
10633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mHandler.post(new Runnable() {
10643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
10653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (callbacks != null) {
10673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppWidget(widget);
10689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
10693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
10703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
10713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
10723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Tell the workspace that we're done.
10743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
10773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
10783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.finishBindingItems();
10799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // If we're profiling, this is the last thing in the queue.
10833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
10843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
10853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
10863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
10873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
10889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
10899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
10903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
10913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
10923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
10933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
10943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
10953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
10963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
10973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
10983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                loadAllAppsByBatch();
10993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
11003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return;
11013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsLoaded = true;
11033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
11043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
11053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
11093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
11103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
11113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
11123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
11133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
11153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
11173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<ApplicationInfo> list
11183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
11193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mHandler.post(new Runnable() {
11203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
11213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
11223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
11233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
11243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
11259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
11263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1128a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
11299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
11303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
11313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            });
11323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
11343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAllAppsByBatch() {
11363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
11393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
11403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
11413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
11423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
11433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
11443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
114631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
11483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1149cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager packageManager = mContext.getPackageManager();
11513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            List<ResolveInfo> apps = null;
11523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int N = Integer.MAX_VALUE;
11543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int startIndex;
11563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int i=0;
11573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int batchSize = -1;
11583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            while (i < N && !mStopped) {
11593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (i == 0) {
11603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mAllAppsList.clear();
11613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    apps = packageManager.queryIntentActivities(mainIntent, 0);
11633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities took "
11653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-qiaTime) + "ms");
11663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (apps == null) {
1168cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        return;
1169cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
11703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    N = apps.size();
11713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "queryIntentActivities got " + N + " apps");
11733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (N == 0) {
11753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // There are no apps?!?
11763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return;
11773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (mBatchSize == 0) {
11793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = N;
11803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } else {
11813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        batchSize = mBatchSize;
11823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
11833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Collections.sort(apps,
11863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            new ResolveInfo.DisplayNameComparator(packageManager));
11873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
11883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "sort took "
11893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                + (SystemClock.uptimeMillis()-sortTime) + "ms");
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
1191cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1192cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
11933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
11943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
11953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                startIndex = i;
11963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int j=0; i<N && j<batchSize; j++) {
11973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    // This builds the icon bitmaps.
11983d605d5bbef35e3b8aded44c5ef7fe3948f8f7d5Patrick Dubroy                    mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
11993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    i++;
1200cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                }
1201cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean first = i <= batchSize;
12033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
12043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> added = mAllAppsList.added;
12053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
12063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1207cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                mHandler.post(new Runnable() {
1208cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    public void run() {
1209cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        final long t = SystemClock.uptimeMillis();
1210cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        if (callbacks != null) {
12113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (first) {
12123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAllApplications(added);
12133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            } else {
12143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                callbacks.bindAppsAdded(added);
12153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
12163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            if (DEBUG_LOADERS) {
12173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                Log.d(TAG, "bound " + added.size() + " apps in "
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    + (SystemClock.uptimeMillis() - t) + "ms");
12193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
12203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        } else {
12213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.i(TAG, "not binding apps: no Launcher activity");
1222cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                        }
1223cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                    }
1224cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato                });
1225cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
12273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
12283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t2) + "ms");
12293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
12303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
12313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mAllAppsLoadDelay > 0 && i < N) {
12323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
12333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
12343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
12353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
12363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Thread.sleep(mAllAppsLoadDelay);
12373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException exc) { }
12383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1239cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato            }
1240cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
12413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
12423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "cached all " + N + " apps in "
12433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (SystemClock.uptimeMillis()-t) + "ms"
12443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
12453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
12463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1247dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
12493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mContext=" + mContext);
12503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
12513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
12523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
12533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
12543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
12553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
125631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
1258700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(task);
12593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1260d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
12623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
12633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
1264d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
12663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
12673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
12683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
12693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
127031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1271d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
12733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
12743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
12753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1276d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
12773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
12783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mApp;
1279dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
12803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
12813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
12823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
12833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
12843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
12863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.addPackage(context, packages[i]);
1287dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
12883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
12903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
12923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.updatePackage(context, packages[i]);
12933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
12943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
12953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
12963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
12973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
12983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
12993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        mAllAppsList.removePackage(packages[i]);
13003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
13023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1303dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
13043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> added = null;
13053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> removed = null;
13063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> modified = null;
13073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
13083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.added.size() > 0) {
13093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                added = mAllAppsList.added;
13103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.added = new ArrayList<ApplicationInfo>();
13113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.removed.size() > 0) {
13133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                removed = mAllAppsList.removed;
13143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.removed = new ArrayList<ApplicationInfo>();
13153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (ApplicationInfo info: removed) {
13163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mIconCache.remove(info.intent.getComponent());
131731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
131831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
13193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mAllAppsList.modified.size() > 0) {
13203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                modified = mAllAppsList.modified;
13213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mAllAppsList.modified = new ArrayList<ApplicationInfo>();
13223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
132331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
13253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
13263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
13273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
1328be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
1329be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
13303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
13313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> addedFinal = added;
13323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsAdded(addedFinal);
13363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
13413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> modifiedFinal = modified;
13423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
13463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
13493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
13503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (removed != null) {
13513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final boolean permanent = mOp != OP_UNAVAILABLE;
13523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> removedFinal = removed;
13533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
13543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
13553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (callbacks == mCallbacks.get()) {
13563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsRemoved(removedFinal, permanent);
13573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
13583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
13593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
1360be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
136180baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
136280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
136380baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
136480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
136580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    if (callbacks == mCallbacks.get()) {
136680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                        callbacks.bindPackagesUpdated();
136780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
136880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
136980baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
137031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
137131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
137231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
137456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
137556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
13769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
137756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
1378e74daedc3b41d958315f7d5bf69deb232ca89468Joe Onorato        return getShortcutInfo(manager, intent, context, null, -1, -1);
137956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
138056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
138156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
138256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
138356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
138456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
138556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
138656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
138756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Cursor c, int iconIndex, int titleIndex) {
138856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
138956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
139031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
139156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
139256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (componentName == null) {
13939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
139431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
139531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13968ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
13978ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
13988ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
139956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
140056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
140156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
140256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
140356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
140456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
140556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = mIconCache.getIcon(componentName, resolveInfo);
140656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
140756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
140856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
140956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
141056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
141156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
14129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
141356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
141456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
141556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
141656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
141756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
141856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
141956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
142056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
142156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
142256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = resolveInfo.activityInfo.loadLabel(manager);
142356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
142456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
142556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
142656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
142756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
142856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
142956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
143056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
14319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
143256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
14339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
14349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
14359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
14369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
143731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
14390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
14409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
14410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
144256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
144356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
144431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
144556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
14460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = new ShortcutInfo();
14479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
14487376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
14498ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
14508ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
145156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
145256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
14539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
14549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
14559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
14569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
14579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
14589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
145956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
146056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
14619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
14629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
146356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
146456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
146556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
146656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
14679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
146856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
146956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
147056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
147156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
147256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getIconFromCursor(c, iconIndex);
147356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
147456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
147556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
147656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
147756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
14787376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
14799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
148156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getIconFromCursor(c, iconIndex);
148256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
148356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
148456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
148556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
148656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
148756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
14889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
14899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
14909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
1491d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
149256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
14939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
14949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
149531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1496d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
14979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
1498f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
149931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
150056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    Bitmap getIconFromCursor(Cursor c, int iconIndex) {
150156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (false) {
150256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
150356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
150456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
150556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
150656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
150756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return BitmapFactory.decodeByteArray(data, 0, data.length);
150856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
150956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
151056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
151156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
151256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    ShortcutInfo addShortcut(Context context, Intent data,
15140280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            int screen, int cellX, int cellY, boolean notify) {
15150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        final ShortcutInfo info = infoFromShortcutIntent(context, data);
15170589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
15180280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                screen, cellX, cellY, notify);
15190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
15210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
15220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
15240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
15250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
15260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
15270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
15290589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean filtered = false;
15300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
15310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
15320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
15340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
15350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            filtered = true;
15360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
15370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
15380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
15390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
15400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
15410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
15420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
15430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
15440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
15450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
15460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
15470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
15480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
15490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
15500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
15510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
15520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
155356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        final ShortcutInfo info = new ShortcutInfo();
155456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
155656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
155756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
15580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
15590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
156056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
15610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
15620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
15630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
15640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
15650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
15670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
15680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
15699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
15709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
157131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
15729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
15739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
15749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
15759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
15769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
15779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
15789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
15799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
15809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int id = resources.getIdentifier(resourceName, null, null);
15810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
15820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
15839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
15840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                liveFolderInfo.icon = Utilities.createIconBitmap(
15850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context.getResources().getDrawable(R.drawable.ic_launcher_folder),
15860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        context);
15879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
15889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
15899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.packageName = packageName;
15909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            liveFolderInfo.iconResource.resourceName = resourceName;
15919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
15929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
15930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            liveFolderInfo.icon = Utilities.createIconBitmap(
15940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context.getResources().getDrawable(R.drawable.ic_launcher_folder),
15950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    context);
15969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
159731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1598bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
159956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
160056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
160156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
160256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
160356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
160456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
160556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
1606ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
160756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            boolean needSave;
160856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            byte[] data = c.getBlob(iconIndex);
160956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            try {
161056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (data != null) {
161156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
161256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap loaded = info.getIcon(mIconCache);
161356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = !saved.sameAs(loaded);
161456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                } else {
161556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    needSave = true;
161656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
161756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } catch (Exception e) {
161856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
161956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
162056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (needSave) {
162156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                Log.d(TAG, "going to save icon bitmap for info=" + info);
162256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // This is slower than is ideal, but this only happens either
162356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // after the froyo OTA or when the app is updated with a new
162456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // icon.
162556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                updateItemInDatabase(context, info);
162656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
162756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
162856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
162956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
163031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
16319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously,
16329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
163331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
16349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
16359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
16369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
16379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
16389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
16399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new UserFolderInfo();
16409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
164131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (UserFolderInfo) folderInfo;
16439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
164431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
16469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
16479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * new one.
16489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
16499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
16509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
16519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
16529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
16539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
16549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folderInfo = new LiveFolderInfo();
16559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
165631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return (LiveFolderInfo) folderInfo;
165831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
165931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
166031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
166131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        String label = activityInfo.loadLabel(manager).toString();
166231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (label == null) {
166331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
166431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (label == null) {
166531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                label = activityInfo.name;
166631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
166731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
166831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return label;
166931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
167031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private static final Collator sCollator = Collator.getInstance();
1672b0c27f254a9929be208d5e04554f438076c833bcJoe Onorato    public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
16739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            = new Comparator<ApplicationInfo>() {
16749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public final int compare(ApplicationInfo a, ApplicationInfo b) {
16759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return sCollator.compare(a.title.toString(), b.title.toString());
167631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    };
1678be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
1679be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
1680be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
1681be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1682be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1683be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1684be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
16853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Log.d(TAG, "mItems size=" + mItems.size());
16863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
16873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
16883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
16893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
16903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1691be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
169231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
1693