LauncherModel.java revision d64d1763c590b1c954b759156683abf39ff22945
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
703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
71ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung    private final boolean mAppsCanBeOnRemoveableStorage;
72dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
73cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler    private final LauncherAppState mApp;
749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
77b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private boolean mIsLoaderTaskRunning;
78c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    private volatile boolean mFlushingWorkerThread;
799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
8081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    // Specific runnable types that are run on the main thread deferred handler, this allows us to
8181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    // clear all queued binding runnables when the Launcher activity is destroyed.
8281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
8381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
8481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
8581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
86700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
87700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    static {
88700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorkerThread.start();
89700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    }
90700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
91700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick
92cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
93cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
94cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
95cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
96cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
97cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
98b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // When we are loading pages synchronously, we can't just post the binding of items on the side
99b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // pages as this delays the rotation process.  Instead, we wait for a callback from the first
100b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // draw (in Workspace) to initiate the binding of the remaining side pages.  Any time we start
101b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // a normal load, we also clear this set of Runnables.
102b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
103b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
1059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
106a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // < only access in worker thread >
107487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    private AllAppsList mBgAllAppsList;
108a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
109b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // The lock that must be acquired before referencing any static bg data structures.  Unlike
110b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // other locks, this one can generally be held long-term because we never expect any of these
111b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // static data structures to be referenced outside of the worker thread except on the first
112b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // load after configuration change.
1132abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung    static final Object sBgLock = new Object();
114b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
115487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
116a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // LauncherModel to their ids
117487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
118a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
119487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
120487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    //       created by LauncherModel that are directly on the home screen (however, no widgets or
121487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    //       shortcuts within folders).
122487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
1230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
124487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
125487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
126a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        new ArrayList<LauncherAppWidgetInfo>();
127a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
128487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
129487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
130b1094bd9630f659cc606853b8a244b6a6b6c6943Winson Chung
131487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
132487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
133dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
134dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    // sBgWorkspaceScreens is the ordered set of workspace screens.
135dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
136dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
137a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // </ only access in worker thread >
138a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
139a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    private IconCache mIconCache;
1400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
1419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
142d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountX;
143d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static int mCellCountY;
144aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
14599a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee    protected int mPreviousConfigMcc;
14693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
148ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        public boolean setLoadOnResume();
1499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
1509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
15164359a53dc827797917a8d9a0697a91996789801Winson Chung        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
15264359a53dc827797917a8d9a0697a91996789801Winson Chung                              boolean forceAnimateIcons);
153dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        public void bindScreens(ArrayList<Long> orderedScreenIds);
15464359a53dc827797917a8d9a0697a91996789801Winson Chung        public void bindAddScreens(ArrayList<Long> orderedScreenIds);
155ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
156e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        public void finishBindingItems(boolean upgradePath);
1579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
1589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAllApplications(ArrayList<ApplicationInfo> apps);
159d64d1763c590b1c954b759156683abf39ff22945Winson Chung        public void bindAppsAdded(ArrayList<Long> newScreens,
160d64d1763c590b1c954b759156683abf39ff22945Winson Chung                                  ArrayList<ItemInfo> addNotAnimated,
161d64d1763c590b1c954b759156683abf39ff22945Winson Chung                                  ArrayList<ItemInfo> addAnimated);
16264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
16383892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        public void bindComponentsRemoved(ArrayList<String> packageNames,
16483892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                        ArrayList<ApplicationInfo> appInfos,
16583892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                        boolean matchPackageNamesOnly);
166c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
167cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath        public void bindSearchablesChanged();
1681462de39f01cec0ba785386532719cb0207dd827Adam Cohen        public void onPageBoundSynchronously(int page);
1699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
17031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17164359a53dc827797917a8d9a0697a91996789801Winson Chung    public interface ItemInfoFilter {
17264359a53dc827797917a8d9a0697a91996789801Winson Chung        public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
17364359a53dc827797917a8d9a0697a91996789801Winson Chung    }
17464359a53dc827797917a8d9a0697a91996789801Winson Chung
175e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler    LauncherModel(LauncherAppState app, IconCache iconCache) {
176e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler        final Context context = app.getContext();
177e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler
178ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
179e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler        mApp = app;
180487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        mBgAllAppsList = new AllAppsList(iconCache);
1810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
1820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
1830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mDefaultIcon = Utilities.createIconBitmap(
184cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler                mIconCache.getFullResDefaultActivityIcon(), context);
1852ff10b3b0137d258569ce5471808842e88696133Daniel Sandler
186cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler        final Resources res = context.getResources();
18799a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee        Configuration config = res.getConfiguration();
18899a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee        mPreviousConfigMcc = config.mcc;
1890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
1900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
191b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Runs the specified runnable immediately if called from the main thread, otherwise it is
192b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung     * posted on the main thread handler. */
193b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private void runOnMainThread(Runnable r) {
19481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        runOnMainThread(r, 0);
19581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    }
19681b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private void runOnMainThread(Runnable r, int type) {
197b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
198b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we are on the worker thread, post onto the main handler
199b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mHandler.post(r);
200b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        } else {
201b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r.run();
202b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
203b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
204b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
205b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
206b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung     * posted on the worker thread handler. */
207b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private static void runOnWorkerThread(Runnable r) {
208b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
209b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r.run();
210b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        } else {
211b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we are not on the worker thread, then post to the worker handler
212b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sWorker.post(r);
213b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
214b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
215b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
216c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
217c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                                 long screen) {
218c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        final int xCount = LauncherModel.getCellCountX();
219c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        final int yCount = LauncherModel.getCellCountY();
220c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        boolean[][] occupied = new boolean[xCount][yCount];
221c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
222c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        int cellX, cellY, spanX, spanY;
223c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        for (int i = 0; i < items.size(); ++i) {
224c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            final ItemInfo item = items.get(i);
225c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
226c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                if (item.screenId == screen) {
227c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    cellX = item.cellX;
228c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    cellY = item.cellY;
229c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    spanX = item.spanX;
230c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    spanY = item.spanY;
231c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
232c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
233c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                            occupied[x][y] = true;
234c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        }
235c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    }
236c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                }
237c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            }
238c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        }
239c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
240c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
241c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    }
242c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
243156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung                                                        Intent launchIntent,
24476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                                        int firstScreenIndex,
24576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                                        ArrayList<Long> workspaceScreens) {
246c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        // Lock on the app so that we don't try and get the items while apps are being added
247c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        LauncherAppState app = LauncherAppState.getInstance();
248c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        LauncherModel model = app.getModel();
249c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        boolean found = false;
250c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        synchronized (app) {
25164359a53dc827797917a8d9a0697a91996789801Winson Chung            if (sWorkerThread.getThreadId() != Process.myTid()) {
25264359a53dc827797917a8d9a0697a91996789801Winson Chung                // Flush the LauncherModel worker thread, so that if we just did another
25364359a53dc827797917a8d9a0697a91996789801Winson Chung                // processInstallShortcut, we give it time for its shortcut to get added to the
25464359a53dc827797917a8d9a0697a91996789801Winson Chung                // database (getItemsInLocalCoordinates reads the database)
25564359a53dc827797917a8d9a0697a91996789801Winson Chung                model.flushWorkerThread();
25664359a53dc827797917a8d9a0697a91996789801Winson Chung            }
257c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
258c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
259c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            // Try adding to the workspace screens incrementally, starting at the default or center
260c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
26176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
26276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            int count = workspaceScreens.size();
263156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung            for (int screen = firstScreenIndex; screen < count && !found; screen++) {
264c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                int[] tmpCoordinates = new int[2];
265c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
26676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                        workspaceScreens.get(screen))) {
267c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    // Update the Launcher db
26876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
269c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                }
270c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            }
271c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        }
272c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        return null;
273c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    }
274c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
275997a92348a6d6e061737399321393449c16cd4d8Winson Chung    public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added) {
276997a92348a6d6e061737399321393449c16cd4d8Winson Chung        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
277997a92348a6d6e061737399321393449c16cd4d8Winson Chung        addAndBindAddedApps(context, added, cb);
278997a92348a6d6e061737399321393449c16cd4d8Winson Chung    }
279997a92348a6d6e061737399321393449c16cd4d8Winson Chung    public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
28064359a53dc827797917a8d9a0697a91996789801Winson Chung                                    final Callbacks callbacks) {
281d64d1763c590b1c954b759156683abf39ff22945Winson Chung        Log.w(TAG, "10249126 - addAndBindAddedApps()");
282997a92348a6d6e061737399321393449c16cd4d8Winson Chung        if (added.isEmpty()) {
283997a92348a6d6e061737399321393449c16cd4d8Winson Chung            throw new RuntimeException("EMPTY ADDED ARRAY?");
284997a92348a6d6e061737399321393449c16cd4d8Winson Chung        }
28564359a53dc827797917a8d9a0697a91996789801Winson Chung        // Process the newly added applications and add them to the database first
28664359a53dc827797917a8d9a0697a91996789801Winson Chung        Runnable r = new Runnable() {
28764359a53dc827797917a8d9a0697a91996789801Winson Chung            public void run() {
28864359a53dc827797917a8d9a0697a91996789801Winson Chung                final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
28964359a53dc827797917a8d9a0697a91996789801Winson Chung                final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
29064359a53dc827797917a8d9a0697a91996789801Winson Chung
29176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // Get the list of workspace screens.  We need to append to this list and
29276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
29376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // called.
29476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                ArrayList<Long> workspaceScreens = new ArrayList<Long>();
29576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
29676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                for (Integer i : orderedScreens.keySet()) {
29776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = orderedScreens.get(i);
29876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    workspaceScreens.add(screenId);
29976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                }
30076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
30164359a53dc827797917a8d9a0697a91996789801Winson Chung                synchronized(sBgLock) {
302997a92348a6d6e061737399321393449c16cd4d8Winson Chung                    Iterator<ItemInfo> iter = added.iterator();
30364359a53dc827797917a8d9a0697a91996789801Winson Chung                    while (iter.hasNext()) {
304997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        ItemInfo a = iter.next();
30564359a53dc827797917a8d9a0697a91996789801Winson Chung                        final String name = a.title.toString();
306997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        final Intent launchIntent = a.getIntent();
30764359a53dc827797917a8d9a0697a91996789801Winson Chung
30864359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Short-circuit this logic if the icon exists somewhere on the workspace
30964359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (LauncherModel.shortcutExists(context, name, launchIntent)) {
31064359a53dc827797917a8d9a0697a91996789801Winson Chung                            continue;
31164359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
31264359a53dc827797917a8d9a0697a91996789801Winson Chung
31364359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Add this icon to the db, creating a new page if necessary
314156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung                        int startSearchPageIndex = 1;
31564359a53dc827797917a8d9a0697a91996789801Winson Chung                        Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
31676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                name, launchIntent, startSearchPageIndex, workspaceScreens);
31764359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (coords == null) {
31864359a53dc827797917a8d9a0697a91996789801Winson Chung                            LauncherAppState appState = LauncherAppState.getInstance();
31964359a53dc827797917a8d9a0697a91996789801Winson Chung                            LauncherProvider lp = appState.getLauncherProvider();
320c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
321c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // If we can't find a valid position, then just add a new screen.
322c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // This takes time so we need to re-queue the add until the new
323c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // page is added.  Create as many screens as necessary to satisfy
324c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // the startSearchPageIndex.
325c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
32676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                    workspaceScreens.size());
327c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            while (numPagesToAdd > 0) {
328c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                long screenId = lp.generateNewScreenId();
32976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                Log.w(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")");
330c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                // Save the screen id for binding in the workspace
33176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                workspaceScreens.add(screenId);
332c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                addedWorkspaceScreensFinal.add(screenId);
333c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                numPagesToAdd--;
334c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            }
33576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
33664359a53dc827797917a8d9a0697a91996789801Winson Chung                            // Find the coordinate again
33764359a53dc827797917a8d9a0697a91996789801Winson Chung                            coords = LauncherModel.findNextAvailableIconSpace(context,
33876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                    name, launchIntent, startSearchPageIndex, workspaceScreens);
33964359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
34064359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (coords == null) {
34164359a53dc827797917a8d9a0697a91996789801Winson Chung                            throw new RuntimeException("Coordinates should not be null");
34264359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
34364359a53dc827797917a8d9a0697a91996789801Winson Chung
344997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        ShortcutInfo shortcutInfo;
345997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        if (a instanceof ShortcutInfo) {
346997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            shortcutInfo = (ShortcutInfo) a;
347997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        } else if (a instanceof ApplicationInfo) {
348997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            shortcutInfo = ((ApplicationInfo) a).makeShortcut();
349997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        } else {
350997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            throw new RuntimeException("Unexpected info type");
351997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        }
35264359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Add the shortcut to the db
35364359a53dc827797917a8d9a0697a91996789801Winson Chung                        addItemToDatabase(context, shortcutInfo,
35464359a53dc827797917a8d9a0697a91996789801Winson Chung                                LauncherSettings.Favorites.CONTAINER_DESKTOP,
35564359a53dc827797917a8d9a0697a91996789801Winson Chung                                coords.first, coords.second[0], coords.second[1], false);
35664359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Save the ShortcutInfo for binding in the workspace
35764359a53dc827797917a8d9a0697a91996789801Winson Chung                        addedShortcutsFinal.add(shortcutInfo);
35864359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
35964359a53dc827797917a8d9a0697a91996789801Winson Chung                }
36064359a53dc827797917a8d9a0697a91996789801Winson Chung
361d64d1763c590b1c954b759156683abf39ff22945Winson Chung                Log.w(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")");
362d64d1763c590b1c954b759156683abf39ff22945Winson Chung
36376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // Update the workspace screens
36476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                updateWorkspaceScreenOrder(context, workspaceScreens);
36576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
366997a92348a6d6e061737399321393449c16cd4d8Winson Chung                if (!addedShortcutsFinal.isEmpty()) {
367997a92348a6d6e061737399321393449c16cd4d8Winson Chung                    runOnMainThread(new Runnable() {
368997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        public void run() {
369997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
370997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            if (callbacks == cb && cb != null) {
371997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
372997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                long lastScreenId = info.screenId;
373997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
374997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
375997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                for (ItemInfo i : addedShortcutsFinal) {
376997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                    if (i.screenId == lastScreenId) {
377997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                        addAnimated.add(i);
378997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                    } else {
379997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                        addNotAnimated.add(i);
380997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                    }
381997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                }
382d64d1763c590b1c954b759156683abf39ff22945Winson Chung                                callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
383d64d1763c590b1c954b759156683abf39ff22945Winson Chung                                        addNotAnimated, addAnimated);
384997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            }
38564359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
386997a92348a6d6e061737399321393449c16cd4d8Winson Chung                    });
387997a92348a6d6e061737399321393449c16cd4d8Winson Chung                }
38864359a53dc827797917a8d9a0697a91996789801Winson Chung            }
38964359a53dc827797917a8d9a0697a91996789801Winson Chung        };
39064359a53dc827797917a8d9a0697a91996789801Winson Chung        runOnWorkerThread(r);
39164359a53dc827797917a8d9a0697a91996789801Winson Chung    }
39264359a53dc827797917a8d9a0697a91996789801Winson Chung
39356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
3940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
395f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
39631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
39781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    public void unbindItemInfosAndClearQueuedBindRunnables() {
39881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
39981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung            throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
40081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    "main thread");
40181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        }
40281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
40381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Clear any deferred bind runnables
40481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        mDeferredBindRunnables.clear();
40581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Remove any queued bind runnables
40681b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
40781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Unbind all the workspace items
40881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        unbindWorkspaceItemsOnMainThread();
409603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung    }
410603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung
411b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
41281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    void unbindWorkspaceItemsOnMainThread() {
413603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        // Ensure that we don't use the same workspace items data structure on the main thread
414603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        // by making a copy of workspace items first.
415b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
416b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
4172abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung        synchronized (sBgLock) {
418b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            tmpWorkspaceItems.addAll(sBgWorkspaceItems);
419b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            tmpAppWidgets.addAll(sBgAppWidgets);
420b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
421b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        Runnable r = new Runnable() {
422b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                @Override
423b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                public void run() {
424b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   for (ItemInfo item : tmpWorkspaceItems) {
425b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                       item.unbind();
426b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   }
427b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   for (ItemInfo item : tmpAppWidgets) {
428b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                       item.unbind();
429b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   }
430b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
431b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
432b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnMainThread(r);
4334eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen    }
4344eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen
4359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
4369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
4379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
4389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
4399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
440dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long screenId, int cellX, int cellY) {
4419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
4429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
443dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
4449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
4459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
446dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            moveItemInDatabase(context, item, container, screenId, cellX, cellY);
4479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
4489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
44931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
450b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka    static void checkItemInfoLocked(
451b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
452b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        ItemInfo modelItem = sBgItemsIdMap.get(itemId);
453b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        if (modelItem != null && item != modelItem) {
454b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // check all the data is consistent
455b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
456b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
457b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                ShortcutInfo shortcut = (ShortcutInfo) item;
458b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
459b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.intent.filterEquals(shortcut.intent) &&
460b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.id == shortcut.id &&
461b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.itemType == shortcut.itemType &&
462b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.container == shortcut.container &&
463dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        modelShortcut.screenId == shortcut.screenId &&
464b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.cellX == shortcut.cellX &&
465b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.cellY == shortcut.cellY &&
466b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.spanX == shortcut.spanX &&
467b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.spanY == shortcut.spanY &&
468b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
469b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        (modelShortcut.dropPos != null &&
470b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                                shortcut.dropPos != null &&
471b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                                modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
472b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
473b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    // For all intents and purposes, this is the same object
474b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    return;
475b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                }
476b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
477b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka
478b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // the modelItem needs to match up perfectly with item if our model is
479b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // to be consistent with the database-- for now, just require
480b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // modelItem == item or the equality check above
481b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            String msg = "item: " + ((item != null) ? item.toString() : "null") +
482b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    "modelItem: " +
483b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    ((modelItem != null) ? modelItem.toString() : "null") +
484b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    "Error: ItemInfo passed to checkItemInfo doesn't match original";
485b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            RuntimeException e = new RuntimeException(msg);
486b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            if (stackTrace != null) {
487b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                e.setStackTrace(stackTrace);
488b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
489e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen            // TODO: something breaks this in the upgrade path
490e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen            //throw e;
491b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        }
492b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka    }
493b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka
494816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka    static void checkItemInfo(final ItemInfo item) {
495816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
496816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        final long itemId = item.id;
497816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        Runnable r = new Runnable() {
498b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            public void run() {
499b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                synchronized (sBgLock) {
500b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    checkItemInfoLocked(itemId, item, stackTrace);
501816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka                }
502b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
503b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        };
504b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
505816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka    }
506816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka
507c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void updateItemInDatabaseHelper(Context context, final ContentValues values,
508c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            final ItemInfo item, final String callingFunction) {
509c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final long itemId = item.id;
510c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
511c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ContentResolver cr = context.getContentResolver();
512c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
513487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
514c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
515c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            public void run() {
516c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.update(uri, values, null, null);
517f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                updateItemArrays(item, itemId, stackTrace);
518f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
519f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        };
520f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        runOnWorkerThread(r);
521f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
522c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
523f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
524f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final ArrayList<ItemInfo> items, final String callingFunction) {
525f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        final ContentResolver cr = context.getContentResolver();
526c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
527f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
528f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        Runnable r = new Runnable() {
529f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            public void run() {
530f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                ArrayList<ContentProviderOperation> ops =
531f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        new ArrayList<ContentProviderOperation>();
532f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                int count = items.size();
533f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                for (int i = 0; i < count; i++) {
534f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ItemInfo item = items.get(i);
535f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    final long itemId = item.id;
536f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
537f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ContentValues values = valuesList.get(i);
538f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
539f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
540f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    updateItemArrays(item, itemId, stackTrace);
541487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
542f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
543f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                try {
544f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    cr.applyBatch(LauncherProvider.AUTHORITY, ops);
545f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                } catch (Exception e) {
546f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    e.printStackTrace();
547c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                }
548c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            }
549c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
550b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
551c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    }
552bebf042666cffe52039b875a549a582abd78a431Adam Cohen
553f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
554f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        // Lock on mBgLock *after* the db operation
555f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        synchronized (sBgLock) {
556f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            checkItemInfoLocked(itemId, item, stackTrace);
557f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
558f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
559f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
560f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                // Item is in a folder, make sure this folder exists
561f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                if (!sBgFolders.containsKey(item.container)) {
562f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    // An items container is being set to a that of an item which is not in
563f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    // the list of Folders.
564f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    String msg = "item: " + item + " container being set to: " +
565f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                            item.container + ", not in the list of folders";
566f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    Log.e(TAG, msg);
567f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    Launcher.dumpDebugLogsToConsole();
568f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
569f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
570f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
571f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // Items are added/removed from the corresponding FolderInfo elsewhere, such
572f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // as in Workspace.onDrop. Here, we just add/remove them from the list of items
573f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // that are on the desktop, as appropriate
574f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo modelItem = sBgItemsIdMap.get(itemId);
575f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
576f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
577f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                switch (modelItem.itemType) {
578f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
579f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
580f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
581f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        if (!sBgWorkspaceItems.contains(modelItem)) {
582f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                            sBgWorkspaceItems.add(modelItem);
583f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        }
584f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        break;
585f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    default:
586f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        break;
587f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
588f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            } else {
589f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                sBgWorkspaceItems.remove(modelItem);
590f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
591f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
592f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
593f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
594c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    public void flushWorkerThread() {
595c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        mFlushingWorkerThread = true;
596c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        Runnable waiter = new Runnable() {
597c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                public void run() {
598c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    synchronized (this) {
599c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        notifyAll();
600c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        mFlushingWorkerThread = false;
601c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    }
602c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
603c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            };
604c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka
605c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        synchronized(waiter) {
606c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            runOnWorkerThread(waiter);
607c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            if (mLoaderTask != null) {
608c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                synchronized(mLoaderTask) {
609c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    mLoaderTask.notify();
610c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
611c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            }
612c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            boolean success = false;
613c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            while (!success) {
614c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                try {
615c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    waiter.wait();
616c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    success = true;
617c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                } catch (InterruptedException e) {
618c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
619c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            }
620c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        }
621c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    }
622c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka
6239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
6249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
6259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
626a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
627dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY) {
6285524b4957fc7c1c858037327429411efdb8fe7d6Brian Muramatsu        String transaction = "DbDebug    Modify item (" + item.title + ") in db, id: " + item.id +
629dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
630dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
631487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Launcher.sDumpLogs.add(transaction);
632487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Log.d(TAG, transaction);
6339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
6349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
6359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
636c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
6373d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // We store hotseat items in canonical form which is this orientation invariant position
6383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // in the hotseat
639dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
6403d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
641dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
6423d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
643dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
6443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
64531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
6469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
6479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
6483d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
6493d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
650dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
651ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
652c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
65331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
65431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
65531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
656f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
657f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     * cellX, cellY have already been updated on the ItemInfos.
658f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     */
659f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
660f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final long container, final int screen) {
661f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
662f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
663f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        int count = items.size();
664f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
665f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        for (int i = 0; i < count; i++) {
666f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo item = items.get(i);
667f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            String transaction = "DbDebug    Modify item (" + item.title + ") in db, id: "
668dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
669f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
670f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    + item.cellX + ", " + item.cellY + ")";
671f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            Launcher.sDumpLogs.add(transaction);
672f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            item.container = container;
673f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
674f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // We store hotseat items in canonical form which is this orientation invariant position
675f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // in the hotseat
676f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (context instanceof Launcher && screen < 0 &&
677f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
678dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
679f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        item.cellY);
680f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            } else {
681dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = screen;
682f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
683f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
684f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final ContentValues values = new ContentValues();
685f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CONTAINER, item.container);
686f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CELLX, item.cellX);
687f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CELLY, item.cellY);
688dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
689f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
690f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            contentValues.add(values);
691f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
692f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
693f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
694f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
695f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    /**
696bebf042666cffe52039b875a549a582abd78a431Adam Cohen     * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
697d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen     */
698bebf042666cffe52039b875a549a582abd78a431Adam Cohen    static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
699dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
7005524b4957fc7c1c858037327429411efdb8fe7d6Brian Muramatsu        String transaction = "DbDebug    Modify item (" + item.title + ") in db, id: " + item.id +
701dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
702dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
703487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Launcher.sDumpLogs.add(transaction);
704487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Log.d(TAG, transaction);
705d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellX = cellX;
706d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellY = cellY;
707bebf042666cffe52039b875a549a582abd78a431Adam Cohen        item.spanX = spanX;
708bebf042666cffe52039b875a549a582abd78a431Adam Cohen        item.spanY = spanY;
709bebf042666cffe52039b875a549a582abd78a431Adam Cohen
710bebf042666cffe52039b875a549a582abd78a431Adam Cohen        // We store hotseat items in canonical form which is this orientation invariant position
711bebf042666cffe52039b875a549a582abd78a431Adam Cohen        // in the hotseat
712dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
713bebf042666cffe52039b875a549a582abd78a431Adam Cohen                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
714dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
715bebf042666cffe52039b875a549a582abd78a431Adam Cohen        } else {
716dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
717bebf042666cffe52039b875a549a582abd78a431Adam Cohen        }
718d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
719d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        final ContentValues values = new ContentValues();
720d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
721bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
722bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
723bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.SPANX, item.spanX);
724bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.SPANY, item.spanY);
725dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
726d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
727816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
728bebf042666cffe52039b875a549a582abd78a431Adam Cohen    }
729c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
730c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    /**
731c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * Update an item to the database in a specified container.
732c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     */
733c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void updateItemInDatabase(Context context, final ItemInfo item) {
734c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ContentValues values = new ContentValues();
735c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        item.onAddToDatabase(values);
736c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
737c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
738d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    }
739d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
740d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    /**
7419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
7429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
743bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
7449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
7459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
7469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
7479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
7489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
7499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
7509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
7519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
7529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
7539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
7549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
7559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
756bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
757bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
758bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
759aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
760aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
761aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
762aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
763aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
764aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
765aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
766aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
767aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
768aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
769aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
770aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
771aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
772aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
773aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
774aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
775aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
776aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
777aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
778aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
779aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
780c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                ItemInfo item = new ItemInfo();
781aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
782aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
783aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanX = c.getInt(spanXIndex);
784aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.spanY = c.getInt(spanYIndex);
785aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
786aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
787dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = c.getInt(screenIndex);
788aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
789aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
790aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
791aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
792aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
793aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
794aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
795aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
796aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
797aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
798aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
799aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
800aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
8019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
80231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
8039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
8049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
8059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
8069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
8079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
808df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
809f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
8119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
8129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
8139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
8149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
8159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
8169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
8179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
818f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
8199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
8209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
821df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
822df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        folderInfo = findOrMakeFolder(folderList, id);
8239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
8249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
82531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
8279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
8289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
829dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                folderInfo.screenId = c.getInt(screenIndex);
830d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
831d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
832ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
8339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
8349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
8359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
8369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
8372fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
838ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
8399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
8409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
84131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
8439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
8449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
8459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
8463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    static void addItemToDatabase(Context context, final ItemInfo item, final long container,
847dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY, final boolean notify) {
8489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
8499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
8509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
8513d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // We store hotseat items in canonical form which is this orientation invariant position
8523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // in the hotseat
853dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
8543d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
855dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
8563d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
857dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
8583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
859ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
8609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
8619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
8629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
863ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
864cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler        LauncherAppState app = LauncherAppState.getInstance();
865dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        item.id = app.getLauncherProvider().generateNewItemId();
866a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        values.put(LauncherSettings.Favorites._ID, item.id);
8673d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
868aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
869c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
870a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            public void run() {
871487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                String transaction = "DbDebug    Add item (" + item.title + ") to db, id: "
872dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
873487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        + cellY + ")";
874487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                Launcher.sDumpLogs.add(transaction);
875487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                Log.d(TAG, transaction);
876487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
877a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
878a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                        LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
879ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
880b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
8812abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
882b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    checkItemInfoLocked(item.id, item, null);
883b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.put(item.id, item);
884b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    switch (item.itemType) {
885b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
886b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgFolders.put(item.id, (FolderInfo) item);
887b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            // Fall through
888b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
889b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
890b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
891b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
892b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgWorkspaceItems.add(item);
893b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            } else {
894b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!sBgFolders.containsKey(item.container)) {
895b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // Adding an item to a folder that doesn't exist.
896b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String msg = "adding item: " + item + " to a folder that " +
897b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            " doesn't exist";
89828b3e10fdea4d719b46d2e0da9a916bacc238fd1Adam Cohen                                    Log.e(TAG, msg);
899b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Launcher.dumpDebugLogsToConsole();
900b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
901487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                            }
902b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
903b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
904b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgAppWidgets.add((LauncherAppWidgetInfo) item);
905b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
906b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
907a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                }
908a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            }
909c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
910b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
91131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
91231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
914aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
915aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
916845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka    static int getCellLayoutChildId(
917dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
9183d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return (((int) container & 0xFF) << 24)
919dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
920aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
921aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
922d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountX() {
923d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountX;
924aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
925aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
926d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    static int getCellCountY() {
927d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        return mCellCountY;
928aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
929aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
930aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
931aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Updates the model orientation helper to take into account the current layout dimensions
932aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * when performing local/canonical coordinate transformations.
933aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
934aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
935d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountX = shortAxisCellCount;
936d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        mCellCountY = longAxisCellCount;
937aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
938aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
939aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
940c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * Removes the specified item from the database
941c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * @param context
942c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * @param item
9439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
944c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void deleteItemFromDatabase(Context context, final ItemInfo item) {
9459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
946c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
947487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
94883df188ce0822f4058406e5f2878d1cf34bf413eMichael Jurka        Runnable r = new Runnable() {
949a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            public void run() {
950487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                String transaction = "DbDebug    Delete item (" + item.title + ") from db, id: "
951dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
952487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        ", " + item.cellY + ")";
953487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                Launcher.sDumpLogs.add(transaction);
954487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                Log.d(TAG, transaction);
955487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
956c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(uriToDelete, null, null);
957b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
958b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
9592abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
960b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    switch (item.itemType) {
961b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
962b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgFolders.remove(item.id);
963b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            for (ItemInfo info: sBgItemsIdMap.values()) {
964b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (info.container == item.id) {
965b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // We are deleting a folder which still contains items that
966b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // think they are contained by that folder.
967b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String msg = "deleting a folder (" + item + ") which still " +
968b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            "contains items (" + info + ")";
96928b3e10fdea4d719b46d2e0da9a916bacc238fd1Adam Cohen                                    Log.e(TAG, msg);
970b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Launcher.dumpDebugLogsToConsole();
971b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
972487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                            }
973b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgWorkspaceItems.remove(item);
974b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
975b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
976b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
977b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgWorkspaceItems.remove(item);
978b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
979b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
980b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
981b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
982b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
983b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.remove(item.id);
984b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgDbIconCache.remove(item);
985a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                }
986a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            }
98783df188ce0822f4058406e5f2878d1cf34bf413eMichael Jurka        };
988b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
98931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
99031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
992dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * Update the order of the workspace screens in the database. The array list contains
993dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * a list of screen ids in the order that they should appear.
994dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     */
995c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
99664359a53dc827797917a8d9a0697a91996789801Winson Chung        final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
997dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final ContentResolver cr = context.getContentResolver();
998dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
999dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1000dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        // Remove any negative screen ids -- these aren't persisted
100164359a53dc827797917a8d9a0697a91996789801Winson Chung        Iterator<Long> iter = screensCopy.iterator();
1002dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        while (iter.hasNext()) {
1003dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long id = iter.next();
1004dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (id < 0) {
1005dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                iter.remove();
1006dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1007dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
1008dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1009dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        Runnable r = new Runnable() {
1010dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            @Override
1011dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            public void run() {
1012dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                // Clear the table
1013dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                cr.delete(uri, null, null);
101476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                int count = screensCopy.size();
1015dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                ContentValues[] values = new ContentValues[count];
1016dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                for (int i = 0; i < count; i++) {
1017dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    ContentValues v = new ContentValues();
101876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = screensCopy.get(i);
1019dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1020dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
102176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    Log.w(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")");
1022dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    values[i] = v;
1023dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
1024dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                cr.bulkInsert(uri, values);
1025dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                sBgWorkspaceScreens.clear();
1026dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                sBgWorkspaceScreens.addAll(screensCopy);
1027dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1028dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        };
1029dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        runOnWorkerThread(r);
1030dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
1031dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1032dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    /**
10339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
10349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1035a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
10369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1037f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1038c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
1039c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            public void run() {
1040c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
1041b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
10422abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1043b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.remove(info.id);
1044b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgFolders.remove(info.id);
1045b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgDbIconCache.remove(info);
1046b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgWorkspaceItems.remove(info);
1047b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1048c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
1049c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1050c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                        LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
1051b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
10522abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1053b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (ItemInfo childInfo : info.contents) {
1054b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        sBgItemsIdMap.remove(childInfo.id);
1055b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        sBgDbIconCache.remove(childInfo);
1056b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1057afb01ee74243cede19088e694ca82cea5983c603Adam Cohen                }
1058c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            }
1059c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
1060b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
10619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
1062f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
10639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
10649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
10659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
10669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
10679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
10689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
1069f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
1070f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
1071f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
10721d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
10739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
10749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
10759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1076cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    @Override
1077f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
10783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
1079aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
10803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
10819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
10823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
10833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
10843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
10853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
10863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1087f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
10883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
108964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
10903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
10913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
10923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
10933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
109456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
10953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
10963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
10973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
10983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
10993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
110056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
11013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
11023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
11033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
11043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
11053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
11063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
11073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
110856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
11093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
111056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
11113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
11123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1113f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
1114f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
11153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
1116cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // First, schedule to add these apps back in.
1117cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1118cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1119cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            // Then, rebind everything.
1120e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            startLoaderFromBackground();
11213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
11223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
11233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            enqueuePackageUpdated(new PackageUpdatedTask(
11243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        PackageUpdatedTask.OP_UNAVAILABLE, packages));
1125e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
112693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // If we have changed locale we need to clear out the labels in all apps/workspace.
112793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            forceReload();
112893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
112993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // Check if configuration change was an mcc/mnc change which would affect app resources
113093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // and we would need to clear out the labels in all apps/workspace. Same handling as
113193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // above for ACTION_LOCALE_CHANGED
113293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             Configuration currentConfig = context.getResources().getConfiguration();
113399a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee             if (mPreviousConfigMcc != currentConfig.mcc) {
113493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                   Log.d(TAG, "Reload apps on config change. curr_mcc:"
113599a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee                       + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
113693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                   forceReload();
113793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             }
113893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // Update previousConfig
113999a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee             mPreviousConfigMcc = currentConfig.mcc;
1140cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1141cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung                   SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
1142ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka            if (mCallbacks != null) {
1143ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                Callbacks callbacks = mCallbacks.get();
1144ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                if (callbacks != null) {
1145ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                    callbacks.bindSearchablesChanged();
1146ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                }
1147cfdf7ee64db8820d91a1cd82bf7b961fb2083dceWinson Chung            }
1148e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
1149e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    }
1150e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato
115193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    private void forceReload() {
1152f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        resetLoadedState(true, true);
1153f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1154f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Do this here because if the launcher activity is running it will be restarted.
1155f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // If it's not running startLoaderFromBackground will merely tell it that it needs
1156f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // to reload.
1157f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        startLoaderFromBackground();
1158f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    }
1159f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1160f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
116193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        synchronized (mLock) {
116293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // Stop any existing loaders first, so they don't set mAllAppsLoaded or
116393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // mWorkspaceLoaded to true later
116493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            stopLoaderLocked();
1165f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            if (resetAllAppsLoaded) mAllAppsLoaded = false;
1166f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
116793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        }
116893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    }
116993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1170e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    /**
1171e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * When the launcher is in the background, it's possible for it to miss paired
1172e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * configuration changes.  So whenever we trigger the loader from the background
1173e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * tell the launcher that it needs to re-run the loader when it comes back instead
1174e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * of doing it now.
1175e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     */
1176e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    public void startLoaderFromBackground() {
1177e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        boolean runLoader = false;
1178e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (mCallbacks != null) {
1179e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            Callbacks callbacks = mCallbacks.get();
1180e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            if (callbacks != null) {
1181e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                // Only actually run the loader if they're not paused.
1182e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                if (!callbacks.setLoadOnResume()) {
1183e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                    runLoader = true;
1184e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                }
1185e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            }
1186e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
1187e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (runLoader) {
1188b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            startLoader(false, -1);
1189f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1191f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
119293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    // If there is already a loader task running, tell it to stop.
119393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    // returns true if isLaunching() was true on the old task
119493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    private boolean stopLoaderLocked() {
119593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        boolean isLaunching = false;
119693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        LoaderTask oldTask = mLoaderTask;
119793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        if (oldTask != null) {
119893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            if (oldTask.isLaunching()) {
119993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                isLaunching = true;
120093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            }
120193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            oldTask.stopLocked();
120293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        }
120393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        return isLaunching;
120493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    }
120593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1206b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    public void startLoader(boolean isLaunching, int synchronousBindPage) {
12073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
12083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
12093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
12103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1211843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
1212b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Clear any deferred bind-runnables from the synchronized load process
1213b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // We must do this before any loading/binding is scheduled below.
1214b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
1215b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
12163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
12173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
121993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                // also, don't downgrade isLaunching if we're already running
122093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                isLaunching = isLaunching || stopLoaderLocked();
1221cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler                mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
1222b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1223b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1224b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
1225b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1226b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sWorker.post(mLoaderTask);
1227b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1228b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1229b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
1230b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
1231b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1232b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    void bindRemainingSynchronousPages() {
1233b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        // Post the remaining side pages to be loaded
1234b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (!mDeferredBindRunnables.isEmpty()) {
1235b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (final Runnable r : mDeferredBindRunnables) {
123681b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
1237b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
1238b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
1239b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
12403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1241b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
12423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
12433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
12443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
12453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
12469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
1247f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
12483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1249b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
125076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    /** Loads the workspace screens db into a map of Rank -> ScreenId */
125176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
125276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final ContentResolver contentResolver = context.getContentResolver();
125376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
125476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
125576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
125676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
125776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        try {
125876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            final int idIndex = sc.getColumnIndexOrThrow(
125976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    LauncherSettings.WorkspaceScreens._ID);
126076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            final int rankIndex = sc.getColumnIndexOrThrow(
126176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    LauncherSettings.WorkspaceScreens.SCREEN_RANK);
126276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            while (sc.moveToNext()) {
126376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                try {
126476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = sc.getLong(idIndex);
126576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    int rank = sc.getInt(rankIndex);
126676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
126776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    Log.w(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")");
126876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
126976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    orderedScreens.put(rank, screenId);
127076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                } catch (Exception e) {
127176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    Log.w(TAG, "Desktop items loading interrupted - invalid screens: ", e);
127276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                }
127376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            }
127476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        } finally {
127576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            sc.close();
127676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        }
127776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        return orderedScreens;
127876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    }
127976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
1280c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    public boolean isAllAppsLoaded() {
1281c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        return mAllAppsLoaded;
1282c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    }
1283c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka
128436a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    boolean isLoadingWorkspace() {
128536a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        synchronized (mLock) {
128636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            if (mLoaderTask != null) {
128736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung                return mLoaderTask.isLoadingWorkspace();
128836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            }
128936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        }
129036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        return false;
129136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    }
129236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
12933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
12943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
12953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
12963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
12973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
12983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
12993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
13003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
13013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
130236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        private boolean mIsLoadingAndBindingWorkspace;
13033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
13043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
1305b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1306c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        private HashMap<Object, CharSequence> mLabelCache;
13073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
13083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        LoaderTask(Context context, boolean isLaunching) {
13093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
13103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
1311c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = new HashMap<Object, CharSequence>();
13123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1313f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
13143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
13153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
13163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1317f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
131836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        boolean isLoadingWorkspace() {
131936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            return mIsLoadingAndBindingWorkspace;
132036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        }
132136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
1322c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        /** Returns whether this is an upgrade path */
1323c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private boolean loadAndBindWorkspace() {
132436a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            mIsLoadingAndBindingWorkspace = true;
132536a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
13263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
13273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
13283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
13293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1330288a36b563801a26d00084cb823435a3ba4cb76cMichael Jurka
1331c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean isUpgradePath = false;
1332a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            if (!mWorkspaceLoaded) {
1333c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                isUpgradePath = loadWorkspace();
133493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                synchronized (LoaderTask.this) {
133593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    if (mStopped) {
1336c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        return isUpgradePath;
133793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    }
133893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    mWorkspaceLoaded = true;
13399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
13403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1341cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
13423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
1343c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            bindWorkspace(-1, isUpgradePath);
1344c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            return isUpgradePath;
13453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1346f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
13473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
13483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
13493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
13503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
13513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
13523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1353843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
13543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
13553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
13563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
13573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
13583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
13593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
13609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
13613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
13629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
13639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
13643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
13653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1366c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
13673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
1368c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        // Just in case mFlushingWorkerThread changes but we aren't woken up,
1369c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        // wait no longer than 1sec at a time
1370c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        this.wait(1000);
13713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
13723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
13739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
13743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
13753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
13763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
1377aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
13783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
13799c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
1380843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
13813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1382f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1383b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        void runBindSynchronousPage(int synchronousBindPage) {
1384b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (synchronousBindPage < 0) {
1385b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Ensure that we have a valid page index to load synchronously
1386b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1387b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        "valid page index");
1388b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1389b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1390b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Ensure that we don't try and bind a specified page when the pages have not been
1391b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // loaded already (we should load everything asynchronously in that case)
1392b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1393b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1394b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            synchronized (mLock) {
1395b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (mIsLoaderTaskRunning) {
1396b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // Ensure that we are never running the background loading at this point since
1397b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // we also touch the background collections
1398b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    throw new RuntimeException("Error! Background loading is already running");
1399b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1400b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1401b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1402b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: Throw an exception if we are already loading (since we touch the worker thread
1403b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      data structures, we can't allow any other thread to touch that data, but because
1404b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      this call is synchronous, we can get away with not locking).
1405b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1406cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler            // The LauncherModel is static in the LauncherAppState and mHandler may have queued
1407a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            // operations from the previous activity.  We need to ensure that all queued operations
1408a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            // are executed before any synchronous binding work is done.
1409a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            mHandler.flush();
1410a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen
1411b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Divide the set of loaded items into those that we are binding synchronously, and
1412b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // everything else that is to be bound normally (asynchronously).
1413c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            bindWorkspace(synchronousBindPage, false);
1414b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: For now, continue posting the binding of AllApps as there are other issues that
1415b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      arise from that.
1416b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            onlyBindAllApps();
1417b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
1418b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
14193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
1420c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean isUpgrade = false;
1421c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1422b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            synchronized (mLock) {
1423b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                mIsLoaderTaskRunning = true;
1424b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
14253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
14263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
14273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
14283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks cbk = mCallbacks.get();
14293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
1430843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
1431843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
1432dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
1433aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                    if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1434aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                            (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
1435843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
1436843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1437dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
143864359a53dc827797917a8d9a0697a91996789801Winson Chung                if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1439c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                isUpgrade = loadAndBindWorkspace();
1440843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
14413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
14423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
14433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
14443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
14453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
14463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
14479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
1448843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
1449aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                        if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
1450843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
14519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
14529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
14533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
1454f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1455843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
145664359a53dc827797917a8d9a0697a91996789801Winson Chung                if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
145764359a53dc827797917a8d9a0697a91996789801Winson Chung                loadAndBindAllApps();
14587ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung
14597ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                // Restore the default thread priority after we are done loading items
14607ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                synchronized (mLock) {
14617ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                    android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
14627ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                }
14633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1464843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
1465aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // Update the saved icons if necessary
1466aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
14672abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
1468b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                for (Object key : sBgDbIconCache.keySet()) {
1469b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1470b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1471b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgDbIconCache.clear();
1472aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            }
1473aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung
1474c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Ensure that all the applications that are in the system are represented on the home
1475c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // screen.
147676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            Log.w(TAG, "10249126 - verifyApplications(" + isUpgrade + ")");
1477c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            if (!isUpgrade) {
1478c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                verifyApplications();
1479c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1480c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
14813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
14823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
14833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
1484f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
14853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
14863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
14873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
14883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
1489843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
1490b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                mIsLoaderTaskRunning = false;
14913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
14923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1493f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
14943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
14953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
14963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
14973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
1498f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
14993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1500f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
15013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
15023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
15033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
15043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
15053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
15063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
15073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
15083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
15093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
15103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
15113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
15123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1513ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
15143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
15153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
15163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1517f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
15183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
15193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
15203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
15213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
15223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
15233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
15243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
15259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
15263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
15273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
1528f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
15293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1530f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1531c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private void verifyApplications() {
1532c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            final Context context = mApp.getContext();
1533c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1534c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Cross reference all the applications in our apps list with items in the workspace
1535c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            ArrayList<ItemInfo> tmpInfos;
1536695ff6b6fada4e1037592d7c4961321e12890b7dMichael Jurka            ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
1537c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            synchronized (sBgLock) {
1538c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                for (ApplicationInfo app : mBgAllAppsList.data) {
1539c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    tmpInfos = getItemInfoForComponentName(app.componentName);
154076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    Log.w(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty());
1541c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    if (tmpInfos.isEmpty()) {
1542c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        // We are missing an application icon, so add this to the workspace
1543c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        added.add(app);
1544c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        // This is a rare event, so lets log it
1545c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        Log.e(TAG, "Missing Application on load: " + app);
1546c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    }
1547c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                }
1548c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1549c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            if (!added.isEmpty()) {
1550c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1551c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                addAndBindAddedApps(context, added, cb);
1552c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1553c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        }
1554c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
15555f8afe6280eae34620067696173e71943e1a30a3Winson Chung        private boolean checkItemDimensions(ItemInfo info) {
15565f8afe6280eae34620067696173e71943e1a30a3Winson Chung            return (info.cellX + info.spanX) > mCellCountX ||
15575f8afe6280eae34620067696173e71943e1a30a3Winson Chung                    (info.cellY + info.spanY) > mCellCountY;
15585f8afe6280eae34620067696173e71943e1a30a3Winson Chung        }
15595f8afe6280eae34620067696173e71943e1a30a3Winson Chung
15603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
1561dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
1562dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long containerIndex = item.screenId;
1563f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1564dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1565dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1566dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            [(int) item.screenId][0] != null) {
1567dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        Log.e(TAG, "Error loading shortcut into hotseat " + item
1568dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + " into position (" + item.screenId + ":" + item.cellX + ","
1569dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + item.cellY + ") occupied by "
1570dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1571dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                [(int) item.screenId][0]);
1572dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            return false;
1573dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
15746ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                } else {
1575dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1576dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    items[(int) item.screenId][0] = item;
1577dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
15786ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                    return true;
15796ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                }
1580f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung            } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1581f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung                // Skip further checking if it is not the hotseat or workspace container
15823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
15833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1584f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung
1585dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (!occupied.containsKey(item.screenId)) {
1586dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1587dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                occupied.put(item.screenId, items);
1588dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1589dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1590dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            ItemInfo[][] screens = occupied.get(item.screenId);
15916ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung            // Check if any workspace icons overlap with each other
15923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
15933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
1594dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (screens[x][y] != null) {
15953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
1596dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            + " into cell (" + containerIndex + "-" + item.screenId + ":"
15973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
1598aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
1599dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            + screens[x][y]);
16003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
16018802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
16028802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
16033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
16043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
16053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
1606dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    screens[x][y] = item;
16078802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
16088802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
1609f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung
16103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
16113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
16128802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
1613c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        /** Returns whether this is an upgradge path */
1614c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private boolean loadWorkspace() {
16153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1616f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
16173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
16183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
16193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
16203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
16213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
1622f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1623b85f8a44b51258f22938773ca30dd85845345010Michael Jurka            // Make sure the default workspace is loaded, if needed
1624c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1625e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
1626c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Check if we need to do any upgrade-path logic
1627c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean loadedOldDb = mApp.getLauncherProvider().justLoadedOldDb();
1628b85f8a44b51258f22938773ca30dd85845345010Michael Jurka
16292abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
1630b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgWorkspaceItems.clear();
1631b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgAppWidgets.clear();
1632b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgFolders.clear();
1633b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgItemsIdMap.clear();
1634b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgDbIconCache.clear();
1635dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                sBgWorkspaceScreens.clear();
163676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                Log.w(TAG, "10249126 - loadWorkspace()");
1637b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1638b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
1639c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
1640e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                final Cursor c = contentResolver.query(contentUri, null, null, null, null);
1641b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1642b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // +1 for the hotseat (it can be larger than the workspace)
1643b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Load workspace in reverse order to ensure that latest items are loaded first (and
1644b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // before any earlier duplicates)
1645dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
1646b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1647b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                try {
1648b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1649b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int intentIndex = c.getColumnIndexOrThrow
1650b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.INTENT);
1651b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int titleIndex = c.getColumnIndexOrThrow
1652b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.TITLE);
1653b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconTypeIndex = c.getColumnIndexOrThrow(
1654b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_TYPE);
1655b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1656b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconPackageIndex = c.getColumnIndexOrThrow(
1657b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_PACKAGE);
1658b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconResourceIndex = c.getColumnIndexOrThrow(
1659b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_RESOURCE);
1660b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int containerIndex = c.getColumnIndexOrThrow(
1661b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.CONTAINER);
1662b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int itemTypeIndex = c.getColumnIndexOrThrow(
1663b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ITEM_TYPE);
1664b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1665b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.APPWIDGET_ID);
1666b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int screenIndex = c.getColumnIndexOrThrow(
1667b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.SCREEN);
1668b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int cellXIndex = c.getColumnIndexOrThrow
1669b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.CELLX);
1670b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int cellYIndex = c.getColumnIndexOrThrow
1671b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.CELLY);
1672b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int spanXIndex = c.getColumnIndexOrThrow
1673b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.SPANX);
1674b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int spanYIndex = c.getColumnIndexOrThrow(
1675b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.SPANY);
1676b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1677b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //final int displayModeIndex = c.getColumnIndexOrThrow(
1678b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //        LauncherSettings.Favorites.DISPLAY_MODE);
1679b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1680b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    ShortcutInfo info;
1681b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    String intentDescription;
1682b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    LauncherAppWidgetInfo appWidgetInfo;
1683b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    int container;
1684b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    long id;
1685b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Intent intent;
1686b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1687b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    while (!mStopped && c.moveToNext()) {
1688b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        try {
1689b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            int itemType = c.getInt(itemTypeIndex);
1690b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1691b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            switch (itemType) {
1692b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1693b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1694ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                id = c.getLong(idIndex);
1695b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                intentDescription = c.getString(intentIndex);
1696b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                try {
1697b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    intent = Intent.parseUri(intentDescription, 0);
1698ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    ComponentName cn = intent.getComponent();
16991323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                    if (!isValidPackageComponent(manager, cn)) {
1700ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        if (!mAppsCanBeOnRemoveableStorage) {
17011323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            // Log the invalid package, and remove it from the db
17021323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            Uri uri = LauncherSettings.Favorites.getContentUri(id,
17031323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                                    false);
1704ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                            contentResolver.delete(uri, null, null);
17051323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            Log.e(TAG, "Invalid package removed: " + cn);
1706ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        } else {
17071323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            // If apps can be on external storage, then we just
17081323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            // leave them for the user to remove (maybe add
17091323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            // visual treatment to it)
17101323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                            Log.e(TAG, "Invalid package found: " + cn);
1711ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        }
1712ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        continue;
1713ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    }
1714b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } catch (URISyntaxException e) {
1715b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    continue;
1716b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1717968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka
1718b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1719b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info = getShortcutInfo(manager, intent, context, c, iconIndex,
1720b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            titleIndex, mLabelCache);
1721b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } else {
1722b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info = getShortcutInfo(c, context, iconTypeIndex,
1723b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            iconPackageIndex, iconResourceIndex, iconIndex,
1724b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            titleIndex);
1725b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1726b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // App shortcuts that used to be automatically added to Launcher
1727b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // didn't always have the correct intent flags set, so do that
1728b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // here
1729b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (intent.getAction() != null &&
17309ad005639e1698c94674cdfadf3ae342fb56c9b5Michael Jurka                                        intent.getCategories() != null &&
17319ad005639e1698c94674cdfadf3ae342fb56c9b5Michael Jurka                                        intent.getAction().equals(Intent.ACTION_MAIN) &&
1732968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka                                        intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
1733b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        intent.addFlags(
1734b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            Intent.FLAG_ACTIVITY_NEW_TASK |
1735b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1736b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1737968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka                                }
17383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1739b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (info != null) {
1740ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    info.id = id;
1741b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.intent = intent;
1742b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    container = c.getInt(containerIndex);
1743b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.container = container;
1744dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                    info.screenId = c.getInt(screenIndex);
1745b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.cellX = c.getInt(cellXIndex);
1746b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.cellY = c.getInt(cellYIndex);
17475f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    info.spanX = 1;
17485f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    info.spanY = 1;
17495f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    // Skip loading items that are out of bounds
17505f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
17515f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        if (checkItemDimensions(info)) {
17525f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                            Log.d(TAG, "Skipped loading out of bounds shortcut: "
17535f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                                    + info.intent);
17545f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                            continue;
17555f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        }
17565f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    }
1757b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // check & update map of what's occupied
1758b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (!checkItemPlacement(occupied, info)) {
1759b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1760b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1761b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1762b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    switch (container) {
1763b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1764b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1765b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        sBgWorkspaceItems.add(info);
1766b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1767b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    default:
1768b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        // Item is in a user folder
1769b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        FolderInfo folderInfo =
1770b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                                findOrMakeFolder(sBgFolders, container);
1771b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        folderInfo.add(info);
1772b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1773b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1774b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgItemsIdMap.put(info.id, info);
1775b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1776b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // now that we've loaded everthing re-save it with the
1777b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // icon in case it disappears somehow.
1778b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
17791323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                } else {
17801323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                    throw new RuntimeException("Unexpected null ShortcutInfo");
1781b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1782b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                break;
1783b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1784b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1785b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                id = c.getLong(idIndex);
1786b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1787b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1788b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.title = c.getString(titleIndex);
1789b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.id = id;
17903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
1791b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.container = container;
1792dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                folderInfo.screenId = c.getInt(screenIndex);
1793b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.cellX = c.getInt(cellXIndex);
1794b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.cellY = c.getInt(cellYIndex);
17955f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                folderInfo.spanX = 1;
17965f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                folderInfo.spanY = 1;
17975f8afe6280eae34620067696173e71943e1a30a3Winson Chung
17985f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                // Skip loading items that are out of bounds
17995f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
18005f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    int iconSpan = 1;
18015f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    if (checkItemDimensions(folderInfo)) {
18025f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        Log.d(TAG, "Skipped loading out of bounds folder");
18035f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        continue;
18045f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    }
18055f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                }
18063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
1807b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!checkItemPlacement(occupied, folderInfo)) {
18083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
18099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
18105f8afe6280eae34620067696173e71943e1a30a3Winson Chung
18113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
1812b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1813b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1814b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        sBgWorkspaceItems.add(folderInfo);
1815b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
18169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
181717a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato
1818b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgItemsIdMap.put(folderInfo.id, folderInfo);
1819b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgFolders.put(folderInfo.id, folderInfo);
1820b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                break;
1821b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1822b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1823b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                // Read all Launcher-specific widget details
1824b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                int appWidgetId = c.getInt(appWidgetIdIndex);
18253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
1826f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1827b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                final AppWidgetProviderInfo provider =
1828b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        widgets.getAppWidgetInfo(appWidgetId);
1829b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1830b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!isSafeMode && (provider == null || provider.provider == null ||
1831b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        provider.provider.getPackageName() == null)) {
1832b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String log = "Deleting widget that isn't installed anymore: id="
1833b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        + id + " appWidgetId=" + appWidgetId;
1834b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Log.e(TAG, log);
1835b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Launcher.sDumpLogs.add(log);
1836b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    itemsToRemove.add(id);
1837b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } else {
1838b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1839b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            provider.provider);
1840b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.id = id;
1841dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                    appWidgetInfo.screenId = c.getInt(screenIndex);
1842b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.cellX = c.getInt(cellXIndex);
1843b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.cellY = c.getInt(cellYIndex);
1844b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.spanX = c.getInt(spanXIndex);
1845b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.spanY = c.getInt(spanYIndex);
1846b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1847b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.minSpanX = minSpan[0];
1848b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.minSpanY = minSpan[1];
1849b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1850b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    container = c.getInt(containerIndex);
1851b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1852b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1853b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        Log.e(TAG, "Widget found where container != " +
1854b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1855b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        continue;
1856b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1857b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1858e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                                    appWidgetInfo.container = c.getInt(containerIndex);
18595f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    // Skip loading items that are out of bounds
18605f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
18615f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        if (checkItemDimensions(appWidgetInfo)) {
18625f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                            Log.d(TAG, "Skipped loading out of bounds app widget");
18635f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                            continue;
18645f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                        }
18655f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    }
1866b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // check & update map of what's occupied
1867b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (!checkItemPlacement(occupied, appWidgetInfo)) {
1868b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1869b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1870b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1871b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgAppWidgets.add(appWidgetInfo);
1872b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
18739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
18743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
1875b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        } catch (Exception e) {
1876b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.w(TAG, "Desktop items loading interrupted:", e);
1877b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
1878b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1879b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } finally {
188047b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                    if (c != null) {
188147b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                        c.close();
188247b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                    }
1883b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1884ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
1885b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (itemsToRemove.size() > 0) {
1886b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    ContentProviderClient client = contentResolver.acquireContentProviderClient(
1887b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    LauncherSettings.Favorites.CONTENT_URI);
1888b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // Remove dead items
1889b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (long id : itemsToRemove) {
1890b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (DEBUG_LOADERS) {
1891b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.d(TAG, "Removed id = " + id);
1892b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
1893b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        // Don't notify content observers
1894b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        try {
1895b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1896b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    null, null);
1897b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        } catch (RemoteException e) {
1898b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.w(TAG, "Could not remove id = " + id);
1899b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
1900b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1901b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
19028802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
1903c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                if (loadedOldDb) {
1904d64d1763c590b1c954b759156683abf39ff22945Winson Chung                    Log.w(TAG, "10249126 - loadWorkspace - loadedOldDb");
1905dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long maxScreenId = 0;
1906dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // If we're importing we use the old screen order.
1907dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    for (ItemInfo item: sBgItemsIdMap.values()) {
1908dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        long screenId = item.screenId;
1909dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1910dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                !sBgWorkspaceScreens.contains(screenId)) {
191176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                            Log.w(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")");
1912dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            sBgWorkspaceScreens.add(screenId);
1913dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            if (screenId > maxScreenId) {
1914dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                maxScreenId = screenId;
1915dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            }
1916dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        }
1917dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
1918dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    Collections.sort(sBgWorkspaceScreens);
1919dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    mApp.getLauncherProvider().updateMaxScreenId(maxScreenId);
1920dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1921c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1922c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    // Update the max item id after we load an old db
1923c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    long maxItemId = 0;
1924c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    // If we're importing we use the old screen order.
1925c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    for (ItemInfo item: sBgItemsIdMap.values()) {
1926c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        maxItemId = Math.max(maxItemId, item.id);
1927c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    }
1928c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    LauncherAppState app = LauncherAppState.getInstance();
1929c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    app.getLauncherProvider().updateMaxItemId(maxItemId);
1930dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                } else {
1931d64d1763c590b1c954b759156683abf39ff22945Winson Chung                    Log.w(TAG, "10249126 - loadWorkspace - !loadedOldDb");
193276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
193376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    for (Integer i : orderedScreens.keySet()) {
193476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                        sBgWorkspaceScreens.add(orderedScreens.get(i));
1935dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
1936dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1937dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // Remove any empty screens
1938dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    ArrayList<Long> unusedScreens = new ArrayList<Long>();
1939dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    unusedScreens.addAll(sBgWorkspaceScreens);
1940dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1941dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    for (ItemInfo item: sBgItemsIdMap.values()) {
1942dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        long screenId = item.screenId;
1943dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1944dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1945dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                unusedScreens.contains(screenId)) {
1946dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            unusedScreens.remove(screenId);
1947dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        }
1948dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
1949dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1950dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // If there are any empty screens remove them, and update.
1951dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (unusedScreens.size() != 0) {
1952dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        sBgWorkspaceScreens.removeAll(unusedScreens);
1953dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1954dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
1955dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
1956dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1957b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (DEBUG_LOADERS) {
1958b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1959b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Log.d(TAG, "workspace layout: ");
1960dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    int nScreens = occupied.size();
1961b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (int y = 0; y < mCellCountY; y++) {
1962b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        String line = "";
1963dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1964566da1026c33a68157bf9caf93d2071ad2870f46Daniel Sandler                        Iterator<Long> iter = occupied.keySet().iterator();
1965c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        while (iter.hasNext()) {
1966dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            long screenId = iter.next();
1967c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                            if (screenId > 0) {
1968b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                line += " | ";
1969b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            }
1970b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            for (int x = 0; x < mCellCountX; x++) {
1971dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
19729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
19739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
1974b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Log.d(TAG, "[ " + line + " ]");
19759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
19769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
19773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1978c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            return loadedOldDb;
1979e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        }
1980e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
1981b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of items who are directly or indirectly (via another container) on the
1982b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * specified screen. */
1983b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void filterCurrentWorkspaceItems(int currentScreen,
1984b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> allWorkspaceItems,
1985b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> currentScreenItems,
1986b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> otherScreenItems) {
19872abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            // Purge any null ItemInfos
19882abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
19892abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            while (iter.hasNext()) {
19902abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                ItemInfo i = iter.next();
19912abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (i == null) {
19922abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                    iter.remove();
19932abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                }
19942abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            }
19952abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung
1996b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we aren't filtering on a screen, then the set of items to load is the full set of
1997b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // items given.
1998b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (currentScreen < 0) {
1999b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                currentScreenItems.addAll(allWorkspaceItems);
2000b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2001b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2002b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Order the set of items by their containers first, this allows use to walk through the
2003b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // list sequentially, build up a list of containers that are in the specified screen,
2004b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // as well as all items in those containers.
2005b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Set<Long> itemsOnScreen = new HashSet<Long>();
2006b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2007b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                @Override
2008b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                public int compare(ItemInfo lhs, ItemInfo rhs) {
2009b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    return (int) (lhs.container - rhs.container);
2010b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
2011b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            });
2012b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (ItemInfo info : allWorkspaceItems) {
2013b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
2014dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (info.screenId == currentScreen) {
2015b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        currentScreenItems.add(info);
2016b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        itemsOnScreen.add(info.id);
2017b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    } else {
2018b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        otherScreenItems.add(info);
20193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
2020b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2021b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenItems.add(info);
2022b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    itemsOnScreen.add(info.id);
2023b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2024b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    if (itemsOnScreen.contains(info.container)) {
2025b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        currentScreenItems.add(info);
2026b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        itemsOnScreen.add(info.id);
2027b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    } else {
2028b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        otherScreenItems.add(info);
20295c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
20305c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
20313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2032b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
20335c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
2034b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of widgets which are on the specified screen. */
2035b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void filterCurrentAppWidgets(int currentScreen,
2036b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> appWidgets,
2037b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2038b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2039b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we aren't filtering on a screen, then the set of items to load is the full set of
2040b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // widgets given.
2041b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (currentScreen < 0) {
2042b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                currentScreenWidgets.addAll(appWidgets);
2043b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2044b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2045b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (LauncherAppWidgetInfo widget : appWidgets) {
20462abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (widget == null) continue;
2047b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2048dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        widget.screenId == currentScreen) {
2049b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenWidgets.add(widget);
2050b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2051b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherScreenWidgets.add(widget);
2052a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
20539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
20543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
205531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2056b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of folders which are on the specified screen. */
2057b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void filterCurrentFolders(int currentScreen,
2058b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, ItemInfo> itemsIdMap,
2059b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> folders,
2060b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> currentScreenFolders,
2061b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> otherScreenFolders) {
2062b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we aren't filtering on a screen, then the set of items to load is the full set of
2063b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // widgets given.
2064b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (currentScreen < 0) {
2065b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                currentScreenFolders.putAll(folders);
2066b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
20673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
2068b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (long id : folders.keySet()) {
2069b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ItemInfo info = itemsIdMap.get(id);
2070b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                FolderInfo folder = folders.get(id);
20712abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (info == null || folder == null) continue;
2072b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2073dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        info.screenId == currentScreen) {
2074b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenFolders.put(id, folder);
2075b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2076b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherScreenFolders.put(id, folder);
2077b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
20783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2079b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
208031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2081b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2082b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * right) */
2083b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
2084b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: review this
2085b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
2086db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                @Override
2087db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                public int compare(ItemInfo lhs, ItemInfo rhs) {
2088db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int cellCountX = LauncherModel.getCellCountX();
2089db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int cellCountY = LauncherModel.getCellCountY();
2090db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int screenOffset = cellCountX * cellCountY;
2091db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
2092dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
2093db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                            lhs.cellY * cellCountX + lhs.cellX);
2094dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
2095db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                            rhs.cellY * cellCountX + rhs.cellX);
2096db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    return (int) (lr - rr);
2097db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                }
2098db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung            });
2099b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
2100db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung
2101dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2102dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                final ArrayList<Long> orderedScreens) {
210376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            Log.w(TAG, "10249126 - bindWorkspaceScreens()");
2104dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final Runnable r = new Runnable() {
2105dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                @Override
2106dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                public void run() {
2107dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2108dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (callbacks != null) {
2109dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        callbacks.bindScreens(orderedScreens);
2110dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2111dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
2112dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            };
2113dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2114dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
2115dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2116b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void bindWorkspaceItems(final Callbacks oldCallbacks,
2117b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<ItemInfo> workspaceItems,
2118b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<LauncherAppWidgetInfo> appWidgets,
2119b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final HashMap<Long, FolderInfo> folders,
2120b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<Runnable> deferredBindRunnables) {
2121b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2122b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final boolean postOnMainThread = (deferredBindRunnables != null);
2123603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung
2124b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the workspace items
2125db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung            int N = workspaceItems.size();
2126b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (int i = 0; i < N; i += ITEMS_CHUNK) {
21273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
21283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
2129b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2130b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    @Override
21319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
2132c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
21339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
213464359a53dc827797917a8d9a0697a91996789801Winson Chung                            callbacks.bindItems(workspaceItems, start, start+chunkSize,
213564359a53dc827797917a8d9a0697a91996789801Winson Chung                                    false);
21369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
21379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
2138b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2139b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2140b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2141b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
214281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
21433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
2144b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2145b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2146b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the folders
2147b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (!folders.isEmpty()) {
2148b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2149b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    public void run() {
2150b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2151b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (callbacks != null) {
2152b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            callbacks.bindFolders(folders);
21539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
2154b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2155b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2156b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2157b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2158b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
215981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
21609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
21613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2162b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2163b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the widgets, one at a time
2164b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            N = appWidgets.size();
2165b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (int i = 0; i < N; i++) {
2166b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final LauncherAppWidgetInfo widget = appWidgets.get(i);
2167b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2168b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    public void run() {
2169b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2170b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (callbacks != null) {
2171b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            callbacks.bindAppWidget(widget);
21723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
2173b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2174b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2175b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2176b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2177b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
217881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
21793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
21803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2181b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
2182b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2183b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /**
2184b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * Binds all loaded data to actual views on the main thread.
2185b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         */
2186c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
2187b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final long t = SystemClock.uptimeMillis();
2188b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Runnable r;
2189b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2190b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Don't use these two variables in any of the callback runnables.
2191b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Otherwise we hold a reference to them.
2192b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final Callbacks oldCallbacks = mCallbacks.get();
2193b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (oldCallbacks == null) {
2194b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // This launcher has exited and nobody bothered to tell us.  Just bail.
2195b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.w(TAG, "LoaderTask running with no launcher");
2196b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                return;
2197b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2198b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
21994a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
22004a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
2201b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                oldCallbacks.getCurrentWorkspaceScreen();
2202b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2203b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Load all the items that are on the current page first (and in the process, unbind
2204b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // all the existing workspace items before we call startBinding() below.
2205b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            unbindWorkspaceItemsOnMainThread();
2206b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2207b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> appWidgets =
2208b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2209b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2210b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
2211dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
22122abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
2213b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                workspaceItems.addAll(sBgWorkspaceItems);
2214b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                appWidgets.addAll(sBgAppWidgets);
2215b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                folders.putAll(sBgFolders);
2216b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                itemsIdMap.putAll(sBgItemsIdMap);
2217dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                orderedScreenIds.addAll(sBgWorkspaceScreens);
2218b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2219b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2220b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2221b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2222b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2223b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2224b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2225b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2226b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2227b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2228b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2229b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Separate the items that are on the current screen, and all the other remaining items
2230b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2231b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherWorkspaceItems);
2232b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2233b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherAppWidgets);
2234b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2235b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherFolders);
2236b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sortWorkspaceItemsSpatially(currentWorkspaceItems);
2237b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sortWorkspaceItemsSpatially(otherWorkspaceItems);
2238b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2239b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Tell the workspace that we're about to start binding items
2240b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r = new Runnable() {
22413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
22423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
22433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
2244b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        callbacks.startBinding();
22459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
22469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
2247b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
224881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung            runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2249b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2250dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2251dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2252b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Load items on the current page
2253b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2254b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentFolders, null);
22551462de39f01cec0ba785386532719cb0207dd827Adam Cohen            if (isLoadingSynchronously) {
22561462de39f01cec0ba785386532719cb0207dd827Adam Cohen                r = new Runnable() {
22571462de39f01cec0ba785386532719cb0207dd827Adam Cohen                    public void run() {
22581462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
22591462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        if (callbacks != null) {
22601462de39f01cec0ba785386532719cb0207dd827Adam Cohen                            callbacks.onPageBoundSynchronously(currentScreen);
22611462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        }
22621462de39f01cec0ba785386532719cb0207dd827Adam Cohen                    }
22631462de39f01cec0ba785386532719cb0207dd827Adam Cohen                };
226481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
22651462de39f01cec0ba785386532719cb0207dd827Adam Cohen            }
2266b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
22674a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            // Load all the remaining pages (if we are loading synchronously, we want to defer this
22684a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            // work until after the first render)
2269b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
2270b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
22714a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                    (isLoadingSynchronously ? mDeferredBindRunnables : null));
2272b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2273b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Tell the workspace that we're done binding items
2274b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r = new Runnable() {
22753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
2276b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2277b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    if (callbacks != null) {
2278c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        callbacks.finishBindingItems(isUpgradePath);
2279b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2280b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
228198e030be2abc03e90e9fd9f8bf67e2e01db39cc0Winson Chung                    // If we're profiling, ensure this is the last thing in the queue.
22823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
22833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
22843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
22859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
228636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
228736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung                    mIsLoadingAndBindingWorkspace = false;
22889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
2289b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
22904a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            if (isLoadingSynchronously) {
22914a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                mDeferredBindRunnables.add(r);
22924a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            } else {
229381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
22944a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            }
22953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
22963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
22973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
22983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
22993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
23003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
23013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
230264359a53dc827797917a8d9a0697a91996789801Winson Chung                loadAllApps();
230393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                synchronized (LoaderTask.this) {
230493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    if (mStopped) {
230593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                        return;
230693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    }
230793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    mAllAppsLoaded = true;
23083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
23093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
23103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
23113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
23123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
23133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
23143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
23153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
23163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
23173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
23183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
23193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
23203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
23213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
23223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
2323c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            @SuppressWarnings("unchecked")
23243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ArrayList<ApplicationInfo> list
2325487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                    = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
2326c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            Runnable r = new Runnable() {
23273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
23283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
23293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
23303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
23313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
23329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
23333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
23343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2335a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
23369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
23373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
2338c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            };
2339c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
234064359a53dc827797917a8d9a0697a91996789801Winson Chung            if (isRunningOnMainThread) {
2341c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                r.run();
2342c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            } else {
2343c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                mHandler.post(r);
2344c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            }
23453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
23463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
234764359a53dc827797917a8d9a0697a91996789801Winson Chung        private void loadAllApps() {
234864359a53dc827797917a8d9a0697a91996789801Winson Chung            final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
23493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
23503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't use these two variables in any of the callback runnables.
23513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Otherwise we hold a reference to them.
23523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
23533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
23543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
235564359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
23563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
23579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
235831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
235964359a53dc827797917a8d9a0697a91996789801Winson Chung            final PackageManager packageManager = mContext.getPackageManager();
23603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
23613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2362cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
236364359a53dc827797917a8d9a0697a91996789801Winson Chung            // Clear the list of apps
236464359a53dc827797917a8d9a0697a91996789801Winson Chung            mBgAllAppsList.clear();
2365cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
236664359a53dc827797917a8d9a0697a91996789801Winson Chung            // Query for the set of apps
236764359a53dc827797917a8d9a0697a91996789801Winson Chung            final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
236864359a53dc827797917a8d9a0697a91996789801Winson Chung            List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
236964359a53dc827797917a8d9a0697a91996789801Winson Chung            if (DEBUG_LOADERS) {
237064359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "queryIntentActivities took "
237164359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis()-qiaTime) + "ms");
237264359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
237364359a53dc827797917a8d9a0697a91996789801Winson Chung            }
237464359a53dc827797917a8d9a0697a91996789801Winson Chung            // Fail if we don't have any apps
237564359a53dc827797917a8d9a0697a91996789801Winson Chung            if (apps == null || apps.isEmpty()) {
237664359a53dc827797917a8d9a0697a91996789801Winson Chung                return;
237764359a53dc827797917a8d9a0697a91996789801Winson Chung            }
237864359a53dc827797917a8d9a0697a91996789801Winson Chung            // Sort the applications by name
237964359a53dc827797917a8d9a0697a91996789801Winson Chung            final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
238064359a53dc827797917a8d9a0697a91996789801Winson Chung            Collections.sort(apps,
238164359a53dc827797917a8d9a0697a91996789801Winson Chung                    new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
238264359a53dc827797917a8d9a0697a91996789801Winson Chung            if (DEBUG_LOADERS) {
238364359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "sort took "
238464359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis()-sortTime) + "ms");
238564359a53dc827797917a8d9a0697a91996789801Winson Chung            }
23863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
238764359a53dc827797917a8d9a0697a91996789801Winson Chung            // Create the ApplicationInfos
238864359a53dc827797917a8d9a0697a91996789801Winson Chung            final long addTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
238964359a53dc827797917a8d9a0697a91996789801Winson Chung            for (int i = 0; i < apps.size(); i++) {
239064359a53dc827797917a8d9a0697a91996789801Winson Chung                // This builds the icon bitmaps.
239164359a53dc827797917a8d9a0697a91996789801Winson Chung                mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
239264359a53dc827797917a8d9a0697a91996789801Winson Chung                        mIconCache, mLabelCache));
239364359a53dc827797917a8d9a0697a91996789801Winson Chung            }
2394cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
239564359a53dc827797917a8d9a0697a91996789801Winson Chung            final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
239664359a53dc827797917a8d9a0697a91996789801Winson Chung            final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
239764359a53dc827797917a8d9a0697a91996789801Winson Chung            mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
23983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
239964359a53dc827797917a8d9a0697a91996789801Winson Chung            // Post callback on main thread
240064359a53dc827797917a8d9a0697a91996789801Winson Chung            mHandler.post(new Runnable() {
240164359a53dc827797917a8d9a0697a91996789801Winson Chung                public void run() {
240264359a53dc827797917a8d9a0697a91996789801Winson Chung                    final long bindTime = SystemClock.uptimeMillis();
240364359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (callbacks != null) {
240464359a53dc827797917a8d9a0697a91996789801Winson Chung                        callbacks.bindAllApplications(added);
24053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
240664359a53dc827797917a8d9a0697a91996789801Winson Chung                            Log.d(TAG, "bound " + added.size() + " apps in "
240764359a53dc827797917a8d9a0697a91996789801Winson Chung                                + (SystemClock.uptimeMillis() - bindTime) + "ms");
24083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
240964359a53dc827797917a8d9a0697a91996789801Winson Chung                    } else {
241064359a53dc827797917a8d9a0697a91996789801Winson Chung                        Log.i(TAG, "not binding apps: no Launcher activity");
241164359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
24123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
241364359a53dc827797917a8d9a0697a91996789801Winson Chung            });
2414cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
24153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
241664359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "Icons processed in "
241764359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis() - loadTime) + "ms");
24183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
24193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
2420dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
24213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
24222abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
2423b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2424b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2425b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2426b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2427b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2428b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
24293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
24303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
243131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
24323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
2433700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(task);
24343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
2435d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
24363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
24373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
24383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
2439d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
24403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
24413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
24423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
24433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
24443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
244531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2446d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
24473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
24483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
24493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
24503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
2451d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
24523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
2453cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler            final Context context = mApp.getContext();
2454dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
24553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
24563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
24573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
24583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
24593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
24603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
2461487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.addPackage(context, packages[i]);
2462dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
24633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
24643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
24653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
24663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
2467487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.updatePackage(context, packages[i]);
2468d9cb4a124ac5c83080ace5ac92980df9c6f49e8eMichael Jurka                        WidgetPreviewLoader.removeFromDb(
2469e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                                mApp.getWidgetPreviewCacheDb(), packages[i]);
24703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
24713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
24723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
24733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
24743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
24753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2476487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.removePackage(packages[i]);
2477d9cb4a124ac5c83080ace5ac92980df9c6f49e8eMichael Jurka                        WidgetPreviewLoader.removeFromDb(
2478e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                                mApp.getWidgetPreviewCacheDb(), packages[i]);
24793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
24803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
24813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2482dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
24833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> added = null;
24843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            ArrayList<ApplicationInfo> modified = null;
248583892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
24863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
2487487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            if (mBgAllAppsList.added.size() > 0) {
24885d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
24895d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.added.clear();
24903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2491487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            if (mBgAllAppsList.modified.size() > 0) {
24925d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
24935d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.modified.clear();
24943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
24955d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung            if (mBgAllAppsList.removed.size() > 0) {
249683892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                removedApps.addAll(mBgAllAppsList.removed);
24975d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.removed.clear();
2498cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung            }
2499cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung
25003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
25013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
25023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
25033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
2504be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
2505be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
25063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
250764359a53dc827797917a8d9a0697a91996789801Winson Chung                // Ensure that we add all the workspace applications to the db
2508997a92348a6d6e061737399321393449c16cd4d8Winson Chung                final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
250964359a53dc827797917a8d9a0697a91996789801Winson Chung                Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2510997a92348a6d6e061737399321393449c16cd4d8Winson Chung                addAndBindAddedApps(context, addedInfos, cb);
25113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
25133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final ArrayList<ApplicationInfo> modifiedFinal = modified;
251464359a53dc827797917a8d9a0697a91996789801Winson Chung
251564359a53dc827797917a8d9a0697a91996789801Winson Chung                // Update the launcher db to reflect the changes
251664359a53dc827797917a8d9a0697a91996789801Winson Chung                for (ApplicationInfo a : modifiedFinal) {
251764359a53dc827797917a8d9a0697a91996789801Winson Chung                    ArrayList<ItemInfo> infos =
251864359a53dc827797917a8d9a0697a91996789801Winson Chung                            getItemInfoForComponentName(a.componentName);
251964359a53dc827797917a8d9a0697a91996789801Winson Chung                    for (ItemInfo i : infos) {
252064359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (isShortcutInfoUpdateable(i)) {
252164359a53dc827797917a8d9a0697a91996789801Winson Chung                            ShortcutInfo info = (ShortcutInfo) i;
252264359a53dc827797917a8d9a0697a91996789801Winson Chung                            info.title = a.title.toString();
252364359a53dc827797917a8d9a0697a91996789801Winson Chung                            updateItemInDatabase(context, info);
252464359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
252564359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
252664359a53dc827797917a8d9a0697a91996789801Winson Chung                }
252764359a53dc827797917a8d9a0697a91996789801Winson Chung
25283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
25293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
2530cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2531cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        if (callbacks == cb && cb != null) {
25323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
25333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
25343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
25353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
25363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
253783892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            // If a package has been removed, or an app has been removed as a result of
253883892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            // an update (for example), make the removed callback.
253983892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
254064359a53dc827797917a8d9a0697a91996789801Winson Chung                final boolean packageRemoved = (mOp == OP_REMOVE);
254183892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                final ArrayList<String> removedPackageNames =
254283892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                        new ArrayList<String>(Arrays.asList(packages));
254383892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung
254464359a53dc827797917a8d9a0697a91996789801Winson Chung                // Update the launcher db to reflect the removal of apps
254564359a53dc827797917a8d9a0697a91996789801Winson Chung                if (packageRemoved) {
254664359a53dc827797917a8d9a0697a91996789801Winson Chung                    for (String pn : removedPackageNames) {
254764359a53dc827797917a8d9a0697a91996789801Winson Chung                        ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
254864359a53dc827797917a8d9a0697a91996789801Winson Chung                        for (ItemInfo i : infos) {
254964359a53dc827797917a8d9a0697a91996789801Winson Chung                            deleteItemFromDatabase(context, i);
255064359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
255164359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
255264359a53dc827797917a8d9a0697a91996789801Winson Chung                } else {
255364359a53dc827797917a8d9a0697a91996789801Winson Chung                    for (ApplicationInfo a : removedApps) {
255464359a53dc827797917a8d9a0697a91996789801Winson Chung                        ArrayList<ItemInfo> infos =
255564359a53dc827797917a8d9a0697a91996789801Winson Chung                                getItemInfoForComponentName(a.componentName);
255664359a53dc827797917a8d9a0697a91996789801Winson Chung                        for (ItemInfo i : infos) {
255764359a53dc827797917a8d9a0697a91996789801Winson Chung                            deleteItemFromDatabase(context, i);
255864359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
255964359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
256064359a53dc827797917a8d9a0697a91996789801Winson Chung                }
256164359a53dc827797917a8d9a0697a91996789801Winson Chung
25623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
25633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
2564cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2565cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        if (callbacks == cb && cb != null) {
256683892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                            callbacks.bindComponentsRemoved(removedPackageNames,
256764359a53dc827797917a8d9a0697a91996789801Winson Chung                                    removedApps, packageRemoved);
25683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
25693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
25703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
2571be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
257280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
2573c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            final ArrayList<Object> widgetsAndShortcuts =
2574c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                getSortedWidgetsAndShortcuts(context);
257580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
257680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
257780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
2578cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                    Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2579cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                    if (callbacks == cb && cb != null) {
2580c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                        callbacks.bindPackagesUpdated(widgetsAndShortcuts);
258180baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
258280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
258380baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
258431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
258531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
258631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2587c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2588c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2589c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        PackageManager packageManager = context.getPackageManager();
2590c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2591c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2592c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2593c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2594c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        Collections.sort(widgetsAndShortcuts,
2595c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2596c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        return widgetsAndShortcuts;
2597c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    }
2598c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka
25991323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung    private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
2600ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        if (cn == null) {
2601ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return false;
2602ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        }
2603ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung
2604ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        try {
2605ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return (pm.getActivityInfo(cn, 0) != null);
2606ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        } catch (NameNotFoundException e) {
2607ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return false;
2608ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        }
2609ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung    }
2610ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung
26119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
261256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
261356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
26149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
261556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
2616c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        return getShortcutInfo(manager, intent, context, null, -1, -1, null);
261756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
261856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
261956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
262056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
262156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
262256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
262356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
262456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
2625c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
262656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
26271323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        final ShortcutInfo info = new ShortcutInfo();
26281323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        if (!isValidPackageComponent(manager, componentName)) {
2629ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
26309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
26311323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        } else {
26321323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            try {
26331323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
26341323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                info.initFlagsAndFirstInstallTime(pi);
26351323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            } catch (NameNotFoundException e) {
26361323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                Log.d(TAG, "getPackInfo failed for package " +
26371323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                        componentName.getPackageName());
26381323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            }
263931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
264031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
26418ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
26428ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
26438ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
264456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
264556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
264656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
264756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
2648c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung
2649c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2650c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2651c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // via resolveActivity().
2652ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        Bitmap icon = null;
2653c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        ResolveInfo resolveInfo = null;
2654c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        ComponentName oldComponent = intent.getComponent();
2655c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        Intent newIntent = new Intent(intent.getAction(), null);
2656c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2657c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        newIntent.setPackage(oldComponent.getPackageName());
2658c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2659c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        for (ResolveInfo i : infos) {
2660c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            ComponentName cn = new ComponentName(i.activityInfo.packageName,
2661c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung                    i.activityInfo.name);
2662c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            if (cn.equals(oldComponent)) {
2663c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung                resolveInfo = i;
2664c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            }
2665c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        }
2666c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        if (resolveInfo == null) {
2667c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            resolveInfo = manager.resolveActivity(intent, 0);
2668c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        }
266956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
2670aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
267156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
267256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
267356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
267456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
2675931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                icon = getIconFromCursor(c, iconIndex, context);
267656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
26779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
267856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
267956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
268056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
268156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
268256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
268356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
268456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
268556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
268656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
26875308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
26885308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (labelCache != null && labelCache.containsKey(key)) {
26895308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                info.title = labelCache.get(key);
2690c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
2691c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                info.title = resolveInfo.activityInfo.loadLabel(manager);
2692c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                if (labelCache != null) {
26935308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                    labelCache.put(key, info.title);
2694c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                }
2695c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
269656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
269756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
269856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
269956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
270056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
270156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
270256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
270356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
27049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
270556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
27069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
27079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
27089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
27099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
271031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
271164359a53dc827797917a8d9a0697a91996789801Winson Chung    static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
271264359a53dc827797917a8d9a0697a91996789801Winson Chung            ItemInfoFilter f) {
271364359a53dc827797917a8d9a0697a91996789801Winson Chung        HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
271464359a53dc827797917a8d9a0697a91996789801Winson Chung        for (ItemInfo i : infos) {
271564359a53dc827797917a8d9a0697a91996789801Winson Chung            if (i instanceof ShortcutInfo) {
271664359a53dc827797917a8d9a0697a91996789801Winson Chung                ShortcutInfo info = (ShortcutInfo) i;
271764359a53dc827797917a8d9a0697a91996789801Winson Chung                ComponentName cn = info.intent.getComponent();
271864359a53dc827797917a8d9a0697a91996789801Winson Chung                if (cn != null && f.filterItem(null, info, cn)) {
271964359a53dc827797917a8d9a0697a91996789801Winson Chung                    filtered.add(info);
272064359a53dc827797917a8d9a0697a91996789801Winson Chung                }
272164359a53dc827797917a8d9a0697a91996789801Winson Chung            } else if (i instanceof FolderInfo) {
272264359a53dc827797917a8d9a0697a91996789801Winson Chung                FolderInfo info = (FolderInfo) i;
272364359a53dc827797917a8d9a0697a91996789801Winson Chung                for (ShortcutInfo s : info.contents) {
272464359a53dc827797917a8d9a0697a91996789801Winson Chung                    ComponentName cn = s.intent.getComponent();
272564359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (cn != null && f.filterItem(info, s, cn)) {
272664359a53dc827797917a8d9a0697a91996789801Winson Chung                        filtered.add(s);
27278a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung                    }
27288a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung                }
272964359a53dc827797917a8d9a0697a91996789801Winson Chung            } else if (i instanceof LauncherAppWidgetInfo) {
273064359a53dc827797917a8d9a0697a91996789801Winson Chung                LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
273164359a53dc827797917a8d9a0697a91996789801Winson Chung                ComponentName cn = info.providerName;
273264359a53dc827797917a8d9a0697a91996789801Winson Chung                if (cn != null && f.filterItem(null, info, cn)) {
273364359a53dc827797917a8d9a0697a91996789801Winson Chung                    filtered.add(info);
273464359a53dc827797917a8d9a0697a91996789801Winson Chung                }
27358a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung            }
27368a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung        }
273764359a53dc827797917a8d9a0697a91996789801Winson Chung        return new ArrayList<ItemInfo>(filtered);
273864359a53dc827797917a8d9a0697a91996789801Winson Chung    }
273964359a53dc827797917a8d9a0697a91996789801Winson Chung
274064359a53dc827797917a8d9a0697a91996789801Winson Chung    private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
274164359a53dc827797917a8d9a0697a91996789801Winson Chung        HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
274264359a53dc827797917a8d9a0697a91996789801Winson Chung        ItemInfoFilter filter  = new ItemInfoFilter() {
274364359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
274464359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
274564359a53dc827797917a8d9a0697a91996789801Winson Chung                return cn.getPackageName().equals(pn);
274664359a53dc827797917a8d9a0697a91996789801Winson Chung            }
274764359a53dc827797917a8d9a0697a91996789801Winson Chung        };
274864359a53dc827797917a8d9a0697a91996789801Winson Chung        return filterItemInfos(sBgItemsIdMap.values(), filter);
274964359a53dc827797917a8d9a0697a91996789801Winson Chung    }
275064359a53dc827797917a8d9a0697a91996789801Winson Chung
275164359a53dc827797917a8d9a0697a91996789801Winson Chung    private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
275264359a53dc827797917a8d9a0697a91996789801Winson Chung        HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
275364359a53dc827797917a8d9a0697a91996789801Winson Chung        ItemInfoFilter filter  = new ItemInfoFilter() {
275464359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
275564359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
275664359a53dc827797917a8d9a0697a91996789801Winson Chung                return cn.equals(cname);
275764359a53dc827797917a8d9a0697a91996789801Winson Chung            }
275864359a53dc827797917a8d9a0697a91996789801Winson Chung        };
275964359a53dc827797917a8d9a0697a91996789801Winson Chung        return filterItemInfos(sBgItemsIdMap.values(), filter);
276064359a53dc827797917a8d9a0697a91996789801Winson Chung    }
276164359a53dc827797917a8d9a0697a91996789801Winson Chung
276264359a53dc827797917a8d9a0697a91996789801Winson Chung    public static boolean isShortcutInfoUpdateable(ItemInfo i) {
276364359a53dc827797917a8d9a0697a91996789801Winson Chung        if (i instanceof ShortcutInfo) {
276464359a53dc827797917a8d9a0697a91996789801Winson Chung            ShortcutInfo info = (ShortcutInfo) i;
276564359a53dc827797917a8d9a0697a91996789801Winson Chung            // We need to check for ACTION_MAIN otherwise getComponent() might
276664359a53dc827797917a8d9a0697a91996789801Winson Chung            // return null for some shortcuts (for instance, for shortcuts to
276764359a53dc827797917a8d9a0697a91996789801Winson Chung            // web pages.)
276864359a53dc827797917a8d9a0697a91996789801Winson Chung            Intent intent = info.intent;
276964359a53dc827797917a8d9a0697a91996789801Winson Chung            ComponentName name = intent.getComponent();
277064359a53dc827797917a8d9a0697a91996789801Winson Chung            if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
277164359a53dc827797917a8d9a0697a91996789801Winson Chung                    Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
277264359a53dc827797917a8d9a0697a91996789801Winson Chung                return true;
277364359a53dc827797917a8d9a0697a91996789801Winson Chung            }
277464359a53dc827797917a8d9a0697a91996789801Winson Chung        }
277564359a53dc827797917a8d9a0697a91996789801Winson Chung        return false;
27768a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung    }
27778a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung
27788a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung    /**
27790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
27809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
27810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
278256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
278356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
278431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
278556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
2786c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ShortcutInfo info = new ShortcutInfo();
27879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
27887376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
27898ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
27908ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
279156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
279256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
27939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
27949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
27959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
27969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
27979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
27989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
279956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
280056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
28019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
28029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
280356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
280456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
2805c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
2806c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
280756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
28089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
280956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
281056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
281156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
281256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
2813931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                icon = getIconFromCursor(c, iconIndex, context);
281456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
281556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
281656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
281756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
281856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
28197376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
28209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
28219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
2822931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            icon = getIconFromCursor(c, iconIndex, context);
282356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
282456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
282556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
282656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
282756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
282856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
28299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
28309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
28319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
2832d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
283356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
28349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
28359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
283631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
2837d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
28389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
2839f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
284031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2841931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka    Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
28423a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        @SuppressWarnings("all") // suppress dead code warning
28433a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        final boolean debug = false;
28443a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        if (debug) {
284556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
284656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
284756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
284856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
284956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
2850931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            return Utilities.createIconBitmap(
2851931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                    BitmapFactory.decodeByteArray(data, 0, data.length), context);
285256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
285356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
285456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
285556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
285656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
28573d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
28583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellX, int cellY, boolean notify) {
2859a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
2860d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (info == null) {
2861d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return null;
2862d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
28633d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
28640589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
28650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
28660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
28670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
2868a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    /**
286955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     * Attempts to find an AppWidgetProviderInfo that matches the given component.
287055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     */
287155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
287255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            ComponentName component) {
287355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        List<AppWidgetProviderInfo> widgets =
287455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
287555cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        for (AppWidgetProviderInfo info : widgets) {
287655cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            if (info.provider.equals(component)) {
287755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                return info;
287855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            }
287955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        }
288055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        return null;
2881a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
2882a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
288368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    /**
288468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     * Returns a list of all the widgets that can handle configuration with a particular mimeType.
288568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     */
288668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
288768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final PackageManager packageManager = context.getPackageManager();
288868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
288968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new ArrayList<WidgetMimeTypeHandlerData>();
289068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
289168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final Intent supportsIntent =
289268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
289368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        supportsIntent.setType(mimeType);
289468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
289568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Create a set of widget configuration components that we can test against
289668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<AppWidgetProviderInfo> widgets =
289768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
289868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
289968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new HashMap<ComponentName, AppWidgetProviderInfo>();
290068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (AppWidgetProviderInfo info : widgets) {
290168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            configurationComponentToWidget.put(info.configure, info);
290268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
290368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
290468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Run through each of the intents that can handle this type of clip data, and cross
290568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // reference them with the components that are actual configuration components
290668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
290768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                PackageManager.MATCH_DEFAULT_ONLY);
290868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (ResolveInfo info : activities) {
290968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ActivityInfo activityInfo = info.activityInfo;
291068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
291168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                    activityInfo.name);
291268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            if (configurationComponentToWidget.containsKey(infoComponent)) {
291368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                supportedConfigurationActivities.add(
291468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
291568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                                configurationComponentToWidget.get(infoComponent)));
291668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            }
291768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
291868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        return supportedConfigurationActivities;
291968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    }
292068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
2921a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
29220589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
29230589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
29240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
29250589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
2926d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (intent == null) {
2927d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2928d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2929d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return null;
2930d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
2931d919882006a457d16dea46bd546472e6534b5c19Adam Cohen
29320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
29330589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
29340589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
29350589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
29360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
29370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
29380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
29390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
29400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
29410589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
29420589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
29430589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
29440589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
29450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
29460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
29470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
2948c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
2949c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
29500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
29510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
29520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
29530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
29540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
29550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
2956c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ShortcutInfo info = new ShortcutInfo();
295756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
29580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
2959a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            if (fallbackIcon != null) {
2960a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = fallbackIcon;
2961a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            } else {
2962a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = getFallbackIcon();
2963a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                info.usingFallbackIcon = true;
2964a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            }
29650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
29660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
296756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
29680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
29690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
29700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
29710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
29720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
29730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
29740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
29750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
2976aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2977aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            int iconIndex) {
297817a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        // If apps can't be on SD, don't even bother.
2979ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        if (!mAppsCanBeOnRemoveableStorage) {
2980aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            return false;
298117a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        }
298256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
298356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
298456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
298556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
298656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
298756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
2988ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
2989aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            cache.put(info, c.getBlob(iconIndex));
2990aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            return true;
2991aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        }
2992aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        return false;
2993aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    }
2994aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2995aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        boolean needSave = false;
2996aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        try {
2997aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            if (data != null) {
2998aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2999aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                Bitmap loaded = info.getIcon(mIconCache);
3000aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                needSave = !saved.sameAs(loaded);
3001aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            } else {
300256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
300356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
3004aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        } catch (Exception e) {
3005aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            needSave = true;
3006aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        }
3007aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        if (needSave) {
3008aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            Log.d(TAG, "going to save icon bitmap for info=" + info);
3009aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // This is slower than is ideal, but this only happens once
3010aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // or when the app is updated with a new icon.
3011aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            updateItemInDatabase(context, info);
301256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
301356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
301456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
301531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
3016df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     * Return an existing FolderInfo object if we have encountered this ID previously,
30179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
301831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
3019df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
30209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
30219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
3022df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        if (folderInfo == null) {
30239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
3024c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            folderInfo = new FolderInfo();
30259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
302631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
3027df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        return folderInfo;
302831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
302931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
30301190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    public static final Comparator<ApplicationInfo> getAppNameComparator() {
30311190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        final Collator collator = Collator.getInstance();
30321190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        return new Comparator<ApplicationInfo>() {
30331190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            public final int compare(ApplicationInfo a, ApplicationInfo b) {
30341190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                int result = collator.compare(a.title.toString(), b.title.toString());
30351190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                if (result == 0) {
30361190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                    result = a.componentName.compareTo(b.componentName);
30371190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                }
30381190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                return result;
30395b1808da75ed3e3528cb31dee56a4e363812cc21Michael Jurka            }
30401190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        };
30411190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    }
304278403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung    public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
304378403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            = new Comparator<ApplicationInfo>() {
304478403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung        public final int compare(ApplicationInfo a, ApplicationInfo b) {
304578403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime < b.firstInstallTime) return 1;
304678403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime > b.firstInstallTime) return -1;
304778403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            return 0;
304878403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung        }
304978403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung    };
30501190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
30511190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        final Collator collator = Collator.getInstance();
30521190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        return new Comparator<AppWidgetProviderInfo>() {
30531190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
30541190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                return collator.compare(a.label.toString(), b.label.toString());
30551190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            }
30561190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        };
30571190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    }
30585308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung    static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
30595308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        if (info.activityInfo != null) {
30605308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
30615308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        } else {
30625308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
30635308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        }
30645308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung    }
3065785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
30661190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        private Collator mCollator;
3067785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        private PackageManager mPackageManager;
3068c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        private HashMap<Object, CharSequence> mLabelCache;
3069785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        ShortcutNameComparator(PackageManager pm) {
3070785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mPackageManager = pm;
3071c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = new HashMap<Object, CharSequence>();
30721190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
3073c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        }
3074c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3075c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mPackageManager = pm;
3076c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = labelCache;
30771190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
3078785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3079785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        public final int compare(ResolveInfo a, ResolveInfo b) {
3080c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            CharSequence labelA, labelB;
30815308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
30825308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
30835308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (mLabelCache.containsKey(keyA)) {
30845308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                labelA = mLabelCache.get(keyA);
3085c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3086c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = a.loadLabel(mPackageManager).toString();
3087c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung
30885308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                mLabelCache.put(keyA, labelA);
3089c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
30905308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (mLabelCache.containsKey(keyB)) {
30915308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                labelB = mLabelCache.get(keyB);
3092c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3093c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = b.loadLabel(mPackageManager).toString();
3094c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung
30955308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                mLabelCache.put(keyB, labelB);
3096c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
30971190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            return mCollator.compare(labelA, labelB);
3098785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3099785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    };
31001ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung    public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
31011190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        private Collator mCollator;
31021ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        private PackageManager mPackageManager;
31031ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        private HashMap<Object, String> mLabelCache;
31041ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        WidgetAndShortcutNameComparator(PackageManager pm) {
31051ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            mPackageManager = pm;
31061ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            mLabelCache = new HashMap<Object, String>();
31071190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
31081ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        }
31091ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        public final int compare(Object a, Object b) {
31101ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            String labelA, labelB;
3111c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            if (mLabelCache.containsKey(a)) {
3112c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = mLabelCache.get(a);
3113c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3114c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = (a instanceof AppWidgetProviderInfo) ?
31151ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((AppWidgetProviderInfo) a).label :
31161ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((ResolveInfo) a).loadLabel(mPackageManager).toString();
3117c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                mLabelCache.put(a, labelA);
3118c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
3119c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            if (mLabelCache.containsKey(b)) {
3120c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = mLabelCache.get(b);
3121c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3122c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = (b instanceof AppWidgetProviderInfo) ?
31231ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((AppWidgetProviderInfo) b).label :
31241ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((ResolveInfo) b).loadLabel(mPackageManager).toString();
3125c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                mLabelCache.put(b, labelB);
3126c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
31271190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            return mCollator.compare(labelA, labelB);
31281ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        }
31291ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung    };
3130be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
3131be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
3132be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
3133487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3134487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3135487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3136487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
31373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
31383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
31393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
31403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
31413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
3142be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
314331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
3144