LauncherModel.java revision aeff7ea43409d817490fbb8c22b8d4b9725bb54f
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;
42c3919c0e994309982efc8779af57e2bc78dcac43Chris Wrenimport android.provider.BaseColumns;
43a90303bb6fdad59e4a55855edba68c9902192599Winson Chungimport android.text.TextUtils;
44aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.util.Log;
45c916834ad0da535f08325bcd902a60070f2f7e53Winson Chungimport android.util.Pair;
4634c2e6cf7af328aba25c98158161dbad15ae986dMichael Jurka
47325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerimport com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
48edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
49c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.lang.ref.WeakReference;
50c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.net.URISyntaxException;
51c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.text.Collator;
52c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.ArrayList;
53dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohenimport java.util.Arrays;
5464359a53dc827797917a8d9a0697a91996789801Winson Chungimport java.util.Collection;
55c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.Collections;
56c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.Comparator;
57c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.HashMap;
58b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chungimport java.util.HashSet;
592abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chungimport java.util.Iterator;
60c2f801e770b4f80354b3edb722ec04222251990cMichael Jurkaimport java.util.List;
61b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chungimport java.util.Set;
62dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohenimport java.util.TreeMap;
63a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chungimport java.util.concurrent.atomic.AtomicBoolean;
64c2f801e770b4f80354b3edb722ec04222251990cMichael Jurka
6531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
6631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Maintains in-memory state of the Launcher. It is expected that there should be only one
6731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * LauncherModel object held in a static. Also provide APIs for updating the database state
68bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project * for the Launcher.
6931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
70f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onoratopublic class LauncherModel extends BroadcastReceiver {
71a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final boolean DEBUG_LOADERS = false;
729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final String TAG = "Launcher.Model";
739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
748707e0fa1ca2c79be862c29cb919b7372f0b6a50Daniel Sandler    // true = use a "More Apps" folder for non-workspace apps on upgrade
758707e0fa1ca2c79be862c29cb919b7372f0b6a50Daniel Sandler    // false = strew non-workspace apps across the workspace on upgrade
768707e0fa1ca2c79be862c29cb919b7372f0b6a50Daniel Sandler    public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
778707e0fa1ca2c79be862c29cb919b7372f0b6a50Daniel Sandler
78d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler    public static final int LOADER_FLAG_NONE = 0;
79d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler    public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
80d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler    public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
81d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler
823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
837aff399974c756930070d82d7b2df88f125dacd6Derek Prothro    private static final long INVALID_SCREEN_ID = -1L;
84a694524047fda0a51dede4eefb1201a598d2d3a7Winson Chung
85ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung    private final boolean mAppsCanBeOnRemoveableStorage;
86a694524047fda0a51dede4eefb1201a598d2d3a7Winson Chung    private final boolean mOldContentProviderExists;
87dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
88cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler    private final LauncherAppState mApp;
899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private final Object mLock = new Object();
909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private DeferredHandler mHandler = new DeferredHandler();
913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private LoaderTask mLoaderTask;
92b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private boolean mIsLoaderTaskRunning;
93c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    private volatile boolean mFlushingWorkerThread;
949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
9581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    // Specific runnable types that are run on the main thread deferred handler, this allows us to
9681b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    // clear all queued binding runnables when the Launcher activity is destroyed.
9781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
9881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
9981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
10081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
101700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
102700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    static {
103700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorkerThread.start();
104700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    }
105700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick    private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
106700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick
107cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // We start off with everything not loaded.  After that, we assume that
108cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // our monitoring of the package manager provides all updates and we never
109cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    // need to do a requery.  These are only ever touched from the loader thread.
110cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mWorkspaceLoaded;
111cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    private boolean mAllAppsLoaded;
112cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
113b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // When we are loading pages synchronously, we can't just post the binding of items on the side
114b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // pages as this delays the rotation process.  Instead, we wait for a callback from the first
115b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // draw (in Workspace) to initiate the binding of the remaining side pages.  Any time we start
116b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // a normal load, we also clear this set of Runnables.
117b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
118b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private WeakReference<Callbacks> mCallbacks;
1209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
121a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // < only access in worker thread >
1224caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen    AllAppsList mBgAllAppsList;
123a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
124b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // The lock that must be acquired before referencing any static bg data structures.  Unlike
125b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // other locks, this one can generally be held long-term because we never expect any of these
126b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // static data structures to be referenced outside of the worker thread except on the first
127b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    // load after configuration change.
1282abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung    static final Object sBgLock = new Object();
129b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
130487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
131a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // LauncherModel to their ids
132487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
133a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
134487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
135487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    //       created by LauncherModel that are directly on the home screen (however, no widgets or
136487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    //       shortcuts within folders).
137487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
1380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
139487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
140487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
141a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        new ArrayList<LauncherAppWidgetInfo>();
142a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
143487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
144487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
145b1094bd9630f659cc606853b8a244b6a6b6c6943Winson Chung
146487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
147487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
148dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
149dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    // sBgWorkspaceScreens is the ordered set of workspace screens.
150dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
151dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
152a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    // </ only access in worker thread >
153a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka
154a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    private IconCache mIconCache;
1550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private Bitmap mDefaultIcon;
1569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
15799a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee    protected int mPreviousConfigMcc;
15893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public interface Callbacks {
160ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        public boolean setLoadOnResume();
1619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public int getCurrentWorkspaceScreen();
1629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void startBinding();
16364359a53dc827797917a8d9a0697a91996789801Winson Chung        public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
16464359a53dc827797917a8d9a0697a91996789801Winson Chung                              boolean forceAnimateIcons);
165dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        public void bindScreens(ArrayList<Long> orderedScreenIds);
16664359a53dc827797917a8d9a0697a91996789801Winson Chung        public void bindAddScreens(ArrayList<Long> orderedScreenIds);
167ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato        public void bindFolders(HashMap<Long,FolderInfo> folders);
168e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        public void finishBindingItems(boolean upgradePath);
1699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        public void bindAppWidget(LauncherAppWidgetInfo info);
170eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        public void bindAllApplications(ArrayList<AppInfo> apps);
171d64d1763c590b1c954b759156683abf39ff22945Winson Chung        public void bindAppsAdded(ArrayList<Long> newScreens,
172d64d1763c590b1c954b759156683abf39ff22945Winson Chung                                  ArrayList<ItemInfo> addNotAnimated,
173c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                                  ArrayList<ItemInfo> addAnimated,
174c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                                  ArrayList<AppInfo> addedApps);
175eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        public void bindAppsUpdated(ArrayList<AppInfo> apps);
176aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        public void updatePackageState(String pkgName, int state);
17783892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        public void bindComponentsRemoved(ArrayList<String> packageNames,
178df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                        ArrayList<AppInfo> appInfos);
179c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
180cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath        public void bindSearchablesChanged();
181a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung        public boolean isAllAppsButtonRank(int rank);
1821462de39f01cec0ba785386532719cb0207dd827Adam Cohen        public void onPageBoundSynchronously(int page);
183ede4129e77f8347e32c0da9c615bcff2cbc06fd8Winson Chung        public void dumpLogsToLocalData();
1849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
18531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
18664359a53dc827797917a8d9a0697a91996789801Winson Chung    public interface ItemInfoFilter {
18764359a53dc827797917a8d9a0697a91996789801Winson Chung        public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
18864359a53dc827797917a8d9a0697a91996789801Winson Chung    }
18964359a53dc827797917a8d9a0697a91996789801Winson Chung
1901307f63f8a93541bf6e2d29c11fab69778a25e42Bjorn Bringert    LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
191a694524047fda0a51dede4eefb1201a598d2d3a7Winson Chung        Context context = app.getContext();
192a694524047fda0a51dede4eefb1201a598d2d3a7Winson Chung        ContentResolver contentResolver = context.getContentResolver();
193e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler
194ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
195eb23cb2a90a7f1fa794a375b9d7a111d7f630a02Winson Chung        ContentProviderClient client = contentResolver.acquireContentProviderClient(
196eb23cb2a90a7f1fa794a375b9d7a111d7f630a02Winson Chung                LauncherSettings.Favorites.OLD_CONTENT_URI);
197eb23cb2a90a7f1fa794a375b9d7a111d7f630a02Winson Chung        mOldContentProviderExists = (client != null);
198eb23cb2a90a7f1fa794a375b9d7a111d7f630a02Winson Chung        if (client != null) {
199eb23cb2a90a7f1fa794a375b9d7a111d7f630a02Winson Chung            client.release();
200eb23cb2a90a7f1fa794a375b9d7a111d7f630a02Winson Chung        }
201e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler        mApp = app;
2021307f63f8a93541bf6e2d29c11fab69778a25e42Bjorn Bringert        mBgAllAppsList = new AllAppsList(iconCache, appFilter);
2030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = iconCache;
2040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
205cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler        final Resources res = context.getResources();
20699a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee        Configuration config = res.getConfiguration();
20799a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee        mPreviousConfigMcc = config.mcc;
2080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
2090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
210b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Runs the specified runnable immediately if called from the main thread, otherwise it is
211b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung     * posted on the main thread handler. */
212b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private void runOnMainThread(Runnable r) {
21381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        runOnMainThread(r, 0);
21481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    }
21581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    private void runOnMainThread(Runnable r, int type) {
216b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
217b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we are on the worker thread, post onto the main handler
218b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mHandler.post(r);
219b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        } else {
220b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r.run();
221b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
222b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
223b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
224b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
225b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung     * posted on the worker thread handler. */
226b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    private static void runOnWorkerThread(Runnable r) {
227b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
228b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r.run();
229b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        } else {
230b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // If we are not on the worker thread, then post to the worker handler
231b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sWorker.post(r);
232b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
233b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
234b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
235e43a1e788223ab5a4405c5eeb0133ff57efbc768Winson Chung    boolean canMigrateFromOldLauncherDb(Launcher launcher) {
236e43a1e788223ab5a4405c5eeb0133ff57efbc768Winson Chung        return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
237a694524047fda0a51dede4eefb1201a598d2d3a7Winson Chung    }
238a694524047fda0a51dede4eefb1201a598d2d3a7Winson Chung
239c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
240c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                                 long screen) {
241892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        LauncherAppState app = LauncherAppState.getInstance();
242892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
243892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        final int xCount = (int) grid.numColumns;
244892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        final int yCount = (int) grid.numRows;
245c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        boolean[][] occupied = new boolean[xCount][yCount];
246c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
247c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        int cellX, cellY, spanX, spanY;
248c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        for (int i = 0; i < items.size(); ++i) {
249c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            final ItemInfo item = items.get(i);
250c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
251c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                if (item.screenId == screen) {
252c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    cellX = item.cellX;
253c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    cellY = item.cellY;
254c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    spanX = item.spanX;
255c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    spanY = item.spanY;
256c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
257c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
258c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                            occupied[x][y] = true;
259c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        }
260c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    }
261c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                }
262c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            }
263c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        }
264c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
265c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
266c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    }
267c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
268156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung                                                        Intent launchIntent,
26976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                                        int firstScreenIndex,
27076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                                        ArrayList<Long> workspaceScreens) {
271c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        // Lock on the app so that we don't try and get the items while apps are being added
272c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        LauncherAppState app = LauncherAppState.getInstance();
273c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        LauncherModel model = app.getModel();
274c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        boolean found = false;
275c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        synchronized (app) {
27664359a53dc827797917a8d9a0697a91996789801Winson Chung            if (sWorkerThread.getThreadId() != Process.myTid()) {
27764359a53dc827797917a8d9a0697a91996789801Winson Chung                // Flush the LauncherModel worker thread, so that if we just did another
27864359a53dc827797917a8d9a0697a91996789801Winson Chung                // processInstallShortcut, we give it time for its shortcut to get added to the
27964359a53dc827797917a8d9a0697a91996789801Winson Chung                // database (getItemsInLocalCoordinates reads the database)
28064359a53dc827797917a8d9a0697a91996789801Winson Chung                model.flushWorkerThread();
28164359a53dc827797917a8d9a0697a91996789801Winson Chung            }
282c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
283c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
284c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            // Try adding to the workspace screens incrementally, starting at the default or center
285c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
28676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
28776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            int count = workspaceScreens.size();
288156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung            for (int screen = firstScreenIndex; screen < count && !found; screen++) {
289c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                int[] tmpCoordinates = new int[2];
290c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
29176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                        workspaceScreens.get(screen))) {
292c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                    // Update the Launcher db
29376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
294c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                }
295c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung            }
296c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        }
297c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        return null;
298c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    }
299c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
300aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren    public void setPackageState(final String pkgName, final int state) {
301aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        // Process the updated package state
302aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        Runnable r = new Runnable() {
303aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            public void run() {
304aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
305aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                if (callbacks != null) {
306aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    callbacks.updatePackageState(pkgName, state);
307aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                }
308aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            }
309aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        };
310aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        mHandler.post(r);
311aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren    }
312aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
31376a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen    public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
31476a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
31576a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen
31676a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        if (allAppsApps == null) {
31776a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen            throw new RuntimeException("allAppsApps must not be null");
31876a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        }
31976a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        if (allAppsApps.isEmpty()) {
32076a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen            return;
32176a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        }
32276a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen
323b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
324b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        Iterator<AppInfo> iter = allAppsApps.iterator();
325b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        while (iter.hasNext()) {
326b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            ItemInfo a = iter.next();
327b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            if (LauncherModel.appWasRestored(ctx, a.getIntent())) {
328b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                restoredAppsFinal.add((AppInfo) a);
329b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            }
330b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        }
331b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren
33276a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        // Process the newly added applications and add them to the database first
33376a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        Runnable r = new Runnable() {
33476a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen            public void run() {
33576a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                runOnMainThread(new Runnable() {
33676a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                    public void run() {
33776a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
33876a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                        if (callbacks == cb && cb != null) {
339b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                            if (!restoredAppsFinal.isEmpty()) {
3406d0dde01f307051ee1849481c989d9e87774b894Chris Wren                                for (AppInfo info : restoredAppsFinal) {
3416d0dde01f307051ee1849481c989d9e87774b894Chris Wren                                    final Intent intent = info.getIntent();
3426d0dde01f307051ee1849481c989d9e87774b894Chris Wren                                    if (intent != null) {
3436d0dde01f307051ee1849481c989d9e87774b894Chris Wren                                        mIconCache.deletePreloadedIcon(intent.getComponent());
3446d0dde01f307051ee1849481c989d9e87774b894Chris Wren                                    }
3456d0dde01f307051ee1849481c989d9e87774b894Chris Wren                                }
346b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                                callbacks.bindAppsUpdated(restoredAppsFinal);
347b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                            }
3486d0dde01f307051ee1849481c989d9e87774b894Chris Wren                            callbacks.bindAppsAdded(null, null, null, allAppsApps);
34976a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                        }
35076a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                    }
35176a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                });
35276a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen            }
35376a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        };
35476a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        runOnWorkerThread(r);
355997a92348a6d6e061737399321393449c16cd4d8Winson Chung    }
35676a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen
35776a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen    public void addAndBindAddedWorkspaceApps(final Context context,
35876a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen            final ArrayList<ItemInfo> workspaceApps) {
35976a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
36076a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen
36176a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        if (workspaceApps == null) {
362fe9d96a39651de99819208e95c7ec7d432a402e0Winson Chung            throw new RuntimeException("workspaceApps and allAppsApps must not be null");
363fe9d96a39651de99819208e95c7ec7d432a402e0Winson Chung        }
36476a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen        if (workspaceApps.isEmpty()) {
3659e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung            return;
366997a92348a6d6e061737399321393449c16cd4d8Winson Chung        }
36764359a53dc827797917a8d9a0697a91996789801Winson Chung        // Process the newly added applications and add them to the database first
36864359a53dc827797917a8d9a0697a91996789801Winson Chung        Runnable r = new Runnable() {
36964359a53dc827797917a8d9a0697a91996789801Winson Chung            public void run() {
37064359a53dc827797917a8d9a0697a91996789801Winson Chung                final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
37164359a53dc827797917a8d9a0697a91996789801Winson Chung                final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
372b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
37364359a53dc827797917a8d9a0697a91996789801Winson Chung
37476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // Get the list of workspace screens.  We need to append to this list and
37576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
37676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // called.
37776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                ArrayList<Long> workspaceScreens = new ArrayList<Long>();
37876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
37976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                for (Integer i : orderedScreens.keySet()) {
38076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = orderedScreens.get(i);
38176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    workspaceScreens.add(screenId);
38276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                }
38376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
38464359a53dc827797917a8d9a0697a91996789801Winson Chung                synchronized(sBgLock) {
38594d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                    Iterator<ItemInfo> iter = workspaceApps.iterator();
38664359a53dc827797917a8d9a0697a91996789801Winson Chung                    while (iter.hasNext()) {
387997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        ItemInfo a = iter.next();
38864359a53dc827797917a8d9a0697a91996789801Winson Chung                        final String name = a.title.toString();
389997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        final Intent launchIntent = a.getIntent();
39064359a53dc827797917a8d9a0697a91996789801Winson Chung
39164359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Short-circuit this logic if the icon exists somewhere on the workspace
39264359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (LauncherModel.shortcutExists(context, name, launchIntent)) {
393b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                            // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
394b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                            if (a instanceof AppInfo &&
395b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                                    LauncherModel.appWasRestored(context, launchIntent)) {
396b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                                restoredAppsFinal.add((AppInfo) a);
397b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                            }
39864359a53dc827797917a8d9a0697a91996789801Winson Chung                            continue;
39964359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
40064359a53dc827797917a8d9a0697a91996789801Winson Chung
4018741298d3c6a17015dabc4aacc261fd8eb59992dWinson Chung                        // Add this icon to the db, creating a new page if necessary.  If there
4028741298d3c6a17015dabc4aacc261fd8eb59992dWinson Chung                        // is only the empty page then we just add items to the first page.
4038741298d3c6a17015dabc4aacc261fd8eb59992dWinson Chung                        // Otherwise, we add them to the next pages.
4048741298d3c6a17015dabc4aacc261fd8eb59992dWinson Chung                        int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
40564359a53dc827797917a8d9a0697a91996789801Winson Chung                        Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
40676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                name, launchIntent, startSearchPageIndex, workspaceScreens);
40764359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (coords == null) {
408414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka                            LauncherProvider lp = LauncherAppState.getLauncherProvider();
409c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
410c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // If we can't find a valid position, then just add a new screen.
411c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // This takes time so we need to re-queue the add until the new
412c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // page is added.  Create as many screens as necessary to satisfy
413c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            // the startSearchPageIndex.
414c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
41576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                    workspaceScreens.size());
416c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            while (numPagesToAdd > 0) {
417c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                long screenId = lp.generateNewScreenId();
418c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                // Save the screen id for binding in the workspace
41976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                workspaceScreens.add(screenId);
420c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                addedWorkspaceScreensFinal.add(screenId);
421c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                                numPagesToAdd--;
422c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                            }
42376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
42464359a53dc827797917a8d9a0697a91996789801Winson Chung                            // Find the coordinate again
42564359a53dc827797917a8d9a0697a91996789801Winson Chung                            coords = LauncherModel.findNextAvailableIconSpace(context,
42676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                                    name, launchIntent, startSearchPageIndex, workspaceScreens);
42764359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
42864359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (coords == null) {
42964359a53dc827797917a8d9a0697a91996789801Winson Chung                            throw new RuntimeException("Coordinates should not be null");
43064359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
43164359a53dc827797917a8d9a0697a91996789801Winson Chung
432997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        ShortcutInfo shortcutInfo;
433997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        if (a instanceof ShortcutInfo) {
434997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            shortcutInfo = (ShortcutInfo) a;
435eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                        } else if (a instanceof AppInfo) {
436eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                            shortcutInfo = ((AppInfo) a).makeShortcut();
437997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        } else {
438997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            throw new RuntimeException("Unexpected info type");
439997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        }
44094d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung
44164359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Add the shortcut to the db
44264359a53dc827797917a8d9a0697a91996789801Winson Chung                        addItemToDatabase(context, shortcutInfo,
44364359a53dc827797917a8d9a0697a91996789801Winson Chung                                LauncherSettings.Favorites.CONTAINER_DESKTOP,
44464359a53dc827797917a8d9a0697a91996789801Winson Chung                                coords.first, coords.second[0], coords.second[1], false);
44564359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Save the ShortcutInfo for binding in the workspace
44664359a53dc827797917a8d9a0697a91996789801Winson Chung                        addedShortcutsFinal.add(shortcutInfo);
44764359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
44864359a53dc827797917a8d9a0697a91996789801Winson Chung                }
44964359a53dc827797917a8d9a0697a91996789801Winson Chung
45076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                // Update the workspace screens
45176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                updateWorkspaceScreenOrder(context, workspaceScreens);
45276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
45376a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                if (!addedShortcutsFinal.isEmpty()) {
454997a92348a6d6e061737399321393449c16cd4d8Winson Chung                    runOnMainThread(new Runnable() {
455997a92348a6d6e061737399321393449c16cd4d8Winson Chung                        public void run() {
456997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
457997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            if (callbacks == cb && cb != null) {
458997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
459997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
46094d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                if (!addedShortcutsFinal.isEmpty()) {
46194d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                    ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
46294d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                    long lastScreenId = info.screenId;
46394d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                    for (ItemInfo i : addedShortcutsFinal) {
46494d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                        if (i.screenId == lastScreenId) {
46594d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                            addAnimated.add(i);
46694d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                        } else {
46794d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                            addNotAnimated.add(i);
46894d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                                        }
469997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                    }
470997a92348a6d6e061737399321393449c16cd4d8Winson Chung                                }
471d64d1763c590b1c954b759156683abf39ff22945Winson Chung                                callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
47276a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                                        addNotAnimated, addAnimated, null);
473b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                                if (!restoredAppsFinal.isEmpty()) {
474b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                                    callbacks.bindAppsUpdated(restoredAppsFinal);
475b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                                }
476997a92348a6d6e061737399321393449c16cd4d8Winson Chung                            }
47764359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
478997a92348a6d6e061737399321393449c16cd4d8Winson Chung                    });
479997a92348a6d6e061737399321393449c16cd4d8Winson Chung                }
48064359a53dc827797917a8d9a0697a91996789801Winson Chung            }
48164359a53dc827797917a8d9a0697a91996789801Winson Chung        };
48264359a53dc827797917a8d9a0697a91996789801Winson Chung        runOnWorkerThread(r);
48364359a53dc827797917a8d9a0697a91996789801Winson Chung    }
48464359a53dc827797917a8d9a0697a91996789801Winson Chung
48556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public Bitmap getFallbackIcon() {
4865801ef048adea9ca1a2e31839b73655d3d258b21Winson Chung        if (mDefaultIcon == null) {
4875801ef048adea9ca1a2e31839b73655d3d258b21Winson Chung            final Context context = LauncherAppState.getInstance().getContext();
4885801ef048adea9ca1a2e31839b73655d3d258b21Winson Chung            mDefaultIcon = Utilities.createIconBitmap(
4895801ef048adea9ca1a2e31839b73655d3d258b21Winson Chung                    mIconCache.getFullResDefaultActivityIcon(), context);
4905801ef048adea9ca1a2e31839b73655d3d258b21Winson Chung        }
4910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return Bitmap.createBitmap(mDefaultIcon);
492f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    }
49331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
49481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    public void unbindItemInfosAndClearQueuedBindRunnables() {
49581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        if (sWorkerThread.getThreadId() == Process.myTid()) {
49681b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung            throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
49781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    "main thread");
49881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        }
49981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung
50081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Clear any deferred bind runnables
50181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        mDeferredBindRunnables.clear();
50281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Remove any queued bind runnables
50381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
50481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Unbind all the workspace items
50581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        unbindWorkspaceItemsOnMainThread();
506603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung    }
507603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung
508b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
50981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    void unbindWorkspaceItemsOnMainThread() {
510603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        // Ensure that we don't use the same workspace items data structure on the main thread
511603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        // by making a copy of workspace items first.
512b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
513b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
5142abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung        synchronized (sBgLock) {
515b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            tmpWorkspaceItems.addAll(sBgWorkspaceItems);
516b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            tmpAppWidgets.addAll(sBgAppWidgets);
517b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
518b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        Runnable r = new Runnable() {
519b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                @Override
520b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                public void run() {
521b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   for (ItemInfo item : tmpWorkspaceItems) {
522b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                       item.unbind();
523b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   }
524b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   for (ItemInfo item : tmpAppWidgets) {
525b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                       item.unbind();
526b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   }
527b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
528b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
529b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnMainThread(r);
5304eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen    }
5314eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen
5329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
5339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
5349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
5359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
5369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
537dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long screenId, int cellX, int cellY) {
5389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
5399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
540dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
5419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
5429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
543dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            moveItemInDatabase(context, item, container, screenId, cellX, cellY);
5449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
5459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
54631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
547b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka    static void checkItemInfoLocked(
548b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
549b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        ItemInfo modelItem = sBgItemsIdMap.get(itemId);
550b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        if (modelItem != null && item != modelItem) {
551b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // check all the data is consistent
552b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
553b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
554b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                ShortcutInfo shortcut = (ShortcutInfo) item;
555b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
556b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.intent.filterEquals(shortcut.intent) &&
557b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.id == shortcut.id &&
558b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.itemType == shortcut.itemType &&
559b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.container == shortcut.container &&
560dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        modelShortcut.screenId == shortcut.screenId &&
561b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.cellX == shortcut.cellX &&
562b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.cellY == shortcut.cellY &&
563b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.spanX == shortcut.spanX &&
564b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.spanY == shortcut.spanY &&
565b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
566b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        (modelShortcut.dropPos != null &&
567b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                                shortcut.dropPos != null &&
568b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                                modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
569b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
570b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    // For all intents and purposes, this is the same object
571b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    return;
572b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                }
573b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
574b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka
575b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // the modelItem needs to match up perfectly with item if our model is
576b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // to be consistent with the database-- for now, just require
577b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // modelItem == item or the equality check above
578b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            String msg = "item: " + ((item != null) ? item.toString() : "null") +
579b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    "modelItem: " +
580b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    ((modelItem != null) ? modelItem.toString() : "null") +
581b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    "Error: ItemInfo passed to checkItemInfo doesn't match original";
582b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            RuntimeException e = new RuntimeException(msg);
583b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            if (stackTrace != null) {
584b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                e.setStackTrace(stackTrace);
585b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
586b9ada65af30c61c5b35da9b9efac75ffe6c56fd7Adam Cohen            throw e;
587b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        }
588b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka    }
589b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka
590816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka    static void checkItemInfo(final ItemInfo item) {
591816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
592816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        final long itemId = item.id;
593816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        Runnable r = new Runnable() {
594b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            public void run() {
595b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                synchronized (sBgLock) {
596b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    checkItemInfoLocked(itemId, item, stackTrace);
597816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka                }
598b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
599b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        };
600b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
601816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka    }
602816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka
603c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void updateItemInDatabaseHelper(Context context, final ContentValues values,
604c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            final ItemInfo item, final String callingFunction) {
605c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final long itemId = item.id;
606c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
607c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ContentResolver cr = context.getContentResolver();
608c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
609487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
610c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
611c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            public void run() {
612c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.update(uri, values, null, null);
613f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                updateItemArrays(item, itemId, stackTrace);
614f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
615f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        };
616f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        runOnWorkerThread(r);
617f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
618c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
619f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
620f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final ArrayList<ItemInfo> items, final String callingFunction) {
621f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        final ContentResolver cr = context.getContentResolver();
622c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
623f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
624f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        Runnable r = new Runnable() {
625f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            public void run() {
626f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                ArrayList<ContentProviderOperation> ops =
627f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        new ArrayList<ContentProviderOperation>();
628f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                int count = items.size();
629f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                for (int i = 0; i < count; i++) {
630f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ItemInfo item = items.get(i);
631f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    final long itemId = item.id;
632f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
633f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ContentValues values = valuesList.get(i);
634f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
635f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
636f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    updateItemArrays(item, itemId, stackTrace);
637487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
638f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
639f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                try {
640f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    cr.applyBatch(LauncherProvider.AUTHORITY, ops);
641f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                } catch (Exception e) {
642f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    e.printStackTrace();
643c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                }
644c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            }
645c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
646b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
647c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    }
648bebf042666cffe52039b875a549a582abd78a431Adam Cohen
649f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
650f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        // Lock on mBgLock *after* the db operation
651f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        synchronized (sBgLock) {
652f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            checkItemInfoLocked(itemId, item, stackTrace);
653f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
654f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
655f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
656f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                // Item is in a folder, make sure this folder exists
657f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                if (!sBgFolders.containsKey(item.container)) {
658f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    // An items container is being set to a that of an item which is not in
659f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    // the list of Folders.
660f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    String msg = "item: " + item + " container being set to: " +
661f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                            item.container + ", not in the list of folders";
662f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    Log.e(TAG, msg);
663f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
664f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
665f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
666f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // Items are added/removed from the corresponding FolderInfo elsewhere, such
667f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // as in Workspace.onDrop. Here, we just add/remove them from the list of items
668f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // that are on the desktop, as appropriate
669f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo modelItem = sBgItemsIdMap.get(itemId);
67033231f587e1a6f716af68e6198df774d2847a8e5Winson Chung            if (modelItem != null &&
67133231f587e1a6f716af68e6198df774d2847a8e5Winson Chung                    (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
67233231f587e1a6f716af68e6198df774d2847a8e5Winson Chung                     modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
673f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                switch (modelItem.itemType) {
674f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
675f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
676f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
677f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        if (!sBgWorkspaceItems.contains(modelItem)) {
678f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                            sBgWorkspaceItems.add(modelItem);
679f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        }
680f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        break;
681f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    default:
682f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        break;
683f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
684f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            } else {
685f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                sBgWorkspaceItems.remove(modelItem);
686f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
687f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
688f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
689f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
690c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    public void flushWorkerThread() {
691c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        mFlushingWorkerThread = true;
692c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        Runnable waiter = new Runnable() {
693c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                public void run() {
694c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    synchronized (this) {
695c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        notifyAll();
696c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        mFlushingWorkerThread = false;
697c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    }
698c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
699c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            };
700c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka
701c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        synchronized(waiter) {
702c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            runOnWorkerThread(waiter);
703c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            if (mLoaderTask != null) {
704c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                synchronized(mLoaderTask) {
705c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    mLoaderTask.notify();
706c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
707c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            }
708c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            boolean success = false;
709c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            while (!success) {
710c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                try {
711c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    waiter.wait();
712c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    success = true;
713c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                } catch (InterruptedException e) {
714c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
715c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            }
716c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        }
717c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    }
718c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka
7199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
7209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
7219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
722a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
723dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY) {
7249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
7259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
7269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
727c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
7283d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // We store hotseat items in canonical form which is this orientation invariant position
7293d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // in the hotseat
730dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
7313d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
732dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
7333d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
734dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
7353d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
73631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
7379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
7389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
7393d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
7403d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
741dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
742ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
743c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
74431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
74531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
74631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
747f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
748f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     * cellX, cellY have already been updated on the ItemInfos.
749f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     */
750f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
751f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final long container, final int screen) {
752f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
753f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
754f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        int count = items.size();
755f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
756f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        for (int i = 0; i < count; i++) {
757f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo item = items.get(i);
758f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            item.container = container;
759f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
760f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // We store hotseat items in canonical form which is this orientation invariant position
761f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // in the hotseat
762f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (context instanceof Launcher && screen < 0 &&
763f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
764dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
765f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        item.cellY);
766f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            } else {
767dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = screen;
768f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
769f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
770f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final ContentValues values = new ContentValues();
771f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CONTAINER, item.container);
772f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CELLX, item.cellX);
773f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CELLY, item.cellY);
774dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
775f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
776f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            contentValues.add(values);
777f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
778f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
779f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
780f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
781f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    /**
782bebf042666cffe52039b875a549a582abd78a431Adam Cohen     * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
783d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen     */
784bebf042666cffe52039b875a549a582abd78a431Adam Cohen    static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
785dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
7860f84a604563037ea65883b99d30e7ff802d986c2Winson Chung        item.container = container;
787d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellX = cellX;
788d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellY = cellY;
789bebf042666cffe52039b875a549a582abd78a431Adam Cohen        item.spanX = spanX;
790bebf042666cffe52039b875a549a582abd78a431Adam Cohen        item.spanY = spanY;
791bebf042666cffe52039b875a549a582abd78a431Adam Cohen
792bebf042666cffe52039b875a549a582abd78a431Adam Cohen        // We store hotseat items in canonical form which is this orientation invariant position
793bebf042666cffe52039b875a549a582abd78a431Adam Cohen        // in the hotseat
794dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
795bebf042666cffe52039b875a549a582abd78a431Adam Cohen                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
796dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
797bebf042666cffe52039b875a549a582abd78a431Adam Cohen        } else {
798dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
799bebf042666cffe52039b875a549a582abd78a431Adam Cohen        }
800d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
801d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        final ContentValues values = new ContentValues();
802d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
803bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
804bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
805bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.SPANX, item.spanX);
806bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.SPANY, item.spanY);
807dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
808d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
809816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
810bebf042666cffe52039b875a549a582abd78a431Adam Cohen    }
811c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
812c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    /**
813c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * Update an item to the database in a specified container.
814c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     */
815c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void updateItemInDatabase(Context context, final ItemInfo item) {
816c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ContentValues values = new ContentValues();
817c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        item.onAddToDatabase(values);
818c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
819c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
820d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    }
821d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
822d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    /**
8239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
8249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
825bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
8269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
8279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
8289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
8299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
8309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
8319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
8329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
8339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
8349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
8359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
8369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
8379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
838bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
839bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
840bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
841b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren     * Returns true if the shortcuts already exists in the database.
842b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren     * we identify a shortcut by the component name of the intent.
843b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren     */
844b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren    static boolean appWasRestored(Context context, Intent intent) {
845b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final ContentResolver cr = context.getContentResolver();
846b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final ComponentName component = intent.getComponent();
847b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        if (component == null) {
848b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            return false;
849b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        }
850b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        String componentName = component.flattenToString();
851b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
852b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
853b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                new String[]{"intent", "restored"}, where, null, null);
854b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        boolean result = false;
855b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        try {
856b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            result = c.moveToFirst();
857b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        } finally {
858b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            c.close();
859b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        }
860b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
861b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        return result;
862b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren    }
863b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren
864b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren    /**
865aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
866aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
867aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
868aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
869aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
870aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
871aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
872aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
873aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
874aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
875aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
876aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
877aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
878aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
879aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
880aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
881aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
882aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
883aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
884aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
885aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
886c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                ItemInfo item = new ItemInfo();
887aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
888aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
88961c69869b3947f1d242b07384bb4e9cd5b266915Winson Chung                item.spanX = Math.max(1, c.getInt(spanXIndex));
89061c69869b3947f1d242b07384bb4e9cd5b266915Winson Chung                item.spanY = Math.max(1, c.getInt(spanYIndex));
891aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
892aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
893dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = c.getInt(screenIndex);
894aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
895aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
896aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
897aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
898aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
899aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
900aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
901aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
902aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
903aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
904aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
905aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
906aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
9079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
90831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
9099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
9109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
9119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
9129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
9139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
914df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
915f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
9169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
9179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
9189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
9199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
9209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
9219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
9229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
9239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
924f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
9259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
9269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
927df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
928df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        folderInfo = findOrMakeFolder(folderList, id);
9299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
9309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
93131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
9339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
9349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
935dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                folderInfo.screenId = c.getInt(screenIndex);
936d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
937d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
938ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
9399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
9409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
9419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
9429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
9432fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
944ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
9459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
9469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
94731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
9499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
9509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
9519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
9523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    static void addItemToDatabase(Context context, final ItemInfo item, final long container,
953dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY, final boolean notify) {
9549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
9559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
9569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
9573d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // We store hotseat items in canonical form which is this orientation invariant position
9583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // in the hotseat
959dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
9603d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
961dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
9623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
963dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
9643d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
965ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
9669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
9679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
9689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
969ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
970414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka        item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
971a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        values.put(LauncherSettings.Favorites._ID, item.id);
9723d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
973aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
974c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
975a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            public void run() {
976a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
977a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                        LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
978ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
979b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
9802abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
981b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    checkItemInfoLocked(item.id, item, null);
982b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.put(item.id, item);
983b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    switch (item.itemType) {
984b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
985b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgFolders.put(item.id, (FolderInfo) item);
986b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            // Fall through
987b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
988b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
989b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
990b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
991b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgWorkspaceItems.add(item);
992b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            } else {
993b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!sBgFolders.containsKey(item.container)) {
994b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // Adding an item to a folder that doesn't exist.
995b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String msg = "adding item: " + item + " to a folder that " +
996b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            " doesn't exist";
99728b3e10fdea4d719b46d2e0da9a916bacc238fd1Adam Cohen                                    Log.e(TAG, msg);
998b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
999487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                            }
1000b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1001b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1002b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1003b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1004b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1005a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                }
1006a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            }
1007c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
1008b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
100931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
101031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1012aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
1013aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
1014845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka    static int getCellLayoutChildId(
1015dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
10163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return (((int) container & 0xFF) << 24)
1017dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
1018aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
1019aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1020aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
1021c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * Removes the specified item from the database
1022c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * @param context
1023c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * @param item
10249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1025c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void deleteItemFromDatabase(Context context, final ItemInfo item) {
10269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1027c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
1028487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
102983df188ce0822f4058406e5f2878d1cf34bf413eMichael Jurka        Runnable r = new Runnable() {
1030a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            public void run() {
1031c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(uriToDelete, null, null);
1032b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1033b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
10342abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1035b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    switch (item.itemType) {
1036b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1037b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgFolders.remove(item.id);
1038b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            for (ItemInfo info: sBgItemsIdMap.values()) {
1039b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (info.container == item.id) {
1040b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // We are deleting a folder which still contains items that
1041b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // think they are contained by that folder.
1042b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String msg = "deleting a folder (" + item + ") which still " +
1043b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            "contains items (" + info + ")";
104428b3e10fdea4d719b46d2e0da9a916bacc238fd1Adam Cohen                                    Log.e(TAG, msg);
1045b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1046487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                            }
1047b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgWorkspaceItems.remove(item);
1048b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1049b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1050b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1051b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgWorkspaceItems.remove(item);
1052b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1053b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1054b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1055b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1056b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1057b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.remove(item.id);
1058b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgDbIconCache.remove(item);
1059a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                }
1060a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            }
106183df188ce0822f4058406e5f2878d1cf34bf413eMichael Jurka        };
1062b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
106331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
106431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1066dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * Update the order of the workspace screens in the database. The array list contains
1067dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * a list of screen ids in the order that they should appear.
1068dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     */
1069c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
1070a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        // Log to disk
1071a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1072a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        Launcher.addDumpLog(TAG, "11683562 -   screens: " + TextUtils.join(", ", screens), true);
1073a90303bb6fdad59e4a55855edba68c9902192599Winson Chung
107464359a53dc827797917a8d9a0697a91996789801Winson Chung        final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
1075dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final ContentResolver cr = context.getContentResolver();
1076dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1077dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1078dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        // Remove any negative screen ids -- these aren't persisted
107964359a53dc827797917a8d9a0697a91996789801Winson Chung        Iterator<Long> iter = screensCopy.iterator();
1080dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        while (iter.hasNext()) {
1081dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long id = iter.next();
1082dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (id < 0) {
1083dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                iter.remove();
1084dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1085dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
1086dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1087dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        Runnable r = new Runnable() {
1088dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            @Override
1089dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            public void run() {
1090085c853a5702c45865e9b017d21fa15cf2b151b9Yura                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
1091dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                // Clear the table
1092085c853a5702c45865e9b017d21fa15cf2b151b9Yura                ops.add(ContentProviderOperation.newDelete(uri).build());
109376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                int count = screensCopy.size();
1094dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                for (int i = 0; i < count; i++) {
1095dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    ContentValues v = new ContentValues();
109676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = screensCopy.get(i);
1097dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1098dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1099085c853a5702c45865e9b017d21fa15cf2b151b9Yura                    ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
1100085c853a5702c45865e9b017d21fa15cf2b151b9Yura                }
1101085c853a5702c45865e9b017d21fa15cf2b151b9Yura
1102085c853a5702c45865e9b017d21fa15cf2b151b9Yura                try {
1103085c853a5702c45865e9b017d21fa15cf2b151b9Yura                    cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1104085c853a5702c45865e9b017d21fa15cf2b151b9Yura                } catch (Exception ex) {
1105085c853a5702c45865e9b017d21fa15cf2b151b9Yura                    throw new RuntimeException(ex);
1106dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
11079e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
1108ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                synchronized (sBgLock) {
1109ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    sBgWorkspaceScreens.clear();
1110ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    sBgWorkspaceScreens.addAll(screensCopy);
11114caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                }
1112dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1113dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        };
1114dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        runOnWorkerThread(r);
1115dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
1116dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1117dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    /**
11189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
11199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1120a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
11219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1122f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1123c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
1124c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            public void run() {
1125c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
1126b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
11272abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1128b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.remove(info.id);
1129b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgFolders.remove(info.id);
1130b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgDbIconCache.remove(info);
1131b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgWorkspaceItems.remove(info);
1132b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1133c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
1134c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1135c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                        LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
1136b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
11372abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1138b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (ItemInfo childInfo : info.contents) {
1139b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        sBgItemsIdMap.remove(childInfo.id);
1140b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        sBgDbIconCache.remove(childInfo);
1141b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1142afb01ee74243cede19088e694ca82cea5983c603Adam Cohen                }
1143c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            }
1144c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
1145b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
11469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
1147f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
11489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
11499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
11509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
11519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
11529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
11539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
1154f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
1155f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
1156f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
11571d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
11589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
11599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
11609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1161cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    @Override
1162f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
11633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
1164aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
11653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
11669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
11673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
11683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
11693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
11703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
11713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1172f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
11733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
117464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
11753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
11763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
11773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
117956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
11803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
11813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
11823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
11833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
11843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
118556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
11863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
11873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
11883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
11893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
11913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
11923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
119356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
11943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
119556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
11963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
11973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1198f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
1199f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
12003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
12013ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1202cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
12033ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            if (!replacing) {
12043ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
12053ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                if (mAppsCanBeOnRemoveableStorage) {
12063ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                    // Only rebind if we support removable storage.  It catches the case where
12073ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                    // apps on the external sd card need to be reloaded
12083ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                    startLoaderFromBackground();
12093ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                }
12103ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            } else {
12113ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                // If we are replacing then just update the packages in the list
12123ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
12133ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                        packages));
12143ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            }
12153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
12163ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
12173ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            if (!replacing) {
12183ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
12193ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                enqueuePackageUpdated(new PackageUpdatedTask(
12203ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                            PackageUpdatedTask.OP_UNAVAILABLE, packages));
12213ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            }
12223ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            // else, we are replacing the packages, so ignore this event and wait for
12233ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
1224e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
122593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // If we have changed locale we need to clear out the labels in all apps/workspace.
122693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            forceReload();
122793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
122893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // Check if configuration change was an mcc/mnc change which would affect app resources
122993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // and we would need to clear out the labels in all apps/workspace. Same handling as
123093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // above for ACTION_LOCALE_CHANGED
123193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             Configuration currentConfig = context.getResources().getConfiguration();
123299a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee             if (mPreviousConfigMcc != currentConfig.mcc) {
123393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                   Log.d(TAG, "Reload apps on config change. curr_mcc:"
123499a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee                       + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
123593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                   forceReload();
123693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             }
123793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // Update previousConfig
123899a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee             mPreviousConfigMcc = currentConfig.mcc;
1239cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1240cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung                   SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
1241ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka            if (mCallbacks != null) {
1242ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                Callbacks callbacks = mCallbacks.get();
1243ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                if (callbacks != null) {
1244ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                    callbacks.bindSearchablesChanged();
1245ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                }
1246cfdf7ee64db8820d91a1cd82bf7b961fb2083dceWinson Chung            }
1247e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
1248e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    }
1249e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato
125093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    private void forceReload() {
1251f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        resetLoadedState(true, true);
1252f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1253f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Do this here because if the launcher activity is running it will be restarted.
1254f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // If it's not running startLoaderFromBackground will merely tell it that it needs
1255f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // to reload.
1256f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        startLoaderFromBackground();
1257f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    }
1258f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1259f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
126093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        synchronized (mLock) {
126193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // Stop any existing loaders first, so they don't set mAllAppsLoaded or
126293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // mWorkspaceLoaded to true later
126393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            stopLoaderLocked();
1264f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            if (resetAllAppsLoaded) mAllAppsLoaded = false;
1265f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
126693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        }
126793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    }
126893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1269e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    /**
1270e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * When the launcher is in the background, it's possible for it to miss paired
1271e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * configuration changes.  So whenever we trigger the loader from the background
1272e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * tell the launcher that it needs to re-run the loader when it comes back instead
1273e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * of doing it now.
1274e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     */
1275e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    public void startLoaderFromBackground() {
1276e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        boolean runLoader = false;
1277e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (mCallbacks != null) {
1278e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            Callbacks callbacks = mCallbacks.get();
1279e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            if (callbacks != null) {
1280e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                // Only actually run the loader if they're not paused.
1281e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                if (!callbacks.setLoadOnResume()) {
1282e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                    runLoader = true;
1283e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                }
1284e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            }
1285e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
1286e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (runLoader) {
12877aff399974c756930070d82d7b2df88f125dacd6Derek Prothro            startLoader(false, PagedView.INVALID_RESTORE_PAGE);
1288f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
12893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1290f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
129193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    // If there is already a loader task running, tell it to stop.
129293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    // returns true if isLaunching() was true on the old task
129393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    private boolean stopLoaderLocked() {
129493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        boolean isLaunching = false;
129593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        LoaderTask oldTask = mLoaderTask;
129693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        if (oldTask != null) {
129793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            if (oldTask.isLaunching()) {
129893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                isLaunching = true;
129993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            }
130093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            oldTask.stopLocked();
130193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        }
130293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        return isLaunching;
130393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    }
130493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1305b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    public void startLoader(boolean isLaunching, int synchronousBindPage) {
1306d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler        startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1307d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler    }
1308d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler
1309d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler    public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
13103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
13113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
13123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
13133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1314843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
1315b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Clear any deferred bind-runnables from the synchronized load process
1316b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // We must do this before any loading/binding is scheduled below.
1317b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
1318b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
13193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
13203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
13213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
132293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                // also, don't downgrade isLaunching if we're already running
132393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                isLaunching = isLaunching || stopLoaderLocked();
1324d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
13257aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
13267aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                        && mAllAppsLoaded && mWorkspaceLoaded) {
1327b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1328b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
1329b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1330b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sWorker.post(mLoaderTask);
1331b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1332b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1333b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
1334b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
1335b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1336b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    void bindRemainingSynchronousPages() {
1337b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        // Post the remaining side pages to be loaded
1338b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (!mDeferredBindRunnables.isEmpty()) {
1339b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (final Runnable r : mDeferredBindRunnables) {
134081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
1341b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
1342b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
1343b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
13443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1345b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
13463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
13473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
13483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
13493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
13509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
1351f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
13523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1353b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
135476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    /** Loads the workspace screens db into a map of Rank -> ScreenId */
135576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
135676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final ContentResolver contentResolver = context.getContentResolver();
135776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
135876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
135976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
136076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
136176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        try {
136276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            final int idIndex = sc.getColumnIndexOrThrow(
136376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    LauncherSettings.WorkspaceScreens._ID);
136476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            final int rankIndex = sc.getColumnIndexOrThrow(
136576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    LauncherSettings.WorkspaceScreens.SCREEN_RANK);
136676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            while (sc.moveToNext()) {
136776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                try {
136876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = sc.getLong(idIndex);
136976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    int rank = sc.getInt(rankIndex);
137076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    orderedScreens.put(rank, screenId);
137176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                } catch (Exception e) {
1372ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
137376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                }
137476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            }
137576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        } finally {
137676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            sc.close();
137776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        }
1378a90303bb6fdad59e4a55855edba68c9902192599Winson Chung
1379a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        // Log to disk
1380a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1381a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1382a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        for (Integer i : orderedScreens.keySet()) {
1383a90303bb6fdad59e4a55855edba68c9902192599Winson Chung            orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1384a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        }
1385a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        Launcher.addDumpLog(TAG, "11683562 -   screens: " +
1386a90303bb6fdad59e4a55855edba68c9902192599Winson Chung                TextUtils.join(", ", orderedScreensPairs), true);
138776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        return orderedScreens;
138876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    }
138976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
1390c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    public boolean isAllAppsLoaded() {
1391c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        return mAllAppsLoaded;
1392c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    }
1393c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka
139436a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    boolean isLoadingWorkspace() {
139536a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        synchronized (mLock) {
139636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            if (mLoaderTask != null) {
139736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung                return mLoaderTask.isLoadingWorkspace();
139836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            }
139936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        }
140036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        return false;
140136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    }
140236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
14033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
14043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
14053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
14063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
14073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
14083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
14093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
14103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
14113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
141236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        private boolean mIsLoadingAndBindingWorkspace;
14133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
14143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
1415d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler        private int mFlags;
1416b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1417c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        private HashMap<Object, CharSequence> mLabelCache;
14183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1419d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler        LoaderTask(Context context, boolean isLaunching, int flags) {
14203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
14213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
1422c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = new HashMap<Object, CharSequence>();
1423d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            mFlags = flags;
14243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1425f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
14263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
14273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
14283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1429f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
143036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        boolean isLoadingWorkspace() {
143136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            return mIsLoadingAndBindingWorkspace;
143236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        }
143336a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
1434c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        /** Returns whether this is an upgrade path */
1435c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private boolean loadAndBindWorkspace() {
143636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            mIsLoadingAndBindingWorkspace = true;
143736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
14383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
14393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
14403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
14413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1442288a36b563801a26d00084cb823435a3ba4cb76cMichael Jurka
1443c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean isUpgradePath = false;
1444a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            if (!mWorkspaceLoaded) {
1445c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                isUpgradePath = loadWorkspace();
144693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                synchronized (LoaderTask.this) {
144793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    if (mStopped) {
1448c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        return isUpgradePath;
144993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    }
145093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    mWorkspaceLoaded = true;
14519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
14523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1453cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
14543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
1455c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            bindWorkspace(-1, isUpgradePath);
1456c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            return isUpgradePath;
14573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1458f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
14593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
14603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
14613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
14623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
14633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
14643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1465843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
14663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
14673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
14683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
14693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
14703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
14713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
14729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
14733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
14749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
14759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
14763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
14773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1478c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
14793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
1480c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        // Just in case mFlushingWorkerThread changes but we aren't woken up,
1481c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        // wait no longer than 1sec at a time
1482c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        this.wait(1000);
14833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
14843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
14859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
14863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
14873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
14883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
1489aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
14903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
14919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
1492843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
14933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1494f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1495b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        void runBindSynchronousPage(int synchronousBindPage) {
14967aff399974c756930070d82d7b2df88f125dacd6Derek Prothro            if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
1497b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Ensure that we have a valid page index to load synchronously
1498b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1499b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        "valid page index");
1500b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1501b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1502b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Ensure that we don't try and bind a specified page when the pages have not been
1503b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // loaded already (we should load everything asynchronously in that case)
1504b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1505b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1506b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            synchronized (mLock) {
1507b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (mIsLoaderTaskRunning) {
1508b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // Ensure that we are never running the background loading at this point since
1509b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // we also touch the background collections
1510b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    throw new RuntimeException("Error! Background loading is already running");
1511b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1512b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1513b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1514b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: Throw an exception if we are already loading (since we touch the worker thread
1515b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      data structures, we can't allow any other thread to touch that data, but because
1516b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      this call is synchronous, we can get away with not locking).
1517b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1518cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler            // The LauncherModel is static in the LauncherAppState and mHandler may have queued
1519a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            // operations from the previous activity.  We need to ensure that all queued operations
1520a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            // are executed before any synchronous binding work is done.
1521a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            mHandler.flush();
1522a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen
1523b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Divide the set of loaded items into those that we are binding synchronously, and
1524b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // everything else that is to be bound normally (asynchronously).
1525c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            bindWorkspace(synchronousBindPage, false);
1526b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: For now, continue posting the binding of AllApps as there are other issues that
1527b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      arise from that.
1528b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            onlyBindAllApps();
1529b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
1530b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
15313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
1532c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean isUpgrade = false;
1533c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1534b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            synchronized (mLock) {
1535b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                mIsLoaderTaskRunning = true;
1536b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
15373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
15383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
15393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
15403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
1541843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
1542843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
1543dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
1544aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                    if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1545aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                            (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
1546843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
1547843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1548dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
154964359a53dc827797917a8d9a0697a91996789801Winson Chung                if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1550c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                isUpgrade = loadAndBindWorkspace();
1551843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
15523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
15533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
15543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
15553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
15563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
15573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
15589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
1559843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
1560aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                        if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
1561843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
15629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
15639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
15643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
1565f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1566843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
156764359a53dc827797917a8d9a0697a91996789801Winson Chung                if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
156864359a53dc827797917a8d9a0697a91996789801Winson Chung                loadAndBindAllApps();
15697ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung
15707ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                // Restore the default thread priority after we are done loading items
15717ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                synchronized (mLock) {
15727ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                    android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
15737ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                }
15743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1575843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
1576aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // Update the saved icons if necessary
1577aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
15782abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
1579b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                for (Object key : sBgDbIconCache.keySet()) {
1580b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1581b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1582b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgDbIconCache.clear();
1583aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            }
1584aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung
158516f3ea870aac47292cd6cbe1a4b4343173097aa9Nilesh Agrawal            if (LauncherAppState.isDisableAllApps()) {
1586c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                // Ensure that all the applications that are in the system are
1587c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                // represented on the home screen.
1588c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
1589c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                    verifyApplications();
1590c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                }
1591c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1592c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
15933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
15943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
15953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
1596f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
15973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
15983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
15993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
16003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
1601843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
1602b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                mIsLoaderTaskRunning = false;
16033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
16043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1605f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
16063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
16073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
16083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
16093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
1610f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
16113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1612f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
16133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
16143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
16153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
16163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
16173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
16183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
16193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
16203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
16213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
16223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
16233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
16243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1625ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
16263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
16273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
16283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1629f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
16303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
16313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
16323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
16333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
16343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
16353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
16363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
16379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
16383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
16393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
1640f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
16413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1642f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1643c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private void verifyApplications() {
1644c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            final Context context = mApp.getContext();
1645c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1646c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Cross reference all the applications in our apps list with items in the workspace
1647c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            ArrayList<ItemInfo> tmpInfos;
1648695ff6b6fada4e1037592d7c4961321e12890b7dMichael Jurka            ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
1649c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            synchronized (sBgLock) {
1650eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                for (AppInfo app : mBgAllAppsList.data) {
1651c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    tmpInfos = getItemInfoForComponentName(app.componentName);
1652c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    if (tmpInfos.isEmpty()) {
1653c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        // We are missing an application icon, so add this to the workspace
1654c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        added.add(app);
1655c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        // This is a rare event, so lets log it
1656c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        Log.e(TAG, "Missing Application on load: " + app);
1657c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    }
1658c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                }
1659c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1660c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            if (!added.isEmpty()) {
166176a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                addAndBindAddedWorkspaceApps(context, added);
1662c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1663c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        }
1664c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
16653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
1666a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung        private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1667ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                           AtomicBoolean deleteOnInvalidPlacement) {
1668892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            LauncherAppState app = LauncherAppState.getInstance();
1669892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1670295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler            final int countX = (int) grid.numColumns;
1671295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler            final int countY = (int) grid.numRows;
1672892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung
1673dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long containerIndex = item.screenId;
1674f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1675a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                // Return early if we detect that an item is under the hotseat button
1676a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                if (mCallbacks == null ||
1677a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                        mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1678ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    deleteOnInvalidPlacement.set(true);
1679295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                    Log.e(TAG, "Error loading shortcut into hotseat " + item
1680295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                            + " into position (" + item.screenId + ":" + item.cellX + ","
1681295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                            + item.cellY + ") occupied by all apps");
1682a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                    return false;
1683a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                }
1684a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung
1685295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                final ItemInfo[][] hotseatItems =
1686295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                        occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1687295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler
1688ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                if (item.screenId >= grid.numHotseatIcons) {
1689ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    Log.e(TAG, "Error loading shortcut " + item
1690ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                            + " into hotseat position " + item.screenId
1691ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                            + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1692ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                            + ")");
1693ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    return false;
1694ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                }
1695ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen
1696295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                if (hotseatItems != null) {
1697295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                    if (hotseatItems[(int) item.screenId][0] != null) {
1698dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        Log.e(TAG, "Error loading shortcut into hotseat " + item
1699dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + " into position (" + item.screenId + ":" + item.cellX + ","
1700dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + item.cellY + ") occupied by "
1701dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1702dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                [(int) item.screenId][0]);
1703dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            return false;
1704295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                    } else {
1705295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                        hotseatItems[(int) item.screenId][0] = item;
1706295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                        return true;
1707dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
17086ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                } else {
1709ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
1710dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    items[(int) item.screenId][0] = item;
1711dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
17126ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                    return true;
17136ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                }
1714f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung            } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1715f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung                // Skip further checking if it is not the hotseat or workspace container
17163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
17173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1718f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung
1719dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (!occupied.containsKey(item.screenId)) {
1720892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
1721dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                occupied.put(item.screenId, items);
1722dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1723dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1724295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler            final ItemInfo[][] screens = occupied.get(item.screenId);
1725ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1726ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    item.cellX < 0 || item.cellY < 0 ||
1727ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1728ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                Log.e(TAG, "Error loading shortcut " + item
1729ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                        + " into cell (" + containerIndex + "-" + item.screenId + ":"
1730ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                        + item.cellX + "," + item.cellY
1731ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                        + ") out of screen bounds ( " + countX + "x" + countY + ")");
1732ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                return false;
1733ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen            }
1734ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen
17356ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung            // Check if any workspace icons overlap with each other
17363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
17373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
1738dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (screens[x][y] != null) {
17393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
1740dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            + " into cell (" + containerIndex + "-" + item.screenId + ":"
17413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
1742aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
1743dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            + screens[x][y]);
17443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
17458802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
17468802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
17473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
17483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
17493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
1750dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    screens[x][y] = item;
17518802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
17528802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
1753f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung
17543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
17553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
17568802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
1757ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        /** Clears all the sBg data structures */
1758ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        private void clearSBgDataStructures() {
1759ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            synchronized (sBgLock) {
1760ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgWorkspaceItems.clear();
1761ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgAppWidgets.clear();
1762ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgFolders.clear();
1763ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgItemsIdMap.clear();
1764ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgDbIconCache.clear();
1765ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgWorkspaceScreens.clear();
1766ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            }
1767ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        }
1768ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung
1769d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler        /** Returns whether this is an upgrade path */
1770c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private boolean loadWorkspace() {
17719f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung            // Log to disk
17729f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung            Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
17739f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung
17743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1775f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
17763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
17773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
17783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
17793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
17803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
1781f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1782892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            LauncherAppState app = LauncherAppState.getInstance();
1783892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1784892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            int countX = (int) grid.numColumns;
1785892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            int countY = (int) grid.numRows;
1786892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung
1787d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1788d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1789d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                LauncherAppState.getLauncherProvider().deleteDatabase();
1790d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            }
1791d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler
1792d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1793d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                // append the user's Launcher2 shortcuts
1794d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1795d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1796d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            } else {
1797d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                // Make sure the default workspace is loaded
1798d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1799d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1800d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            }
1801e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
1802c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Check if we need to do any upgrade-path logic
1803f0b8dacf1516e0d901c71109c906dc15ef855fc5Dan Sandler            // (Includes having just imported default favorites)
1804414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka            boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
1805f0b8dacf1516e0d901c71109c906dc15ef855fc5Dan Sandler
18069f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung            // Log to disk
18079f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung            Launcher.addDumpLog(TAG, "11683562 -   loadedOldDb: " + loadedOldDb, true);
1808b85f8a44b51258f22938773ca30dd85845345010Michael Jurka
18092abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
1810ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                clearSBgDataStructures();
1811b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1812b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
1813f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                final ArrayList<Long> restoredRows = new ArrayList<Long>();
1814c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
1815e523e70f75ff90ca5d613fb8398d37208e6cd32dChris Wren                if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
1816e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                final Cursor c = contentResolver.query(contentUri, null, null, null, null);
1817b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1818b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // +1 for the hotseat (it can be larger than the workspace)
1819b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Load workspace in reverse order to ensure that latest items are loaded first (and
1820b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // before any earlier duplicates)
1821dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
1822b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1823b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                try {
1824b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1825b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int intentIndex = c.getColumnIndexOrThrow
1826b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.INTENT);
1827b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int titleIndex = c.getColumnIndexOrThrow
1828b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.TITLE);
1829b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconTypeIndex = c.getColumnIndexOrThrow(
1830b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_TYPE);
1831b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1832b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconPackageIndex = c.getColumnIndexOrThrow(
1833b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_PACKAGE);
1834b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconResourceIndex = c.getColumnIndexOrThrow(
1835b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_RESOURCE);
1836b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int containerIndex = c.getColumnIndexOrThrow(
1837b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.CONTAINER);
1838b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int itemTypeIndex = c.getColumnIndexOrThrow(
1839b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ITEM_TYPE);
1840b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1841b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.APPWIDGET_ID);
1842c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                    final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1843c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                            LauncherSettings.Favorites.APPWIDGET_PROVIDER);
1844b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int screenIndex = c.getColumnIndexOrThrow(
1845b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.SCREEN);
1846b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int cellXIndex = c.getColumnIndexOrThrow
1847b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.CELLX);
1848b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int cellYIndex = c.getColumnIndexOrThrow
1849b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.CELLY);
1850b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int spanXIndex = c.getColumnIndexOrThrow
1851b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.SPANX);
1852b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int spanYIndex = c.getColumnIndexOrThrow(
1853b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.SPANY);
1854f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    final int restoredIndex = c.getColumnIndexOrThrow(
1855f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                            LauncherSettings.Favorites.RESTORED);
1856b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1857b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //final int displayModeIndex = c.getColumnIndexOrThrow(
1858b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //        LauncherSettings.Favorites.DISPLAY_MODE);
1859b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1860b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    ShortcutInfo info;
1861b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    String intentDescription;
1862b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    LauncherAppWidgetInfo appWidgetInfo;
1863b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    int container;
1864b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    long id;
1865b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Intent intent;
1866b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1867b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    while (!mStopped && c.moveToNext()) {
1868ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                        AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
1869b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        try {
1870b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            int itemType = c.getInt(itemTypeIndex);
1871f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                            boolean restored = 0 != c.getInt(restoredIndex);
1872b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1873b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            switch (itemType) {
1874b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1875b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1876ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                id = c.getLong(idIndex);
1877b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                intentDescription = c.getString(intentIndex);
1878b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                try {
1879b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    intent = Intent.parseUri(intentDescription, 0);
1880ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    ComponentName cn = intent.getComponent();
188168fd3c33daf7a6d4de287f8691306a55fa54706cWinson Chung                                    if (cn != null && !isValidPackageComponent(manager, cn)) {
1882f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        if (restored) {
1883f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            // might be installed later
1884f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            Launcher.addDumpLog(TAG,
1885f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                    "package not yet restored: " + cn, true);
1886ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        } else {
1887f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            if (!mAppsCanBeOnRemoveableStorage) {
1888f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                // Log the invalid package, and remove it
1889f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                Launcher.addDumpLog(TAG,
1890f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                        "Invalid package removed: " + cn, true);
1891f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                itemsToRemove.add(id);
1892f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            } else {
1893f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                // If apps can be on external storage, then we just
1894f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                // leave them for the user to remove (maybe add
1895f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                // visual treatment to it)
1896f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                Launcher.addDumpLog(TAG,
1897f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                        "Invalid package found: " + cn, true);
1898f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            }
1899f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            continue;
1900ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        }
1901f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    } else if (restored) {
1902f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        // no special handling necessary for this restored item
1903f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        restoredRows.add(id);
1904f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        restored = false;
1905ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    }
1906b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } catch (URISyntaxException e) {
1907f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    Launcher.addDumpLog(TAG,
1908f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            "Invalid uri: " + intentDescription, true);
1909b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    continue;
1910b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1911968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka
1912f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                if (restored) {
1913f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    Launcher.addDumpLog(TAG,
1914f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            "constructing info for partially restored package",
1915f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            true);
1916b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                                    info = getRestoredItemInfo(c, titleIndex, intent);
1917f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    intent = getRestoredItemIntent(c, context, intent);
1918f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                } else if (itemType ==
1919f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1920b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info = getShortcutInfo(manager, intent, context, c, iconIndex,
1921b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            titleIndex, mLabelCache);
1922b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } else {
1923b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info = getShortcutInfo(c, context, iconTypeIndex,
1924b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            iconPackageIndex, iconResourceIndex, iconIndex,
1925b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            titleIndex);
1926b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1927b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // App shortcuts that used to be automatically added to Launcher
1928b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // didn't always have the correct intent flags set, so do that
1929b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // here
1930b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (intent.getAction() != null &&
19319ad005639e1698c94674cdfadf3ae342fb56c9b5Michael Jurka                                        intent.getCategories() != null &&
19329ad005639e1698c94674cdfadf3ae342fb56c9b5Michael Jurka                                        intent.getAction().equals(Intent.ACTION_MAIN) &&
1933968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka                                        intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
1934b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        intent.addFlags(
1935b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            Intent.FLAG_ACTIVITY_NEW_TASK |
1936b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1937b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1938968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka                                }
19393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1940b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (info != null) {
1941ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    info.id = id;
1942b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.intent = intent;
1943b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    container = c.getInt(containerIndex);
1944b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.container = container;
1945dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                    info.screenId = c.getInt(screenIndex);
1946b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.cellX = c.getInt(cellXIndex);
1947b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.cellY = c.getInt(cellYIndex);
19485f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    info.spanX = 1;
19495f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    info.spanY = 1;
1950ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen
1951b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // check & update map of what's occupied
1952ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                    deleteOnInvalidPlacement.set(false);
1953ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                    if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1954ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                        if (deleteOnInvalidPlacement.get()) {
1955a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                            itemsToRemove.add(id);
1956a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                        }
1957b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1958b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1959b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1960b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    switch (container) {
1961b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1962b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1963b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        sBgWorkspaceItems.add(info);
1964b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1965b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    default:
1966b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        // Item is in a user folder
1967b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        FolderInfo folderInfo =
1968b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                                findOrMakeFolder(sBgFolders, container);
1969b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        folderInfo.add(info);
1970b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1971b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1972b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgItemsIdMap.put(info.id, info);
1973b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1974b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // now that we've loaded everthing re-save it with the
1975b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // icon in case it disappears somehow.
1976b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
19771323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                } else {
19781323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                    throw new RuntimeException("Unexpected null ShortcutInfo");
1979b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1980b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                break;
1981b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1982b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1983b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                id = c.getLong(idIndex);
1984b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1985b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1986b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.title = c.getString(titleIndex);
1987b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.id = id;
19883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
1989b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.container = container;
1990dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                folderInfo.screenId = c.getInt(screenIndex);
1991b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.cellX = c.getInt(cellXIndex);
1992b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.cellY = c.getInt(cellYIndex);
19935f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                folderInfo.spanX = 1;
19945f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                folderInfo.spanY = 1;
19955f8afe6280eae34620067696173e71943e1a30a3Winson Chung
19963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
1997ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                deleteOnInvalidPlacement.set(false);
1998a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                if (!checkItemPlacement(occupied, folderInfo,
1999ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                        deleteOnInvalidPlacement)) {
2000ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                    if (deleteOnInvalidPlacement.get()) {
2001a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                        itemsToRemove.add(id);
2002a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                    }
20033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
20049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
20055f8afe6280eae34620067696173e71943e1a30a3Winson Chung
20063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
2007b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2008b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2009b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        sBgWorkspaceItems.add(folderInfo);
2010b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
20119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
201217a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato
2013f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                if (restored) {
2014f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    // no special handling required for restored folders
2015f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    restoredRows.add(id);
2016f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                }
2017f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren
2018b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgItemsIdMap.put(folderInfo.id, folderInfo);
2019b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgFolders.put(folderInfo.id, folderInfo);
2020b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                break;
2021b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2022b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2023b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                // Read all Launcher-specific widget details
2024b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                int appWidgetId = c.getInt(appWidgetIdIndex);
2025c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                String savedProvider = c.getString(appWidgetProviderIndex);
2026c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren
20273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
2028f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2029b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                final AppWidgetProviderInfo provider =
2030b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        widgets.getAppWidgetInfo(appWidgetId);
2031b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2032b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!isSafeMode && (provider == null || provider.provider == null ||
2033b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        provider.provider.getPackageName() == null)) {
2034b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String log = "Deleting widget that isn't installed anymore: id="
2035b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        + id + " appWidgetId=" + appWidgetId;
2036b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Log.e(TAG, log);
20374caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                                    Launcher.addDumpLog(TAG, log, false);
2038b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    itemsToRemove.add(id);
2039b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } else {
2040b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2041b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            provider.provider);
2042b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.id = id;
2043dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                    appWidgetInfo.screenId = c.getInt(screenIndex);
2044b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.cellX = c.getInt(cellXIndex);
2045b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.cellY = c.getInt(cellYIndex);
2046b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.spanX = c.getInt(spanXIndex);
2047b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.spanY = c.getInt(spanYIndex);
2048b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2049b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.minSpanX = minSpan[0];
2050b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.minSpanY = minSpan[1];
2051b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2052b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    container = c.getInt(containerIndex);
2053b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2054b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2055b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        Log.e(TAG, "Widget found where container != " +
2056b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2057b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        continue;
2058b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
2059b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2060e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                                    appWidgetInfo.container = c.getInt(containerIndex);
2061b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // check & update map of what's occupied
2062ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                    deleteOnInvalidPlacement.set(false);
2063a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                    if (!checkItemPlacement(occupied, appWidgetInfo,
2064ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                            deleteOnInvalidPlacement)) {
2065ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                        if (deleteOnInvalidPlacement.get()) {
2066a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                            itemsToRemove.add(id);
2067a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                        }
2068b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
2069b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
2070c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                    String providerName = provider.provider.flattenToString();
2071c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                    if (!providerName.equals(savedProvider)) {
2072c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        ContentValues values = new ContentValues();
2073c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2074c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                                providerName);
2075c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        String where = BaseColumns._ID + "= ?";
2076c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        String[] args = {Integer.toString(c.getInt(idIndex))};
2077c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        contentResolver.update(contentUri, values, where, args);
2078c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                    }
2079b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2080b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgAppWidgets.add(appWidgetInfo);
2081b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
20829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
20833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
2084b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        } catch (Exception e) {
2085295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                            Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
2086b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
2087b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2088b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } finally {
208947b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                    if (c != null) {
209047b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                        c.close();
209147b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                    }
2092b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
2093ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
2094ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                // Break early if we've stopped loading
2095ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                if (mStopped) {
2096ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    clearSBgDataStructures();
2097ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    return false;
2098ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                }
2099ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung
2100b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (itemsToRemove.size() > 0) {
2101b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    ContentProviderClient client = contentResolver.acquireContentProviderClient(
21024caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                            LauncherSettings.Favorites.CONTENT_URI);
2103b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // Remove dead items
2104b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (long id : itemsToRemove) {
2105b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (DEBUG_LOADERS) {
2106b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.d(TAG, "Removed id = " + id);
2107b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
2108b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        // Don't notify content observers
2109b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        try {
2110b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2111b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    null, null);
2112b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        } catch (RemoteException e) {
2113b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.w(TAG, "Could not remove id = " + id);
2114b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
2115b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2116b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
21178802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
2118f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                if (restoredRows.size() > 0) {
2119f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2120f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                            LauncherSettings.Favorites.CONTENT_URI);
2121f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    // Update restored items that no longer require special handling
2122f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    try {
2123f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        StringBuilder selectionBuilder = new StringBuilder();
2124f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        selectionBuilder.append(LauncherSettings.Favorites._ID);
2125f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        selectionBuilder.append(" IN (");
2126f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        selectionBuilder.append(TextUtils.join(", ", restoredRows));
2127f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        selectionBuilder.append(")");
2128f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        ContentValues values = new ContentValues();
2129f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        values.put(LauncherSettings.Favorites.RESTORED, 0);
2130f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        updater.update(LauncherSettings.Favorites.CONTENT_URI,
2131f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                values, selectionBuilder.toString(), null);
2132f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    } catch (RemoteException e) {
2133f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        Log.w(TAG, "Could not update restored rows");
2134f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    }
2135f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                }
2136f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren
2137c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                if (loadedOldDb) {
2138dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long maxScreenId = 0;
2139dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // If we're importing we use the old screen order.
2140dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    for (ItemInfo item: sBgItemsIdMap.values()) {
2141dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        long screenId = item.screenId;
2142dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2143dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                !sBgWorkspaceScreens.contains(screenId)) {
2144dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            sBgWorkspaceScreens.add(screenId);
2145dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            if (screenId > maxScreenId) {
2146dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                maxScreenId = screenId;
2147dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            }
2148dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        }
2149dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2150dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    Collections.sort(sBgWorkspaceScreens);
21519f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    // Log to disk
21529f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    Launcher.addDumpLog(TAG, "11683562 -   maxScreenId: " + maxScreenId, true);
21539f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    Launcher.addDumpLog(TAG, "11683562 -   sBgWorkspaceScreens: " +
21549f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                            TextUtils.join(", ", sBgWorkspaceScreens), true);
21559e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
2156414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka                    LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
2157dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2158c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
2159c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    // Update the max item id after we load an old db
2160c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    long maxItemId = 0;
2161c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    // If we're importing we use the old screen order.
2162c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    for (ItemInfo item: sBgItemsIdMap.values()) {
2163c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        maxItemId = Math.max(maxItemId, item.id);
2164c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    }
2165414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka                    LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
2166dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                } else {
216776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
216876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    for (Integer i : orderedScreens.keySet()) {
216976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                        sBgWorkspaceScreens.add(orderedScreens.get(i));
2170dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
21719f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    // Log to disk
21729f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    Launcher.addDumpLog(TAG, "11683562 -   sBgWorkspaceScreens: " +
21739f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                            TextUtils.join(", ", sBgWorkspaceScreens), true);
2174dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2175dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // Remove any empty screens
2176933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                    ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
2177dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    for (ItemInfo item: sBgItemsIdMap.values()) {
2178dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        long screenId = item.screenId;
2179dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2180dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                unusedScreens.contains(screenId)) {
2181dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            unusedScreens.remove(screenId);
2182dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        }
2183dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2184dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2185dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // If there are any empty screens remove them, and update.
2186dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (unusedScreens.size() != 0) {
21879f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                        // Log to disk
21889f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                        Launcher.addDumpLog(TAG, "11683562 -   unusedScreens (to be removed): " +
21899f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                                TextUtils.join(", ", unusedScreens), true);
21909f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung
2191dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        sBgWorkspaceScreens.removeAll(unusedScreens);
2192dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2193dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2194dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
2195dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2196b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (DEBUG_LOADERS) {
2197b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2198b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Log.d(TAG, "workspace layout: ");
2199dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    int nScreens = occupied.size();
2200892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    for (int y = 0; y < countY; y++) {
2201b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        String line = "";
2202dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2203566da1026c33a68157bf9caf93d2071ad2870f46Daniel Sandler                        Iterator<Long> iter = occupied.keySet().iterator();
2204c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        while (iter.hasNext()) {
2205dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            long screenId = iter.next();
2206c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                            if (screenId > 0) {
2207b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                line += " | ";
2208b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            }
2209892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                            for (int x = 0; x < countX; x++) {
2210aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                ItemInfo[][] screen = occupied.get(screenId);
2211aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                if (x < screen.length && y < screen[x].length) {
2212aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                    line += (screen[x][y] != null) ? "#" : ".";
2213aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                } else {
2214aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                    line += "!";
2215aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                }
22169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
22179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
2218b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Log.d(TAG, "[ " + line + " ]");
22199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
22209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
22213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2222c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            return loadedOldDb;
2223e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        }
2224e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
2225b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of items who are directly or indirectly (via another container) on the
2226b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * specified screen. */
22279b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung        private void filterCurrentWorkspaceItems(long currentScreenId,
2228b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> allWorkspaceItems,
2229b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> currentScreenItems,
2230b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> otherScreenItems) {
22312abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            // Purge any null ItemInfos
22322abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
22332abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            while (iter.hasNext()) {
22342abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                ItemInfo i = iter.next();
22352abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (i == null) {
22362abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                    iter.remove();
22372abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                }
22382abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            }
22392abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung
2240b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Order the set of items by their containers first, this allows use to walk through the
2241b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // list sequentially, build up a list of containers that are in the specified screen,
2242b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // as well as all items in those containers.
2243b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Set<Long> itemsOnScreen = new HashSet<Long>();
2244b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2245b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                @Override
2246b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                public int compare(ItemInfo lhs, ItemInfo rhs) {
2247b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    return (int) (lhs.container - rhs.container);
2248b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
2249b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            });
2250b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (ItemInfo info : allWorkspaceItems) {
2251b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
22529b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung                    if (info.screenId == currentScreenId) {
2253b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        currentScreenItems.add(info);
2254b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        itemsOnScreen.add(info.id);
2255b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    } else {
2256b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        otherScreenItems.add(info);
22573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
2258b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2259b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenItems.add(info);
2260b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    itemsOnScreen.add(info.id);
2261b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2262b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    if (itemsOnScreen.contains(info.container)) {
2263b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        currentScreenItems.add(info);
2264b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        itemsOnScreen.add(info.id);
2265b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    } else {
2266b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        otherScreenItems.add(info);
22675c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
22685c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
22693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2270b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
22715c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
2272b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of widgets which are on the specified screen. */
22739b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung        private void filterCurrentAppWidgets(long currentScreenId,
2274b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> appWidgets,
2275b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2276b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2277b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2278b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (LauncherAppWidgetInfo widget : appWidgets) {
22792abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (widget == null) continue;
2280b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
22819b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung                        widget.screenId == currentScreenId) {
2282b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenWidgets.add(widget);
2283b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2284b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherScreenWidgets.add(widget);
2285a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
22869c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
22873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
228831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2289b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of folders which are on the specified screen. */
22909b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung        private void filterCurrentFolders(long currentScreenId,
2291b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, ItemInfo> itemsIdMap,
2292b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> folders,
2293b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> currentScreenFolders,
2294b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> otherScreenFolders) {
22953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
2296b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (long id : folders.keySet()) {
2297b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ItemInfo info = itemsIdMap.get(id);
2298b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                FolderInfo folder = folders.get(id);
22992abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (info == null || folder == null) continue;
2300b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
23019b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung                        info.screenId == currentScreenId) {
2302b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenFolders.put(id, folder);
2303b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2304b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherScreenFolders.put(id, folder);
2305b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
23063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2307b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
230831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2309b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2310b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * right) */
2311b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
2312892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            final LauncherAppState app = LauncherAppState.getInstance();
2313892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
2314b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: review this
2315b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
2316db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                @Override
2317db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                public int compare(ItemInfo lhs, ItemInfo rhs) {
2318892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    int cellCountX = (int) grid.numColumns;
2319892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    int cellCountY = (int) grid.numRows;
2320db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int screenOffset = cellCountX * cellCountY;
2321db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
2322dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
2323db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                            lhs.cellY * cellCountX + lhs.cellX);
2324dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
2325db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                            rhs.cellY * cellCountX + rhs.cellX);
2326db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    return (int) (lr - rr);
2327db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                }
2328db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung            });
2329b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
2330db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung
2331dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2332dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                final ArrayList<Long> orderedScreens) {
2333dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final Runnable r = new Runnable() {
2334dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                @Override
2335dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                public void run() {
2336dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2337dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (callbacks != null) {
2338dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        callbacks.bindScreens(orderedScreens);
2339dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2340dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
2341dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            };
2342dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2343dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
2344dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2345b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void bindWorkspaceItems(final Callbacks oldCallbacks,
2346b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<ItemInfo> workspaceItems,
2347b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<LauncherAppWidgetInfo> appWidgets,
2348b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final HashMap<Long, FolderInfo> folders,
2349b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<Runnable> deferredBindRunnables) {
2350b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2351b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final boolean postOnMainThread = (deferredBindRunnables != null);
2352603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung
2353b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the workspace items
2354db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung            int N = workspaceItems.size();
2355b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (int i = 0; i < N; i += ITEMS_CHUNK) {
23563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
23573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
2358b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2359b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    @Override
23609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
2361c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
23629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
236364359a53dc827797917a8d9a0697a91996789801Winson Chung                            callbacks.bindItems(workspaceItems, start, start+chunkSize,
236464359a53dc827797917a8d9a0697a91996789801Winson Chung                                    false);
23659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
23669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
2367b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2368b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2369b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2370b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
237181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
23723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
2373b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2374b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2375b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the folders
2376b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (!folders.isEmpty()) {
2377b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2378b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    public void run() {
2379b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2380b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (callbacks != null) {
2381b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            callbacks.bindFolders(folders);
23829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
2383b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2384b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2385b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2386b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2387b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
238881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
23899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
23903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2391b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2392b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the widgets, one at a time
2393b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            N = appWidgets.size();
2394b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (int i = 0; i < N; i++) {
2395b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final LauncherAppWidgetInfo widget = appWidgets.get(i);
2396b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2397b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    public void run() {
2398b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2399b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (callbacks != null) {
2400b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            callbacks.bindAppWidget(widget);
24013611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
2402b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2403b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2404b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2405b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2406b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
240781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
24083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
24093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2410b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
2411b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2412b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /**
2413b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * Binds all loaded data to actual views on the main thread.
2414b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         */
2415c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
2416b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final long t = SystemClock.uptimeMillis();
2417b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Runnable r;
2418b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2419b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Don't use these two variables in any of the callback runnables.
2420b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Otherwise we hold a reference to them.
2421b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final Callbacks oldCallbacks = mCallbacks.get();
2422b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (oldCallbacks == null) {
2423b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // This launcher has exited and nobody bothered to tell us.  Just bail.
2424b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.w(TAG, "LoaderTask running with no launcher");
2425b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                return;
2426b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2427b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
24289b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            // Save a copy of all the bg-thread collections
2429b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2430b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> appWidgets =
2431b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2432b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2433b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
2434dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
24352abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
2436b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                workspaceItems.addAll(sBgWorkspaceItems);
2437b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                appWidgets.addAll(sBgAppWidgets);
2438b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                folders.putAll(sBgFolders);
2439b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                itemsIdMap.putAll(sBgItemsIdMap);
2440dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                orderedScreenIds.addAll(sBgWorkspaceScreens);
2441b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2442b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
24437aff399974c756930070d82d7b2df88f125dacd6Derek Prothro            final boolean isLoadingSynchronously =
24447aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                    synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
2445d8dbb46468e0a8860468661a6edfb7969bfdf0a7Adam Cohen            int currScreen = isLoadingSynchronously ? synchronizeBindPage :
24469b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung                oldCallbacks.getCurrentWorkspaceScreen();
2447d8dbb46468e0a8860468661a6edfb7969bfdf0a7Adam Cohen            if (currScreen >= orderedScreenIds.size()) {
2448d8dbb46468e0a8860468661a6edfb7969bfdf0a7Adam Cohen                // There may be no workspace screens (just hotseat items and an empty page).
24497aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                currScreen = PagedView.INVALID_RESTORE_PAGE;
24509b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            }
2451d8dbb46468e0a8860468661a6edfb7969bfdf0a7Adam Cohen            final int currentScreen = currScreen;
24527aff399974c756930070d82d7b2df88f125dacd6Derek Prothro            final long currentScreenId = currentScreen < 0
24537aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                    ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
24549b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung
24559b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            // Load all the items that are on the current page first (and in the process, unbind
24569b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            // all the existing workspace items before we call startBinding() below.
24579b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            unbindWorkspaceItemsOnMainThread();
24589b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung
24599b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            // Separate the items that are on the current screen, and all the other remaining items
2460b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2461b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2462b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2463b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2464b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2465b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2466b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2467b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2468b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
24699b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
2470b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherWorkspaceItems);
24719b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
2472b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherAppWidgets);
24739b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
2474b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherFolders);
2475b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sortWorkspaceItemsSpatially(currentWorkspaceItems);
2476b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sortWorkspaceItemsSpatially(otherWorkspaceItems);
2477b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2478b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Tell the workspace that we're about to start binding items
2479b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r = new Runnable() {
24803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
24813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
24823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
2483b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        callbacks.startBinding();
24849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
24859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
2486b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
248781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung            runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2488b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2489dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2490dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2491b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Load items on the current page
2492b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2493b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentFolders, null);
24941462de39f01cec0ba785386532719cb0207dd827Adam Cohen            if (isLoadingSynchronously) {
24951462de39f01cec0ba785386532719cb0207dd827Adam Cohen                r = new Runnable() {
24961462de39f01cec0ba785386532719cb0207dd827Adam Cohen                    public void run() {
24971462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
24987aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                        if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
24991462de39f01cec0ba785386532719cb0207dd827Adam Cohen                            callbacks.onPageBoundSynchronously(currentScreen);
25001462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        }
25011462de39f01cec0ba785386532719cb0207dd827Adam Cohen                    }
25021462de39f01cec0ba785386532719cb0207dd827Adam Cohen                };
250381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
25041462de39f01cec0ba785386532719cb0207dd827Adam Cohen            }
2505b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
25064a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            // Load all the remaining pages (if we are loading synchronously, we want to defer this
25074a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            // work until after the first render)
2508b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mDeferredBindRunnables.clear();
2509b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
25104a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                    (isLoadingSynchronously ? mDeferredBindRunnables : null));
2511b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2512b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Tell the workspace that we're done binding items
2513b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r = new Runnable() {
25143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
2515b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2516b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    if (callbacks != null) {
2517c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        callbacks.finishBindingItems(isUpgradePath);
2518b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2519b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
252098e030be2abc03e90e9fd9f8bf67e2e01db39cc0Winson Chung                    // If we're profiling, ensure this is the last thing in the queue.
25213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
25223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
25233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
25249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
252536a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
252636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung                    mIsLoadingAndBindingWorkspace = false;
25279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
2528b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
25294a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            if (isLoadingSynchronously) {
25304a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                mDeferredBindRunnables.add(r);
25314a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            } else {
253281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
25334a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            }
25343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
25353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
25363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
25373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
25383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
25393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
254164359a53dc827797917a8d9a0697a91996789801Winson Chung                loadAllApps();
254293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                synchronized (LoaderTask.this) {
254393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    if (mStopped) {
254493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                        return;
254593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    }
254693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    mAllAppsLoaded = true;
25473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
25483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
25493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
25503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
25523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
25533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
25543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
25553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
25563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
25573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
25583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
25593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
25613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
2562c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            @SuppressWarnings("unchecked")
2563eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> list
2564eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                    = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
2565c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            Runnable r = new Runnable() {
25663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
25673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
25683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
25693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
25703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
25719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
25723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
25733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2574a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
25759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
25763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
2577c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            };
2578c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
257964359a53dc827797917a8d9a0697a91996789801Winson Chung            if (isRunningOnMainThread) {
2580c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                r.run();
2581c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            } else {
2582c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                mHandler.post(r);
2583c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            }
25843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
25853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
258664359a53dc827797917a8d9a0697a91996789801Winson Chung        private void loadAllApps() {
258764359a53dc827797917a8d9a0697a91996789801Winson Chung            final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
25883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
25893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
25903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
25913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
259264359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
25933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
25949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
259531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
259664359a53dc827797917a8d9a0697a91996789801Winson Chung            final PackageManager packageManager = mContext.getPackageManager();
25973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
25983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2599cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
260064359a53dc827797917a8d9a0697a91996789801Winson Chung            // Clear the list of apps
260164359a53dc827797917a8d9a0697a91996789801Winson Chung            mBgAllAppsList.clear();
2602cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
260364359a53dc827797917a8d9a0697a91996789801Winson Chung            // Query for the set of apps
260464359a53dc827797917a8d9a0697a91996789801Winson Chung            final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
260564359a53dc827797917a8d9a0697a91996789801Winson Chung            List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
260664359a53dc827797917a8d9a0697a91996789801Winson Chung            if (DEBUG_LOADERS) {
260764359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "queryIntentActivities took "
260864359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis()-qiaTime) + "ms");
260964359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
261064359a53dc827797917a8d9a0697a91996789801Winson Chung            }
261164359a53dc827797917a8d9a0697a91996789801Winson Chung            // Fail if we don't have any apps
261264359a53dc827797917a8d9a0697a91996789801Winson Chung            if (apps == null || apps.isEmpty()) {
261364359a53dc827797917a8d9a0697a91996789801Winson Chung                return;
261464359a53dc827797917a8d9a0697a91996789801Winson Chung            }
261564359a53dc827797917a8d9a0697a91996789801Winson Chung            // Sort the applications by name
261664359a53dc827797917a8d9a0697a91996789801Winson Chung            final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
261764359a53dc827797917a8d9a0697a91996789801Winson Chung            Collections.sort(apps,
261864359a53dc827797917a8d9a0697a91996789801Winson Chung                    new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
261964359a53dc827797917a8d9a0697a91996789801Winson Chung            if (DEBUG_LOADERS) {
262064359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "sort took "
262164359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis()-sortTime) + "ms");
262264359a53dc827797917a8d9a0697a91996789801Winson Chung            }
26233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
262464359a53dc827797917a8d9a0697a91996789801Winson Chung            // Create the ApplicationInfos
262564359a53dc827797917a8d9a0697a91996789801Winson Chung            for (int i = 0; i < apps.size(); i++) {
262685f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert                ResolveInfo app = apps.get(i);
26271307f63f8a93541bf6e2d29c11fab69778a25e42Bjorn Bringert                // This builds the icon bitmaps.
26281307f63f8a93541bf6e2d29c11fab69778a25e42Bjorn Bringert                mBgAllAppsList.add(new AppInfo(packageManager, app,
26291307f63f8a93541bf6e2d29c11fab69778a25e42Bjorn Bringert                        mIconCache, mLabelCache));
263064359a53dc827797917a8d9a0697a91996789801Winson Chung            }
2631cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
263285f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert            // Huh? Shouldn't this be inside the Runnable below?
2633eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> added = mBgAllAppsList.added;
2634eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            mBgAllAppsList.added = new ArrayList<AppInfo>();
26353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
263664359a53dc827797917a8d9a0697a91996789801Winson Chung            // Post callback on main thread
263764359a53dc827797917a8d9a0697a91996789801Winson Chung            mHandler.post(new Runnable() {
263864359a53dc827797917a8d9a0697a91996789801Winson Chung                public void run() {
263964359a53dc827797917a8d9a0697a91996789801Winson Chung                    final long bindTime = SystemClock.uptimeMillis();
264011a1a53651924b544513f1f6971a735b18d67539Winson Chung                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
264164359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (callbacks != null) {
264264359a53dc827797917a8d9a0697a91996789801Winson Chung                        callbacks.bindAllApplications(added);
26433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
264464359a53dc827797917a8d9a0697a91996789801Winson Chung                            Log.d(TAG, "bound " + added.size() + " apps in "
264564359a53dc827797917a8d9a0697a91996789801Winson Chung                                + (SystemClock.uptimeMillis() - bindTime) + "ms");
26463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
264764359a53dc827797917a8d9a0697a91996789801Winson Chung                    } else {
264864359a53dc827797917a8d9a0697a91996789801Winson Chung                        Log.i(TAG, "not binding apps: no Launcher activity");
264964359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
26503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
265164359a53dc827797917a8d9a0697a91996789801Winson Chung            });
2652cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
26533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
265464359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "Icons processed in "
265564359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis() - loadTime) + "ms");
26563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
26573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
2658dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
26593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
26602abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
2661b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2662b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2663b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2664b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2665b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2666b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
26673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
26683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
266931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
26703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
2671700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(task);
26723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
2673d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
26743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
26753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
26763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
2677d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
26783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
26793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
26803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
26813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
26823611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
268331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2684d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
26853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
26863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
26873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
26883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
2689d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
26903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
2691cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler            final Context context = mApp.getContext();
2692dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
26933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
26943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
26953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
26963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
26973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
26983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
26996d0dde01f307051ee1849481c989d9e87774b894Chris Wren                        mIconCache.remove(packages[i]);
2700487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.addPackage(context, packages[i]);
2701dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
27023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
27033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
27043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
27053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
2706487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.updatePackage(context, packages[i]);
2707eb1bb920507fe7f27b2ecece5b67749dac7850f3Michael Jurka                        WidgetPreviewLoader.removePackageFromDb(
2708e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                                mApp.getWidgetPreviewCacheDb(), packages[i]);
27093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
27103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
27113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
27123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
27133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
27143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2715487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.removePackage(packages[i]);
2716eb1bb920507fe7f27b2ecece5b67749dac7850f3Michael Jurka                        WidgetPreviewLoader.removePackageFromDb(
2717e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                                mApp.getWidgetPreviewCacheDb(), packages[i]);
27183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
27193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
27203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2721dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
2722eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            ArrayList<AppInfo> added = null;
2723eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            ArrayList<AppInfo> modified = null;
2724eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
27253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
2726487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            if (mBgAllAppsList.added.size() > 0) {
2727eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                added = new ArrayList<AppInfo>(mBgAllAppsList.added);
27285d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.added.clear();
27293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2730487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            if (mBgAllAppsList.modified.size() > 0) {
2731eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
27325d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.modified.clear();
27333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
27345d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung            if (mBgAllAppsList.removed.size() > 0) {
273583892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                removedApps.addAll(mBgAllAppsList.removed);
27365d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.removed.clear();
2737cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung            }
2738cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung
27393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
27403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
27413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
27423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
2743be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
2744be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
27453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
274664359a53dc827797917a8d9a0697a91996789801Winson Chung                // Ensure that we add all the workspace applications to the db
274776a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                if (LauncherAppState.isDisableAllApps()) {
274894d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                    final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
274976a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                    addAndBindAddedWorkspaceApps(context, addedInfos);
275076a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                } else {
275176a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                    addAppsToAllApps(context, added);
275294d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                }
27533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
275476a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen
27553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
2756eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                final ArrayList<AppInfo> modifiedFinal = modified;
275764359a53dc827797917a8d9a0697a91996789801Winson Chung
275864359a53dc827797917a8d9a0697a91996789801Winson Chung                // Update the launcher db to reflect the changes
2759eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                for (AppInfo a : modifiedFinal) {
276064359a53dc827797917a8d9a0697a91996789801Winson Chung                    ArrayList<ItemInfo> infos =
276164359a53dc827797917a8d9a0697a91996789801Winson Chung                            getItemInfoForComponentName(a.componentName);
276264359a53dc827797917a8d9a0697a91996789801Winson Chung                    for (ItemInfo i : infos) {
276364359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (isShortcutInfoUpdateable(i)) {
276464359a53dc827797917a8d9a0697a91996789801Winson Chung                            ShortcutInfo info = (ShortcutInfo) i;
276564359a53dc827797917a8d9a0697a91996789801Winson Chung                            info.title = a.title.toString();
276664359a53dc827797917a8d9a0697a91996789801Winson Chung                            updateItemInDatabase(context, info);
276764359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
276864359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
276964359a53dc827797917a8d9a0697a91996789801Winson Chung                }
277064359a53dc827797917a8d9a0697a91996789801Winson Chung
27713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
27723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
2773cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2774cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        if (callbacks == cb && cb != null) {
27753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
27763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
27773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
27783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
27793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2780780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung
2781df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            final ArrayList<String> removedPackageNames =
2782df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                    new ArrayList<String>();
2783df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            if (mOp == OP_REMOVE) {
2784df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                // Mark all packages in the broadcast to be removed
2785df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                removedPackageNames.addAll(Arrays.asList(packages));
2786df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            } else if (mOp == OP_UPDATE) {
2787df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                // Mark disabled packages in the broadcast to be removed
2788df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                final PackageManager pm = context.getPackageManager();
2789df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                for (int i=0; i<N; i++) {
2790df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                    if (isPackageDisabled(pm, packages[i])) {
2791df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                        removedPackageNames.add(packages[i]);
279264359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
279364359a53dc827797917a8d9a0697a91996789801Winson Chung                }
2794df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            }
2795df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            // Remove all the components associated with this package
2796df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            for (String pn : removedPackageNames) {
2797df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2798df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                for (ItemInfo i : infos) {
2799df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                    deleteItemFromDatabase(context, i);
2800df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                }
2801df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            }
2802df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            // Remove all the specific components
2803df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            for (AppInfo a : removedApps) {
2804df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2805df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                for (ItemInfo i : infos) {
2806df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                    deleteItemFromDatabase(context, i);
2807df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                }
2808df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            }
2809df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2810df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                // Remove any queued items from the install queue
2811df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                String spKey = LauncherAppState.getSharedPreferencesKey();
2812df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                SharedPreferences sp =
2813df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                        context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2814df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2815df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                // Call the components-removed callback
28163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
28173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
2818cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2819cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        if (callbacks == cb && cb != null) {
2820df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                            callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
28213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
28223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
28233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
2824be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
282580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
2826c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            final ArrayList<Object> widgetsAndShortcuts =
2827c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                getSortedWidgetsAndShortcuts(context);
282880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
282980baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
283080baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
2831cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                    Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2832cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                    if (callbacks == cb && cb != null) {
2833c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                        callbacks.bindPackagesUpdated(widgetsAndShortcuts);
283480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
283580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
28364caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            });
28374caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen
28384caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            // Write all the logs to disk
28394caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            mHandler.post(new Runnable() {
28404caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                public void run() {
28414caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
28424caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    if (callbacks == cb && cb != null) {
2843ede4129e77f8347e32c0da9c615bcff2cbc06fd8Winson Chung                        callbacks.dumpLogsToLocalData();
28444caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    }
28454caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                }
284680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
284731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
284831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
284931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2850c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2851c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2852c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        PackageManager packageManager = context.getPackageManager();
2853c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2854c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2855c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2856c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2857c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        Collections.sort(widgetsAndShortcuts,
2858c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2859c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        return widgetsAndShortcuts;
2860c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    }
2861c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka
2862556f6135f992dcd5bfbfffe0172627bfbf57d24eAdam Cohen    private static boolean isPackageDisabled(PackageManager pm, String packageName) {
2863df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        try {
2864df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            PackageInfo pi = pm.getPackageInfo(packageName, 0);
2865df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            return !pi.applicationInfo.enabled;
2866df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        } catch (NameNotFoundException e) {
2867df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            // Fall through
2868df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        }
2869df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        return false;
2870df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung    }
2871556f6135f992dcd5bfbfffe0172627bfbf57d24eAdam Cohen
2872556f6135f992dcd5bfbfffe0172627bfbf57d24eAdam Cohen    public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
2873ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        if (cn == null) {
2874ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return false;
2875ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        }
2876df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        if (isPackageDisabled(pm, cn.getPackageName())) {
2877df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            return false;
2878df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        }
2879ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung
2880ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        try {
2881ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            // Check the activity
2882df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2883ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return (pm.getActivityInfo(cn, 0) != null);
2884ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        } catch (NameNotFoundException e) {
2885ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return false;
2886ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        }
2887ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung    }
2888ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung
28899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2890f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     * Make an ShortcutInfo object for a restored application or shortcut item that points
2891f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     * to a package that is not yet installed on the system.
2892f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     */
2893b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren    public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
2894f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        final ShortcutInfo info = new ShortcutInfo();
2895f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        if (cursor != null) {
2896f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren            info.title =  cursor.getString(titleIndex);
2897f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        } else {
2898f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren            info.title = "";
2899f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        }
29006d0dde01f307051ee1849481c989d9e87774b894Chris Wren        info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
2901f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
2902b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        info.restoredIntent = intent;
2903f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        return info;
2904f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    }
2905f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren
2906f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    /**
2907f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     * Make an Intent object for a restored application or shortcut item that points
2908f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     * to the market page for the item.
2909f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     */
2910f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
2911b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final boolean debug = false;
2912f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        ComponentName componentName = intent.getComponent();
2913f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2914f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        Uri marketUri = new Uri.Builder()
2915f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                .scheme("market")
2916f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                .authority("details")
2917f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                .appendQueryParameter("id", componentName.getPackageName())
2918f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                .build();
2919b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
2920f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        marketIntent.setData(marketUri);
2921f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        return marketIntent;
2922f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    }
2923f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren
2924f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    /**
292556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
292656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
29279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
292856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
2929c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        return getShortcutInfo(manager, intent, context, null, -1, -1, null);
293056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
293156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
293256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
293356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
293456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
293556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
293656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
293756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
2938c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
293956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
29401323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        final ShortcutInfo info = new ShortcutInfo();
294168fd3c33daf7a6d4de287f8691306a55fa54706cWinson Chung        if (componentName != null && !isValidPackageComponent(manager, componentName)) {
2942ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
29439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
29441323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        } else {
29451323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            try {
29461323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
29471323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                info.initFlagsAndFirstInstallTime(pi);
29481323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            } catch (NameNotFoundException e) {
29491323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                Log.d(TAG, "getPackInfo failed for package " +
29501323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                        componentName.getPackageName());
29511323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            }
295231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
295331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
29548ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
29558ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
29568ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
295756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
295856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
295956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
296056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
2961c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung
2962c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2963c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2964c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // via resolveActivity().
2965ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        Bitmap icon = null;
2966c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        ResolveInfo resolveInfo = null;
2967c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        ComponentName oldComponent = intent.getComponent();
2968c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        Intent newIntent = new Intent(intent.getAction(), null);
2969c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2970c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        newIntent.setPackage(oldComponent.getPackageName());
2971c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2972c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        for (ResolveInfo i : infos) {
2973c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            ComponentName cn = new ComponentName(i.activityInfo.packageName,
2974c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung                    i.activityInfo.name);
2975c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            if (cn.equals(oldComponent)) {
2976c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung                resolveInfo = i;
2977c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            }
2978c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        }
2979c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        if (resolveInfo == null) {
2980c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            resolveInfo = manager.resolveActivity(intent, 0);
2981c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        }
298256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
2983aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
298456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
298556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
298656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
298756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
2988931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                icon = getIconFromCursor(c, iconIndex, context);
298956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
29909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
299156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
299256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
299356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
299456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
299556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
299656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
299756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
299856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
299956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
30005308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
30015308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (labelCache != null && labelCache.containsKey(key)) {
30025308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                info.title = labelCache.get(key);
3003c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3004c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                info.title = resolveInfo.activityInfo.loadLabel(manager);
3005c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                if (labelCache != null) {
30065308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                    labelCache.put(key, info.title);
3007c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                }
3008c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
300956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
301056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
301156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
301256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
301356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
301456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
301556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
301656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
30179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
301856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
30199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
30209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
30219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
30229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
302331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
302464359a53dc827797917a8d9a0697a91996789801Winson Chung    static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
302564359a53dc827797917a8d9a0697a91996789801Winson Chung            ItemInfoFilter f) {
302664359a53dc827797917a8d9a0697a91996789801Winson Chung        HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
302764359a53dc827797917a8d9a0697a91996789801Winson Chung        for (ItemInfo i : infos) {
302864359a53dc827797917a8d9a0697a91996789801Winson Chung            if (i instanceof ShortcutInfo) {
302964359a53dc827797917a8d9a0697a91996789801Winson Chung                ShortcutInfo info = (ShortcutInfo) i;
303064359a53dc827797917a8d9a0697a91996789801Winson Chung                ComponentName cn = info.intent.getComponent();
303164359a53dc827797917a8d9a0697a91996789801Winson Chung                if (cn != null && f.filterItem(null, info, cn)) {
303264359a53dc827797917a8d9a0697a91996789801Winson Chung                    filtered.add(info);
303364359a53dc827797917a8d9a0697a91996789801Winson Chung                }
303464359a53dc827797917a8d9a0697a91996789801Winson Chung            } else if (i instanceof FolderInfo) {
303564359a53dc827797917a8d9a0697a91996789801Winson Chung                FolderInfo info = (FolderInfo) i;
303664359a53dc827797917a8d9a0697a91996789801Winson Chung                for (ShortcutInfo s : info.contents) {
303764359a53dc827797917a8d9a0697a91996789801Winson Chung                    ComponentName cn = s.intent.getComponent();
303864359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (cn != null && f.filterItem(info, s, cn)) {
303964359a53dc827797917a8d9a0697a91996789801Winson Chung                        filtered.add(s);
30408a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung                    }
30418a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung                }
304264359a53dc827797917a8d9a0697a91996789801Winson Chung            } else if (i instanceof LauncherAppWidgetInfo) {
304364359a53dc827797917a8d9a0697a91996789801Winson Chung                LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
304464359a53dc827797917a8d9a0697a91996789801Winson Chung                ComponentName cn = info.providerName;
304564359a53dc827797917a8d9a0697a91996789801Winson Chung                if (cn != null && f.filterItem(null, info, cn)) {
304664359a53dc827797917a8d9a0697a91996789801Winson Chung                    filtered.add(info);
304764359a53dc827797917a8d9a0697a91996789801Winson Chung                }
30488a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung            }
30498a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung        }
305064359a53dc827797917a8d9a0697a91996789801Winson Chung        return new ArrayList<ItemInfo>(filtered);
305164359a53dc827797917a8d9a0697a91996789801Winson Chung    }
305264359a53dc827797917a8d9a0697a91996789801Winson Chung
305364359a53dc827797917a8d9a0697a91996789801Winson Chung    private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
305464359a53dc827797917a8d9a0697a91996789801Winson Chung        ItemInfoFilter filter  = new ItemInfoFilter() {
305564359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
305664359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
305764359a53dc827797917a8d9a0697a91996789801Winson Chung                return cn.getPackageName().equals(pn);
305864359a53dc827797917a8d9a0697a91996789801Winson Chung            }
305964359a53dc827797917a8d9a0697a91996789801Winson Chung        };
306064359a53dc827797917a8d9a0697a91996789801Winson Chung        return filterItemInfos(sBgItemsIdMap.values(), filter);
306164359a53dc827797917a8d9a0697a91996789801Winson Chung    }
306264359a53dc827797917a8d9a0697a91996789801Winson Chung
306364359a53dc827797917a8d9a0697a91996789801Winson Chung    private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
306464359a53dc827797917a8d9a0697a91996789801Winson Chung        ItemInfoFilter filter  = new ItemInfoFilter() {
306564359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
306664359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
306764359a53dc827797917a8d9a0697a91996789801Winson Chung                return cn.equals(cname);
306864359a53dc827797917a8d9a0697a91996789801Winson Chung            }
306964359a53dc827797917a8d9a0697a91996789801Winson Chung        };
307064359a53dc827797917a8d9a0697a91996789801Winson Chung        return filterItemInfos(sBgItemsIdMap.values(), filter);
307164359a53dc827797917a8d9a0697a91996789801Winson Chung    }
307264359a53dc827797917a8d9a0697a91996789801Winson Chung
307364359a53dc827797917a8d9a0697a91996789801Winson Chung    public static boolean isShortcutInfoUpdateable(ItemInfo i) {
307464359a53dc827797917a8d9a0697a91996789801Winson Chung        if (i instanceof ShortcutInfo) {
307564359a53dc827797917a8d9a0697a91996789801Winson Chung            ShortcutInfo info = (ShortcutInfo) i;
307664359a53dc827797917a8d9a0697a91996789801Winson Chung            // We need to check for ACTION_MAIN otherwise getComponent() might
307764359a53dc827797917a8d9a0697a91996789801Winson Chung            // return null for some shortcuts (for instance, for shortcuts to
307864359a53dc827797917a8d9a0697a91996789801Winson Chung            // web pages.)
307964359a53dc827797917a8d9a0697a91996789801Winson Chung            Intent intent = info.intent;
308064359a53dc827797917a8d9a0697a91996789801Winson Chung            ComponentName name = intent.getComponent();
308164359a53dc827797917a8d9a0697a91996789801Winson Chung            if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
308264359a53dc827797917a8d9a0697a91996789801Winson Chung                    Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
308364359a53dc827797917a8d9a0697a91996789801Winson Chung                return true;
308464359a53dc827797917a8d9a0697a91996789801Winson Chung            }
3085b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            // placeholder shortcuts get special treatment, let them through too.
3086b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            if (info.getRestoredIntent() != null) {
3087b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                return true;
3088b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            }
308964359a53dc827797917a8d9a0697a91996789801Winson Chung        }
309064359a53dc827797917a8d9a0697a91996789801Winson Chung        return false;
30918a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung    }
30928a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung
30938a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung    /**
30940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
30959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
30960589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
309756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
309856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
309931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
310056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
3101c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ShortcutInfo info = new ShortcutInfo();
31029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
31037376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
31048ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
31058ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
310656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
310756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
31089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
31099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
31109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
31119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
31129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
31139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
311456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
311556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
31169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
31179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
311856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
311956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
3120c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
3121c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
312256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
31239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
312456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
312556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
312656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
312756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
3128931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                icon = getIconFromCursor(c, iconIndex, context);
312956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
313056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
313156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
313256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
313356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
31347376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
31359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
31369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
3137931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            icon = getIconFromCursor(c, iconIndex, context);
313856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
313956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
314056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
314156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
314256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
314356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
31449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
31459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
31469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
3147d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
314856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
31499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
31509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
315131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
3152d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
31539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
3154f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
315531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3156931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka    Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
31573a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        @SuppressWarnings("all") // suppress dead code warning
31583a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        final boolean debug = false;
31593a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        if (debug) {
316056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
316156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
316256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
316356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
316456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
3165931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            return Utilities.createIconBitmap(
3166931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                    BitmapFactory.decodeByteArray(data, 0, data.length), context);
316756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
316856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
316956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
317056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
317156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
31723d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
31733d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellX, int cellY, boolean notify) {
3174a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
3175d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (info == null) {
3176d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return null;
3177d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
31783d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
31790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
31800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
31810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
31820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3183a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    /**
318455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     * Attempts to find an AppWidgetProviderInfo that matches the given component.
318555cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     */
318655cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
318755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            ComponentName component) {
318855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        List<AppWidgetProviderInfo> widgets =
318955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
319055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        for (AppWidgetProviderInfo info : widgets) {
319155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            if (info.provider.equals(component)) {
319255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                return info;
319355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            }
319455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        }
319555cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        return null;
3196a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
3197a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
319868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    /**
319968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     * Returns a list of all the widgets that can handle configuration with a particular mimeType.
320068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     */
320168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
320268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final PackageManager packageManager = context.getPackageManager();
320368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
320468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new ArrayList<WidgetMimeTypeHandlerData>();
320568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
320668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final Intent supportsIntent =
320768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
320868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        supportsIntent.setType(mimeType);
320968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
321068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Create a set of widget configuration components that we can test against
321168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<AppWidgetProviderInfo> widgets =
321268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
321368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
321468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new HashMap<ComponentName, AppWidgetProviderInfo>();
321568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (AppWidgetProviderInfo info : widgets) {
321668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            configurationComponentToWidget.put(info.configure, info);
321768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
321868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
321968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Run through each of the intents that can handle this type of clip data, and cross
322068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // reference them with the components that are actual configuration components
322168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
322268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                PackageManager.MATCH_DEFAULT_ONLY);
322368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (ResolveInfo info : activities) {
322468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ActivityInfo activityInfo = info.activityInfo;
322568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
322668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                    activityInfo.name);
322768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            if (configurationComponentToWidget.containsKey(infoComponent)) {
322868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                supportedConfigurationActivities.add(
322968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
323068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                                configurationComponentToWidget.get(infoComponent)));
323168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            }
323268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
323368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        return supportedConfigurationActivities;
323468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    }
323568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
3236a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
32370589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
32380589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
32390589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
32400589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3241d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (intent == null) {
3242d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3243d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3244d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return null;
3245d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
3246d919882006a457d16dea46bd546472e6534b5c19Adam Cohen
32470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
32480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
32490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
32500589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
32510589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
32520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
32530589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
32540589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
32550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
32560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
32570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
32580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
32590589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
32600589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
32610589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
32620589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
3263c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
3264c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
32650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
32660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
32670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
32680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
32690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
32700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3271c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ShortcutInfo info = new ShortcutInfo();
327256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
32730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
3274a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            if (fallbackIcon != null) {
3275a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = fallbackIcon;
3276a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            } else {
3277a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = getFallbackIcon();
3278a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                info.usingFallbackIcon = true;
3279a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            }
32800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
32810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
328256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
32830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
32840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
32850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
32860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
32870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
32880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
32890589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
32900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3291aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3292aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            int iconIndex) {
329317a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        // If apps can't be on SD, don't even bother.
3294ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        if (!mAppsCanBeOnRemoveableStorage) {
3295aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            return false;
329617a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        }
329756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
329856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
329956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
330056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
330156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
330256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
3303ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
3304aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            cache.put(info, c.getBlob(iconIndex));
3305aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            return true;
3306aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        }
3307aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        return false;
3308aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    }
3309aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3310aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        boolean needSave = false;
3311aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        try {
3312aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            if (data != null) {
3313aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3314aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                Bitmap loaded = info.getIcon(mIconCache);
3315aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                needSave = !saved.sameAs(loaded);
3316aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            } else {
331756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
331856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
3319aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        } catch (Exception e) {
3320aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            needSave = true;
3321aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        }
3322aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        if (needSave) {
3323aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            Log.d(TAG, "going to save icon bitmap for info=" + info);
3324aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // This is slower than is ideal, but this only happens once
3325aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // or when the app is updated with a new icon.
3326aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            updateItemInDatabase(context, info);
332756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
332856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
332956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
333031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
3331df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     * Return an existing FolderInfo object if we have encountered this ID previously,
33329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
333331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
3334df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
33359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
33369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
3337df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        if (folderInfo == null) {
33389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
3339c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            folderInfo = new FolderInfo();
33409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
334131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
3342df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        return folderInfo;
334331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
334431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3345eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka    public static final Comparator<AppInfo> getAppNameComparator() {
33461190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        final Collator collator = Collator.getInstance();
3347eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        return new Comparator<AppInfo>() {
3348eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            public final int compare(AppInfo a, AppInfo b) {
3349780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                int result = collator.compare(a.title.toString().trim(),
3350780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                        b.title.toString().trim());
33511190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                if (result == 0) {
33521190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                    result = a.componentName.compareTo(b.componentName);
33531190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                }
33541190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                return result;
33555b1808da75ed3e3528cb31dee56a4e363812cc21Michael Jurka            }
33561190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        };
33571190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    }
3358eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka    public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3359eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            = new Comparator<AppInfo>() {
3360eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        public final int compare(AppInfo a, AppInfo b) {
336178403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime < b.firstInstallTime) return 1;
336278403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime > b.firstInstallTime) return -1;
336378403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            return 0;
336478403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung        }
336578403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung    };
33661190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
33671190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        final Collator collator = Collator.getInstance();
33681190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        return new Comparator<AppWidgetProviderInfo>() {
33691190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3370780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                return collator.compare(a.label.toString().trim(), b.label.toString().trim());
33711190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            }
33721190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        };
33731190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    }
33745308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung    static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
33755308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        if (info.activityInfo != null) {
33765308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
33775308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        } else {
33785308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
33795308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        }
33805308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung    }
3381785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
33821190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        private Collator mCollator;
3383785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        private PackageManager mPackageManager;
3384c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        private HashMap<Object, CharSequence> mLabelCache;
3385785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        ShortcutNameComparator(PackageManager pm) {
3386785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mPackageManager = pm;
3387c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = new HashMap<Object, CharSequence>();
33881190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
3389c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        }
3390c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3391c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mPackageManager = pm;
3392c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = labelCache;
33931190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
3394785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3395785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        public final int compare(ResolveInfo a, ResolveInfo b) {
3396c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            CharSequence labelA, labelB;
33975308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
33985308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
33995308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (mLabelCache.containsKey(keyA)) {
34005308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                labelA = mLabelCache.get(keyA);
3401c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3402780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                labelA = a.loadLabel(mPackageManager).toString().trim();
3403c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung
34045308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                mLabelCache.put(keyA, labelA);
3405c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
34065308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (mLabelCache.containsKey(keyB)) {
34075308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                labelB = mLabelCache.get(keyB);
3408c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3409780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                labelB = b.loadLabel(mPackageManager).toString().trim();
3410c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung
34115308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                mLabelCache.put(keyB, labelB);
3412c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
34131190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            return mCollator.compare(labelA, labelB);
3414785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3415785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    };
34161ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung    public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
34171190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        private Collator mCollator;
34181ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        private PackageManager mPackageManager;
34191ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        private HashMap<Object, String> mLabelCache;
34201ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        WidgetAndShortcutNameComparator(PackageManager pm) {
34211ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            mPackageManager = pm;
34221ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            mLabelCache = new HashMap<Object, String>();
34231190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
34241ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        }
34251ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        public final int compare(Object a, Object b) {
34261ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            String labelA, labelB;
3427c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            if (mLabelCache.containsKey(a)) {
3428c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = mLabelCache.get(a);
3429c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3430c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = (a instanceof AppWidgetProviderInfo) ?
34311ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((AppWidgetProviderInfo) a).label :
3432780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                    ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
3433c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                mLabelCache.put(a, labelA);
3434c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
3435c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            if (mLabelCache.containsKey(b)) {
3436c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = mLabelCache.get(b);
3437c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3438c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = (b instanceof AppWidgetProviderInfo) ?
34391ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((AppWidgetProviderInfo) b).label :
3440780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                    ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
3441c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                mLabelCache.put(b, labelB);
3442c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
34431190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            return mCollator.compare(labelA, labelB);
34441ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        }
34451ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung    };
3446be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
3447be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
3448be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
3449eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3450eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3451eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3452eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
34533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
34543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
34553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
34563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
34573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
3458be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
345931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
3460