LauncherModel.java revision 11a1a53651924b544513f1f6971a735b18d67539
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
17325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerpackage com.android.launcher3;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamathimport android.app.SearchManager;
20629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetManager;
21629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetProviderInfo;
22c916834ad0da535f08325bcd902a60070f2f7e53Winson Chungimport android.content.*;
230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.content.Intent.ShortcutIconResource;
2431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ActivityInfo;
2500fcb49a4cf68b973ac488520f3ed20444c02f8dAdam Cohenimport android.content.pm.PackageInfo;
2631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.PackageManager;
2700fcb49a4cf68b973ac488520f3ed20444c02f8dAdam Cohenimport android.content.pm.PackageManager.NameNotFoundException;
2831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.ResolveInfo;
2993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Leeimport android.content.res.Configuration;
3031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.res.Resources;
3131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.database.Cursor;
3231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Bitmap;
3331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.BitmapFactory;
3431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.net.Uri;
3517a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onoratoimport android.os.Environment;
363611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.Handler;
373611578579b4bfb25616085dafdb1a45207394f9Joe Onoratoimport android.os.HandlerThread;
380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onoratoimport android.os.Parcelable;
3931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.os.Process;
40aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.os.RemoteException;
419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onoratoimport android.os.SystemClock;
42aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.util.Log;
43c916834ad0da535f08325bcd902a60070f2f7e53Winson Chungimport android.util.Pair;
44325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerimport com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
45edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
46c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.lang.ref.WeakReference;
47c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.net.URISyntaxException;
48c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.text.Collator;
49c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.ArrayList;
50dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohenimport java.util.Arrays;
5164359a53dc827797917a8d9a0697a91996789801Winson Chungimport java.util.Collection;
52c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.Collections;
53c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.Comparator;
54c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.HashMap;
55b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chungimport java.util.HashSet;
562abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chungimport java.util.Iterator;
57c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.List;
58b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chungimport java.util.Set;
59dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohenimport java.util.TreeMap;
60c2f801e770b4f80354b3edb722ec04222251990cMichael Jurka
6131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
6231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Maintains in-memory state of the Launcher. It is expected that there should be only one
6331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * LauncherModel object held in a static. Also provide APIs for updating the database state
64bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project * for the Launcher.
6531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
66f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratopublic class LauncherModel extends BroadcastReceiver {
67a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final boolean DEBUG_LOADERS = false;
689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final String TAG = "Launcher.Model";
699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
708707e0fa1ca2c79be862c29cb919b7372f0b6a50Daniel Sandler    // true = use a "More Apps" folder for non-workspace apps on upgrade
718707e0fa1ca2c79be862c29cb919b7372f0b6a50Daniel Sandler    // false = strew non-workspace apps across the workspace on upgrade
728707e0fa1ca2c79be862c29cb919b7372f0b6a50Daniel Sandler    public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
738707e0fa1ca2c79be862c29cb919b7372f0b6a50Daniel Sandler
743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
75ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung    private final boolean mAppsCanBeOnRemoveableStorage;
76dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
77cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler    private final LauncherAppState mApp;
789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
81b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private boolean mIsLoaderTaskRunning;
82c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    private volatile boolean mFlushingWorkerThread;
839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
8481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    // Specific runnable types that are run on the main thread deferred handler, this allows us to
8581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    // clear all queued binding runnables when the Launcher activity is destroyed.
8681b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
8781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
8881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
8981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
90700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
91700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    static {
92700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorkerThread.start();
93700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    }
94700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
95700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick
96cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
97cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
98cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
99cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
100cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
101cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
102b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // When we are loading pages synchronously, we can't just post the binding of items on the side
103b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // pages as this delays the rotation process.  Instead, we wait for a callback from the first
104b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // draw (in Workspace) to initiate the binding of the remaining side pages.  Any time we start
105b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // a normal load, we also clear this set of Runnables.
106b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
107b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
1099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
110a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // < only access in worker thread >
1114caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen    AllAppsList mBgAllAppsList;
112a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
113b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // The lock that must be acquired before referencing any static bg data structures.  Unlike
114b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // other locks, this one can generally be held long-term because we never expect any of these
115b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // static data structures to be referenced outside of the worker thread except on the first
116b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // load after configuration change.
1172abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung    static final Object sBgLock = new Object();
118b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
119487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
120a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // LauncherModel to their ids
121487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
122a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
123487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
124487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    //       created by LauncherModel that are directly on the home screen (however, no widgets or
125487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    //       shortcuts within folders).
126487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
1270589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
128487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
129487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
130a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        new ArrayList<LauncherAppWidgetInfo>();
131a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
132487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
133487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
134b1094bd9630f659cc606853b8a244b6a6b6c6943Winson Chung
135487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
136487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
137dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
138dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    // sBgWorkspaceScreens is the ordered set of workspace screens.
139dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
140dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
141a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // </ only access in worker thread >
142a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
143a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    private IconCache mIconCache;
1440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
1459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
14699a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee    protected int mPreviousConfigMcc;
14793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
149ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        public boolean setLoadOnResume();
1509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
1519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
15264359a53dc827797917a8d9a0697a91996789801Winson Chung        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
15364359a53dc827797917a8d9a0697a91996789801Winson Chung                              boolean forceAnimateIcons);
154dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        public void bindScreens(ArrayList<Long> orderedScreenIds);
15564359a53dc827797917a8d9a0697a91996789801Winson Chung        public void bindAddScreens(ArrayList<Long> orderedScreenIds);
156ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
157e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        public void finishBindingItems(boolean upgradePath);
1589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
15985f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert        public boolean shouldShowApp(ResolveInfo app);
160eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        public void bindAllApplications(ArrayList<AppInfo> apps);
161d64d1763c590b1c954b759156683abf39ff22945Winson Chung        public void bindAppsAdded(ArrayList<Long> newScreens,
162d64d1763c590b1c954b759156683abf39ff22945Winson Chung                                  ArrayList<ItemInfo> addNotAnimated,
163c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                                  ArrayList<ItemInfo> addAnimated,
164c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                                  ArrayList<AppInfo> addedApps);
165eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        public void bindAppsUpdated(ArrayList<AppInfo> apps);
16683892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        public void bindComponentsRemoved(ArrayList<String> packageNames,
167eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                        ArrayList<AppInfo> appInfos,
16883892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                        boolean matchPackageNamesOnly);
169c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
170cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath        public void bindSearchablesChanged();
1711462de39f01cec0ba785386532719cb0207dd827Adam Cohen        public void onPageBoundSynchronously(int page);
1724caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen        public void dumpLogsToLocalData(boolean email);
1739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
17431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17564359a53dc827797917a8d9a0697a91996789801Winson Chung    public interface ItemInfoFilter {
17664359a53dc827797917a8d9a0697a91996789801Winson Chung        public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
17764359a53dc827797917a8d9a0697a91996789801Winson Chung    }
17864359a53dc827797917a8d9a0697a91996789801Winson Chung
179e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler    LauncherModel(LauncherAppState app, IconCache iconCache) {
180e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler        final Context context = app.getContext();
181e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler
182ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
183e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler        mApp = app;
184487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        mBgAllAppsList = new AllAppsList(iconCache);
1850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
1860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mDefaultIcon = Utilities.createIconBitmap(
188cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler                mIconCache.getFullResDefaultActivityIcon(), context);
1892ff10b3b0137d258569ce5471808842e88696133Daniel Sandler
190cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler        final Resources res = context.getResources();
19199a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee        Configuration config = res.getConfiguration();
19299a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee        mPreviousConfigMcc = config.mcc;
1930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
195b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Runs the specified runnable immediately if called from the main thread, otherwise it is
196b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung     * posted on the main thread handler. */
197b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private void runOnMainThread(Runnable r) {
19881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        runOnMainThread(r, 0);
19981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    }
20081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private void runOnMainThread(Runnable r, int type) {
201b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
202b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we are on the worker thread, post onto the main handler
203b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mHandler.post(r);
204b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        } else {
205b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r.run();
206b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
207b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
208b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
209b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
210b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung     * posted on the worker thread handler. */
211b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private static void runOnWorkerThread(Runnable r) {
212b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
213b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r.run();
214b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        } else {
215b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we are not on the worker thread, then post to the worker handler
216b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sWorker.post(r);
217b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
218b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
219b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
220c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
221c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                                 long screen) {
222892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        LauncherAppState app = LauncherAppState.getInstance();
223892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
224892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        final int xCount = (int) grid.numColumns;
225892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        final int yCount = (int) grid.numRows;
226c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        boolean[][] occupied = new boolean[xCount][yCount];
227c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
228c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        int cellX, cellY, spanX, spanY;
229c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        for (int i = 0; i < items.size(); ++i) {
230c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            final ItemInfo item = items.get(i);
231c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
232c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                if (item.screenId == screen) {
233c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    cellX = item.cellX;
234c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    cellY = item.cellY;
235c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    spanX = item.spanX;
236c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    spanY = item.spanY;
237c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
238c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
239c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                            occupied[x][y] = true;
240c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        }
241c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    }
242c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                }
243c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            }
244c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        }
245c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
246c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
247c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    }
248c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
249156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung                                                        Intent launchIntent,
25076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                                        int firstScreenIndex,
25176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                                        ArrayList<Long> workspaceScreens) {
252c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        // Lock on the app so that we don't try and get the items while apps are being added
253c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        LauncherAppState app = LauncherAppState.getInstance();
254c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        LauncherModel model = app.getModel();
255c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        boolean found = false;
256c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        synchronized (app) {
25764359a53dc827797917a8d9a0697a91996789801Winson Chung            if (sWorkerThread.getThreadId() != Process.myTid()) {
25864359a53dc827797917a8d9a0697a91996789801Winson Chung                // Flush the LauncherModel worker thread, so that if we just did another
25964359a53dc827797917a8d9a0697a91996789801Winson Chung                // processInstallShortcut, we give it time for its shortcut to get added to the
26064359a53dc827797917a8d9a0697a91996789801Winson Chung                // database (getItemsInLocalCoordinates reads the database)
26164359a53dc827797917a8d9a0697a91996789801Winson Chung                model.flushWorkerThread();
26264359a53dc827797917a8d9a0697a91996789801Winson Chung            }
263c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
264c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
265c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            // Try adding to the workspace screens incrementally, starting at the default or center
266c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
26776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
26876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            int count = workspaceScreens.size();
269156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung            for (int screen = firstScreenIndex; screen < count && !found; screen++) {
270c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                int[] tmpCoordinates = new int[2];
271c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
27276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                        workspaceScreens.get(screen))) {
273c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    // Update the Launcher db
27476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
275c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                }
276c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            }
277c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        }
278c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        return null;
279c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    }
280c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
281c58497ee619416c4949b16eb43486c11618b69e5Winson Chung    public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
282c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                                    final ArrayList<AppInfo> addedApps) {
283997a92348a6d6e061737399321393449c16cd4d8Winson Chung        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
284c58497ee619416c4949b16eb43486c11618b69e5Winson Chung        addAndBindAddedApps(context, added, cb, addedApps);
285997a92348a6d6e061737399321393449c16cd4d8Winson Chung    }
286997a92348a6d6e061737399321393449c16cd4d8Winson Chung    public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
287c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                                    final Callbacks callbacks, final ArrayList<AppInfo> addedApps) {
2884caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen        Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps()", true);
289997a92348a6d6e061737399321393449c16cd4d8Winson Chung        if (added.isEmpty()) {
2909e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung            return;
291997a92348a6d6e061737399321393449c16cd4d8Winson Chung        }
29264359a53dc827797917a8d9a0697a91996789801Winson Chung        // Process the newly added applications and add them to the database first
29364359a53dc827797917a8d9a0697a91996789801Winson Chung        Runnable r = new Runnable() {
29464359a53dc827797917a8d9a0697a91996789801Winson Chung            public void run() {
29564359a53dc827797917a8d9a0697a91996789801Winson Chung                final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
29664359a53dc827797917a8d9a0697a91996789801Winson Chung                final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
29764359a53dc827797917a8d9a0697a91996789801Winson Chung
29876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // Get the list of workspace screens.  We need to append to this list and
29976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
30076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // called.
30176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                ArrayList<Long> workspaceScreens = new ArrayList<Long>();
30276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
30376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                for (Integer i : orderedScreens.keySet()) {
30476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = orderedScreens.get(i);
30576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    workspaceScreens.add(screenId);
30676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                }
30776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
30864359a53dc827797917a8d9a0697a91996789801Winson Chung                synchronized(sBgLock) {
309997a92348a6d6e061737399321393449c16cd4d8Winson Chung                    Iterator<ItemInfo> iter = added.iterator();
31064359a53dc827797917a8d9a0697a91996789801Winson Chung                    while (iter.hasNext()) {
311997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        ItemInfo a = iter.next();
31264359a53dc827797917a8d9a0697a91996789801Winson Chung                        final String name = a.title.toString();
313997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        final Intent launchIntent = a.getIntent();
31464359a53dc827797917a8d9a0697a91996789801Winson Chung
31564359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Short-circuit this logic if the icon exists somewhere on the workspace
31664359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (LauncherModel.shortcutExists(context, name, launchIntent)) {
31764359a53dc827797917a8d9a0697a91996789801Winson Chung                            continue;
31864359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
31964359a53dc827797917a8d9a0697a91996789801Winson Chung
32064359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Add this icon to the db, creating a new page if necessary
321156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung                        int startSearchPageIndex = 1;
32264359a53dc827797917a8d9a0697a91996789801Winson Chung                        Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
32376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                name, launchIntent, startSearchPageIndex, workspaceScreens);
32464359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (coords == null) {
325414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka                            LauncherProvider lp = LauncherAppState.getLauncherProvider();
326c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
327c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // If we can't find a valid position, then just add a new screen.
328c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // This takes time so we need to re-queue the add until the new
329c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // page is added.  Create as many screens as necessary to satisfy
330c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // the startSearchPageIndex.
331c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
33276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                    workspaceScreens.size());
333c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            while (numPagesToAdd > 0) {
334c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                long screenId = lp.generateNewScreenId();
3354caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                                Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")", true);
336c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                // Save the screen id for binding in the workspace
33776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                workspaceScreens.add(screenId);
338c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                addedWorkspaceScreensFinal.add(screenId);
339c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                numPagesToAdd--;
340c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            }
34176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
34264359a53dc827797917a8d9a0697a91996789801Winson Chung                            // Find the coordinate again
34364359a53dc827797917a8d9a0697a91996789801Winson Chung                            coords = LauncherModel.findNextAvailableIconSpace(context,
34476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                    name, launchIntent, startSearchPageIndex, workspaceScreens);
34564359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
34664359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (coords == null) {
34764359a53dc827797917a8d9a0697a91996789801Winson Chung                            throw new RuntimeException("Coordinates should not be null");
34864359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
34964359a53dc827797917a8d9a0697a91996789801Winson Chung
350997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        ShortcutInfo shortcutInfo;
351997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        if (a instanceof ShortcutInfo) {
352997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            shortcutInfo = (ShortcutInfo) a;
353eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                        } else if (a instanceof AppInfo) {
354eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                            shortcutInfo = ((AppInfo) a).makeShortcut();
355997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        } else {
356997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            throw new RuntimeException("Unexpected info type");
357997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        }
35864359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Add the shortcut to the db
35964359a53dc827797917a8d9a0697a91996789801Winson Chung                        addItemToDatabase(context, shortcutInfo,
36064359a53dc827797917a8d9a0697a91996789801Winson Chung                                LauncherSettings.Favorites.CONTAINER_DESKTOP,
36164359a53dc827797917a8d9a0697a91996789801Winson Chung                                coords.first, coords.second[0], coords.second[1], false);
36264359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Save the ShortcutInfo for binding in the workspace
36364359a53dc827797917a8d9a0697a91996789801Winson Chung                        addedShortcutsFinal.add(shortcutInfo);
36464359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
36564359a53dc827797917a8d9a0697a91996789801Winson Chung                }
36664359a53dc827797917a8d9a0697a91996789801Winson Chung
3674caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")", true);
368d64d1763c590b1c954b759156683abf39ff22945Winson Chung
36976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // Update the workspace screens
37076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                updateWorkspaceScreenOrder(context, workspaceScreens);
37176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
372997a92348a6d6e061737399321393449c16cd4d8Winson Chung                if (!addedShortcutsFinal.isEmpty()) {
373997a92348a6d6e061737399321393449c16cd4d8Winson Chung                    runOnMainThread(new Runnable() {
374997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        public void run() {
375997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
376997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            if (callbacks == cb && cb != null) {
377997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
378997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                long lastScreenId = info.screenId;
379997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
380997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
381997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                for (ItemInfo i : addedShortcutsFinal) {
382997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                    if (i.screenId == lastScreenId) {
383997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                        addAnimated.add(i);
384997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                    } else {
385997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                        addNotAnimated.add(i);
386997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                    }
387997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                }
388d64d1763c590b1c954b759156683abf39ff22945Winson Chung                                callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
389c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                                        addNotAnimated, addAnimated, addedApps);
390997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            }
39164359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
392997a92348a6d6e061737399321393449c16cd4d8Winson Chung                    });
393997a92348a6d6e061737399321393449c16cd4d8Winson Chung                }
39464359a53dc827797917a8d9a0697a91996789801Winson Chung            }
39564359a53dc827797917a8d9a0697a91996789801Winson Chung        };
39664359a53dc827797917a8d9a0697a91996789801Winson Chung        runOnWorkerThread(r);
39764359a53dc827797917a8d9a0697a91996789801Winson Chung    }
39864359a53dc827797917a8d9a0697a91996789801Winson Chung
39956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
4000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
401f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
40231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
40381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    public void unbindItemInfosAndClearQueuedBindRunnables() {
40481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
40581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung            throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
40681b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    "main thread");
40781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        }
40881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
40981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Clear any deferred bind runnables
41081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        mDeferredBindRunnables.clear();
41181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Remove any queued bind runnables
41281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
41381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Unbind all the workspace items
41481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        unbindWorkspaceItemsOnMainThread();
415603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung    }
416603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung
417b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
41881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    void unbindWorkspaceItemsOnMainThread() {
419603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        // Ensure that we don't use the same workspace items data structure on the main thread
420603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        // by making a copy of workspace items first.
421b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
422b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
4232abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung        synchronized (sBgLock) {
424b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            tmpWorkspaceItems.addAll(sBgWorkspaceItems);
425b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            tmpAppWidgets.addAll(sBgAppWidgets);
426b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
427b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        Runnable r = new Runnable() {
428b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                @Override
429b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                public void run() {
430b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   for (ItemInfo item : tmpWorkspaceItems) {
431b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                       item.unbind();
432b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   }
433b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   for (ItemInfo item : tmpAppWidgets) {
434b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                       item.unbind();
435b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   }
436b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
437b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
438b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnMainThread(r);
4394eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen    }
4404eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen
4419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
4429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
4439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
4449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
4459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
446dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long screenId, int cellX, int cellY) {
4479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
4489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
449dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
4509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
4519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
452dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            moveItemInDatabase(context, item, container, screenId, cellX, cellY);
4539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
4549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
45531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
456b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka    static void checkItemInfoLocked(
457b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
458b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        ItemInfo modelItem = sBgItemsIdMap.get(itemId);
459b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        if (modelItem != null && item != modelItem) {
460b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // check all the data is consistent
461b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
462b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
463b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                ShortcutInfo shortcut = (ShortcutInfo) item;
464b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
465b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.intent.filterEquals(shortcut.intent) &&
466b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.id == shortcut.id &&
467b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.itemType == shortcut.itemType &&
468b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.container == shortcut.container &&
469dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        modelShortcut.screenId == shortcut.screenId &&
470b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.cellX == shortcut.cellX &&
471b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.cellY == shortcut.cellY &&
472b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.spanX == shortcut.spanX &&
473b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.spanY == shortcut.spanY &&
474b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
475b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        (modelShortcut.dropPos != null &&
476b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                                shortcut.dropPos != null &&
477b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                                modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
478b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
479b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    // For all intents and purposes, this is the same object
480b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    return;
481b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                }
482b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
483b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka
484b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // the modelItem needs to match up perfectly with item if our model is
485b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // to be consistent with the database-- for now, just require
486b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // modelItem == item or the equality check above
487b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            String msg = "item: " + ((item != null) ? item.toString() : "null") +
488b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    "modelItem: " +
489b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    ((modelItem != null) ? modelItem.toString() : "null") +
490b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    "Error: ItemInfo passed to checkItemInfo doesn't match original";
491b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            RuntimeException e = new RuntimeException(msg);
492b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            if (stackTrace != null) {
493b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                e.setStackTrace(stackTrace);
494b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
495e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen            // TODO: something breaks this in the upgrade path
496e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen            //throw e;
497b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        }
498b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka    }
499b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka
500816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka    static void checkItemInfo(final ItemInfo item) {
501816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
502816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        final long itemId = item.id;
503816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        Runnable r = new Runnable() {
504b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            public void run() {
505b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                synchronized (sBgLock) {
506b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    checkItemInfoLocked(itemId, item, stackTrace);
507816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka                }
508b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
509b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        };
510b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
511816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka    }
512816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka
513c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void updateItemInDatabaseHelper(Context context, final ContentValues values,
514c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            final ItemInfo item, final String callingFunction) {
515c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final long itemId = item.id;
516c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
517c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ContentResolver cr = context.getContentResolver();
518c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
519487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
520c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
521c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            public void run() {
522c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.update(uri, values, null, null);
523f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                updateItemArrays(item, itemId, stackTrace);
524f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
525f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        };
526f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        runOnWorkerThread(r);
527f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
528c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
529f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
530f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final ArrayList<ItemInfo> items, final String callingFunction) {
531f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        final ContentResolver cr = context.getContentResolver();
532c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
533f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
534f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        Runnable r = new Runnable() {
535f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            public void run() {
536f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                ArrayList<ContentProviderOperation> ops =
537f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        new ArrayList<ContentProviderOperation>();
538f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                int count = items.size();
539f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                for (int i = 0; i < count; i++) {
540f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ItemInfo item = items.get(i);
541f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    final long itemId = item.id;
542f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
543f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ContentValues values = valuesList.get(i);
544f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
545f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
546f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    updateItemArrays(item, itemId, stackTrace);
547487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
548f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
549f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                try {
550f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    cr.applyBatch(LauncherProvider.AUTHORITY, ops);
551f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                } catch (Exception e) {
552f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    e.printStackTrace();
553c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                }
554c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            }
555c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
556b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
557c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    }
558bebf042666cffe52039b875a549a582abd78a431Adam Cohen
559f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
560f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        // Lock on mBgLock *after* the db operation
561f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        synchronized (sBgLock) {
562f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            checkItemInfoLocked(itemId, item, stackTrace);
563f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
564f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
565f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
566f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                // Item is in a folder, make sure this folder exists
567f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                if (!sBgFolders.containsKey(item.container)) {
568f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    // An items container is being set to a that of an item which is not in
569f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    // the list of Folders.
570f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    String msg = "item: " + item + " container being set to: " +
571f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                            item.container + ", not in the list of folders";
572f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    Log.e(TAG, msg);
573f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    Launcher.dumpDebugLogsToConsole();
574f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
575f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
576f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
577f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // Items are added/removed from the corresponding FolderInfo elsewhere, such
578f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // as in Workspace.onDrop. Here, we just add/remove them from the list of items
579f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // that are on the desktop, as appropriate
580f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo modelItem = sBgItemsIdMap.get(itemId);
581f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
582f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
583f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                switch (modelItem.itemType) {
584f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
585f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
586f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
587f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        if (!sBgWorkspaceItems.contains(modelItem)) {
588f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                            sBgWorkspaceItems.add(modelItem);
589f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        }
590f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        break;
591f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    default:
592f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        break;
593f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
594f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            } else {
595f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                sBgWorkspaceItems.remove(modelItem);
596f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
597f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
598f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
599f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
600c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    public void flushWorkerThread() {
601c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        mFlushingWorkerThread = true;
602c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        Runnable waiter = new Runnable() {
603c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                public void run() {
604c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    synchronized (this) {
605c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        notifyAll();
606c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        mFlushingWorkerThread = false;
607c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    }
608c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
609c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            };
610c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka
611c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        synchronized(waiter) {
612c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            runOnWorkerThread(waiter);
613c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            if (mLoaderTask != null) {
614c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                synchronized(mLoaderTask) {
615c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    mLoaderTask.notify();
616c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
617c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            }
618c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            boolean success = false;
619c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            while (!success) {
620c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                try {
621c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    waiter.wait();
622c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    success = true;
623c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                } catch (InterruptedException e) {
624c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
625c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            }
626c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        }
627c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    }
628c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka
6299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
6309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
6319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
632a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
633dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY) {
6345524b4957fc7c1c858037327429411efdb8fe7d6Brian Muramatsu        String transaction = "DbDebug    Modify item (" + item.title + ") in db, id: " + item.id +
635dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
636dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
6374caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen        Launcher.addDumpLog(TAG, transaction, true);
6384caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen
6399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
6409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
6419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
642c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
6433d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // We store hotseat items in canonical form which is this orientation invariant position
6443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // in the hotseat
645dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
6463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
647dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
6483d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
649dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
6503d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
65131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
6529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
6539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
6543d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
6553d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
656dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
657ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
658c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
65931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
66031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
66131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
662f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
663f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     * cellX, cellY have already been updated on the ItemInfos.
664f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     */
665f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
666f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final long container, final int screen) {
667f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
668f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
669f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        int count = items.size();
670f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
671f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        for (int i = 0; i < count; i++) {
672f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo item = items.get(i);
673f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            String transaction = "DbDebug    Modify item (" + item.title + ") in db, id: "
674dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
675f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
676f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    + item.cellX + ", " + item.cellY + ")";
6774caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            Launcher.addDumpLog(TAG, transaction, true);
678f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            item.container = container;
679f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
680f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // We store hotseat items in canonical form which is this orientation invariant position
681f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // in the hotseat
682f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (context instanceof Launcher && screen < 0 &&
683f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
684dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
685f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        item.cellY);
686f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            } else {
687dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = screen;
688f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
689f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
690f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final ContentValues values = new ContentValues();
691f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CONTAINER, item.container);
692f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CELLX, item.cellX);
693f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CELLY, item.cellY);
694dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
695f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
696f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            contentValues.add(values);
697f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
698f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
699f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
700f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
701f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    /**
702bebf042666cffe52039b875a549a582abd78a431Adam Cohen     * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
703d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen     */
704bebf042666cffe52039b875a549a582abd78a431Adam Cohen    static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
705dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
7065524b4957fc7c1c858037327429411efdb8fe7d6Brian Muramatsu        String transaction = "DbDebug    Modify item (" + item.title + ") in db, id: " + item.id +
707dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
708dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
7094caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen        Launcher.addDumpLog(TAG, transaction, true);
7104caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen
711d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellX = cellX;
712d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellY = cellY;
713bebf042666cffe52039b875a549a582abd78a431Adam Cohen        item.spanX = spanX;
714bebf042666cffe52039b875a549a582abd78a431Adam Cohen        item.spanY = spanY;
715bebf042666cffe52039b875a549a582abd78a431Adam Cohen
716bebf042666cffe52039b875a549a582abd78a431Adam Cohen        // We store hotseat items in canonical form which is this orientation invariant position
717bebf042666cffe52039b875a549a582abd78a431Adam Cohen        // in the hotseat
718dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
719bebf042666cffe52039b875a549a582abd78a431Adam Cohen                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
720dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
721bebf042666cffe52039b875a549a582abd78a431Adam Cohen        } else {
722dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
723bebf042666cffe52039b875a549a582abd78a431Adam Cohen        }
724d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
725d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        final ContentValues values = new ContentValues();
726d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
727bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
728bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
729bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.SPANX, item.spanX);
730bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.SPANY, item.spanY);
731dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
732d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
733816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
734bebf042666cffe52039b875a549a582abd78a431Adam Cohen    }
735c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
736c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    /**
737c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * Update an item to the database in a specified container.
738c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     */
739c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void updateItemInDatabase(Context context, final ItemInfo item) {
740c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ContentValues values = new ContentValues();
741c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        item.onAddToDatabase(values);
742c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
743c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
744d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    }
745d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
746d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    /**
7479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
7489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
749bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
7509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
7519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
7529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
7539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
7549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
7559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
7569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
7579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
7589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
7599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
7609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
7619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
762bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
763bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
764bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
765aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
766aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
767aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
768aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
769aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
770aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
771aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
772aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
773aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
774aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
775aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
776aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
777aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
778aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
779aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
780aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
781aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
782aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
783aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
784aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
785aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
786c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                ItemInfo item = new ItemInfo();
787aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
788aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
78961c69869b3947f1d242b07384bb4e9cd5b266915Winson Chung                item.spanX = Math.max(1, c.getInt(spanXIndex));
79061c69869b3947f1d242b07384bb4e9cd5b266915Winson Chung                item.spanY = Math.max(1, c.getInt(spanYIndex));
791aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
792aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
793dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = c.getInt(screenIndex);
794aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
795aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
796aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
797aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
798aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
799aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
800aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
801aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
802aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
803aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
804aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
805aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
806aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
8079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
80831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
8099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
8109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
8119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
8129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
8139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
814df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
815f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
8179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
8189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
8199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
8209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
8219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
8229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
8239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
824f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
8269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
827df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
828df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        folderInfo = findOrMakeFolder(folderList, id);
8299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
8309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
83131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
8339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
8349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
835dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                folderInfo.screenId = c.getInt(screenIndex);
836d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
837d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
838ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
8399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
8409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
8419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
8429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
8432fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
844ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
8459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
8469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
84731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
8499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
8509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
8519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
8523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    static void addItemToDatabase(Context context, final ItemInfo item, final long container,
853dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY, final boolean notify) {
8549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
8559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
8569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
8573d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // We store hotseat items in canonical form which is this orientation invariant position
8583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // in the hotseat
859dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
8603d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
861dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
8623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
863dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
8643d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
865ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
8669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
8679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
8689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
869ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
870414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka        item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
871a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        values.put(LauncherSettings.Favorites._ID, item.id);
8723d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
873aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
874c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
875a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            public void run() {
876487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                String transaction = "DbDebug    Add item (" + item.title + ") to db, id: "
877dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
878487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        + cellY + ")";
8794caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                Launcher.addDumpLog(TAG, transaction, true);
880487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
881a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
882a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                        LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
883ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
884b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
8852abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
886b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    checkItemInfoLocked(item.id, item, null);
887b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.put(item.id, item);
888b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    switch (item.itemType) {
889b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
890b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgFolders.put(item.id, (FolderInfo) item);
891b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            // Fall through
892b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
893b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
894b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
895b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
896b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgWorkspaceItems.add(item);
897b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            } else {
898b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!sBgFolders.containsKey(item.container)) {
899b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // Adding an item to a folder that doesn't exist.
900b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String msg = "adding item: " + item + " to a folder that " +
901b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            " doesn't exist";
90228b3e10fdea4d719b46d2e0da9a916bacc238fd1Adam Cohen                                    Log.e(TAG, msg);
903b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Launcher.dumpDebugLogsToConsole();
904b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
905487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                            }
906b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
907b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
908b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgAppWidgets.add((LauncherAppWidgetInfo) item);
909b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
910b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
911a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                }
912a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            }
913c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
914b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
91531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
91631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
918aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
919aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
920845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka    static int getCellLayoutChildId(
921dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
9223d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return (((int) container & 0xFF) << 24)
923dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
924aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
925aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
926aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
927c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * Removes the specified item from the database
928c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * @param context
929c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * @param item
9309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
931c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void deleteItemFromDatabase(Context context, final ItemInfo item) {
9329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
933c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
934487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
93583df188ce0822f4058406e5f2878d1cf34bf413eMichael Jurka        Runnable r = new Runnable() {
936a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            public void run() {
937487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                String transaction = "DbDebug    Delete item (" + item.title + ") from db, id: "
938dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
939487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        ", " + item.cellY + ")";
9404caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                Launcher.addDumpLog(TAG, transaction, true);
941487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
942c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(uriToDelete, null, null);
943b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
944b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
9452abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
946b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    switch (item.itemType) {
947b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
948b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgFolders.remove(item.id);
949b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            for (ItemInfo info: sBgItemsIdMap.values()) {
950b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (info.container == item.id) {
951b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // We are deleting a folder which still contains items that
952b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // think they are contained by that folder.
953b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String msg = "deleting a folder (" + item + ") which still " +
954b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            "contains items (" + info + ")";
95528b3e10fdea4d719b46d2e0da9a916bacc238fd1Adam Cohen                                    Log.e(TAG, msg);
956b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Launcher.dumpDebugLogsToConsole();
957b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
958487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                            }
959b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgWorkspaceItems.remove(item);
960b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
961b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
962b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
963b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgWorkspaceItems.remove(item);
964b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
965b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
966b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
967b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
968b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
969b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.remove(item.id);
970b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgDbIconCache.remove(item);
971a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                }
972a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            }
97383df188ce0822f4058406e5f2878d1cf34bf413eMichael Jurka        };
974b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
97531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
97631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
978dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * Update the order of the workspace screens in the database. The array list contains
979dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * a list of screen ids in the order that they should appear.
980dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     */
981c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
9824caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen        Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder()", true);
98364359a53dc827797917a8d9a0697a91996789801Winson Chung        final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
984dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final ContentResolver cr = context.getContentResolver();
985dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
986dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
987dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        // Remove any negative screen ids -- these aren't persisted
98864359a53dc827797917a8d9a0697a91996789801Winson Chung        Iterator<Long> iter = screensCopy.iterator();
989dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        while (iter.hasNext()) {
990dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long id = iter.next();
991dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (id < 0) {
9924caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - remove: " + id + ")", true);
993dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                iter.remove();
994dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
995dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
996dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
9979e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung        // Dump the screens copy
9984caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen        Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - screensCopy", true);
9999e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung        for (Long l : screensCopy) {
10004caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
10019e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung        }
10029e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
1003dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        Runnable r = new Runnable() {
1004dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            @Override
1005dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            public void run() {
1006dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                // Clear the table
1007dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                cr.delete(uri, null, null);
100876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                int count = screensCopy.size();
1009dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                ContentValues[] values = new ContentValues[count];
1010dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                for (int i = 0; i < count; i++) {
1011dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    ContentValues v = new ContentValues();
101276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = screensCopy.get(i);
1013dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1014dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
10154caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")", true);
1016dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    values[i] = v;
1017dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
1018dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                cr.bulkInsert(uri, values);
10199e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
1020ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                synchronized (sBgLock) {
1021ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    // Dump the sBgWorkspaceScreens
1022ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - pre clear", true);
1023ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    for (Long l : sBgWorkspaceScreens) {
1024ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                        Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1025ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    }
10269e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
1027ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    sBgWorkspaceScreens.clear();
1028ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    sBgWorkspaceScreens.addAll(screensCopy);
10294caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen
1030ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    // Dump the sBgWorkspaceScreens
1031ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - post clear", true);
1032ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    for (Long l : sBgWorkspaceScreens) {
1033ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                        Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1034ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    }
10354caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                }
1036dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1037dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        };
1038dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        runOnWorkerThread(r);
1039dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
1040dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1041dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    /**
10429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
10439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1044a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
10459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1046f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1047c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
1048c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            public void run() {
1049c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
1050b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
10512abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1052b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.remove(info.id);
1053b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgFolders.remove(info.id);
1054b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgDbIconCache.remove(info);
1055b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgWorkspaceItems.remove(info);
1056b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1057c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
1058c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1059c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                        LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
1060b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
10612abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1062b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (ItemInfo childInfo : info.contents) {
1063b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        sBgItemsIdMap.remove(childInfo.id);
1064b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        sBgDbIconCache.remove(childInfo);
1065b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1066afb01ee74243cede19088e694ca82cea5983c603Adam Cohen                }
1067c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            }
1068c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
1069b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
10709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
1071f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
10729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
10739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
10749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
10759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
10769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
10779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
1078f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
1079f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
1080f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
10811d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
10829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
10839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
10849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1085cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    @Override
1086f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
10873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
1088aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
10893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
10909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
10913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
10923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
10933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
10943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
10953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1096f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
10973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
109864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
10993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
11003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
11013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
110356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
11043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
11053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
11063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
11073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
11083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
110956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
11103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
11113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
11123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
11133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
11143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
11153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
11163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
111756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
11183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
111956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
11203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
11213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1122f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
1123f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
11243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
1125cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // First, schedule to add these apps back in.
1126cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1127cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1128cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // Then, rebind everything.
1129e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            startLoaderFromBackground();
11303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
11313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
11323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(
11333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        PackageUpdatedTask.OP_UNAVAILABLE, packages));
1134e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
113593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // If we have changed locale we need to clear out the labels in all apps/workspace.
113693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            forceReload();
113793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
113893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // Check if configuration change was an mcc/mnc change which would affect app resources
113993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // and we would need to clear out the labels in all apps/workspace. Same handling as
114093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // above for ACTION_LOCALE_CHANGED
114193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             Configuration currentConfig = context.getResources().getConfiguration();
114299a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee             if (mPreviousConfigMcc != currentConfig.mcc) {
114393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                   Log.d(TAG, "Reload apps on config change. curr_mcc:"
114499a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee                       + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
114593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                   forceReload();
114693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             }
114793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // Update previousConfig
114899a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee             mPreviousConfigMcc = currentConfig.mcc;
1149cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1150cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung                   SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
1151ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka            if (mCallbacks != null) {
1152ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                Callbacks callbacks = mCallbacks.get();
1153ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                if (callbacks != null) {
1154ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                    callbacks.bindSearchablesChanged();
1155ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                }
1156cfdf7ee64db8820d91a1cd82bf7b961fb2083dceWinson Chung            }
1157e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
1158e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    }
1159e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato
116093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    private void forceReload() {
1161f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        resetLoadedState(true, true);
1162f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1163f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Do this here because if the launcher activity is running it will be restarted.
1164f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // If it's not running startLoaderFromBackground will merely tell it that it needs
1165f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // to reload.
1166f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        startLoaderFromBackground();
1167f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    }
1168f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1169f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
117093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        synchronized (mLock) {
117193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // Stop any existing loaders first, so they don't set mAllAppsLoaded or
117293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // mWorkspaceLoaded to true later
117393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            stopLoaderLocked();
1174f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            if (resetAllAppsLoaded) mAllAppsLoaded = false;
1175f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
117693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        }
117793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    }
117893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1179e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    /**
1180e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * When the launcher is in the background, it's possible for it to miss paired
1181e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * configuration changes.  So whenever we trigger the loader from the background
1182e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * tell the launcher that it needs to re-run the loader when it comes back instead
1183e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * of doing it now.
1184e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     */
1185e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    public void startLoaderFromBackground() {
1186e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        boolean runLoader = false;
1187e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (mCallbacks != null) {
1188e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            Callbacks callbacks = mCallbacks.get();
1189e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            if (callbacks != null) {
1190e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                // Only actually run the loader if they're not paused.
1191e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                if (!callbacks.setLoadOnResume()) {
1192e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                    runLoader = true;
1193e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                }
1194e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            }
1195e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
1196e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (runLoader) {
1197b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            startLoader(false, -1);
1198f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
11993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1200f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
120193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    // If there is already a loader task running, tell it to stop.
120293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    // returns true if isLaunching() was true on the old task
120393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    private boolean stopLoaderLocked() {
120493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        boolean isLaunching = false;
120593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        LoaderTask oldTask = mLoaderTask;
120693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        if (oldTask != null) {
120793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            if (oldTask.isLaunching()) {
120893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                isLaunching = true;
120993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            }
121093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            oldTask.stopLocked();
121193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        }
121293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        return isLaunching;
121393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    }
121493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1215b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    public void startLoader(boolean isLaunching, int synchronousBindPage) {
12163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
12173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
12193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1220843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
1221b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Clear any deferred bind-runnables from the synchronized load process
1222b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // We must do this before any loading/binding is scheduled below.
1223b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
1224b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
12253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
12263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
12273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
122893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                // also, don't downgrade isLaunching if we're already running
122993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                isLaunching = isLaunching || stopLoaderLocked();
1230cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler                mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
1231b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1232b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1233b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
1234b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1235b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sWorker.post(mLoaderTask);
1236b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1237b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1238b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
1239b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
1240b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1241b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    void bindRemainingSynchronousPages() {
1242b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        // Post the remaining side pages to be loaded
1243b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (!mDeferredBindRunnables.isEmpty()) {
1244b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (final Runnable r : mDeferredBindRunnables) {
124581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
1246b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
1247b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
1248b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
12493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1250b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
12513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
12523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
12533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
12543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
12559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
1256f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
12573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1258b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
125976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    /** Loads the workspace screens db into a map of Rank -> ScreenId */
126076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
126176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final ContentResolver contentResolver = context.getContentResolver();
126276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
126376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
126476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
126576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
126676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        try {
126776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            final int idIndex = sc.getColumnIndexOrThrow(
126876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    LauncherSettings.WorkspaceScreens._ID);
126976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            final int rankIndex = sc.getColumnIndexOrThrow(
127076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    LauncherSettings.WorkspaceScreens.SCREEN_RANK);
127176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            while (sc.moveToNext()) {
127276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                try {
127376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = sc.getLong(idIndex);
127476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    int rank = sc.getInt(rankIndex);
127576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
12764caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    Launcher.addDumpLog(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")", true);
127776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
127876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    orderedScreens.put(rank, screenId);
127976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                } catch (Exception e) {
1280ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
128176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                }
128276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            }
128376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        } finally {
128476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            sc.close();
128576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        }
128676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        return orderedScreens;
128776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    }
128876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
1289c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    public boolean isAllAppsLoaded() {
1290c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        return mAllAppsLoaded;
1291c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    }
1292c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka
129336a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    boolean isLoadingWorkspace() {
129436a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        synchronized (mLock) {
129536a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            if (mLoaderTask != null) {
129636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung                return mLoaderTask.isLoadingWorkspace();
129736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            }
129836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        }
129936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        return false;
130036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    }
130136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
13023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
13033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
13043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
13053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
13063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
13073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
13083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
13093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
13103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
131136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        private boolean mIsLoadingAndBindingWorkspace;
13123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
13133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
1314b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1315c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        private HashMap<Object, CharSequence> mLabelCache;
13163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
13173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        LoaderTask(Context context, boolean isLaunching) {
13183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
13193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
1320c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = new HashMap<Object, CharSequence>();
13213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1322f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
13233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
13243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
13253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1326f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
132736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        boolean isLoadingWorkspace() {
132836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            return mIsLoadingAndBindingWorkspace;
132936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        }
133036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
1331c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        /** Returns whether this is an upgrade path */
1332c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private boolean loadAndBindWorkspace() {
133336a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            mIsLoadingAndBindingWorkspace = true;
133436a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
13353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
13363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
13373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
13383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1339288a36b563801a26d00084cb823435a3ba4cb76cMichael Jurka
1340c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean isUpgradePath = false;
1341a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            if (!mWorkspaceLoaded) {
1342c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                isUpgradePath = loadWorkspace();
134393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                synchronized (LoaderTask.this) {
134493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    if (mStopped) {
1345ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                        Launcher.addDumpLog(TAG, "10249126 - loadAndBindWorkspace() stopped", true);
1346c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        return isUpgradePath;
134793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    }
134893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    mWorkspaceLoaded = true;
13499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
13503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1351cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
13523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
1353c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            bindWorkspace(-1, isUpgradePath);
1354c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            return isUpgradePath;
13553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1356f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
13573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
13583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
13593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
13603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
13613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
13623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1363843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
13643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
13653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
13663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
13673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
13683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
13693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
13709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
13713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
13729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
13739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
13743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
13753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1376c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
13773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
1378c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        // Just in case mFlushingWorkerThread changes but we aren't woken up,
1379c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        // wait no longer than 1sec at a time
1380c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        this.wait(1000);
13813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
13823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
13839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
13843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
13853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
13863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
1387aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
13883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
13899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
1390843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
13913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1392f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1393b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        void runBindSynchronousPage(int synchronousBindPage) {
1394b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (synchronousBindPage < 0) {
1395b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Ensure that we have a valid page index to load synchronously
1396b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1397b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        "valid page index");
1398b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1399b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1400b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Ensure that we don't try and bind a specified page when the pages have not been
1401b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // loaded already (we should load everything asynchronously in that case)
1402b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1403b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1404b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            synchronized (mLock) {
1405b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (mIsLoaderTaskRunning) {
1406b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // Ensure that we are never running the background loading at this point since
1407b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // we also touch the background collections
1408b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    throw new RuntimeException("Error! Background loading is already running");
1409b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1410b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1411b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1412b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: Throw an exception if we are already loading (since we touch the worker thread
1413b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      data structures, we can't allow any other thread to touch that data, but because
1414b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      this call is synchronous, we can get away with not locking).
1415b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1416cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler            // The LauncherModel is static in the LauncherAppState and mHandler may have queued
1417a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            // operations from the previous activity.  We need to ensure that all queued operations
1418a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            // are executed before any synchronous binding work is done.
1419a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            mHandler.flush();
1420a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen
1421b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Divide the set of loaded items into those that we are binding synchronously, and
1422b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // everything else that is to be bound normally (asynchronously).
1423c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            bindWorkspace(synchronousBindPage, false);
1424b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: For now, continue posting the binding of AllApps as there are other issues that
1425b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      arise from that.
1426b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            onlyBindAllApps();
1427b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
1428b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
14293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
1430c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean isUpgrade = false;
1431c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1432b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            synchronized (mLock) {
1433b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                mIsLoaderTaskRunning = true;
1434b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
14353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
14363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
14373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
14383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
1439843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
1440843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
1441dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
1442aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                    if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1443aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                            (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
1444843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
1445843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1446dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
144764359a53dc827797917a8d9a0697a91996789801Winson Chung                if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1448c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                isUpgrade = loadAndBindWorkspace();
1449843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
14503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
14513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
14523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
14533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
14543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
14553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
14569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
1457843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
1458aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                        if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
1459843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
14609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
14619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
14623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
1463f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1464843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
146564359a53dc827797917a8d9a0697a91996789801Winson Chung                if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
146664359a53dc827797917a8d9a0697a91996789801Winson Chung                loadAndBindAllApps();
14677ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung
14687ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                // Restore the default thread priority after we are done loading items
14697ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                synchronized (mLock) {
14707ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                    android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
14717ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                }
14723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1473843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
1474aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // Update the saved icons if necessary
1475aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
14762abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
1477b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                for (Object key : sBgDbIconCache.keySet()) {
1478b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1479b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1480b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgDbIconCache.clear();
1481aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            }
1482aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung
1483c58497ee619416c4949b16eb43486c11618b69e5Winson Chung            if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1484c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                // Ensure that all the applications that are in the system are
1485c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                // represented on the home screen.
1486c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                Launcher.addDumpLog(TAG, "10249126 - verifyApplications - useMoreApps="
1487c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                        + UPGRADE_USE_MORE_APPS_FOLDER + " isUpgrade=" + isUpgrade, true);
1488c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
1489c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                    Launcher.addDumpLog(TAG, "10249126 - verifyApplications(" + isUpgrade + ")", true);
1490c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                    verifyApplications();
1491c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                }
1492c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1493c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
14943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
14953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
14963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
1497f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
14983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
14993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
15003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
15013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
1502843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
1503b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                mIsLoaderTaskRunning = false;
15043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
15053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1506f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
15073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
15083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
1509ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                Launcher.addDumpLog(TAG, "10249126 - STOPPED", true);
15103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
15113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
1512f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
15133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1514f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
15153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
15163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
15173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
15183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
15193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
15203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
15213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
15223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
15233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
15243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
15253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
15263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1527ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
15283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
15293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
15303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1531f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
15323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
15333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
15343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
15353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
15363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
15373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
15383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
15399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
15403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
15413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
1542f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
15433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1544f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1545c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private void verifyApplications() {
1546c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            final Context context = mApp.getContext();
1547c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1548c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Cross reference all the applications in our apps list with items in the workspace
1549c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            ArrayList<ItemInfo> tmpInfos;
1550695ff6b6fada4e1037592d7c4961321e12890b7dMichael Jurka            ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
1551c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            synchronized (sBgLock) {
1552eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                for (AppInfo app : mBgAllAppsList.data) {
1553c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    tmpInfos = getItemInfoForComponentName(app.componentName);
15544caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    Launcher.addDumpLog(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty(), true);
1555c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    if (tmpInfos.isEmpty()) {
1556c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        // We are missing an application icon, so add this to the workspace
1557c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        added.add(app);
1558c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        // This is a rare event, so lets log it
1559c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        Log.e(TAG, "Missing Application on load: " + app);
1560c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    }
1561c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                }
1562c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1563c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            if (!added.isEmpty()) {
1564c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1565c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                addAndBindAddedApps(context, added, cb, null);
1566c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1567c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        }
1568c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
15695f8afe6280eae34620067696173e71943e1a30a3Winson Chung        private boolean checkItemDimensions(ItemInfo info) {
1570892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            LauncherAppState app = LauncherAppState.getInstance();
1571892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1572892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            return (info.cellX + info.spanX) > (int) grid.numColumns ||
1573892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    (info.cellY + info.spanY) > (int) grid.numRows;
15745f8afe6280eae34620067696173e71943e1a30a3Winson Chung        }
15755f8afe6280eae34620067696173e71943e1a30a3Winson Chung
15763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
1577dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
1578892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            LauncherAppState app = LauncherAppState.getInstance();
1579892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1580892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            int countX = (int) grid.numColumns;
1581892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            int countY = (int) grid.numRows;
1582892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung
1583dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long containerIndex = item.screenId;
1584f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1585dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1586dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1587dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            [(int) item.screenId][0] != null) {
1588dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        Log.e(TAG, "Error loading shortcut into hotseat " + item
1589dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + " into position (" + item.screenId + ":" + item.cellX + ","
1590dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + item.cellY + ") occupied by "
1591dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1592dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                [(int) item.screenId][0]);
1593dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            return false;
1594dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
15956ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                } else {
1596892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
1597dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    items[(int) item.screenId][0] = item;
1598dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
15996ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                    return true;
16006ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                }
1601f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung            } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1602f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung                // Skip further checking if it is not the hotseat or workspace container
16033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
16043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1605f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung
1606dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (!occupied.containsKey(item.screenId)) {
1607892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
1608dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                occupied.put(item.screenId, items);
1609dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1610dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1611dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            ItemInfo[][] screens = occupied.get(item.screenId);
16126ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung            // Check if any workspace icons overlap with each other
16133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
16143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
1615dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (screens[x][y] != null) {
16163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
1617dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            + " into cell (" + containerIndex + "-" + item.screenId + ":"
16183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
1619aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
1620dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            + screens[x][y]);
16213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
16228802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
16238802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
16243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
16253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
16263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
1627dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    screens[x][y] = item;
16288802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
16298802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
1630f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung
16313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
16323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
16338802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
1634ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        /** Clears all the sBg data structures */
1635ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        private void clearSBgDataStructures() {
1636ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            synchronized (sBgLock) {
1637ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgWorkspaceItems.clear();
1638ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgAppWidgets.clear();
1639ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgFolders.clear();
1640ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgItemsIdMap.clear();
1641ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgDbIconCache.clear();
1642ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgWorkspaceScreens.clear();
1643ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            }
1644ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        }
1645ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung
1646c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        /** Returns whether this is an upgradge path */
1647c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private boolean loadWorkspace() {
16483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1649f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
16503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
16513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
16523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
16533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
16543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
1655f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1656892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            LauncherAppState app = LauncherAppState.getInstance();
1657892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1658892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            int countX = (int) grid.numColumns;
1659892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            int countY = (int) grid.numRows;
1660892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung
1661b85f8a44b51258f22938773ca30dd85845345010Michael Jurka            // Make sure the default workspace is loaded, if needed
1662414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka            LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1663e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
1664c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Check if we need to do any upgrade-path logic
1665414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka            boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
1666b85f8a44b51258f22938773ca30dd85845345010Michael Jurka
16672abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
1668ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                clearSBgDataStructures();
16694caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                Launcher.addDumpLog(TAG, "10249126 - loadWorkspace()", true);
1670b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1671b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
1672c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
1673e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                final Cursor c = contentResolver.query(contentUri, null, null, null, null);
1674b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1675b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // +1 for the hotseat (it can be larger than the workspace)
1676b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Load workspace in reverse order to ensure that latest items are loaded first (and
1677b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // before any earlier duplicates)
1678dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
1679b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1680b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                try {
1681b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1682b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int intentIndex = c.getColumnIndexOrThrow
1683b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.INTENT);
1684b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int titleIndex = c.getColumnIndexOrThrow
1685b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.TITLE);
1686b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconTypeIndex = c.getColumnIndexOrThrow(
1687b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_TYPE);
1688b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1689b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconPackageIndex = c.getColumnIndexOrThrow(
1690b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_PACKAGE);
1691b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconResourceIndex = c.getColumnIndexOrThrow(
1692b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_RESOURCE);
1693b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int containerIndex = c.getColumnIndexOrThrow(
1694b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.CONTAINER);
1695b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int itemTypeIndex = c.getColumnIndexOrThrow(
1696b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ITEM_TYPE);
1697b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1698b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.APPWIDGET_ID);
1699b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int screenIndex = c.getColumnIndexOrThrow(
1700b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.SCREEN);
1701b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int cellXIndex = c.getColumnIndexOrThrow
1702b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.CELLX);
1703b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int cellYIndex = c.getColumnIndexOrThrow
1704b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.CELLY);
1705b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int spanXIndex = c.getColumnIndexOrThrow
1706b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.SPANX);
1707b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int spanYIndex = c.getColumnIndexOrThrow(
1708b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.SPANY);
1709b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1710b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //final int displayModeIndex = c.getColumnIndexOrThrow(
1711b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //        LauncherSettings.Favorites.DISPLAY_MODE);
1712b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1713b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    ShortcutInfo info;
1714b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    String intentDescription;
1715b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    LauncherAppWidgetInfo appWidgetInfo;
1716b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    int container;
1717b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    long id;
1718b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Intent intent;
1719b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1720ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    Launcher.addDumpLog(TAG, "10249126 - Num rows: " + c.getCount(), true);
1721b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    while (!mStopped && c.moveToNext()) {
1722b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        try {
1723b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            int itemType = c.getInt(itemTypeIndex);
1724b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1725b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            switch (itemType) {
1726b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1727b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1728ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                id = c.getLong(idIndex);
1729b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                intentDescription = c.getString(intentIndex);
1730b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                try {
1731b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    intent = Intent.parseUri(intentDescription, 0);
1732ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    ComponentName cn = intent.getComponent();
173368fd3c33daf7a6d4de287f8691306a55fa54706cWinson Chung                                    if (cn != null && !isValidPackageComponent(manager, cn)) {
1734ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        if (!mAppsCanBeOnRemoveableStorage) {
17351323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            // Log the invalid package, and remove it from the db
17361323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            Uri uri = LauncherSettings.Favorites.getContentUri(id,
17371323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                                    false);
1738ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                            contentResolver.delete(uri, null, null);
1739933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                                            Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1740ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        } else {
17411323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            // If apps can be on external storage, then we just
17421323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            // leave them for the user to remove (maybe add
17431323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            // visual treatment to it)
1744933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                                            Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
1745ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        }
1746ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        continue;
1747ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    }
1748b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } catch (URISyntaxException e) {
1749933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                                    Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
1750b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    continue;
1751b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1752968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka
1753b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1754b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info = getShortcutInfo(manager, intent, context, c, iconIndex,
1755b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            titleIndex, mLabelCache);
1756b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } else {
1757b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info = getShortcutInfo(c, context, iconTypeIndex,
1758b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            iconPackageIndex, iconResourceIndex, iconIndex,
1759b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            titleIndex);
1760b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1761b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // App shortcuts that used to be automatically added to Launcher
1762b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // didn't always have the correct intent flags set, so do that
1763b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // here
1764b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (intent.getAction() != null &&
17659ad005639e1698c94674cdfadf3ae342fb56c9b5Michael Jurka                                        intent.getCategories() != null &&
17669ad005639e1698c94674cdfadf3ae342fb56c9b5Michael Jurka                                        intent.getAction().equals(Intent.ACTION_MAIN) &&
1767968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka                                        intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
1768b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        intent.addFlags(
1769b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            Intent.FLAG_ACTIVITY_NEW_TASK |
1770b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1771b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1772968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka                                }
17733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1774b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (info != null) {
1775ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    info.id = id;
1776b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.intent = intent;
1777b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    container = c.getInt(containerIndex);
1778b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.container = container;
1779dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                    info.screenId = c.getInt(screenIndex);
1780b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.cellX = c.getInt(cellXIndex);
1781b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.cellY = c.getInt(cellYIndex);
17825f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    info.spanX = 1;
17835f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    info.spanY = 1;
17845f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    // Skip loading items that are out of bounds
17855f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
17865f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        if (checkItemDimensions(info)) {
1787933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                                            Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1788933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                                                    + info + ", " + grid.numColumns + "x" + grid.numRows, true);
17895f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                            continue;
17905f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        }
17915f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    }
1792b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // check & update map of what's occupied
1793b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (!checkItemPlacement(occupied, info)) {
1794b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1795b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1796b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1797b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    switch (container) {
1798b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1799b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1800b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        sBgWorkspaceItems.add(info);
1801b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1802b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    default:
1803b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        // Item is in a user folder
1804b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        FolderInfo folderInfo =
1805b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                                findOrMakeFolder(sBgFolders, container);
1806b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        folderInfo.add(info);
1807b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1808b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1809b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgItemsIdMap.put(info.id, info);
1810b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1811b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // now that we've loaded everthing re-save it with the
1812b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // icon in case it disappears somehow.
1813b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
18141323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                } else {
18151323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                    throw new RuntimeException("Unexpected null ShortcutInfo");
1816b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1817b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                break;
1818b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1819b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1820b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                id = c.getLong(idIndex);
1821b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1822b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1823b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.title = c.getString(titleIndex);
1824b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.id = id;
18253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
1826b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.container = container;
1827dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                folderInfo.screenId = c.getInt(screenIndex);
1828b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.cellX = c.getInt(cellXIndex);
1829b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.cellY = c.getInt(cellYIndex);
18305f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                folderInfo.spanX = 1;
18315f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                folderInfo.spanY = 1;
18325f8afe6280eae34620067696173e71943e1a30a3Winson Chung
18335f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                // Skip loading items that are out of bounds
18345f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
18355f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    if (checkItemDimensions(folderInfo)) {
18365f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        Log.d(TAG, "Skipped loading out of bounds folder");
18375f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        continue;
18385f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    }
18395f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                }
18403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
1841b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!checkItemPlacement(occupied, folderInfo)) {
18423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
18439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
18445f8afe6280eae34620067696173e71943e1a30a3Winson Chung
18453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
1846b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1847b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1848b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        sBgWorkspaceItems.add(folderInfo);
1849b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
18509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
185117a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato
1852b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgItemsIdMap.put(folderInfo.id, folderInfo);
1853b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgFolders.put(folderInfo.id, folderInfo);
1854b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                break;
1855b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1856b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1857b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                // Read all Launcher-specific widget details
1858b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                int appWidgetId = c.getInt(appWidgetIdIndex);
18593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
1860f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1861b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                final AppWidgetProviderInfo provider =
1862b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        widgets.getAppWidgetInfo(appWidgetId);
1863b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1864b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!isSafeMode && (provider == null || provider.provider == null ||
1865b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        provider.provider.getPackageName() == null)) {
1866b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String log = "Deleting widget that isn't installed anymore: id="
1867b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        + id + " appWidgetId=" + appWidgetId;
1868b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Log.e(TAG, log);
18694caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                                    Launcher.addDumpLog(TAG, log, false);
1870b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    itemsToRemove.add(id);
1871b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } else {
1872b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1873b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            provider.provider);
1874b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.id = id;
1875dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                    appWidgetInfo.screenId = c.getInt(screenIndex);
1876b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.cellX = c.getInt(cellXIndex);
1877b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.cellY = c.getInt(cellYIndex);
1878b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.spanX = c.getInt(spanXIndex);
1879b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.spanY = c.getInt(spanYIndex);
1880b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1881b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.minSpanX = minSpan[0];
1882b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.minSpanY = minSpan[1];
1883b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1884b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    container = c.getInt(containerIndex);
1885b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1886b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1887b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        Log.e(TAG, "Widget found where container != " +
1888b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1889b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        continue;
1890b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1891b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1892e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                                    appWidgetInfo.container = c.getInt(containerIndex);
18935f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    // Skip loading items that are out of bounds
18945f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
18955f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        if (checkItemDimensions(appWidgetInfo)) {
18965f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                            Log.d(TAG, "Skipped loading out of bounds app widget");
18975f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                            continue;
18985f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        }
18995f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    }
1900b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // check & update map of what's occupied
1901b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (!checkItemPlacement(occupied, appWidgetInfo)) {
1902b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1903b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1904b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1905b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgAppWidgets.add(appWidgetInfo);
1906b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
19079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
19083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
1909b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        } catch (Exception e) {
1910ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                            Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
1911b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
1912b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1913b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } finally {
191447b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                    if (c != null) {
191547b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                        c.close();
191647b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                    }
1917b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1918ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
1919ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                // Break early if we've stopped loading
1920ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                if (mStopped) {
1921ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    Launcher.addDumpLog(TAG, "10249126 - loadWorkspace() - Stopped", true);
1922ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    clearSBgDataStructures();
1923ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    return false;
1924ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                }
1925ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung
1926b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (itemsToRemove.size() > 0) {
1927b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    ContentProviderClient client = contentResolver.acquireContentProviderClient(
19284caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                            LauncherSettings.Favorites.CONTENT_URI);
1929b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // Remove dead items
1930b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (long id : itemsToRemove) {
1931b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (DEBUG_LOADERS) {
1932b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.d(TAG, "Removed id = " + id);
1933b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
1934b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        // Don't notify content observers
1935b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        try {
1936b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1937b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    null, null);
1938b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        } catch (RemoteException e) {
1939b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.w(TAG, "Could not remove id = " + id);
1940b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
1941b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1942b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
19438802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
1944c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                if (loadedOldDb) {
19454caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - loadedOldDb", true);
1946dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long maxScreenId = 0;
1947dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // If we're importing we use the old screen order.
1948dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    for (ItemInfo item: sBgItemsIdMap.values()) {
1949dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        long screenId = item.screenId;
1950dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1951dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                !sBgWorkspaceScreens.contains(screenId)) {
19524caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                            Launcher.addDumpLog(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")", true);
1953dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            sBgWorkspaceScreens.add(screenId);
1954dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            if (screenId > maxScreenId) {
1955dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                maxScreenId = screenId;
1956dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            }
1957dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        }
1958dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
1959dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    Collections.sort(sBgWorkspaceScreens);
19609e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
19619e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung                    // Dump the sBgWorkspaceScreens
19624caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens", true);
19639e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung                    for (Long l : sBgWorkspaceScreens) {
19644caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                        Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
19659e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung                    }
19669e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
1967414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka                    LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
1968dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1969c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1970c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    // Update the max item id after we load an old db
1971c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    long maxItemId = 0;
1972c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    // If we're importing we use the old screen order.
1973c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    for (ItemInfo item: sBgItemsIdMap.values()) {
1974c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        maxItemId = Math.max(maxItemId, item.id);
1975c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    }
1976414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka                    LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
1977dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                } else {
1978933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                    Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - !loadedOldDb [" + sWorkerThread.getThreadId() + ", " + Process.myTid() + "]", true);
197976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
198076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    for (Integer i : orderedScreens.keySet()) {
1981933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                        Launcher.addDumpLog(TAG, "10249126 - adding to sBgWorkspaceScreens: " + orderedScreens.get(i), true);
198276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                        sBgWorkspaceScreens.add(orderedScreens.get(i));
1983dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
1984dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1985dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // Remove any empty screens
1986933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                    ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
1987933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                    for (Long l : unusedScreens) {
1988933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                        Launcher.addDumpLog(TAG, "10249126 - unused screens: " + l, true);
1989933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                    }
1990dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1991933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                    Launcher.addDumpLog(TAG, "10249126 - sBgItemsIdMap [" + sWorkerThread.getThreadId() + ", " + Process.myTid() + "]", true);
1992dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    for (ItemInfo item: sBgItemsIdMap.values()) {
1993dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        long screenId = item.screenId;
1994933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                        Launcher.addDumpLog(TAG, "10249126 - \t" + item.container + ", " + screenId + " - " + unusedScreens.contains(screenId) + " | " + item, true);
1995dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1996dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1997dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                unusedScreens.contains(screenId)) {
1998dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            unusedScreens.remove(screenId);
1999933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                            Launcher.addDumpLog(TAG, "10249126 - \t\tRemoving " + screenId, true);
2000933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                            for (Long l : unusedScreens) {
2001933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                                Launcher.addDumpLog(TAG, "10249126 - \t\t\t unused screens: " + l, true);
2002933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                            }
2003dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        }
2004dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2005dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2006dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // If there are any empty screens remove them, and update.
2007dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (unusedScreens.size() != 0) {
2008933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                        // Dump the sBgWorkspaceScreens
2009933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                        Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - pre removeAll", true);
2010933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                        for (Long l : sBgWorkspaceScreens) {
2011933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                            Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
2012933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                        }
2013933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung
2014dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        sBgWorkspaceScreens.removeAll(unusedScreens);
20159e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
20169e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung                        // Dump the sBgWorkspaceScreens
2017933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                        Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - post removeAll", true);
20189e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung                        for (Long l : sBgWorkspaceScreens) {
20194caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                            Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
20209e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung                        }
2021933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung
2022dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2023dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2024dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
2025dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2026b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (DEBUG_LOADERS) {
2027b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2028b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Log.d(TAG, "workspace layout: ");
2029dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    int nScreens = occupied.size();
2030892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    for (int y = 0; y < countY; y++) {
2031b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        String line = "";
2032dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2033566da1026c33a68157bf9caf93d2071ad2870f46Daniel Sandler                        Iterator<Long> iter = occupied.keySet().iterator();
2034c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        while (iter.hasNext()) {
2035dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            long screenId = iter.next();
2036c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                            if (screenId > 0) {
2037b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                line += " | ";
2038b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            }
2039892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                            for (int x = 0; x < countX; x++) {
2040dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
20419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
20429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
2043b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Log.d(TAG, "[ " + line + " ]");
20449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
20459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
20463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2047c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            return loadedOldDb;
2048e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        }
2049e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
2050b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of items who are directly or indirectly (via another container) on the
2051b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * specified screen. */
2052b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void filterCurrentWorkspaceItems(int currentScreen,
2053b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> allWorkspaceItems,
2054b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> currentScreenItems,
2055b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> otherScreenItems) {
20562abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            // Purge any null ItemInfos
20572abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
20582abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            while (iter.hasNext()) {
20592abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                ItemInfo i = iter.next();
20602abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (i == null) {
20612abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                    iter.remove();
20622abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                }
20632abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            }
20642abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung
2065b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we aren't filtering on a screen, then the set of items to load is the full set of
2066b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // items given.
2067b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (currentScreen < 0) {
2068b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                currentScreenItems.addAll(allWorkspaceItems);
2069b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2070b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2071b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Order the set of items by their containers first, this allows use to walk through the
2072b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // list sequentially, build up a list of containers that are in the specified screen,
2073b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // as well as all items in those containers.
2074b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Set<Long> itemsOnScreen = new HashSet<Long>();
2075b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2076b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                @Override
2077b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                public int compare(ItemInfo lhs, ItemInfo rhs) {
2078b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    return (int) (lhs.container - rhs.container);
2079b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
2080b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            });
2081b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (ItemInfo info : allWorkspaceItems) {
2082b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2083dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (info.screenId == currentScreen) {
2084b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        currentScreenItems.add(info);
2085b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        itemsOnScreen.add(info.id);
2086b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    } else {
2087b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        otherScreenItems.add(info);
20883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
2089b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2090b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenItems.add(info);
2091b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    itemsOnScreen.add(info.id);
2092b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2093b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    if (itemsOnScreen.contains(info.container)) {
2094b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        currentScreenItems.add(info);
2095b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        itemsOnScreen.add(info.id);
2096b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    } else {
2097b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        otherScreenItems.add(info);
20985c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
20995c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
21003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2101b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
21025c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
2103b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of widgets which are on the specified screen. */
2104b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void filterCurrentAppWidgets(int currentScreen,
2105b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> appWidgets,
2106b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2107b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2108b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we aren't filtering on a screen, then the set of items to load is the full set of
2109b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // widgets given.
2110b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (currentScreen < 0) {
2111b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                currentScreenWidgets.addAll(appWidgets);
2112b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2113b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2114b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (LauncherAppWidgetInfo widget : appWidgets) {
21152abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (widget == null) continue;
2116b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2117dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        widget.screenId == currentScreen) {
2118b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenWidgets.add(widget);
2119b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2120b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherScreenWidgets.add(widget);
2121a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
21229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
21233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
212431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2125b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of folders which are on the specified screen. */
2126b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void filterCurrentFolders(int currentScreen,
2127b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, ItemInfo> itemsIdMap,
2128b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> folders,
2129b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> currentScreenFolders,
2130b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> otherScreenFolders) {
2131b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we aren't filtering on a screen, then the set of items to load is the full set of
2132b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // widgets given.
2133b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (currentScreen < 0) {
2134b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                currentScreenFolders.putAll(folders);
2135b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
21363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
2137b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (long id : folders.keySet()) {
2138b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ItemInfo info = itemsIdMap.get(id);
2139b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                FolderInfo folder = folders.get(id);
21402abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (info == null || folder == null) continue;
2141b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2142dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        info.screenId == currentScreen) {
2143b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenFolders.put(id, folder);
2144b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2145b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherScreenFolders.put(id, folder);
2146b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
21473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2148b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
214931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2150b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2151b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * right) */
2152b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
2153892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            final LauncherAppState app = LauncherAppState.getInstance();
2154892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
2155b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: review this
2156b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
2157db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                @Override
2158db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                public int compare(ItemInfo lhs, ItemInfo rhs) {
2159892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    int cellCountX = (int) grid.numColumns;
2160892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    int cellCountY = (int) grid.numRows;
2161db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int screenOffset = cellCountX * cellCountY;
2162db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
2163dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
2164db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                            lhs.cellY * cellCountX + lhs.cellX);
2165dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
2166db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                            rhs.cellY * cellCountX + rhs.cellX);
2167db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    return (int) (lr - rr);
2168db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                }
2169db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung            });
2170b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
2171db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung
2172dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2173dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                final ArrayList<Long> orderedScreens) {
21744caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            Launcher.addDumpLog(TAG, "10249126 - bindWorkspaceScreens()", true);
21759e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
21769e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung            // Dump the orderedScreens
2177ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            synchronized (sBgLock) {
2178ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                Launcher.addDumpLog(TAG, "10249126 - orderedScreens", true);
2179ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                for (Long l : sBgWorkspaceScreens) {
2180ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
2181ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                }
21829e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung            }
21839e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
2184dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final Runnable r = new Runnable() {
2185dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                @Override
2186dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                public void run() {
2187dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2188dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (callbacks != null) {
2189dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        callbacks.bindScreens(orderedScreens);
2190dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2191dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
2192dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            };
2193dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2194dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
2195dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2196b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void bindWorkspaceItems(final Callbacks oldCallbacks,
2197b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<ItemInfo> workspaceItems,
2198b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<LauncherAppWidgetInfo> appWidgets,
2199b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final HashMap<Long, FolderInfo> folders,
2200b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<Runnable> deferredBindRunnables) {
2201b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2202b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final boolean postOnMainThread = (deferredBindRunnables != null);
2203603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung
2204b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the workspace items
2205db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung            int N = workspaceItems.size();
2206b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (int i = 0; i < N; i += ITEMS_CHUNK) {
22073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
22083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
2209b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2210b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    @Override
22119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
2212c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
22139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
221464359a53dc827797917a8d9a0697a91996789801Winson Chung                            callbacks.bindItems(workspaceItems, start, start+chunkSize,
221564359a53dc827797917a8d9a0697a91996789801Winson Chung                                    false);
22169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
22179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
2218b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2219b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2220b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2221b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
222281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
22233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
2224b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2225b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2226b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the folders
2227b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (!folders.isEmpty()) {
2228b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2229b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    public void run() {
2230b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2231b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (callbacks != null) {
2232b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            callbacks.bindFolders(folders);
22339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
2234b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2235b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2236b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2237b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2238b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
223981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
22409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
22413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2242b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2243b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the widgets, one at a time
2244b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            N = appWidgets.size();
2245b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (int i = 0; i < N; i++) {
2246b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final LauncherAppWidgetInfo widget = appWidgets.get(i);
2247b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2248b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    public void run() {
2249b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2250b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (callbacks != null) {
2251b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            callbacks.bindAppWidget(widget);
22523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
2253b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2254b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2255b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2256b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2257b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
225881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
22593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
22603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2261b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
2262b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2263b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /**
2264b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * Binds all loaded data to actual views on the main thread.
2265b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         */
2266c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
2267ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            Launcher.addDumpLog(TAG, "10249126 - bindWorkspace(" + synchronizeBindPage + ", " + isUpgradePath + ")", true);
2268b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final long t = SystemClock.uptimeMillis();
2269b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Runnable r;
2270b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2271b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Don't use these two variables in any of the callback runnables.
2272b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Otherwise we hold a reference to them.
2273b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final Callbacks oldCallbacks = mCallbacks.get();
2274b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (oldCallbacks == null) {
2275b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // This launcher has exited and nobody bothered to tell us.  Just bail.
2276b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.w(TAG, "LoaderTask running with no launcher");
2277b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                return;
2278b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2279b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
22804a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
22814a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
2282b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                oldCallbacks.getCurrentWorkspaceScreen();
2283b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2284b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Load all the items that are on the current page first (and in the process, unbind
2285b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // all the existing workspace items before we call startBinding() below.
2286b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            unbindWorkspaceItemsOnMainThread();
2287b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2288b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> appWidgets =
2289b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2290b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2291b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
2292dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
22932abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
2294b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                workspaceItems.addAll(sBgWorkspaceItems);
2295b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                appWidgets.addAll(sBgAppWidgets);
2296b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                folders.putAll(sBgFolders);
2297b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                itemsIdMap.putAll(sBgItemsIdMap);
2298dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                orderedScreenIds.addAll(sBgWorkspaceScreens);
2299b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2300b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2301b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2302b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2303b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2304b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2305b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2306b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2307b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2308b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2309b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2310b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Separate the items that are on the current screen, and all the other remaining items
2311b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2312b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherWorkspaceItems);
2313b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2314b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherAppWidgets);
2315b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2316b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherFolders);
2317b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sortWorkspaceItemsSpatially(currentWorkspaceItems);
2318b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sortWorkspaceItemsSpatially(otherWorkspaceItems);
2319b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2320b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Tell the workspace that we're about to start binding items
2321b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r = new Runnable() {
23223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
23233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
23243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
2325b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        callbacks.startBinding();
23269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
23279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
2328b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
232981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung            runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2330b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2331dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2332dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2333b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Load items on the current page
2334b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2335b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentFolders, null);
23361462de39f01cec0ba785386532719cb0207dd827Adam Cohen            if (isLoadingSynchronously) {
23371462de39f01cec0ba785386532719cb0207dd827Adam Cohen                r = new Runnable() {
23381462de39f01cec0ba785386532719cb0207dd827Adam Cohen                    public void run() {
23391462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
23401462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        if (callbacks != null) {
23411462de39f01cec0ba785386532719cb0207dd827Adam Cohen                            callbacks.onPageBoundSynchronously(currentScreen);
23421462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        }
23431462de39f01cec0ba785386532719cb0207dd827Adam Cohen                    }
23441462de39f01cec0ba785386532719cb0207dd827Adam Cohen                };
234581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
23461462de39f01cec0ba785386532719cb0207dd827Adam Cohen            }
2347b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
23484a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            // Load all the remaining pages (if we are loading synchronously, we want to defer this
23494a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            // work until after the first render)
2350b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
2351b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
23524a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                    (isLoadingSynchronously ? mDeferredBindRunnables : null));
2353b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2354b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Tell the workspace that we're done binding items
2355b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r = new Runnable() {
23563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
2357b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2358b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    if (callbacks != null) {
2359c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        callbacks.finishBindingItems(isUpgradePath);
2360b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2361b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
236298e030be2abc03e90e9fd9f8bf67e2e01db39cc0Winson Chung                    // If we're profiling, ensure this is the last thing in the queue.
23633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
23643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
23653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
23669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
236736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
236836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung                    mIsLoadingAndBindingWorkspace = false;
23699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
2370b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
23714a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            if (isLoadingSynchronously) {
23724a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                mDeferredBindRunnables.add(r);
23734a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            } else {
237481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
23754a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            }
23763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
23773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
23783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
23793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
23803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
23813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
23823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
238364359a53dc827797917a8d9a0697a91996789801Winson Chung                loadAllApps();
238493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                synchronized (LoaderTask.this) {
238593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    if (mStopped) {
238693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                        return;
238793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    }
238893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    mAllAppsLoaded = true;
23893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
23903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
23913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
23923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
23933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
23943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
23953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
23963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
23973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
23983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
23993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
24003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
24013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
24023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
24033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
2404c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            @SuppressWarnings("unchecked")
2405eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> list
2406eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                    = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
2407c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            Runnable r = new Runnable() {
24083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
24093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
24103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
24113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
24123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
24139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
24143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
24153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2416a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
24179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
24183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
2419c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            };
2420c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
242164359a53dc827797917a8d9a0697a91996789801Winson Chung            if (isRunningOnMainThread) {
2422c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                r.run();
2423c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            } else {
2424c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                mHandler.post(r);
2425c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            }
24263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
24273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
242864359a53dc827797917a8d9a0697a91996789801Winson Chung        private void loadAllApps() {
242964359a53dc827797917a8d9a0697a91996789801Winson Chung            final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
24303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
24313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
24323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
24333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
243464359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
24353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
24369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
243731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
243864359a53dc827797917a8d9a0697a91996789801Winson Chung            final PackageManager packageManager = mContext.getPackageManager();
24393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
24403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2441cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
244264359a53dc827797917a8d9a0697a91996789801Winson Chung            // Clear the list of apps
244364359a53dc827797917a8d9a0697a91996789801Winson Chung            mBgAllAppsList.clear();
2444cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
244564359a53dc827797917a8d9a0697a91996789801Winson Chung            // Query for the set of apps
244664359a53dc827797917a8d9a0697a91996789801Winson Chung            final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
244764359a53dc827797917a8d9a0697a91996789801Winson Chung            List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
244864359a53dc827797917a8d9a0697a91996789801Winson Chung            if (DEBUG_LOADERS) {
244964359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "queryIntentActivities took "
245064359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis()-qiaTime) + "ms");
245164359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
245264359a53dc827797917a8d9a0697a91996789801Winson Chung            }
245364359a53dc827797917a8d9a0697a91996789801Winson Chung            // Fail if we don't have any apps
245464359a53dc827797917a8d9a0697a91996789801Winson Chung            if (apps == null || apps.isEmpty()) {
245564359a53dc827797917a8d9a0697a91996789801Winson Chung                return;
245664359a53dc827797917a8d9a0697a91996789801Winson Chung            }
245764359a53dc827797917a8d9a0697a91996789801Winson Chung            // Sort the applications by name
245864359a53dc827797917a8d9a0697a91996789801Winson Chung            final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
245964359a53dc827797917a8d9a0697a91996789801Winson Chung            Collections.sort(apps,
246064359a53dc827797917a8d9a0697a91996789801Winson Chung                    new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
246164359a53dc827797917a8d9a0697a91996789801Winson Chung            if (DEBUG_LOADERS) {
246264359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "sort took "
246364359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis()-sortTime) + "ms");
246464359a53dc827797917a8d9a0697a91996789801Winson Chung            }
24653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
246664359a53dc827797917a8d9a0697a91996789801Winson Chung            // Create the ApplicationInfos
246764359a53dc827797917a8d9a0697a91996789801Winson Chung            for (int i = 0; i < apps.size(); i++) {
246885f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert                ResolveInfo app = apps.get(i);
246985f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert                if (oldCallbacks.shouldShowApp(app)) {
247085f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert                    // This builds the icon bitmaps.
247185f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert                    mBgAllAppsList.add(new AppInfo(packageManager, app,
247285f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert                            mIconCache, mLabelCache));
247385f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert                }
247464359a53dc827797917a8d9a0697a91996789801Winson Chung            }
2475cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
247685f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert            // Huh? Shouldn't this be inside the Runnable below?
2477eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> added = mBgAllAppsList.added;
2478eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            mBgAllAppsList.added = new ArrayList<AppInfo>();
24793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
248064359a53dc827797917a8d9a0697a91996789801Winson Chung            // Post callback on main thread
248164359a53dc827797917a8d9a0697a91996789801Winson Chung            mHandler.post(new Runnable() {
248264359a53dc827797917a8d9a0697a91996789801Winson Chung                public void run() {
248364359a53dc827797917a8d9a0697a91996789801Winson Chung                    final long bindTime = SystemClock.uptimeMillis();
248411a1a53651924b544513f1f6971a735b18d67539Winson Chung                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
248564359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (callbacks != null) {
248664359a53dc827797917a8d9a0697a91996789801Winson Chung                        callbacks.bindAllApplications(added);
24873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
248864359a53dc827797917a8d9a0697a91996789801Winson Chung                            Log.d(TAG, "bound " + added.size() + " apps in "
248964359a53dc827797917a8d9a0697a91996789801Winson Chung                                + (SystemClock.uptimeMillis() - bindTime) + "ms");
24903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
249164359a53dc827797917a8d9a0697a91996789801Winson Chung                    } else {
249264359a53dc827797917a8d9a0697a91996789801Winson Chung                        Log.i(TAG, "not binding apps: no Launcher activity");
249364359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
24943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
249564359a53dc827797917a8d9a0697a91996789801Winson Chung            });
2496cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
24973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
249864359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "Icons processed in "
249964359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis() - loadTime) + "ms");
25003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
2502dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
25033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
25042abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
2505b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2506b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2507b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2508b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2509b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2510b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
25113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
25123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
251331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
25143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
2515700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(task);
25163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
2517d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
25183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
25193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
25203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
2521d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
25223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
25233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
25243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
25253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
25263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
252731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2528d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
25293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
25303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
25313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
25323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
2533d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
25343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
2535cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler            final Context context = mApp.getContext();
2536dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
25373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
25383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
25393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
25403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
25413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
25423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
2543487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.addPackage(context, packages[i]);
2544dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
25453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
25463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
25473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
25483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
2549487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.updatePackage(context, packages[i]);
2550d9cb4a124ac5c83080ace5ac92980df9c6f49e8eMichael Jurka                        WidgetPreviewLoader.removeFromDb(
2551e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                                mApp.getWidgetPreviewCacheDb(), packages[i]);
25523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
25533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
25543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
25553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
25563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
25573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2558487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.removePackage(packages[i]);
2559d9cb4a124ac5c83080ace5ac92980df9c6f49e8eMichael Jurka                        WidgetPreviewLoader.removeFromDb(
2560e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                                mApp.getWidgetPreviewCacheDb(), packages[i]);
25613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
25623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
25633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2564dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
2565eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            ArrayList<AppInfo> added = null;
2566eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            ArrayList<AppInfo> modified = null;
2567eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
25683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
2569487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            if (mBgAllAppsList.added.size() > 0) {
2570eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                added = new ArrayList<AppInfo>(mBgAllAppsList.added);
25715d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.added.clear();
25723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2573487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            if (mBgAllAppsList.modified.size() > 0) {
2574eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
25755d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.modified.clear();
25763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25775d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung            if (mBgAllAppsList.removed.size() > 0) {
257883892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                removedApps.addAll(mBgAllAppsList.removed);
25795d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.removed.clear();
2580cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung            }
2581cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung
25823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
25833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
25843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
25853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
2586be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
2587be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
25883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
258964359a53dc827797917a8d9a0697a91996789801Winson Chung                // Ensure that we add all the workspace applications to the db
2590997a92348a6d6e061737399321393449c16cd4d8Winson Chung                final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
259164359a53dc827797917a8d9a0697a91996789801Winson Chung                Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2592c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                addAndBindAddedApps(context, addedInfos, cb, added);
25933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
2595eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                final ArrayList<AppInfo> modifiedFinal = modified;
259664359a53dc827797917a8d9a0697a91996789801Winson Chung
259764359a53dc827797917a8d9a0697a91996789801Winson Chung                // Update the launcher db to reflect the changes
2598eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                for (AppInfo a : modifiedFinal) {
259964359a53dc827797917a8d9a0697a91996789801Winson Chung                    ArrayList<ItemInfo> infos =
260064359a53dc827797917a8d9a0697a91996789801Winson Chung                            getItemInfoForComponentName(a.componentName);
260164359a53dc827797917a8d9a0697a91996789801Winson Chung                    for (ItemInfo i : infos) {
260264359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (isShortcutInfoUpdateable(i)) {
260364359a53dc827797917a8d9a0697a91996789801Winson Chung                            ShortcutInfo info = (ShortcutInfo) i;
260464359a53dc827797917a8d9a0697a91996789801Winson Chung                            info.title = a.title.toString();
260564359a53dc827797917a8d9a0697a91996789801Winson Chung                            updateItemInDatabase(context, info);
260664359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
260764359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
260864359a53dc827797917a8d9a0697a91996789801Winson Chung                }
260964359a53dc827797917a8d9a0697a91996789801Winson Chung
26103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
26113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
2612cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2613cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        if (callbacks == cb && cb != null) {
26143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
26153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
26163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
26173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
26183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
261983892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            // If a package has been removed, or an app has been removed as a result of
262083892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            // an update (for example), make the removed callback.
262183892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
262264359a53dc827797917a8d9a0697a91996789801Winson Chung                final boolean packageRemoved = (mOp == OP_REMOVE);
262383892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                final ArrayList<String> removedPackageNames =
262483892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                        new ArrayList<String>(Arrays.asList(packages));
262583892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung
262664359a53dc827797917a8d9a0697a91996789801Winson Chung                // Update the launcher db to reflect the removal of apps
262764359a53dc827797917a8d9a0697a91996789801Winson Chung                if (packageRemoved) {
262864359a53dc827797917a8d9a0697a91996789801Winson Chung                    for (String pn : removedPackageNames) {
262964359a53dc827797917a8d9a0697a91996789801Winson Chung                        ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
263064359a53dc827797917a8d9a0697a91996789801Winson Chung                        for (ItemInfo i : infos) {
263164359a53dc827797917a8d9a0697a91996789801Winson Chung                            deleteItemFromDatabase(context, i);
263264359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
263364359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
263464359a53dc827797917a8d9a0697a91996789801Winson Chung                } else {
2635eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                    for (AppInfo a : removedApps) {
263664359a53dc827797917a8d9a0697a91996789801Winson Chung                        ArrayList<ItemInfo> infos =
263764359a53dc827797917a8d9a0697a91996789801Winson Chung                                getItemInfoForComponentName(a.componentName);
263864359a53dc827797917a8d9a0697a91996789801Winson Chung                        for (ItemInfo i : infos) {
263964359a53dc827797917a8d9a0697a91996789801Winson Chung                            deleteItemFromDatabase(context, i);
264064359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
264164359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
264264359a53dc827797917a8d9a0697a91996789801Winson Chung                }
264364359a53dc827797917a8d9a0697a91996789801Winson Chung
26443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
26453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
2646cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2647cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        if (callbacks == cb && cb != null) {
264883892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                            callbacks.bindComponentsRemoved(removedPackageNames,
264964359a53dc827797917a8d9a0697a91996789801Winson Chung                                    removedApps, packageRemoved);
26503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
26513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
26523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
2653be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
265480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
2655c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            final ArrayList<Object> widgetsAndShortcuts =
2656c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                getSortedWidgetsAndShortcuts(context);
265780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
265880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
265980baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
2660cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                    Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2661cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                    if (callbacks == cb && cb != null) {
2662c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                        callbacks.bindPackagesUpdated(widgetsAndShortcuts);
266380baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
266480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
26654caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            });
26664caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen
26674caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            // Write all the logs to disk
26684caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            Launcher.addDumpLog(TAG, "10249126 - PackageUpdatedTask - dumping logs to disk", true);
26694caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            mHandler.post(new Runnable() {
26704caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                public void run() {
26714caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
26724caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    if (callbacks == cb && cb != null) {
26734caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                        callbacks.dumpLogsToLocalData(false);
26744caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    }
26754caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                }
267680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
267731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
267831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
267931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2680c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2681c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2682c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        PackageManager packageManager = context.getPackageManager();
2683c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2684c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2685c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2686c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2687c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        Collections.sort(widgetsAndShortcuts,
2688c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2689c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        return widgetsAndShortcuts;
2690c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    }
2691c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka
26921323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung    private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
2693ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        if (cn == null) {
2694ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return false;
2695ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        }
2696ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung
2697ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        try {
2698ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            // Skip if the application is disabled
2699ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2700ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            if (!pi.applicationInfo.enabled) {
2701ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                return false;
2702ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            }
2703ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung
2704ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            // Check the activity
2705ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return (pm.getActivityInfo(cn, 0) != null);
2706ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        } catch (NameNotFoundException e) {
2707ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return false;
2708ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        }
2709ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung    }
2710ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung
27119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
271256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
271356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
27149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
271556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
2716c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        return getShortcutInfo(manager, intent, context, null, -1, -1, null);
271756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
271856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
271956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
272056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
272156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
272256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
272356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
272456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
2725c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
272656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
27271323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        final ShortcutInfo info = new ShortcutInfo();
272868fd3c33daf7a6d4de287f8691306a55fa54706cWinson Chung        if (componentName != null && !isValidPackageComponent(manager, componentName)) {
2729ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
27309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
27311323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        } else {
27321323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            try {
27331323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
27341323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                info.initFlagsAndFirstInstallTime(pi);
27351323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            } catch (NameNotFoundException e) {
27361323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                Log.d(TAG, "getPackInfo failed for package " +
27371323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                        componentName.getPackageName());
27381323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            }
273931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
274031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
27418ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
27428ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
27438ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
274456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
274556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
274656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
274756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
2748c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung
2749c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2750c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2751c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // via resolveActivity().
2752ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        Bitmap icon = null;
2753c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        ResolveInfo resolveInfo = null;
2754c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        ComponentName oldComponent = intent.getComponent();
2755c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        Intent newIntent = new Intent(intent.getAction(), null);
2756c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2757c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        newIntent.setPackage(oldComponent.getPackageName());
2758c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2759c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        for (ResolveInfo i : infos) {
2760c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            ComponentName cn = new ComponentName(i.activityInfo.packageName,
2761c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung                    i.activityInfo.name);
2762c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            if (cn.equals(oldComponent)) {
2763c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung                resolveInfo = i;
2764c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            }
2765c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        }
2766c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        if (resolveInfo == null) {
2767c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            resolveInfo = manager.resolveActivity(intent, 0);
2768c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        }
276956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
2770aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
277156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
277256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
277356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
277456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
2775931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                icon = getIconFromCursor(c, iconIndex, context);
277656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
27779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
277856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
277956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
278056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
278156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
278256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
278356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
278456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
278556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
278656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
27875308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
27885308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (labelCache != null && labelCache.containsKey(key)) {
27895308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                info.title = labelCache.get(key);
2790c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
2791c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                info.title = resolveInfo.activityInfo.loadLabel(manager);
2792c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                if (labelCache != null) {
27935308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                    labelCache.put(key, info.title);
2794c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                }
2795c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
279656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
279756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
279856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
279956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
280056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
280156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
280256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
280356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
28049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
280556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
28069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
28079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
28089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
28099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
281031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
281164359a53dc827797917a8d9a0697a91996789801Winson Chung    static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
281264359a53dc827797917a8d9a0697a91996789801Winson Chung            ItemInfoFilter f) {
281364359a53dc827797917a8d9a0697a91996789801Winson Chung        HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
281464359a53dc827797917a8d9a0697a91996789801Winson Chung        for (ItemInfo i : infos) {
281564359a53dc827797917a8d9a0697a91996789801Winson Chung            if (i instanceof ShortcutInfo) {
281664359a53dc827797917a8d9a0697a91996789801Winson Chung                ShortcutInfo info = (ShortcutInfo) i;
281764359a53dc827797917a8d9a0697a91996789801Winson Chung                ComponentName cn = info.intent.getComponent();
281864359a53dc827797917a8d9a0697a91996789801Winson Chung                if (cn != null && f.filterItem(null, info, cn)) {
281964359a53dc827797917a8d9a0697a91996789801Winson Chung                    filtered.add(info);
282064359a53dc827797917a8d9a0697a91996789801Winson Chung                }
282164359a53dc827797917a8d9a0697a91996789801Winson Chung            } else if (i instanceof FolderInfo) {
282264359a53dc827797917a8d9a0697a91996789801Winson Chung                FolderInfo info = (FolderInfo) i;
282364359a53dc827797917a8d9a0697a91996789801Winson Chung                for (ShortcutInfo s : info.contents) {
282464359a53dc827797917a8d9a0697a91996789801Winson Chung                    ComponentName cn = s.intent.getComponent();
282564359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (cn != null && f.filterItem(info, s, cn)) {
282664359a53dc827797917a8d9a0697a91996789801Winson Chung                        filtered.add(s);
28278a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung                    }
28288a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung                }
282964359a53dc827797917a8d9a0697a91996789801Winson Chung            } else if (i instanceof LauncherAppWidgetInfo) {
283064359a53dc827797917a8d9a0697a91996789801Winson Chung                LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
283164359a53dc827797917a8d9a0697a91996789801Winson Chung                ComponentName cn = info.providerName;
283264359a53dc827797917a8d9a0697a91996789801Winson Chung                if (cn != null && f.filterItem(null, info, cn)) {
283364359a53dc827797917a8d9a0697a91996789801Winson Chung                    filtered.add(info);
283464359a53dc827797917a8d9a0697a91996789801Winson Chung                }
28358a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung            }
28368a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung        }
283764359a53dc827797917a8d9a0697a91996789801Winson Chung        return new ArrayList<ItemInfo>(filtered);
283864359a53dc827797917a8d9a0697a91996789801Winson Chung    }
283964359a53dc827797917a8d9a0697a91996789801Winson Chung
284064359a53dc827797917a8d9a0697a91996789801Winson Chung    private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
284164359a53dc827797917a8d9a0697a91996789801Winson Chung        ItemInfoFilter filter  = new ItemInfoFilter() {
284264359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
284364359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
284464359a53dc827797917a8d9a0697a91996789801Winson Chung                return cn.getPackageName().equals(pn);
284564359a53dc827797917a8d9a0697a91996789801Winson Chung            }
284664359a53dc827797917a8d9a0697a91996789801Winson Chung        };
284764359a53dc827797917a8d9a0697a91996789801Winson Chung        return filterItemInfos(sBgItemsIdMap.values(), filter);
284864359a53dc827797917a8d9a0697a91996789801Winson Chung    }
284964359a53dc827797917a8d9a0697a91996789801Winson Chung
285064359a53dc827797917a8d9a0697a91996789801Winson Chung    private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
285164359a53dc827797917a8d9a0697a91996789801Winson Chung        ItemInfoFilter filter  = new ItemInfoFilter() {
285264359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
285364359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
285464359a53dc827797917a8d9a0697a91996789801Winson Chung                return cn.equals(cname);
285564359a53dc827797917a8d9a0697a91996789801Winson Chung            }
285664359a53dc827797917a8d9a0697a91996789801Winson Chung        };
285764359a53dc827797917a8d9a0697a91996789801Winson Chung        return filterItemInfos(sBgItemsIdMap.values(), filter);
285864359a53dc827797917a8d9a0697a91996789801Winson Chung    }
285964359a53dc827797917a8d9a0697a91996789801Winson Chung
286064359a53dc827797917a8d9a0697a91996789801Winson Chung    public static boolean isShortcutInfoUpdateable(ItemInfo i) {
286164359a53dc827797917a8d9a0697a91996789801Winson Chung        if (i instanceof ShortcutInfo) {
286264359a53dc827797917a8d9a0697a91996789801Winson Chung            ShortcutInfo info = (ShortcutInfo) i;
286364359a53dc827797917a8d9a0697a91996789801Winson Chung            // We need to check for ACTION_MAIN otherwise getComponent() might
286464359a53dc827797917a8d9a0697a91996789801Winson Chung            // return null for some shortcuts (for instance, for shortcuts to
286564359a53dc827797917a8d9a0697a91996789801Winson Chung            // web pages.)
286664359a53dc827797917a8d9a0697a91996789801Winson Chung            Intent intent = info.intent;
286764359a53dc827797917a8d9a0697a91996789801Winson Chung            ComponentName name = intent.getComponent();
286864359a53dc827797917a8d9a0697a91996789801Winson Chung            if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
286964359a53dc827797917a8d9a0697a91996789801Winson Chung                    Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
287064359a53dc827797917a8d9a0697a91996789801Winson Chung                return true;
287164359a53dc827797917a8d9a0697a91996789801Winson Chung            }
287264359a53dc827797917a8d9a0697a91996789801Winson Chung        }
287364359a53dc827797917a8d9a0697a91996789801Winson Chung        return false;
28748a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung    }
28758a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung
28768a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung    /**
28770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
28789c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
28790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
288056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
288156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
288231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
288356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
2884c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ShortcutInfo info = new ShortcutInfo();
28859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
28867376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
28878ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
28888ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
288956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
289056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
28919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
28929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
28939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
28949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
28959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
28969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
289756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
289856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
28999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
29009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
290156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
290256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
2903c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
2904c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
290556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
29069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
290756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
290856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
290956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
291056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
2911931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                icon = getIconFromCursor(c, iconIndex, context);
291256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
291356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
291456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
291556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
291656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
29177376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
29189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
29199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
2920931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            icon = getIconFromCursor(c, iconIndex, context);
292156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
292256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
292356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
292456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
292556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
292656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
29279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
29289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
29299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
2930d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
293156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
29329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
29339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
293431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
2935d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
29369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
2937f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
293831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2939931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka    Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
29403a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        @SuppressWarnings("all") // suppress dead code warning
29413a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        final boolean debug = false;
29423a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        if (debug) {
294356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
294456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
294556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
294656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
294756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
2948931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            return Utilities.createIconBitmap(
2949931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                    BitmapFactory.decodeByteArray(data, 0, data.length), context);
295056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
295156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
295256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
295356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
295456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
29553d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
29563d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellX, int cellY, boolean notify) {
2957a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
2958d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (info == null) {
2959d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return null;
2960d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
29613d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
29620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
29630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
29640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
29650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
2966a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    /**
296755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     * Attempts to find an AppWidgetProviderInfo that matches the given component.
296855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     */
296955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
297055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            ComponentName component) {
297155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        List<AppWidgetProviderInfo> widgets =
297255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
297355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        for (AppWidgetProviderInfo info : widgets) {
297455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            if (info.provider.equals(component)) {
297555cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                return info;
297655cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            }
297755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        }
297855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        return null;
2979a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
2980a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
298168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    /**
298268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     * Returns a list of all the widgets that can handle configuration with a particular mimeType.
298368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     */
298468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
298568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final PackageManager packageManager = context.getPackageManager();
298668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
298768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new ArrayList<WidgetMimeTypeHandlerData>();
298868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
298968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final Intent supportsIntent =
299068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
299168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        supportsIntent.setType(mimeType);
299268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
299368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Create a set of widget configuration components that we can test against
299468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<AppWidgetProviderInfo> widgets =
299568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
299668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
299768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new HashMap<ComponentName, AppWidgetProviderInfo>();
299868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (AppWidgetProviderInfo info : widgets) {
299968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            configurationComponentToWidget.put(info.configure, info);
300068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
300168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
300268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Run through each of the intents that can handle this type of clip data, and cross
300368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // reference them with the components that are actual configuration components
300468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
300568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                PackageManager.MATCH_DEFAULT_ONLY);
300668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (ResolveInfo info : activities) {
300768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ActivityInfo activityInfo = info.activityInfo;
300868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
300968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                    activityInfo.name);
301068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            if (configurationComponentToWidget.containsKey(infoComponent)) {
301168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                supportedConfigurationActivities.add(
301268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
301368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                                configurationComponentToWidget.get(infoComponent)));
301468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            }
301568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
301668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        return supportedConfigurationActivities;
301768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    }
301868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
3019a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
30200589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
30210589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
30220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
30230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3024d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (intent == null) {
3025d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3026d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3027d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return null;
3028d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
3029d919882006a457d16dea46bd546472e6534b5c19Adam Cohen
30300589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
30310589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
30320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
30330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
30340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
30350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
30360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
30370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
30380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
30390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
30400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
30410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
30420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
30430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
30440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
30450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
3046c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
3047c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
30480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
30490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
30500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
30510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
30520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
30530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3054c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ShortcutInfo info = new ShortcutInfo();
305556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
30560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
3057a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            if (fallbackIcon != null) {
3058a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = fallbackIcon;
3059a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            } else {
3060a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = getFallbackIcon();
3061a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                info.usingFallbackIcon = true;
3062a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            }
30630589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
30640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
306556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
30660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
30670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
30680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
30690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
30700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
30710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
30720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
30730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3074aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3075aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            int iconIndex) {
307617a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        // If apps can't be on SD, don't even bother.
3077ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        if (!mAppsCanBeOnRemoveableStorage) {
3078aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            return false;
307917a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        }
308056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
308156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
308256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
308356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
308456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
308556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
3086ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
3087aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            cache.put(info, c.getBlob(iconIndex));
3088aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            return true;
3089aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        }
3090aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        return false;
3091aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    }
3092aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3093aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        boolean needSave = false;
3094aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        try {
3095aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            if (data != null) {
3096aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3097aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                Bitmap loaded = info.getIcon(mIconCache);
3098aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                needSave = !saved.sameAs(loaded);
3099aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            } else {
310056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
310156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
3102aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        } catch (Exception e) {
3103aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            needSave = true;
3104aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        }
3105aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        if (needSave) {
3106aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            Log.d(TAG, "going to save icon bitmap for info=" + info);
3107aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // This is slower than is ideal, but this only happens once
3108aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // or when the app is updated with a new icon.
3109aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            updateItemInDatabase(context, info);
311056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
311156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
311256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
311331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
3114df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     * Return an existing FolderInfo object if we have encountered this ID previously,
31159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
311631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
3117df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
31189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
31199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
3120df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        if (folderInfo == null) {
31219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
3122c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            folderInfo = new FolderInfo();
31239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
312431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
3125df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        return folderInfo;
312631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
312731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3128eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka    public static final Comparator<AppInfo> getAppNameComparator() {
31291190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        final Collator collator = Collator.getInstance();
3130eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        return new Comparator<AppInfo>() {
3131eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            public final int compare(AppInfo a, AppInfo b) {
31321190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                int result = collator.compare(a.title.toString(), b.title.toString());
31331190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                if (result == 0) {
31341190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                    result = a.componentName.compareTo(b.componentName);
31351190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                }
31361190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                return result;
31375b1808da75ed3e3528cb31dee56a4e363812cc21Michael Jurka            }
31381190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        };
31391190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    }
3140eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka    public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3141eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            = new Comparator<AppInfo>() {
3142eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        public final int compare(AppInfo a, AppInfo b) {
314378403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime < b.firstInstallTime) return 1;
314478403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime > b.firstInstallTime) return -1;
314578403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            return 0;
314678403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung        }
314778403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung    };
31481190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
31491190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        final Collator collator = Collator.getInstance();
31501190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        return new Comparator<AppWidgetProviderInfo>() {
31511190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
31521190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                return collator.compare(a.label.toString(), b.label.toString());
31531190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            }
31541190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        };
31551190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    }
31565308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung    static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
31575308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        if (info.activityInfo != null) {
31585308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
31595308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        } else {
31605308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
31615308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        }
31625308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung    }
3163785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
31641190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        private Collator mCollator;
3165785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        private PackageManager mPackageManager;
3166c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        private HashMap<Object, CharSequence> mLabelCache;
3167785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        ShortcutNameComparator(PackageManager pm) {
3168785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mPackageManager = pm;
3169c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = new HashMap<Object, CharSequence>();
31701190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
3171c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        }
3172c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3173c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mPackageManager = pm;
3174c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = labelCache;
31751190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
3176785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3177785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        public final int compare(ResolveInfo a, ResolveInfo b) {
3178c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            CharSequence labelA, labelB;
31795308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
31805308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
31815308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (mLabelCache.containsKey(keyA)) {
31825308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                labelA = mLabelCache.get(keyA);
3183c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3184c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = a.loadLabel(mPackageManager).toString();
3185c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung
31865308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                mLabelCache.put(keyA, labelA);
3187c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
31885308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (mLabelCache.containsKey(keyB)) {
31895308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                labelB = mLabelCache.get(keyB);
3190c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3191c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = b.loadLabel(mPackageManager).toString();
3192c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung
31935308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                mLabelCache.put(keyB, labelB);
3194c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
31951190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            return mCollator.compare(labelA, labelB);
3196785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3197785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    };
31981ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung    public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
31991190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        private Collator mCollator;
32001ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        private PackageManager mPackageManager;
32011ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        private HashMap<Object, String> mLabelCache;
32021ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        WidgetAndShortcutNameComparator(PackageManager pm) {
32031ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            mPackageManager = pm;
32041ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            mLabelCache = new HashMap<Object, String>();
32051190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
32061ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        }
32071ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        public final int compare(Object a, Object b) {
32081ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            String labelA, labelB;
3209c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            if (mLabelCache.containsKey(a)) {
3210c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = mLabelCache.get(a);
3211c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3212c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = (a instanceof AppWidgetProviderInfo) ?
32131ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((AppWidgetProviderInfo) a).label :
32141ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((ResolveInfo) a).loadLabel(mPackageManager).toString();
3215c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                mLabelCache.put(a, labelA);
3216c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
3217c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            if (mLabelCache.containsKey(b)) {
3218c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = mLabelCache.get(b);
3219c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3220c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = (b instanceof AppWidgetProviderInfo) ?
32211ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((AppWidgetProviderInfo) b).label :
32221ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((ResolveInfo) b).loadLabel(mPackageManager).toString();
3223c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                mLabelCache.put(b, labelB);
3224c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
32251190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            return mCollator.compare(labelA, labelB);
32261ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        }
32271ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung    };
3228be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
3229be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
3230be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
3231eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3232eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3233eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3234eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
32353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
32363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
32373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
32383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
32393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
3240be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
324131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
3242