LauncherModel.java revision a0a7a7440e860c61c8261c5009dbe351869896b3
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
501a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk        synchronized (mDeferredBindRunnables) {
502a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            mDeferredBindRunnables.clear();
503a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk        }
50481b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Remove any queued bind runnables
50581b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
50681b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Unbind all the workspace items
50781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        unbindWorkspaceItemsOnMainThread();
508603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung    }
509603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung
510b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
51181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung    void unbindWorkspaceItemsOnMainThread() {
512603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        // Ensure that we don't use the same workspace items data structure on the main thread
513603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        // by making a copy of workspace items first.
514b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
515b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
5162abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung        synchronized (sBgLock) {
517b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            tmpWorkspaceItems.addAll(sBgWorkspaceItems);
518b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            tmpAppWidgets.addAll(sBgAppWidgets);
519b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
520b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        Runnable r = new Runnable() {
521b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                @Override
522b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                public void run() {
523b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   for (ItemInfo item : tmpWorkspaceItems) {
524b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                       item.unbind();
525b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   }
526b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   for (ItemInfo item : tmpAppWidgets) {
527b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                       item.unbind();
528b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                   }
529b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
530b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
531b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnMainThread(r);
5324eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen    }
5334eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen
5349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
5359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Adds an item to the DB if it was not created previously, or move it to a new
5369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * <container, screen, cellX, cellY>
5379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
5389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
539dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long screenId, int cellX, int cellY) {
5409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (item.container == ItemInfo.NO_ID) {
5419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From all apps
542dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
5439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } else {
5449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // From somewhere else
545dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            moveItemInDatabase(context, item, container, screenId, cellX, cellY);
5469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
5479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
54831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
549b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka    static void checkItemInfoLocked(
550b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
551b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        ItemInfo modelItem = sBgItemsIdMap.get(itemId);
552b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        if (modelItem != null && item != modelItem) {
553b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // check all the data is consistent
554b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
555b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
556b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                ShortcutInfo shortcut = (ShortcutInfo) item;
557b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
558b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.intent.filterEquals(shortcut.intent) &&
559b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.id == shortcut.id &&
560b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.itemType == shortcut.itemType &&
561b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.container == shortcut.container &&
562dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        modelShortcut.screenId == shortcut.screenId &&
563b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.cellX == shortcut.cellX &&
564b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.cellY == shortcut.cellY &&
565b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.spanX == shortcut.spanX &&
566b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.spanY == shortcut.spanY &&
567b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
568b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        (modelShortcut.dropPos != null &&
569b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                                shortcut.dropPos != null &&
570b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                                modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
571b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                        modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
572b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    // For all intents and purposes, this is the same object
573b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    return;
574b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                }
575b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
576b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka
577b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // the modelItem needs to match up perfectly with item if our model is
578b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // to be consistent with the database-- for now, just require
579b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            // modelItem == item or the equality check above
580b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            String msg = "item: " + ((item != null) ? item.toString() : "null") +
581b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    "modelItem: " +
582b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    ((modelItem != null) ? modelItem.toString() : "null") +
583b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    "Error: ItemInfo passed to checkItemInfo doesn't match original";
584b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            RuntimeException e = new RuntimeException(msg);
585b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            if (stackTrace != null) {
586b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                e.setStackTrace(stackTrace);
587b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
588b9ada65af30c61c5b35da9b9efac75ffe6c56fd7Adam Cohen            throw e;
589b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        }
590b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka    }
591b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka
592816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka    static void checkItemInfo(final ItemInfo item) {
593816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
594816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        final long itemId = item.id;
595816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        Runnable r = new Runnable() {
596b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            public void run() {
597b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                synchronized (sBgLock) {
598b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka                    checkItemInfoLocked(itemId, item, stackTrace);
599816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka                }
600b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka            }
601b2ae8acc0c65a5464a4782ee43e0594a15b9cc16Michael Jurka        };
602b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
603816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka    }
604816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka
605c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void updateItemInDatabaseHelper(Context context, final ContentValues values,
606c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            final ItemInfo item, final String callingFunction) {
607c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final long itemId = item.id;
608c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
609c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ContentResolver cr = context.getContentResolver();
610c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
611487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
612c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
613c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            public void run() {
614c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.update(uri, values, null, null);
615f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                updateItemArrays(item, itemId, stackTrace);
616f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
617f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        };
618f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        runOnWorkerThread(r);
619f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
620c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
621f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
622f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final ArrayList<ItemInfo> items, final String callingFunction) {
623f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        final ContentResolver cr = context.getContentResolver();
624c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
625f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
626f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        Runnable r = new Runnable() {
627f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            public void run() {
628f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                ArrayList<ContentProviderOperation> ops =
629f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        new ArrayList<ContentProviderOperation>();
630f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                int count = items.size();
631f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                for (int i = 0; i < count; i++) {
632f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ItemInfo item = items.get(i);
633f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    final long itemId = item.id;
634f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
635f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ContentValues values = valuesList.get(i);
636f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
637f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
638f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    updateItemArrays(item, itemId, stackTrace);
639487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
640f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
641f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                try {
642f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    cr.applyBatch(LauncherProvider.AUTHORITY, ops);
643f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                } catch (Exception e) {
644f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    e.printStackTrace();
645c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                }
646c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            }
647c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
648b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
649c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    }
650bebf042666cffe52039b875a549a582abd78a431Adam Cohen
651f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
652f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        // Lock on mBgLock *after* the db operation
653f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        synchronized (sBgLock) {
654f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            checkItemInfoLocked(itemId, item, stackTrace);
655f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
656f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
657f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
658f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                // Item is in a folder, make sure this folder exists
659f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                if (!sBgFolders.containsKey(item.container)) {
660f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    // An items container is being set to a that of an item which is not in
661f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    // the list of Folders.
662f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    String msg = "item: " + item + " container being set to: " +
663f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                            item.container + ", not in the list of folders";
664f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    Log.e(TAG, msg);
665f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
666f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
667f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
668f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // Items are added/removed from the corresponding FolderInfo elsewhere, such
669f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // as in Workspace.onDrop. Here, we just add/remove them from the list of items
670f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // that are on the desktop, as appropriate
671f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo modelItem = sBgItemsIdMap.get(itemId);
67233231f587e1a6f716af68e6198df774d2847a8e5Winson Chung            if (modelItem != null &&
67333231f587e1a6f716af68e6198df774d2847a8e5Winson Chung                    (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
67433231f587e1a6f716af68e6198df774d2847a8e5Winson Chung                     modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
675f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                switch (modelItem.itemType) {
676f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
677f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
678f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
679f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        if (!sBgWorkspaceItems.contains(modelItem)) {
680f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                            sBgWorkspaceItems.add(modelItem);
681f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        }
682f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        break;
683f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    default:
684f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        break;
685f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                }
686f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            } else {
687f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                sBgWorkspaceItems.remove(modelItem);
688f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
689f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
690f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
691f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
692c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    public void flushWorkerThread() {
693c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        mFlushingWorkerThread = true;
694c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        Runnable waiter = new Runnable() {
695c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                public void run() {
696c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    synchronized (this) {
697c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        notifyAll();
698c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        mFlushingWorkerThread = false;
699c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    }
700c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
701c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            };
702c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka
703c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        synchronized(waiter) {
704c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            runOnWorkerThread(waiter);
705c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            if (mLoaderTask != null) {
706c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                synchronized(mLoaderTask) {
707c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    mLoaderTask.notify();
708c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
709c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            }
710c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            boolean success = false;
711c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            while (!success) {
712c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                try {
713c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    waiter.wait();
714c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                    success = true;
715c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                } catch (InterruptedException e) {
716c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                }
717c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka            }
718c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka        }
719c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka    }
720c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka
7219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
7229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Move an item in the DB to a new <container, screen, cellX, cellY>
7239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
724a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
725dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY) {
7269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
7279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
7289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
729c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
7303d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // We store hotseat items in canonical form which is this orientation invariant position
7313d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // in the hotseat
732dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
7333d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
734dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
7353d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
736dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
7373d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
73831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
7399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
7409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
7413d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
7423d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
743dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
744ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
745c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
74631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
74731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
74831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
749f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
750f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     * cellX, cellY have already been updated on the ItemInfos.
751f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen     */
752f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
753f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final long container, final int screen) {
754f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
755f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
756f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        int count = items.size();
757f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
758f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        for (int i = 0; i < count; i++) {
759f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo item = items.get(i);
760f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            item.container = container;
761f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
762f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // We store hotseat items in canonical form which is this orientation invariant position
763f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            // in the hotseat
764f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            if (context instanceof Launcher && screen < 0 &&
765f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                    container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
766dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
767f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen                        item.cellY);
768f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            } else {
769dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = screen;
770f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            }
771f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
772f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            final ContentValues values = new ContentValues();
773f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CONTAINER, item.container);
774f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CELLX, item.cellX);
775f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            values.put(LauncherSettings.Favorites.CELLY, item.cellY);
776dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
777f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
778f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            contentValues.add(values);
779f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
780f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
781f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
782f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
783f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    /**
784bebf042666cffe52039b875a549a582abd78a431Adam Cohen     * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
785d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen     */
786bebf042666cffe52039b875a549a582abd78a431Adam Cohen    static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
787dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
7880f84a604563037ea65883b99d30e7ff802d986c2Winson Chung        item.container = container;
789d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellX = cellX;
790d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        item.cellY = cellY;
791bebf042666cffe52039b875a549a582abd78a431Adam Cohen        item.spanX = spanX;
792bebf042666cffe52039b875a549a582abd78a431Adam Cohen        item.spanY = spanY;
793bebf042666cffe52039b875a549a582abd78a431Adam Cohen
794bebf042666cffe52039b875a549a582abd78a431Adam Cohen        // We store hotseat items in canonical form which is this orientation invariant position
795bebf042666cffe52039b875a549a582abd78a431Adam Cohen        // in the hotseat
796dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
797bebf042666cffe52039b875a549a582abd78a431Adam Cohen                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
798dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
799bebf042666cffe52039b875a549a582abd78a431Adam Cohen        } else {
800dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
801bebf042666cffe52039b875a549a582abd78a431Adam Cohen        }
802d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
803d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        final ContentValues values = new ContentValues();
804d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen        values.put(LauncherSettings.Favorites.CONTAINER, item.container);
805bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.CELLX, item.cellX);
806bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.CELLY, item.cellY);
807bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.SPANX, item.spanX);
808bebf042666cffe52039b875a549a582abd78a431Adam Cohen        values.put(LauncherSettings.Favorites.SPANY, item.spanY);
809dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
810d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
811816474f06b9bea400f4511fa35b63a7f18a54c54Michael Jurka        updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
812bebf042666cffe52039b875a549a582abd78a431Adam Cohen    }
813c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
814c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    /**
815c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * Update an item to the database in a specified container.
816c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     */
817c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void updateItemInDatabase(Context context, final ItemInfo item) {
818c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ContentValues values = new ContentValues();
819c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        item.onAddToDatabase(values);
820c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
821c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
822d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    }
823d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
824d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    /**
8259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Returns true if the shortcuts already exists in the database.
8269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * we identify a shortcut by its title and intent.
827bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project     */
8289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static boolean shortcutExists(Context context, String title, Intent intent) {
8299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
8309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
8319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { "title", "intent" }, "title=? and intent=?",
8329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            new String[] { title, intent.toUri(0) }, null);
8339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        boolean result = false;
8349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
8359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            result = c.moveToFirst();
8369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
8379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
8389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
8399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return result;
840bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    }
841bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project
842bc219c3c127cb74cf4fe578f05022b745da921b8The Android Open Source Project    /**
843b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren     * Returns true if the shortcuts already exists in the database.
844b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren     * we identify a shortcut by the component name of the intent.
845b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren     */
846b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren    static boolean appWasRestored(Context context, Intent intent) {
847b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final ContentResolver cr = context.getContentResolver();
848b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final ComponentName component = intent.getComponent();
849b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        if (component == null) {
850b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            return false;
851b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        }
852b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        String componentName = component.flattenToString();
853b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
854b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
855b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                new String[]{"intent", "restored"}, where, null, null);
856b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        boolean result = false;
857b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        try {
858b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            result = c.moveToFirst();
859b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        } finally {
860b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            c.close();
861b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        }
862b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
863b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        return result;
864b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren    }
865b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren
866b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren    /**
867aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Returns an ItemInfo array containing all the items in the LauncherModel.
868aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * The ItemInfo.id is not set through this function.
869aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
870aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
871aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
872aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final ContentResolver cr = context.getContentResolver();
873aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
874aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
875aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
876aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
877aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
878aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
879aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
880aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
881aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
882aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
883aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
884aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
885aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
886aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        try {
887aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            while (c.moveToNext()) {
888c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                ItemInfo item = new ItemInfo();
889aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellX = c.getInt(cellXIndex);
890aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.cellY = c.getInt(cellYIndex);
89161c69869b3947f1d242b07384bb4e9cd5b266915Winson Chung                item.spanX = Math.max(1, c.getInt(spanXIndex));
89261c69869b3947f1d242b07384bb4e9cd5b266915Winson Chung                item.spanY = Math.max(1, c.getInt(spanYIndex));
893aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.container = c.getInt(containerIndex);
894aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                item.itemType = c.getInt(itemTypeIndex);
895dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                item.screenId = c.getInt(screenIndex);
896aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
897aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                items.add(item);
898aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            }
899aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } catch (Exception e) {
900aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            items.clear();
901aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        } finally {
902aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            c.close();
903aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
904aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
905aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        return items;
906aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
907aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
908aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
9099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
91031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
9119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
9129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
9139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
9149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                "_id=? and (itemType=? or itemType=?)",
9159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                new String[] { String.valueOf(id),
916df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
917f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
9189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        try {
9199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (c.moveToFirst()) {
9209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
9219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
9229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
9239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
9249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
9259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
926f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
9279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                FolderInfo folderInfo = null;
9289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                switch (c.getInt(itemTypeIndex)) {
929df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                    case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
930df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        folderInfo = findOrMakeFolder(folderList, id);
9319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        break;
9329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
93331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.title = c.getString(titleIndex);
9359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.id = id;
9369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                folderInfo.container = c.getInt(containerIndex);
937dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                folderInfo.screenId = c.getInt(screenIndex);
938d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellX = c.getInt(cellXIndex);
939d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen                folderInfo.cellY = c.getInt(cellYIndex);
940ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
9419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                return folderInfo;
9429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
9439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        } finally {
9449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            c.close();
9452fcbd686d1c2a9a9e520116a541c9caae0c2dc60Romain Guy        }
946ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
9479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return null;
9489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
94931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
9519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add an item to the database in a specified container. Sets the container, screen, cellX and
9529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * cellY fields of the item. Also assigns an ID to the item.
9539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
9543d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    static void addItemToDatabase(Context context, final ItemInfo item, final long container,
955dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final long screenId, final int cellX, final int cellY, final boolean notify) {
9569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.container = container;
9579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellX = cellX;
9589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.cellY = cellY;
9593d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // We store hotseat items in canonical form which is this orientation invariant position
9603d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // in the hotseat
961dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (context instanceof Launcher && screenId < 0 &&
9623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
963dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
9643d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
965dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            item.screenId = screenId;
9663d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
967ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
9689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentValues values = new ContentValues();
9699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
9709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.onAddToDatabase(values);
971ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
972414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka        item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
973a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka        values.put(LauncherSettings.Favorites._ID, item.id);
9743d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
975aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
9768e19cf22a4ab86f982b08e7d10b86ea7be56acccJason Monk        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
977c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
978a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            public void run() {
979a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
980a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                        LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
981ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
982b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
9832abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
9848e19cf22a4ab86f982b08e7d10b86ea7be56acccJason Monk                    checkItemInfoLocked(item.id, item, stackTrace);
985b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.put(item.id, item);
986b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    switch (item.itemType) {
987b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
988b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgFolders.put(item.id, (FolderInfo) item);
989b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            // Fall through
990b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
991b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
992b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
993b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
994b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgWorkspaceItems.add(item);
995b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            } else {
996b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!sBgFolders.containsKey(item.container)) {
997b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // Adding an item to a folder that doesn't exist.
998b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String msg = "adding item: " + item + " to a folder that " +
999b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            " doesn't exist";
100028b3e10fdea4d719b46d2e0da9a916bacc238fd1Adam Cohen                                    Log.e(TAG, msg);
1001b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1002487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                            }
1003b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1004b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1005b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1006b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1007b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1008a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                }
1009a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            }
1010c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
1011b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
101231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
101331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1015aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     * Creates a new unique child id, for a given cell span across all layouts.
1016aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung     */
1017845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka    static int getCellLayoutChildId(
1018dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
10193d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return (((int) container & 0xFF) << 24)
1020dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
1021aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
1022aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1023aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    /**
1024c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * Removes the specified item from the database
1025c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * @param context
1026c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka     * @param item
10279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1028c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    static void deleteItemFromDatabase(Context context, final ItemInfo item) {
10299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1030c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
1031487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
103283df188ce0822f4058406e5f2878d1cf34bf413eMichael Jurka        Runnable r = new Runnable() {
1033a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            public void run() {
1034c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(uriToDelete, null, null);
1035b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1036b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
10372abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1038b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    switch (item.itemType) {
1039b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1040b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgFolders.remove(item.id);
1041b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            for (ItemInfo info: sBgItemsIdMap.values()) {
1042b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (info.container == item.id) {
1043b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // We are deleting a folder which still contains items that
1044b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // think they are contained by that folder.
1045b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String msg = "deleting a folder (" + item + ") which still " +
1046b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            "contains items (" + info + ")";
104728b3e10fdea4d719b46d2e0da9a916bacc238fd1Adam Cohen                                    Log.e(TAG, msg);
1048b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1049487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                            }
1050b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgWorkspaceItems.remove(item);
1051b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1052b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1053b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1054b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgWorkspaceItems.remove(item);
1055b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1056b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1057b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1058b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            break;
1059b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1060b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.remove(item.id);
1061b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgDbIconCache.remove(item);
1062a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka                }
1063a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            }
106483df188ce0822f4058406e5f2878d1cf34bf413eMichael Jurka        };
1065b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
106631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
106731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
1069dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * Update the order of the workspace screens in the database. The array list contains
1070dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * a list of screen ids in the order that they should appear.
1071dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     */
1072c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
1073a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        // Log to disk
1074a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1075a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        Launcher.addDumpLog(TAG, "11683562 -   screens: " + TextUtils.join(", ", screens), true);
1076a90303bb6fdad59e4a55855edba68c9902192599Winson Chung
107764359a53dc827797917a8d9a0697a91996789801Winson Chung        final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
1078dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final ContentResolver cr = context.getContentResolver();
1079dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1080dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1081dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        // Remove any negative screen ids -- these aren't persisted
108264359a53dc827797917a8d9a0697a91996789801Winson Chung        Iterator<Long> iter = screensCopy.iterator();
1083dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        while (iter.hasNext()) {
1084dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long id = iter.next();
1085dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (id < 0) {
1086dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                iter.remove();
1087dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1088dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
1089dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1090dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        Runnable r = new Runnable() {
1091dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            @Override
1092dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            public void run() {
1093085c853a5702c45865e9b017d21fa15cf2b151b9Yura                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
1094dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                // Clear the table
1095085c853a5702c45865e9b017d21fa15cf2b151b9Yura                ops.add(ContentProviderOperation.newDelete(uri).build());
109676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                int count = screensCopy.size();
1097dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                for (int i = 0; i < count; i++) {
1098dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    ContentValues v = new ContentValues();
109976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = screensCopy.get(i);
1100dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1101dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1102085c853a5702c45865e9b017d21fa15cf2b151b9Yura                    ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
1103085c853a5702c45865e9b017d21fa15cf2b151b9Yura                }
1104085c853a5702c45865e9b017d21fa15cf2b151b9Yura
1105085c853a5702c45865e9b017d21fa15cf2b151b9Yura                try {
1106085c853a5702c45865e9b017d21fa15cf2b151b9Yura                    cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1107085c853a5702c45865e9b017d21fa15cf2b151b9Yura                } catch (Exception ex) {
1108085c853a5702c45865e9b017d21fa15cf2b151b9Yura                    throw new RuntimeException(ex);
1109dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
11109e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
1111ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                synchronized (sBgLock) {
1112ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    sBgWorkspaceScreens.clear();
1113ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    sBgWorkspaceScreens.addAll(screensCopy);
11144caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                }
1115dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1116dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        };
1117dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        runOnWorkerThread(r);
1118dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
1119dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1120dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    /**
11219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Remove the contents of the specified folder from the database
11229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1123a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka    static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
11249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final ContentResolver cr = context.getContentResolver();
1125f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1126c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        Runnable r = new Runnable() {
1127c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            public void run() {
1128c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
1129b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
11302abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1131b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgItemsIdMap.remove(info.id);
1132b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgFolders.remove(info.id);
1133b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgDbIconCache.remove(info);
1134b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sBgWorkspaceItems.remove(info);
1135b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1136c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka
1137c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1138c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                        LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
1139b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Lock on mBgLock *after* the db operation
11402abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                synchronized (sBgLock) {
1141b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (ItemInfo childInfo : info.contents) {
1142b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        sBgItemsIdMap.remove(childInfo.id);
1143b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        sBgDbIconCache.remove(childInfo);
1144b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
1145afb01ee74243cede19088e694ca82cea5983c603Adam Cohen                }
1146c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            }
1147c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        };
1148b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        runOnWorkerThread(r);
11499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
1150f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
11519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
11529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Set this as the current Launcher activity object for the loader.
11539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
11549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void initialize(Callbacks callbacks) {
11559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (mLock) {
11569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mCallbacks = new WeakReference<Callbacks>(callbacks);
1157f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
1158f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
1159f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
11601d8e7bbe09e130af5e9534cc26a65f623be7a424Joe Onorato    /**
11619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
11629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * ACTION_PACKAGE_CHANGED.
11639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
1164cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    @Override
1165f99f8c1ad265cf7bea7e30476911d730b7d0eaaeJoe Onorato    public void onReceive(Context context, Intent intent) {
11663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
1167aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
11683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        final String action = intent.getAction();
11699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
11703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
11713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_REMOVED.equals(action)
11723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
11733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String packageName = intent.getData().getSchemeSpecificPart();
11743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1175f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
11763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            int op = PackageUpdatedTask.OP_NONE;
117764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
11783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (packageName == null || packageName.length() == 0) {
11793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // they sent us a bad intent
11803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
11813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
118256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
11833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
11843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                op = PackageUpdatedTask.OP_UPDATE;
11853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
11863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
11873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_REMOVE;
118856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
11893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // else, we are replacing the package, so a PACKAGE_ADDED will be sent
11903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // later, we will update the package at this time
11913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
11923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (!replacing) {
11933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_ADD;
11943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                } else {
11953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    op = PackageUpdatedTask.OP_UPDATE;
119656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
11973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
119856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
11993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (op != PackageUpdatedTask.OP_NONE) {
12003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1201f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
1202f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
12033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
12043ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1205cec583352cd76bd4319d56beec03a4820a3e9d58Joe Onorato            String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
12063ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            if (!replacing) {
12073ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
12083ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                if (mAppsCanBeOnRemoveableStorage) {
12093ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                    // Only rebind if we support removable storage.  It catches the case where
12103ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                    // apps on the external sd card need to be reloaded
12113ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                    startLoaderFromBackground();
12123ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                }
12133ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            } else {
12143ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                // If we are replacing then just update the packages in the list
12153ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
12163ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                        packages));
12173ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            }
12183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
12193ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
12203ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            if (!replacing) {
12213ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
12223ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                enqueuePackageUpdated(new PackageUpdatedTask(
12233ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung                            PackageUpdatedTask.OP_UNAVAILABLE, packages));
12243ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            }
12253ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            // else, we are replacing the packages, so ignore this event and wait for
12263ee4a474f2a1a76682b1b1ec261d6c68f3b7349dWinson Chung            // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
1227e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
122893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // If we have changed locale we need to clear out the labels in all apps/workspace.
122993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            forceReload();
123093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
123193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // Check if configuration change was an mcc/mnc change which would affect app resources
123293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // and we would need to clear out the labels in all apps/workspace. Same handling as
123393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // above for ACTION_LOCALE_CHANGED
123493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             Configuration currentConfig = context.getResources().getConfiguration();
123599a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee             if (mPreviousConfigMcc != currentConfig.mcc) {
123693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                   Log.d(TAG, "Reload apps on config change. curr_mcc:"
123799a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee                       + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
123893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                   forceReload();
123993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             }
124093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee             // Update previousConfig
124199a73f31b7ebed102f393c1bc1909007d7fa8a56Reena Lee             mPreviousConfigMcc = currentConfig.mcc;
1242cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1243cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung                   SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
1244ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka            if (mCallbacks != null) {
1245ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                Callbacks callbacks = mCallbacks.get();
1246ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                if (callbacks != null) {
1247ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                    callbacks.bindSearchablesChanged();
1248ec9788e6819a24ef5dc8fe4edf44f280e4a703feMichael Jurka                }
1249cfdf7ee64db8820d91a1cd82bf7b961fb2083dceWinson Chung            }
1250e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
1251e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    }
1252e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato
125393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    private void forceReload() {
1254f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        resetLoadedState(true, true);
1255f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1256f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Do this here because if the launcher activity is running it will be restarted.
1257f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // If it's not running startLoaderFromBackground will merely tell it that it needs
1258f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // to reload.
1259f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        startLoaderFromBackground();
1260f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    }
1261f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1262f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
126393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        synchronized (mLock) {
126493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // Stop any existing loaders first, so they don't set mAllAppsLoaded or
126593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            // mWorkspaceLoaded to true later
126693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            stopLoaderLocked();
1267f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            if (resetAllAppsLoaded) mAllAppsLoaded = false;
1268f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
126993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        }
127093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    }
127193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1272e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    /**
1273e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * When the launcher is in the background, it's possible for it to miss paired
1274e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * configuration changes.  So whenever we trigger the loader from the background
1275e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * tell the launcher that it needs to re-run the loader when it comes back instead
1276e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     * of doing it now.
1277e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato     */
1278e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato    public void startLoaderFromBackground() {
1279e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        boolean runLoader = false;
1280e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (mCallbacks != null) {
1281e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            Callbacks callbacks = mCallbacks.get();
1282e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            if (callbacks != null) {
1283e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                // Only actually run the loader if they're not paused.
1284e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                if (!callbacks.setLoadOnResume()) {
1285e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                    runLoader = true;
1286e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato                }
1287e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato            }
1288e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        }
1289e9ad59eba6d8ffd2cbf28520c237ccefd291a33cJoe Onorato        if (runLoader) {
12907aff399974c756930070d82d7b2df88f125dacd6Derek Prothro            startLoader(false, PagedView.INVALID_RESTORE_PAGE);
1291f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
12923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1293f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
129493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    // If there is already a loader task running, tell it to stop.
129593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    // returns true if isLaunching() was true on the old task
129693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    private boolean stopLoaderLocked() {
129793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        boolean isLaunching = false;
129893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        LoaderTask oldTask = mLoaderTask;
129993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        if (oldTask != null) {
130093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            if (oldTask.isLaunching()) {
130193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                isLaunching = true;
130293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            }
130393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee            oldTask.stopLocked();
130493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        }
130593f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee        return isLaunching;
130693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee    }
130793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee
1308b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    public void startLoader(boolean isLaunching, int synchronousBindPage) {
1309d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler        startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1310d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler    }
1311d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler
1312d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler    public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
13133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
13143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
13153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "startLoader isLaunching=" + isLaunching);
13163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1317843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
1318b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Clear any deferred bind-runnables from the synchronized load process
1319b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // We must do this before any loading/binding is scheduled below.
1320a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            synchronized (mDeferredBindRunnables) {
1321a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                mDeferredBindRunnables.clear();
1322a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            }
1323b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
13243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Don't bother to start the thread if we know it's not going to do anything
13253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mCallbacks != null && mCallbacks.get() != null) {
13263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If there is already one running, tell it to stop.
132793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                // also, don't downgrade isLaunching if we're already running
132893f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                isLaunching = isLaunching || stopLoaderLocked();
1329d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
13307aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
13317aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                        && mAllAppsLoaded && mWorkspaceLoaded) {
1332b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1333b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
1334b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1335b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    sWorker.post(mLoaderTask);
1336b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1337b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1338b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
1339b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    }
1340b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1341b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung    void bindRemainingSynchronousPages() {
1342b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        // Post the remaining side pages to be loaded
1343b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        if (!mDeferredBindRunnables.isEmpty()) {
1344a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            Runnable[] deferredBindRunnables = null;
1345a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            synchronized (mDeferredBindRunnables) {
1346a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                deferredBindRunnables = mDeferredBindRunnables.toArray(
1347a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                        new Runnable[mDeferredBindRunnables.size()]);
1348a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                mDeferredBindRunnables.clear();
1349a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            }
1350a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            for (final Runnable r : deferredBindRunnables) {
135181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
1352b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy            }
1353b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy        }
13543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1355b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
13563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void stopLoader() {
13573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        synchronized (mLock) {
13583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (mLoaderTask != null) {
13593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mLoaderTask.stopLocked();
13609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
1361f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
13623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
1363b132a97cb7ee976bc54370fd0d5899876eaaf33aRomain Guy
136476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    /** Loads the workspace screens db into a map of Rank -> ScreenId */
136576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
136676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final ContentResolver contentResolver = context.getContentResolver();
136776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
136876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
136976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
137076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
137176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        try {
137276828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            final int idIndex = sc.getColumnIndexOrThrow(
137376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    LauncherSettings.WorkspaceScreens._ID);
137476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            final int rankIndex = sc.getColumnIndexOrThrow(
137576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    LauncherSettings.WorkspaceScreens.SCREEN_RANK);
137676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            while (sc.moveToNext()) {
137776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                try {
137876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    long screenId = sc.getLong(idIndex);
137976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    int rank = sc.getInt(rankIndex);
138076828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    orderedScreens.put(rank, screenId);
138176828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                } catch (Exception e) {
1382ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
138376828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                }
138476828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            }
138576828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        } finally {
138676828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung            sc.close();
138776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        }
1388a90303bb6fdad59e4a55855edba68c9902192599Winson Chung
1389a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        // Log to disk
1390a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1391a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1392a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        for (Integer i : orderedScreens.keySet()) {
1393a90303bb6fdad59e4a55855edba68c9902192599Winson Chung            orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1394a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        }
1395a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        Launcher.addDumpLog(TAG, "11683562 -   screens: " +
1396a90303bb6fdad59e4a55855edba68c9902192599Winson Chung                TextUtils.join(", ", orderedScreensPairs), true);
139776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung        return orderedScreens;
139876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung    }
139976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung
1400c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    public boolean isAllAppsLoaded() {
1401c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        return mAllAppsLoaded;
1402c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    }
1403c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka
140436a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    boolean isLoadingWorkspace() {
140536a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        synchronized (mLock) {
140636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            if (mLoaderTask != null) {
140736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung                return mLoaderTask.isLoadingWorkspace();
140836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            }
140936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        }
141036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        return false;
141136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    }
141236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
14133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    /**
14143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     * Runnable for the thread that loads the contents of the launcher:
14153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - workspace icons
14163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - widgets
14173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     *   - all apps icons
14183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato     */
14193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class LoaderTask implements Runnable {
14203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private Context mContext;
14213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mIsLaunching;
142236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        private boolean mIsLoadingAndBindingWorkspace;
14233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mStopped;
14243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private boolean mLoadAndBindStepFinished;
1425d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler        private int mFlags;
1426b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1427c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        private HashMap<Object, CharSequence> mLabelCache;
14283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1429d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler        LoaderTask(Context context, boolean isLaunching, int flags) {
14303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = context;
14313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mIsLaunching = isLaunching;
1432c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = new HashMap<Object, CharSequence>();
1433d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            mFlags = flags;
14343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1435f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
14363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        boolean isLaunching() {
14373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return mIsLaunching;
14383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1439f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
144036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        boolean isLoadingWorkspace() {
144136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            return mIsLoadingAndBindingWorkspace;
144236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        }
144336a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
1444c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        /** Returns whether this is an upgrade path */
1445c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private boolean loadAndBindWorkspace() {
144636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung            mIsLoadingAndBindingWorkspace = true;
144736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
14483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Load the workspace
14493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
14503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
14513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1452288a36b563801a26d00084cb823435a3ba4cb76cMichael Jurka
1453c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean isUpgradePath = false;
1454a8c760d395e1d2a78522427738302fbca3a72453Michael Jurka            if (!mWorkspaceLoaded) {
1455c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                isUpgradePath = loadWorkspace();
145693f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                synchronized (LoaderTask.this) {
145793f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    if (mStopped) {
1458c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        return isUpgradePath;
145993f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    }
146093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    mWorkspaceLoaded = true;
14619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
14623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1463cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
14643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Bind the workspace
1465c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            bindWorkspace(-1, isUpgradePath);
1466c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            return isUpgradePath;
14673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1468f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
14693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void waitForIdle() {
14703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Wait until the either we're stopped or the other threads are done.
14713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // This way we don't start loading all apps until the workspace has settled
14723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // down.
14733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
14743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1475843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
14763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.postIdle(new Runnable() {
14773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        public void run() {
14783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            synchronized (LoaderTask.this) {
14793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                mLoadAndBindStepFinished = true;
14803611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                if (DEBUG_LOADERS) {
14813611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    Log.d(TAG, "done with previous binding step");
14829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
14833611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                LoaderTask.this.notify();
14849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
14859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
14863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    });
14873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1488c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
14893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    try {
1490c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        // Just in case mFlushingWorkerThread changes but we aren't woken up,
1491c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        // wait no longer than 1sec at a time
1492c7700af21f7b1df939fce22f4ab5072b3ea29b66Michael Jurka                        this.wait(1000);
14933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    } catch (InterruptedException ex) {
14943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        // Ignore
14959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
14963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
14973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (DEBUG_LOADERS) {
14983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.d(TAG, "waited "
1499aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + (SystemClock.uptimeMillis()-workspaceWaitTime)
15003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + "ms for previous step to finish binding");
15019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
1502843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            }
15033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1504f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1505b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        void runBindSynchronousPage(int synchronousBindPage) {
15067aff399974c756930070d82d7b2df88f125dacd6Derek Prothro            if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
1507b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Ensure that we have a valid page index to load synchronously
1508b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1509b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        "valid page index");
1510b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1511b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1512b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Ensure that we don't try and bind a specified page when the pages have not been
1513b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // loaded already (we should load everything asynchronously in that case)
1514b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1515b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1516b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            synchronized (mLock) {
1517b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (mIsLoaderTaskRunning) {
1518b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // Ensure that we are never running the background loading at this point since
1519b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // we also touch the background collections
1520b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    throw new RuntimeException("Error! Background loading is already running");
1521b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1522b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
1523b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1524b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: Throw an exception if we are already loading (since we touch the worker thread
1525b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      data structures, we can't allow any other thread to touch that data, but because
1526b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      this call is synchronous, we can get away with not locking).
1527b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1528cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler            // The LauncherModel is static in the LauncherAppState and mHandler may have queued
1529a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            // operations from the previous activity.  We need to ensure that all queued operations
1530a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            // are executed before any synchronous binding work is done.
1531a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen            mHandler.flush();
1532a13a2f2a7bd0550d1ad973f760ff25e1a4137c43Adam Cohen
1533b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Divide the set of loaded items into those that we are binding synchronously, and
1534b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // everything else that is to be bound normally (asynchronously).
1535c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            bindWorkspace(synchronousBindPage, false);
1536b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: For now, continue posting the binding of AllApps as there are other issues that
1537b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            //      arise from that.
1538b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            onlyBindAllApps();
1539b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
1540b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
15413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
1542c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            boolean isUpgrade = false;
1543c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1544b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            synchronized (mLock) {
1545b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                mIsLoaderTaskRunning = true;
1546b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
15473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Optimize for end-user experience: if the Launcher is up and // running with the
15483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
15493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // workspace first (default).
15503611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            keep_running: {
1551843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // Elevate priority when Home launches for the first time to avoid
1552843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // starving at boot time. Staring at a blank home is not cool.
1553dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                synchronized (mLock) {
1554aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                    if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1555aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                            (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
1556843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    android.os.Process.setThreadPriority(mIsLaunching
1557843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                            ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1558dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                }
155964359a53dc827797917a8d9a0697a91996789801Winson Chung                if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1560c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                isUpgrade = loadAndBindWorkspace();
1561843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
15623611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
15633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break keep_running;
15643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
15653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
15663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // Whew! Hard work done.  Slow us down, and wait until the UI thread has
15673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // settled down.
15689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                synchronized (mLock) {
1569843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    if (mIsLaunching) {
1570aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                        if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
1571843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
15729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
15739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
15743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                waitForIdle();
1575f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1576843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                // second step
157764359a53dc827797917a8d9a0697a91996789801Winson Chung                if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
157864359a53dc827797917a8d9a0697a91996789801Winson Chung                loadAndBindAllApps();
15797ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung
15807ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                // Restore the default thread priority after we are done loading items
15817ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                synchronized (mLock) {
15827ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                    android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
15837ed3774638133ac603e92ca3174ecdd151269ccaWinson Chung                }
15843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1585843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
1586aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // Update the saved icons if necessary
1587aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
15882abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
1589b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                for (Object key : sBgDbIconCache.keySet()) {
1590b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1591b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
1592b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                sBgDbIconCache.clear();
1593aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            }
1594aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung
159516f3ea870aac47292cd6cbe1a4b4343173097aa9Nilesh Agrawal            if (LauncherAppState.isDisableAllApps()) {
1596c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                // Ensure that all the applications that are in the system are
1597c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                // represented on the home screen.
1598c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
1599c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                    verifyApplications();
1600c58497ee619416c4949b16eb43486c11618b69e5Winson Chung                }
1601c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1602c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
16033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // Clear out this reference, otherwise we end up holding it until all of the
16043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // callback runnables are done.
16053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mContext = null;
1606f3d5ea926d4dd1a739b9d3816543728af723cc26Joe Onorato
16073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
16083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // If we are still the last one to be scheduled, remove ourselves.
16093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mLoaderTask == this) {
16103611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    mLoaderTask = null;
1611843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                }
1612b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                mIsLoaderTaskRunning = false;
16133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
16143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1615f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
16163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void stopLocked() {
16173611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (LoaderTask.this) {
16183611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mStopped = true;
16193611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                this.notify();
1620f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
16213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1622f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
16233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        /**
16243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * Gets the callbacks object.  If we've been stopped, or if the launcher object
16253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * has somehow been garbage collected, return null instead.  Pass in the Callbacks
16263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * object that was around when the deferred message was scheduled, and if there's
16273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * a new Callbacks object around then also return null.  This will save us from
16283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         * calling onto it with data that will be ignored.
16293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato         */
16303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
16313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            synchronized (mLock) {
16323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mStopped) {
16333611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
16343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1635ac03330c9684f98b4dd067a87ca70d3e0be1cfedJoe Onorato
16363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (mCallbacks == null) {
16373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
16383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
1639f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
16403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final Callbacks callbacks = mCallbacks.get();
16413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks != oldCallbacks) {
16423611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
16433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
16443611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                if (callbacks == null) {
16453611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Log.w(TAG, "no mCallbacks");
16463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    return null;
16479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
16483611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
16493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return callbacks;
1650f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
16513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
1652f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1653c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private void verifyApplications() {
1654c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            final Context context = mApp.getContext();
1655c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
1656c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Cross reference all the applications in our apps list with items in the workspace
1657c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            ArrayList<ItemInfo> tmpInfos;
1658695ff6b6fada4e1037592d7c4961321e12890b7dMichael Jurka            ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
1659c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            synchronized (sBgLock) {
1660eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                for (AppInfo app : mBgAllAppsList.data) {
1661c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    tmpInfos = getItemInfoForComponentName(app.componentName);
1662c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    if (tmpInfos.isEmpty()) {
1663c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        // We are missing an application icon, so add this to the workspace
1664c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        added.add(app);
1665c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        // This is a rare event, so lets log it
1666c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        Log.e(TAG, "Missing Application on load: " + app);
1667c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    }
1668c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                }
1669c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1670c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            if (!added.isEmpty()) {
167176a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                addAndBindAddedWorkspaceApps(context, added);
1672c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            }
1673c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        }
1674c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
16753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        // check & update map of what's occupied; used to discard overlapping/invalid items
1676a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung        private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1677ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                           AtomicBoolean deleteOnInvalidPlacement) {
1678892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            LauncherAppState app = LauncherAppState.getInstance();
1679892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1680295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler            final int countX = (int) grid.numColumns;
1681295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler            final int countY = (int) grid.numRows;
1682892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung
1683dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            long containerIndex = item.screenId;
1684f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1685a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                // Return early if we detect that an item is under the hotseat button
1686a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                if (mCallbacks == null ||
1687a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                        mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1688ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    deleteOnInvalidPlacement.set(true);
1689295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                    Log.e(TAG, "Error loading shortcut into hotseat " + item
1690295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                            + " into position (" + item.screenId + ":" + item.cellX + ","
1691295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                            + item.cellY + ") occupied by all apps");
1692a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                    return false;
1693a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                }
1694a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung
1695295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                final ItemInfo[][] hotseatItems =
1696295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                        occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1697295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler
1698ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                if (item.screenId >= grid.numHotseatIcons) {
1699ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    Log.e(TAG, "Error loading shortcut " + item
1700ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                            + " into hotseat position " + item.screenId
1701ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                            + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1702ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                            + ")");
1703ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    return false;
1704ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                }
1705ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen
1706295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                if (hotseatItems != null) {
1707295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                    if (hotseatItems[(int) item.screenId][0] != null) {
1708dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        Log.e(TAG, "Error loading shortcut into hotseat " + item
1709dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + " into position (" + item.screenId + ":" + item.cellX + ","
1710dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + item.cellY + ") occupied by "
1711dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1712dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                [(int) item.screenId][0]);
1713dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            return false;
1714295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                    } else {
1715295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                        hotseatItems[(int) item.screenId][0] = item;
1716295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                        return true;
1717dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
17186ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                } else {
1719ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
1720dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    items[(int) item.screenId][0] = item;
1721dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
17226ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                    return true;
17236ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung                }
1724f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung            } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1725f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung                // Skip further checking if it is not the hotseat or workspace container
17263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return true;
17273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
1728f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung
1729dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (!occupied.containsKey(item.screenId)) {
1730892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
1731dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                occupied.put(item.screenId, items);
1732dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
1733dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1734295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler            final ItemInfo[][] screens = occupied.get(item.screenId);
1735ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen            if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1736ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    item.cellX < 0 || item.cellY < 0 ||
1737ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                    item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1738ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                Log.e(TAG, "Error loading shortcut " + item
1739ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                        + " into cell (" + containerIndex + "-" + item.screenId + ":"
1740ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                        + item.cellX + "," + item.cellY
1741ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                        + ") out of screen bounds ( " + countX + "x" + countY + ")");
1742ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                return false;
1743ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen            }
1744ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen
17456ba2a1b6e7595a14ba33e98f728b39d0ecb3ae36Winson Chung            // Check if any workspace icons overlap with each other
17463611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
17473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
1748dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (screens[x][y] != null) {
17493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.e(TAG, "Error loading shortcut " + item
1750dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            + " into cell (" + containerIndex + "-" + item.screenId + ":"
17513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + x + "," + y
1752aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                            + ") occupied by "
1753dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            + screens[x][y]);
17543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        return false;
17558802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                    }
17568802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
17573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
17583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
17593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
1760dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    screens[x][y] = item;
17618802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler                }
17628802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler            }
1763f30ad5f1bf33baceeca6b770464fb543b58af985Winson Chung
17643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            return true;
17653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
17668802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
1767ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        /** Clears all the sBg data structures */
1768ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        private void clearSBgDataStructures() {
1769ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            synchronized (sBgLock) {
1770ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgWorkspaceItems.clear();
1771ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgAppWidgets.clear();
1772ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgFolders.clear();
1773ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgItemsIdMap.clear();
1774ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgDbIconCache.clear();
1775ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                sBgWorkspaceScreens.clear();
1776ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            }
1777ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung        }
1778ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung
1779d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler        /** Returns whether this is an upgrade path */
1780c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private boolean loadWorkspace() {
17819f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung            // Log to disk
17829f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung            Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
17839f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung
17843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1785f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
17863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Context context = mContext;
17873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final ContentResolver contentResolver = context.getContentResolver();
17883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final PackageManager manager = context.getPackageManager();
17893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
17903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final boolean isSafeMode = manager.isSafeMode();
1791f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
1792892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            LauncherAppState app = LauncherAppState.getInstance();
1793892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1794892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            int countX = (int) grid.numColumns;
1795892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            int countY = (int) grid.numRows;
1796892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung
1797d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1798d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1799d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                LauncherAppState.getLauncherProvider().deleteDatabase();
1800d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            }
1801d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler
1802d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1803d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                // append the user's Launcher2 shortcuts
1804d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1805d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1806d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            } else {
1807d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                // Make sure the default workspace is loaded
1808d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1809d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler                LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1810d502404a44fb7c4ea739622d7f8bdd2a764d97a1Dan Sandler            }
1811e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
1812c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            // Check if we need to do any upgrade-path logic
1813f0b8dacf1516e0d901c71109c906dc15ef855fc5Dan Sandler            // (Includes having just imported default favorites)
1814414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka            boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
1815f0b8dacf1516e0d901c71109c906dc15ef855fc5Dan Sandler
18169f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung            // Log to disk
18179f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung            Launcher.addDumpLog(TAG, "11683562 -   loadedOldDb: " + loadedOldDb, true);
1818b85f8a44b51258f22938773ca30dd85845345010Michael Jurka
18192abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
1820ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                clearSBgDataStructures();
1821b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1822b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
1823f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                final ArrayList<Long> restoredRows = new ArrayList<Long>();
1824c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
1825e523e70f75ff90ca5d613fb8398d37208e6cd32dChris Wren                if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
1826e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                final Cursor c = contentResolver.query(contentUri, null, null, null, null);
1827b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1828b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // +1 for the hotseat (it can be larger than the workspace)
1829b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // Load workspace in reverse order to ensure that latest items are loaded first (and
1830b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // before any earlier duplicates)
1831dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
1832b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1833b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                try {
1834b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1835b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int intentIndex = c.getColumnIndexOrThrow
1836b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.INTENT);
1837b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int titleIndex = c.getColumnIndexOrThrow
1838b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.TITLE);
1839b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconTypeIndex = c.getColumnIndexOrThrow(
1840b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_TYPE);
1841b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1842b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconPackageIndex = c.getColumnIndexOrThrow(
1843b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_PACKAGE);
1844b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int iconResourceIndex = c.getColumnIndexOrThrow(
1845b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ICON_RESOURCE);
1846b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int containerIndex = c.getColumnIndexOrThrow(
1847b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.CONTAINER);
1848b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int itemTypeIndex = c.getColumnIndexOrThrow(
1849b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.ITEM_TYPE);
1850b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1851b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.APPWIDGET_ID);
1852c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                    final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1853c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                            LauncherSettings.Favorites.APPWIDGET_PROVIDER);
1854b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int screenIndex = c.getColumnIndexOrThrow(
1855b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.SCREEN);
1856b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int cellXIndex = c.getColumnIndexOrThrow
1857b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.CELLX);
1858b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int cellYIndex = c.getColumnIndexOrThrow
1859b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.CELLY);
1860b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int spanXIndex = c.getColumnIndexOrThrow
1861b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            (LauncherSettings.Favorites.SPANX);
1862b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    final int spanYIndex = c.getColumnIndexOrThrow(
1863b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            LauncherSettings.Favorites.SPANY);
1864f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    final int restoredIndex = c.getColumnIndexOrThrow(
1865f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                            LauncherSettings.Favorites.RESTORED);
1866b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1867b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //final int displayModeIndex = c.getColumnIndexOrThrow(
1868b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    //        LauncherSettings.Favorites.DISPLAY_MODE);
1869b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1870b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    ShortcutInfo info;
1871b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    String intentDescription;
1872b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    LauncherAppWidgetInfo appWidgetInfo;
1873b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    int container;
1874b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    long id;
1875b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Intent intent;
1876b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1877b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    while (!mStopped && c.moveToNext()) {
1878ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                        AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
1879b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        try {
1880b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            int itemType = c.getInt(itemTypeIndex);
1881f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                            boolean restored = 0 != c.getInt(restoredIndex);
1882b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1883b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            switch (itemType) {
1884b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1885b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1886ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                id = c.getLong(idIndex);
1887b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                intentDescription = c.getString(intentIndex);
1888b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                try {
1889b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    intent = Intent.parseUri(intentDescription, 0);
1890ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    ComponentName cn = intent.getComponent();
189168fd3c33daf7a6d4de287f8691306a55fa54706cWinson Chung                                    if (cn != null && !isValidPackageComponent(manager, cn)) {
1892f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        if (restored) {
1893f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            // might be installed later
1894f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            Launcher.addDumpLog(TAG,
1895f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                    "package not yet restored: " + cn, true);
1896ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        } else {
1897f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            if (!mAppsCanBeOnRemoveableStorage) {
1898f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                // Log the invalid package, and remove it
1899f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                Launcher.addDumpLog(TAG,
1900f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                        "Invalid package removed: " + cn, true);
1901f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                itemsToRemove.add(id);
1902f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            } else {
1903f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                // If apps can be on external storage, then we just
1904f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                // leave them for the user to remove (maybe add
1905f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                // visual treatment to it)
1906f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                Launcher.addDumpLog(TAG,
1907f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                                        "Invalid package found: " + cn, true);
1908f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            }
1909f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            continue;
1910ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                        }
1911f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    } else if (restored) {
1912f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        // no special handling necessary for this restored item
1913f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        restoredRows.add(id);
1914f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        restored = false;
1915ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    }
1916b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } catch (URISyntaxException e) {
1917f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    Launcher.addDumpLog(TAG,
1918f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            "Invalid uri: " + intentDescription, true);
1919b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    continue;
1920b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1921968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka
1922f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                if (restored) {
1923f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    Launcher.addDumpLog(TAG,
1924f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            "constructing info for partially restored package",
1925f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                            true);
1926b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                                    info = getRestoredItemInfo(c, titleIndex, intent);
1927f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    intent = getRestoredItemIntent(c, context, intent);
1928f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                } else if (itemType ==
1929f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                        LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1930b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info = getShortcutInfo(manager, intent, context, c, iconIndex,
1931b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            titleIndex, mLabelCache);
1932b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } else {
1933b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info = getShortcutInfo(c, context, iconTypeIndex,
1934b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            iconPackageIndex, iconResourceIndex, iconIndex,
1935b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            titleIndex);
1936b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1937b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // App shortcuts that used to be automatically added to Launcher
1938b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // didn't always have the correct intent flags set, so do that
1939b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // here
1940b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (intent.getAction() != null &&
19419ad005639e1698c94674cdfadf3ae342fb56c9b5Michael Jurka                                        intent.getCategories() != null &&
19429ad005639e1698c94674cdfadf3ae342fb56c9b5Michael Jurka                                        intent.getAction().equals(Intent.ACTION_MAIN) &&
1943968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka                                        intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
1944b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        intent.addFlags(
1945b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            Intent.FLAG_ACTIVITY_NEW_TASK |
1946b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1947b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1948968795679be0a3f0118d5a6e2e1580808da5716dMichael Jurka                                }
19493611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
1950b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (info != null) {
1951ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung                                    info.id = id;
1952b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.intent = intent;
1953b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    container = c.getInt(containerIndex);
1954b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.container = container;
1955dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                    info.screenId = c.getInt(screenIndex);
1956b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.cellX = c.getInt(cellXIndex);
1957b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    info.cellY = c.getInt(cellYIndex);
19585f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    info.spanX = 1;
19595f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                    info.spanY = 1;
1960ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen
1961b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // check & update map of what's occupied
1962ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                    deleteOnInvalidPlacement.set(false);
1963ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                    if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1964ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                        if (deleteOnInvalidPlacement.get()) {
1965a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                            itemsToRemove.add(id);
1966a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                        }
1967b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1968b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1969b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1970b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    switch (container) {
1971b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1972b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1973b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        sBgWorkspaceItems.add(info);
1974b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1975b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    default:
1976b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        // Item is in a user folder
1977b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        FolderInfo folderInfo =
1978b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                                findOrMakeFolder(sBgFolders, container);
1979b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        folderInfo.add(info);
1980b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
1981b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
1982b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgItemsIdMap.put(info.id, info);
1983b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1984b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // now that we've loaded everthing re-save it with the
1985b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // icon in case it disappears somehow.
1986b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
19871323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                } else {
19881323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                                    throw new RuntimeException("Unexpected null ShortcutInfo");
1989b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
1990b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                break;
1991b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1992b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1993b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                id = c.getLong(idIndex);
1994b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1995b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
1996b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.title = c.getString(titleIndex);
1997b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.id = id;
19983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                container = c.getInt(containerIndex);
1999b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.container = container;
2000dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                folderInfo.screenId = c.getInt(screenIndex);
2001b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.cellX = c.getInt(cellXIndex);
2002b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                folderInfo.cellY = c.getInt(cellYIndex);
20035f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                folderInfo.spanX = 1;
20045f8afe6280eae34620067696173e71943e1a30a3Winson Chung                                folderInfo.spanY = 1;
20055f8afe6280eae34620067696173e71943e1a30a3Winson Chung
20063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                // check & update map of what's occupied
2007ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                deleteOnInvalidPlacement.set(false);
2008a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                if (!checkItemPlacement(occupied, folderInfo,
2009ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                        deleteOnInvalidPlacement)) {
2010ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                    if (deleteOnInvalidPlacement.get()) {
2011a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                        itemsToRemove.add(id);
2012a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                    }
20133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                    break;
20149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
20155f8afe6280eae34620067696173e71943e1a30a3Winson Chung
20163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                switch (container) {
2017b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2018b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2019b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        sBgWorkspaceItems.add(folderInfo);
2020b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
20219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                }
202217a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato
2023f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                if (restored) {
2024f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    // no special handling required for restored folders
2025f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                    restoredRows.add(id);
2026f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                }
2027f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren
2028b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgItemsIdMap.put(folderInfo.id, folderInfo);
2029b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                sBgFolders.put(folderInfo.id, folderInfo);
2030b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                break;
2031b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2032b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2033b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                // Read all Launcher-specific widget details
2034b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                int appWidgetId = c.getInt(appWidgetIdIndex);
2035c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                String savedProvider = c.getString(appWidgetProviderIndex);
2036c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren
20373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                                id = c.getLong(idIndex);
2038f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
2039b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                final AppWidgetProviderInfo provider =
2040b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        widgets.getAppWidgetInfo(appWidgetId);
2041b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2042b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                if (!isSafeMode && (provider == null || provider.provider == null ||
2043b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        provider.provider.getPackageName() == null)) {
2044b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    String log = "Deleting widget that isn't installed anymore: id="
2045b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        + id + " appWidgetId=" + appWidgetId;
2046b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    Log.e(TAG, log);
20474caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                                    Launcher.addDumpLog(TAG, log, false);
2048b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    itemsToRemove.add(id);
2049b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                } else {
2050b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2051b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            provider.provider);
2052b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.id = id;
2053dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                    appWidgetInfo.screenId = c.getInt(screenIndex);
2054b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.cellX = c.getInt(cellXIndex);
2055b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.cellY = c.getInt(cellYIndex);
2056b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.spanX = c.getInt(spanXIndex);
2057b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.spanY = c.getInt(spanYIndex);
2058b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2059b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.minSpanX = minSpan[0];
2060b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    appWidgetInfo.minSpanY = minSpan[1];
2061b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2062b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    container = c.getInt(containerIndex);
2063b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2064b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2065b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        Log.e(TAG, "Widget found where container != " +
2066b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                            "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2067b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        continue;
2068b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
2069b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2070e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                                    appWidgetInfo.container = c.getInt(containerIndex);
2071b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    // check & update map of what's occupied
2072ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                    deleteOnInvalidPlacement.set(false);
2073a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                    if (!checkItemPlacement(occupied, appWidgetInfo,
2074ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                            deleteOnInvalidPlacement)) {
2075ae4409dea97d03f0f0a9195ab648188efa2b5cc1Adam Cohen                                        if (deleteOnInvalidPlacement.get()) {
2076a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                            itemsToRemove.add(id);
2077a0b7e86299ed9baf278e0c1ed73f4a4f6a057322Winson Chung                                        }
2078b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                        break;
2079b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    }
2080c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                    String providerName = provider.provider.flattenToString();
2081c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                    if (!providerName.equals(savedProvider)) {
2082c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        ContentValues values = new ContentValues();
2083c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2084c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                                providerName);
2085c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        String where = BaseColumns._ID + "= ?";
2086c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        String[] args = {Integer.toString(c.getInt(idIndex))};
2087c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                        contentResolver.update(contentUri, values, where, args);
2088c3919c0e994309982efc8779af57e2bc78dcac43Chris Wren                                    }
2089b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2090b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    sBgAppWidgets.add(appWidgetInfo);
2091b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                }
20929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                                break;
20933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            }
2094b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        } catch (Exception e) {
2095295ae18d0fa7c102c50cf95a2ea09e7f2e9b1539Dan Sandler                            Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
2096b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
2097b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2098b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } finally {
209947b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                    if (c != null) {
210047b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                        c.close();
210147b5031c703dbee491af9f49e6e08865452d57efDaniel Sandler                    }
2102b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
2103ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato
2104ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                // Break early if we've stopped loading
2105ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                if (mStopped) {
2106ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    clearSBgDataStructures();
2107ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                    return false;
2108ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung                }
2109ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung
2110b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (itemsToRemove.size() > 0) {
2111b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    ContentProviderClient client = contentResolver.acquireContentProviderClient(
21124caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                            LauncherSettings.Favorites.CONTENT_URI);
2113b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    // Remove dead items
2114b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    for (long id : itemsToRemove) {
2115b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (DEBUG_LOADERS) {
2116b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.d(TAG, "Removed id = " + id);
2117b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
2118b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        // Don't notify content observers
2119b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        try {
2120b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2121b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                    null, null);
2122b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        } catch (RemoteException e) {
2123b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            Log.w(TAG, "Could not remove id = " + id);
2124b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        }
2125b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2126b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
21278802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler
2128f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                if (restoredRows.size() > 0) {
2129f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2130f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                            LauncherSettings.Favorites.CONTENT_URI);
2131f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    // Update restored items that no longer require special handling
2132f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    try {
2133f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        StringBuilder selectionBuilder = new StringBuilder();
2134f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        selectionBuilder.append(LauncherSettings.Favorites._ID);
2135f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        selectionBuilder.append(" IN (");
2136f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        selectionBuilder.append(TextUtils.join(", ", restoredRows));
2137f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        selectionBuilder.append(")");
2138f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        ContentValues values = new ContentValues();
2139f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        values.put(LauncherSettings.Favorites.RESTORED, 0);
2140f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        updater.update(LauncherSettings.Favorites.CONTENT_URI,
2141f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                                values, selectionBuilder.toString(), null);
2142f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    } catch (RemoteException e) {
2143f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                        Log.w(TAG, "Could not update restored rows");
2144f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                    }
2145f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                }
2146f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren
2147c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                if (loadedOldDb) {
2148dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long maxScreenId = 0;
2149dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // If we're importing we use the old screen order.
2150dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    for (ItemInfo item: sBgItemsIdMap.values()) {
2151dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        long screenId = item.screenId;
2152dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2153dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                !sBgWorkspaceScreens.contains(screenId)) {
2154dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            sBgWorkspaceScreens.add(screenId);
2155dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            if (screenId > maxScreenId) {
2156dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                maxScreenId = screenId;
2157dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            }
2158dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        }
2159dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2160dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    Collections.sort(sBgWorkspaceScreens);
21619f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    // Log to disk
21629f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    Launcher.addDumpLog(TAG, "11683562 -   maxScreenId: " + maxScreenId, true);
21639f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    Launcher.addDumpLog(TAG, "11683562 -   sBgWorkspaceScreens: " +
21649f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                            TextUtils.join(", ", sBgWorkspaceScreens), true);
21659e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
2166414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka                    LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
2167dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2168c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung
2169c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    // Update the max item id after we load an old db
2170c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    long maxItemId = 0;
2171c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    // If we're importing we use the old screen order.
2172c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    for (ItemInfo item: sBgItemsIdMap.values()) {
2173c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        maxItemId = Math.max(maxItemId, item.id);
2174c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                    }
2175414300a79d140f8c2c8760d9adab750f69ffeafdMichael Jurka                    LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
2176dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                } else {
217776828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
217876828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                    for (Integer i : orderedScreens.keySet()) {
217976828c87ddb50ee3b423ef26f36387a2f03fd562Winson Chung                        sBgWorkspaceScreens.add(orderedScreens.get(i));
2180dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
21819f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    // Log to disk
21829f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                    Launcher.addDumpLog(TAG, "11683562 -   sBgWorkspaceScreens: " +
21839f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                            TextUtils.join(", ", sBgWorkspaceScreens), true);
2184dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2185dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // Remove any empty screens
2186933bae6958b5333a61577edd6b57b1a7c00743a0Winson Chung                    ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
2187dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    for (ItemInfo item: sBgItemsIdMap.values()) {
2188dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        long screenId = item.screenId;
2189dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2190dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                                unusedScreens.contains(screenId)) {
2191dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            unusedScreens.remove(screenId);
2192dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        }
2193dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2194dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2195dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    // If there are any empty screens remove them, and update.
2196dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (unusedScreens.size() != 0) {
21979f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                        // Log to disk
21989f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                        Launcher.addDumpLog(TAG, "11683562 -   unusedScreens (to be removed): " +
21999f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung                                TextUtils.join(", ", unusedScreens), true);
22009f9f00bca673ed491e623235314a5c4c22ff28c1Winson Chung
2201dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        sBgWorkspaceScreens.removeAll(unusedScreens);
2202dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2203dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2204dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
2205dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2206b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (DEBUG_LOADERS) {
2207b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2208b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Log.d(TAG, "workspace layout: ");
2209dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    int nScreens = occupied.size();
2210892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    for (int y = 0; y < countY; y++) {
2211b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        String line = "";
2212dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2213566da1026c33a68157bf9caf93d2071ad2870f46Daniel Sandler                        Iterator<Long> iter = occupied.keySet().iterator();
2214c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                        while (iter.hasNext()) {
2215dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            long screenId = iter.next();
2216c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                            if (screenId > 0) {
2217b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                                line += " | ";
2218b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            }
2219892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                            for (int x = 0; x < countX; x++) {
2220aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                ItemInfo[][] screen = occupied.get(screenId);
2221aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                if (x < screen.length && y < screen[x].length) {
2222aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                    line += (screen[x][y] != null) ? "#" : ".";
2223aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                } else {
2224aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                    line += "!";
2225aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                                }
22269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                            }
22279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
2228b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Log.d(TAG, "[ " + line + " ]");
22299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
22309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
22313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2232c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung            return loadedOldDb;
2233e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        }
2234e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
2235b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of items who are directly or indirectly (via another container) on the
2236b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * specified screen. */
22379b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung        private void filterCurrentWorkspaceItems(long currentScreenId,
2238b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> allWorkspaceItems,
2239b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> currentScreenItems,
2240b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<ItemInfo> otherScreenItems) {
22412abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            // Purge any null ItemInfos
22422abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
22432abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            while (iter.hasNext()) {
22442abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                ItemInfo i = iter.next();
22452abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (i == null) {
22462abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                    iter.remove();
22472abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                }
22482abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            }
22492abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung
2250b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Order the set of items by their containers first, this allows use to walk through the
2251b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // list sequentially, build up a list of containers that are in the specified screen,
2252b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // as well as all items in those containers.
2253b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Set<Long> itemsOnScreen = new HashSet<Long>();
2254b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2255b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                @Override
2256b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                public int compare(ItemInfo lhs, ItemInfo rhs) {
2257b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    return (int) (lhs.container - rhs.container);
2258b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
2259b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            });
2260b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (ItemInfo info : allWorkspaceItems) {
2261b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
22629b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung                    if (info.screenId == currentScreenId) {
2263b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        currentScreenItems.add(info);
2264b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        itemsOnScreen.add(info.id);
2265b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    } else {
2266b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        otherScreenItems.add(info);
22673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
2268b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2269b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenItems.add(info);
2270b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    itemsOnScreen.add(info.id);
2271b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2272b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    if (itemsOnScreen.contains(info.container)) {
2273b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        currentScreenItems.add(info);
2274b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        itemsOnScreen.add(info.id);
2275b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    } else {
2276b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        otherScreenItems.add(info);
22775c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
22785c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                }
22793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2280b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
22815c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy
2282b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of widgets which are on the specified screen. */
22839b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung        private void filterCurrentAppWidgets(long currentScreenId,
2284b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> appWidgets,
2285b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2286b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2287b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2288b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (LauncherAppWidgetInfo widget : appWidgets) {
22892abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (widget == null) continue;
2290b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
22919b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung                        widget.screenId == currentScreenId) {
2292b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenWidgets.add(widget);
2293b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2294b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherScreenWidgets.add(widget);
2295a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                }
22969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
22973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
229831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2299b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Filters the set of folders which are on the specified screen. */
23009b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung        private void filterCurrentFolders(long currentScreenId,
2301b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, ItemInfo> itemsIdMap,
2302b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> folders,
2303b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> currentScreenFolders,
2304b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                HashMap<Long, FolderInfo> otherScreenFolders) {
23053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
2306b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (long id : folders.keySet()) {
2307b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ItemInfo info = itemsIdMap.get(id);
2308b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                FolderInfo folder = folders.get(id);
23092abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung                if (info == null || folder == null) continue;
2310b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
23119b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung                        info.screenId == currentScreenId) {
2312b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentScreenFolders.put(id, folder);
2313b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
2314b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherScreenFolders.put(id, folder);
2315b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                }
23163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2317b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
231831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2319b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2320b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * right) */
2321b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
2322892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            final LauncherAppState app = LauncherAppState.getInstance();
2323892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung            final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
2324b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // XXX: review this
2325b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
2326db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                @Override
2327db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                public int compare(ItemInfo lhs, ItemInfo rhs) {
2328892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    int cellCountX = (int) grid.numColumns;
2329892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                    int cellCountY = (int) grid.numRows;
2330db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int screenOffset = cellCountX * cellCountY;
2331db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
2332dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
2333db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                            lhs.cellY * cellCountX + lhs.cellX);
2334dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
2335db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                            rhs.cellY * cellCountX + rhs.cellX);
2336db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                    return (int) (lr - rr);
2337db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung                }
2338db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung            });
2339b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
2340db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung
2341dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2342dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                final ArrayList<Long> orderedScreens) {
2343dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            final Runnable r = new Runnable() {
2344dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                @Override
2345dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                public void run() {
2346dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2347dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    if (callbacks != null) {
2348dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        callbacks.bindScreens(orderedScreens);
2349dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    }
2350dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                }
2351dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            };
2352dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2353dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
2354dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2355b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        private void bindWorkspaceItems(final Callbacks oldCallbacks,
2356b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<ItemInfo> workspaceItems,
2357b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final ArrayList<LauncherAppWidgetInfo> appWidgets,
2358b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final HashMap<Long, FolderInfo> folders,
2359b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                ArrayList<Runnable> deferredBindRunnables) {
2360b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2361b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final boolean postOnMainThread = (deferredBindRunnables != null);
2362603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung
2363b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the workspace items
2364db8a8944ede3be4ee63b43e24c407a3aaabee4deWinson Chung            int N = workspaceItems.size();
2365b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (int i = 0; i < N; i += ITEMS_CHUNK) {
23663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int start = i;
23673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
2368b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2369b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    @Override
23709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    public void run() {
2371c131b74922e6323331c374856bda830010d761c8Joe Onorato                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
23729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        if (callbacks != null) {
237364359a53dc827797917a8d9a0697a91996789801Winson Chung                            callbacks.bindItems(workspaceItems, start, start+chunkSize,
237464359a53dc827797917a8d9a0697a91996789801Winson Chung                                    false);
23759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
23769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
2377b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2378b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2379a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                    synchronized (deferredBindRunnables) {
2380a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                        deferredBindRunnables.add(r);
2381a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                    }
2382b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
238381b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
23843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
2385b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2386b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2387b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the folders
2388b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (!folders.isEmpty()) {
2389b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2390b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    public void run() {
2391b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2392b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (callbacks != null) {
2393b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            callbacks.bindFolders(folders);
23949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                        }
2395b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2396b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2397b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2398a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                    synchronized (deferredBindRunnables) {
2399a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                        deferredBindRunnables.add(r);
2400a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                    }
2401b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
240281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
24039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
24043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2405b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2406b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Bind the widgets, one at a time
2407b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            N = appWidgets.size();
2408b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            for (int i = 0; i < N; i++) {
2409b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final LauncherAppWidgetInfo widget = appWidgets.get(i);
2410b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                final Runnable r = new Runnable() {
2411b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    public void run() {
2412b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2413b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        if (callbacks != null) {
2414b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                            callbacks.bindAppWidget(widget);
24153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
2416b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2417b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                };
2418b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                if (postOnMainThread) {
2419b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    deferredBindRunnables.add(r);
2420b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                } else {
242181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                    runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
24223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
24233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2424b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        }
2425b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2426b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        /**
2427b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         * Binds all loaded data to actual views on the main thread.
2428b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung         */
2429c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung        private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
2430b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final long t = SystemClock.uptimeMillis();
2431b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            Runnable r;
2432b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2433b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Don't use these two variables in any of the callback runnables.
2434b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Otherwise we hold a reference to them.
2435b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            final Callbacks oldCallbacks = mCallbacks.get();
2436b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            if (oldCallbacks == null) {
2437b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                // This launcher has exited and nobody bothered to tell us.  Just bail.
2438b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.w(TAG, "LoaderTask running with no launcher");
2439b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                return;
2440b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2441b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
24429b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            // Save a copy of all the bg-thread collections
2443b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2444b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> appWidgets =
2445b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2446b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2447b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
2448dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
24492abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
2450b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                workspaceItems.addAll(sBgWorkspaceItems);
2451b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                appWidgets.addAll(sBgAppWidgets);
2452b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                folders.putAll(sBgFolders);
2453b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                itemsIdMap.putAll(sBgItemsIdMap);
2454dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                orderedScreenIds.addAll(sBgWorkspaceScreens);
2455b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
2456b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
24577aff399974c756930070d82d7b2df88f125dacd6Derek Prothro            final boolean isLoadingSynchronously =
24587aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                    synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
2459d8dbb46468e0a8860468661a6edfb7969bfdf0a7Adam Cohen            int currScreen = isLoadingSynchronously ? synchronizeBindPage :
24609b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung                oldCallbacks.getCurrentWorkspaceScreen();
2461d8dbb46468e0a8860468661a6edfb7969bfdf0a7Adam Cohen            if (currScreen >= orderedScreenIds.size()) {
2462d8dbb46468e0a8860468661a6edfb7969bfdf0a7Adam Cohen                // There may be no workspace screens (just hotseat items and an empty page).
24637aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                currScreen = PagedView.INVALID_RESTORE_PAGE;
24649b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            }
2465d8dbb46468e0a8860468661a6edfb7969bfdf0a7Adam Cohen            final int currentScreen = currScreen;
24667aff399974c756930070d82d7b2df88f125dacd6Derek Prothro            final long currentScreenId = currentScreen < 0
24677aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                    ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
24689b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung
24699b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            // Load all the items that are on the current page first (and in the process, unbind
24709b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            // all the existing workspace items before we call startBinding() below.
24719b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            unbindWorkspaceItemsOnMainThread();
24729b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung
24739b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            // Separate the items that are on the current screen, and all the other remaining items
2474b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2475b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2476b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2477b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2478b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2479b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    new ArrayList<LauncherAppWidgetInfo>();
2480b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2481b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2482b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
24839b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
2484b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherWorkspaceItems);
24859b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
2486b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherAppWidgets);
24879b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung            filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
2488b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    otherFolders);
2489b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sortWorkspaceItemsSpatially(currentWorkspaceItems);
2490b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            sortWorkspaceItemsSpatially(otherWorkspaceItems);
2491b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2492b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Tell the workspace that we're about to start binding items
2493b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r = new Runnable() {
24943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
24953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
24963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
2497b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                        callbacks.startBinding();
24989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
24999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
2500b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
250181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung            runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2502b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2503dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2504dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
2505b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Load items on the current page
2506b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2507b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    currentFolders, null);
25081462de39f01cec0ba785386532719cb0207dd827Adam Cohen            if (isLoadingSynchronously) {
25091462de39f01cec0ba785386532719cb0207dd827Adam Cohen                r = new Runnable() {
25101462de39f01cec0ba785386532719cb0207dd827Adam Cohen                    public void run() {
25111462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        Callbacks callbacks = tryGetCallbacks(oldCallbacks);
25127aff399974c756930070d82d7b2df88f125dacd6Derek Prothro                        if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
25131462de39f01cec0ba785386532719cb0207dd827Adam Cohen                            callbacks.onPageBoundSynchronously(currentScreen);
25141462de39f01cec0ba785386532719cb0207dd827Adam Cohen                        }
25151462de39f01cec0ba785386532719cb0207dd827Adam Cohen                    }
25161462de39f01cec0ba785386532719cb0207dd827Adam Cohen                };
251781b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
25181462de39f01cec0ba785386532719cb0207dd827Adam Cohen            }
2519b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
25204a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            // Load all the remaining pages (if we are loading synchronously, we want to defer this
25214a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            // work until after the first render)
2522a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            synchronized (mDeferredBindRunnables) {
2523a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                mDeferredBindRunnables.clear();
2524a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk            }
2525b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
25264a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                    (isLoadingSynchronously ? mDeferredBindRunnables : null));
2527b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
2528b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            // Tell the workspace that we're done binding items
2529b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            r = new Runnable() {
25303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
2531b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2532b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    if (callbacks != null) {
2533c763c4e4d28c256d1368be3fc1c4526c8b9bd232Winson Chung                        callbacks.finishBindingItems(isUpgradePath);
2534b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                    }
2535b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
253698e030be2abc03e90e9fd9f8bf67e2e01db39cc0Winson Chung                    // If we're profiling, ensure this is the last thing in the queue.
25373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
25383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound workspace in "
25393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            + (SystemClock.uptimeMillis()-t) + "ms");
25409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
254136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
254236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung                    mIsLoadingAndBindingWorkspace = false;
25439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                }
2544b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            };
25454a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            if (isLoadingSynchronously) {
2546a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                synchronized (mDeferredBindRunnables) {
2547a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                    mDeferredBindRunnables.add(r);
2548a0a7a7440e860c61c8261c5009dbe351869896b3Jason Monk                }
25494a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            } else {
255081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung                runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
25514a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            }
25523611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
25533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
25543611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void loadAndBindAllApps() {
25553611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
25563611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
25573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (!mAllAppsLoaded) {
255964359a53dc827797917a8d9a0697a91996789801Winson Chung                loadAllApps();
256093f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                synchronized (LoaderTask.this) {
256193f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    if (mStopped) {
256293f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                        return;
256393f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    }
256493f824ac2f2f3ec92a775cad58dcf59e4302fe64Reena Lee                    mAllAppsLoaded = true;
25653611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
25663611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            } else {
25673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                onlyBindAllApps();
25683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25693611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
25703611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
25713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        private void onlyBindAllApps() {
25723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
25733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
25743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
25753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
25763611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
25773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
25783611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
25793611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            // shallow copy
2580c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            @SuppressWarnings("unchecked")
2581eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> list
2582eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                    = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
2583c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            Runnable r = new Runnable() {
25843611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                public void run() {
25853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final long t = SystemClock.uptimeMillis();
25863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
25873611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (callbacks != null) {
25883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        callbacks.bindAllApplications(list);
25899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
25903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    if (DEBUG_LOADERS) {
25913611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2592a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato                                + (SystemClock.uptimeMillis()-t) + "ms");
25939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    }
25943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
2595c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            };
2596c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
259764359a53dc827797917a8d9a0697a91996789801Winson Chung            if (isRunningOnMainThread) {
2598c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                r.run();
2599c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            } else {
2600c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                mHandler.post(r);
2601c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            }
26023611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
26033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
260464359a53dc827797917a8d9a0697a91996789801Winson Chung        private void loadAllApps() {
260564359a53dc827797917a8d9a0697a91996789801Winson Chung            final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
26063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
26073611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks oldCallbacks = mCallbacks.get();
26083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (oldCallbacks == null) {
26093611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                // This launcher has exited and nobody bothered to tell us.  Just bail.
261064359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
26113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
26129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
261331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
261464359a53dc827797917a8d9a0697a91996789801Winson Chung            final PackageManager packageManager = mContext.getPackageManager();
26153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
26163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2617cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
261864359a53dc827797917a8d9a0697a91996789801Winson Chung            // Clear the list of apps
261964359a53dc827797917a8d9a0697a91996789801Winson Chung            mBgAllAppsList.clear();
2620cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
262164359a53dc827797917a8d9a0697a91996789801Winson Chung            // Query for the set of apps
262264359a53dc827797917a8d9a0697a91996789801Winson Chung            final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
262364359a53dc827797917a8d9a0697a91996789801Winson Chung            List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
262464359a53dc827797917a8d9a0697a91996789801Winson Chung            if (DEBUG_LOADERS) {
262564359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "queryIntentActivities took "
262664359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis()-qiaTime) + "ms");
262764359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
262864359a53dc827797917a8d9a0697a91996789801Winson Chung            }
262964359a53dc827797917a8d9a0697a91996789801Winson Chung            // Fail if we don't have any apps
263064359a53dc827797917a8d9a0697a91996789801Winson Chung            if (apps == null || apps.isEmpty()) {
263164359a53dc827797917a8d9a0697a91996789801Winson Chung                return;
263264359a53dc827797917a8d9a0697a91996789801Winson Chung            }
263364359a53dc827797917a8d9a0697a91996789801Winson Chung            // Sort the applications by name
263464359a53dc827797917a8d9a0697a91996789801Winson Chung            final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
263564359a53dc827797917a8d9a0697a91996789801Winson Chung            Collections.sort(apps,
263664359a53dc827797917a8d9a0697a91996789801Winson Chung                    new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
263764359a53dc827797917a8d9a0697a91996789801Winson Chung            if (DEBUG_LOADERS) {
263864359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "sort took "
263964359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis()-sortTime) + "ms");
264064359a53dc827797917a8d9a0697a91996789801Winson Chung            }
26413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
264264359a53dc827797917a8d9a0697a91996789801Winson Chung            // Create the ApplicationInfos
264364359a53dc827797917a8d9a0697a91996789801Winson Chung            for (int i = 0; i < apps.size(); i++) {
264485f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert                ResolveInfo app = apps.get(i);
26451307f63f8a93541bf6e2d29c11fab69778a25e42Bjorn Bringert                // This builds the icon bitmaps.
26461307f63f8a93541bf6e2d29c11fab69778a25e42Bjorn Bringert                mBgAllAppsList.add(new AppInfo(packageManager, app,
26471307f63f8a93541bf6e2d29c11fab69778a25e42Bjorn Bringert                        mIconCache, mLabelCache));
264864359a53dc827797917a8d9a0697a91996789801Winson Chung            }
2649cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
265085f418d0f5607c66efef1673075a6d3486e5af83Bjorn Bringert            // Huh? Shouldn't this be inside the Runnable below?
2651eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> added = mBgAllAppsList.added;
2652eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            mBgAllAppsList.added = new ArrayList<AppInfo>();
26533611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
265464359a53dc827797917a8d9a0697a91996789801Winson Chung            // Post callback on main thread
265564359a53dc827797917a8d9a0697a91996789801Winson Chung            mHandler.post(new Runnable() {
265664359a53dc827797917a8d9a0697a91996789801Winson Chung                public void run() {
265764359a53dc827797917a8d9a0697a91996789801Winson Chung                    final long bindTime = SystemClock.uptimeMillis();
265811a1a53651924b544513f1f6971a735b18d67539Winson Chung                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
265964359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (callbacks != null) {
266064359a53dc827797917a8d9a0697a91996789801Winson Chung                        callbacks.bindAllApplications(added);
26613611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) {
266264359a53dc827797917a8d9a0697a91996789801Winson Chung                            Log.d(TAG, "bound " + added.size() + " apps in "
266364359a53dc827797917a8d9a0697a91996789801Winson Chung                                + (SystemClock.uptimeMillis() - bindTime) + "ms");
26643611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
266564359a53dc827797917a8d9a0697a91996789801Winson Chung                    } else {
266664359a53dc827797917a8d9a0697a91996789801Winson Chung                        Log.i(TAG, "not binding apps: no Launcher activity");
266764359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
26683611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                }
266964359a53dc827797917a8d9a0697a91996789801Winson Chung            });
2670cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato
26713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (DEBUG_LOADERS) {
267264359a53dc827797917a8d9a0697a91996789801Winson Chung                Log.d(TAG, "Icons processed in "
267364359a53dc827797917a8d9a0697a91996789801Winson Chung                        + (SystemClock.uptimeMillis() - loadTime) + "ms");
26743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
26753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
2676dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
26773611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void dumpState() {
26782abf94d37c220c3af01aec05cc6ca22f9854d8e0Winson Chung            synchronized (sBgLock) {
2679b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2680b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2681b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2682b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2683b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung                Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2684b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            }
26853611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
26863611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
268731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
26883611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    void enqueuePackageUpdated(PackageUpdatedTask task) {
2689700889f504ef134ab307d95b6bfbbb426ea730e3Brad Fitzpatrick        sWorker.post(task);
26903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    }
2691d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
26923611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    private class PackageUpdatedTask implements Runnable {
26933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        int mOp;
26943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        String[] mPackages;
2695d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
26963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_NONE = 0;
26973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_ADD = 1;
26983611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UPDATE = 2;
26993611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_REMOVE = 3; // uninstlled
27003611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public static final int OP_UNAVAILABLE = 4; // external media unmounted
270131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2702d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
27033611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public PackageUpdatedTask(int op, String[] packages) {
27043611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mOp = op;
27053611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mPackages = packages;
27063611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
2707d65d08e709ec0916446100bae0a7276d0800382fJoe Onorato
27083611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        public void run() {
2709cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler            final Context context = mApp.getContext();
2710dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
27113611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final String[] packages = mPackages;
27123611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final int N = packages.length;
27133611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            switch (mOp) {
27143611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_ADD:
27153611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
27163611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
27176d0dde01f307051ee1849481c989d9e87774b894Chris Wren                        mIconCache.remove(packages[i]);
2718487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.addPackage(context, packages[i]);
2719dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler                    }
27203611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
27213611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UPDATE:
27223611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
27233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
2724487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.updatePackage(context, packages[i]);
2725eb1bb920507fe7f27b2ecece5b67749dac7850f3Michael Jurka                        WidgetPreviewLoader.removePackageFromDb(
2726e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                                mApp.getWidgetPreviewCacheDb(), packages[i]);
27273611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
27283611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
27293611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_REMOVE:
27303611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                case OP_UNAVAILABLE:
27313611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    for (int i=0; i<N; i++) {
27323611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2733487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mBgAllAppsList.removePackage(packages[i]);
2734eb1bb920507fe7f27b2ecece5b67749dac7850f3Michael Jurka                        WidgetPreviewLoader.removePackageFromDb(
2735e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                                mApp.getWidgetPreviewCacheDb(), packages[i]);
27363611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
27373611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    break;
27383611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2739dca661236c73ecd819cfea964c6f8170e5cc40aeDaniel Sandler
2740eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            ArrayList<AppInfo> added = null;
2741eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            ArrayList<AppInfo> modified = null;
2742eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
27433611578579b4bfb25616085dafdb1a45207394f9Joe Onorato
2744487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            if (mBgAllAppsList.added.size() > 0) {
2745eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                added = new ArrayList<AppInfo>(mBgAllAppsList.added);
27465d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.added.clear();
27473611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2748487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            if (mBgAllAppsList.modified.size() > 0) {
2749eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
27505d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.modified.clear();
27513611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
27525d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung            if (mBgAllAppsList.removed.size() > 0) {
275383892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                removedApps.addAll(mBgAllAppsList.removed);
27545d55f33a1d7a16a11fb759d8f37eeef45e132b98Winson Chung                mBgAllAppsList.removed.clear();
2755cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung            }
2756cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung
27573611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
27583611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (callbacks == null) {
27593611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                Log.w(TAG, "Nobody to tell about the new app.  Launcher is probably loading.");
27603611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                return;
2761be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
2762be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
27633611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (added != null) {
276464359a53dc827797917a8d9a0697a91996789801Winson Chung                // Ensure that we add all the workspace applications to the db
276576a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                if (LauncherAppState.isDisableAllApps()) {
276694d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                    final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
276776a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                    addAndBindAddedWorkspaceApps(context, addedInfos);
276876a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                } else {
276976a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen                    addAppsToAllApps(context, added);
277094d6768c65929efa21bb893fdef7f269d65da3c3Winson Chung                }
27713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
277276a47a1b2273c4d7dfab0beeadc1a8387e6a2840Adam Cohen
27733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            if (modified != null) {
2774eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                final ArrayList<AppInfo> modifiedFinal = modified;
277564359a53dc827797917a8d9a0697a91996789801Winson Chung
277664359a53dc827797917a8d9a0697a91996789801Winson Chung                // Update the launcher db to reflect the changes
2777eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                for (AppInfo a : modifiedFinal) {
277864359a53dc827797917a8d9a0697a91996789801Winson Chung                    ArrayList<ItemInfo> infos =
277964359a53dc827797917a8d9a0697a91996789801Winson Chung                            getItemInfoForComponentName(a.componentName);
278064359a53dc827797917a8d9a0697a91996789801Winson Chung                    for (ItemInfo i : infos) {
278164359a53dc827797917a8d9a0697a91996789801Winson Chung                        if (isShortcutInfoUpdateable(i)) {
278264359a53dc827797917a8d9a0697a91996789801Winson Chung                            ShortcutInfo info = (ShortcutInfo) i;
278364359a53dc827797917a8d9a0697a91996789801Winson Chung                            info.title = a.title.toString();
278464359a53dc827797917a8d9a0697a91996789801Winson Chung                            updateItemInDatabase(context, info);
278564359a53dc827797917a8d9a0697a91996789801Winson Chung                        }
278664359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
278764359a53dc827797917a8d9a0697a91996789801Winson Chung                }
278864359a53dc827797917a8d9a0697a91996789801Winson Chung
27893611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
27903611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
2791cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2792cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        if (callbacks == cb && cb != null) {
27933611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                            callbacks.bindAppsUpdated(modifiedFinal);
27943611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
27953611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
27963611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
27973611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            }
2798780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung
2799df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            final ArrayList<String> removedPackageNames =
2800df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                    new ArrayList<String>();
2801df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            if (mOp == OP_REMOVE) {
2802df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                // Mark all packages in the broadcast to be removed
2803df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                removedPackageNames.addAll(Arrays.asList(packages));
2804df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            } else if (mOp == OP_UPDATE) {
2805df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                // Mark disabled packages in the broadcast to be removed
2806df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                final PackageManager pm = context.getPackageManager();
2807df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                for (int i=0; i<N; i++) {
2808df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                    if (isPackageDisabled(pm, packages[i])) {
2809df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                        removedPackageNames.add(packages[i]);
281064359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
281164359a53dc827797917a8d9a0697a91996789801Winson Chung                }
2812df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            }
2813df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            // Remove all the components associated with this package
2814df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            for (String pn : removedPackageNames) {
2815df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2816df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                for (ItemInfo i : infos) {
2817df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                    deleteItemFromDatabase(context, i);
2818df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                }
2819df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            }
2820df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            // Remove all the specific components
2821df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            for (AppInfo a : removedApps) {
2822df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2823df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                for (ItemInfo i : infos) {
2824df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                    deleteItemFromDatabase(context, i);
2825df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                }
2826df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            }
2827df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2828df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                // Remove any queued items from the install queue
2829df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                String spKey = LauncherAppState.getSharedPreferencesKey();
2830df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                SharedPreferences sp =
2831df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                        context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2832df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2833df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                // Call the components-removed callback
28343611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                mHandler.post(new Runnable() {
28353611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    public void run() {
2836cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2837cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                        if (callbacks == cb && cb != null) {
2838df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung                            callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
28393611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                        }
28403611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                    }
28413611578579b4bfb25616085dafdb1a45207394f9Joe Onorato                });
2842be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato            }
284380baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
2844c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            final ArrayList<Object> widgetsAndShortcuts =
2845c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                getSortedWidgetsAndShortcuts(context);
284680baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            mHandler.post(new Runnable() {
284780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                @Override
284880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                public void run() {
2849cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                    Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2850cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung                    if (callbacks == cb && cb != null) {
2851c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                        callbacks.bindPackagesUpdated(widgetsAndShortcuts);
285280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                    }
285380baf5a6b3c62a62265f626d43d1167783c94131Winson Chung                }
28544caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            });
28554caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen
28564caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            // Write all the logs to disk
28574caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen            mHandler.post(new Runnable() {
28584caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                public void run() {
28594caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
28604caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    if (callbacks == cb && cb != null) {
2861ede4129e77f8347e32c0da9c615bcff2cbc06fd8Winson Chung                        callbacks.dumpLogsToLocalData();
28624caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                    }
28634caf298a2c9d861d69f5a47b747ada911b125b6dAdam Cohen                }
286480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung            });
286531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
286631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
286731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2868c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2869c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2870c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        PackageManager packageManager = context.getPackageManager();
2871c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2872c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2873c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2874c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2875c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        Collections.sort(widgetsAndShortcuts,
2876c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2877c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        return widgetsAndShortcuts;
2878c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    }
2879c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka
2880556f6135f992dcd5bfbfffe0172627bfbf57d24eAdam Cohen    private static boolean isPackageDisabled(PackageManager pm, String packageName) {
2881df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        try {
2882df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            PackageInfo pi = pm.getPackageInfo(packageName, 0);
2883df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            return !pi.applicationInfo.enabled;
2884df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        } catch (NameNotFoundException e) {
2885df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            // Fall through
2886df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        }
2887df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        return false;
2888df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung    }
2889556f6135f992dcd5bfbfffe0172627bfbf57d24eAdam Cohen
2890556f6135f992dcd5bfbfffe0172627bfbf57d24eAdam Cohen    public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
2891ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        if (cn == null) {
2892ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return false;
2893ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        }
2894df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        if (isPackageDisabled(pm, cn.getPackageName())) {
2895df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            return false;
2896df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung        }
2897ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung
2898ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        try {
2899ba9c37f41c41121a38d5a52232953d4b103e9471Winson Chung            // Check the activity
2900df95eb12a4d288ffa143a0c3db60e7e98f3e03b9Winson Chung            PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2901ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return (pm.getActivityInfo(cn, 0) != null);
2902ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        } catch (NameNotFoundException e) {
2903ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            return false;
2904ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        }
2905ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung    }
2906ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung
29079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
2908f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     * Make an ShortcutInfo object for a restored application or shortcut item that points
2909f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     * to a package that is not yet installed on the system.
2910f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     */
2911b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren    public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
2912f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        final ShortcutInfo info = new ShortcutInfo();
2913f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        if (cursor != null) {
2914f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren            info.title =  cursor.getString(titleIndex);
2915f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        } else {
2916f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren            info.title = "";
2917f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        }
29186d0dde01f307051ee1849481c989d9e87774b894Chris Wren        info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
2919f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
2920b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        info.restoredIntent = intent;
2921f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        return info;
2922f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    }
2923f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren
2924f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    /**
2925f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     * Make an Intent object for a restored application or shortcut item that points
2926f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     * to the market page for the item.
2927f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren     */
2928f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
2929b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        final boolean debug = false;
2930f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        ComponentName componentName = intent.getComponent();
2931f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2932f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        Uri marketUri = new Uri.Builder()
2933f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                .scheme("market")
2934f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                .authority("details")
2935f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                .appendQueryParameter("id", componentName.getPackageName())
2936f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren                .build();
2937b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren        if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
2938f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        marketIntent.setData(marketUri);
2939f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren        return marketIntent;
2940f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    }
2941f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren
2942f4d081107f5984bfb92cb1a627667fea5bf1498cChris Wren    /**
294356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * This is called from the code that adds shortcuts from the intent receiver.  This
294456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * doesn't have a Cursor, but
29459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
294656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
2947c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        return getShortcutInfo(manager, intent, context, null, -1, -1, null);
294856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
294956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
295056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    /**
295156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * Make an ShortcutInfo object for a shortcut that is an application.
295256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     *
295356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     * If c is not null, then it will be used to fill in missing data like the title and icon.
295456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato     */
295556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
2956c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
295756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ComponentName componentName = intent.getComponent();
29581323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        final ShortcutInfo info = new ShortcutInfo();
295968fd3c33daf7a6d4de287f8691306a55fa54706cWinson Chung        if (componentName != null && !isValidPackageComponent(manager, componentName)) {
2960ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung            Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
29619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            return null;
29621323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung        } else {
29631323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            try {
29641323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
29651323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                info.initFlagsAndFirstInstallTime(pi);
29661323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            } catch (NameNotFoundException e) {
29671323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                Log.d(TAG, "getPackInfo failed for package " +
29681323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung                        componentName.getPackageName());
29691323b4856a2a822af77293cadeda9910a5d1ba0eWinson Chung            }
297031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
297131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
29728ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: See if the PackageManager knows about this case.  If it doesn't
29738ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // then return null & delete this.
29748ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
297556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the resource -- This may implicitly give us back the fallback icon,
297656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // but don't worry about that.  All we're doing with usingFallbackIcon is
297756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // to avoid saving lots of copies of that in the database, and most apps
297856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // have icons anyway.
2979c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung
2980c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2981c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2982c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        // via resolveActivity().
2983ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        Bitmap icon = null;
2984c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        ResolveInfo resolveInfo = null;
2985c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        ComponentName oldComponent = intent.getComponent();
2986c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        Intent newIntent = new Intent(intent.getAction(), null);
2987c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2988c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        newIntent.setPackage(oldComponent.getPackageName());
2989c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2990c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        for (ResolveInfo i : infos) {
2991c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            ComponentName cn = new ComponentName(i.activityInfo.packageName,
2992c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung                    i.activityInfo.name);
2993c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            if (cn.equals(oldComponent)) {
2994c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung                resolveInfo = i;
2995c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            }
2996c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        }
2997c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        if (resolveInfo == null) {
2998c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung            resolveInfo = manager.resolveActivity(intent, 0);
2999c208ff9c5ded4953ded6a3358097c9f7280df825Winson Chung        }
300056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
3001aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
300256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
300356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the db
300456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
300556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
3006931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                icon = getIconFromCursor(c, iconIndex, context);
300756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
30089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
300956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the fallback icon
301056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (icon == null) {
301156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            icon = getFallbackIcon();
301256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
301356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
301456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.setIcon(icon);
301556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
301656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the resource
301756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (resolveInfo != null) {
30185308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
30195308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (labelCache != null && labelCache.containsKey(key)) {
30205308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                info.title = labelCache.get(key);
3021c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3022c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                info.title = resolveInfo.activityInfo.loadLabel(manager);
3023c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                if (labelCache != null) {
30245308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                    labelCache.put(key, info.title);
3025c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                }
3026c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
302756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
302856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // from the db
302956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        if (info.title == null) {
303056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (c != null) {
303156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.title =  c.getString(titleIndex);
303256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
303356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
303456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // fall back to the class name of the activity
30359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (info.title == null) {
303656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.title = componentName.getClassName();
30379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
30389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
30399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
30409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
304131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
304264359a53dc827797917a8d9a0697a91996789801Winson Chung    static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
304364359a53dc827797917a8d9a0697a91996789801Winson Chung            ItemInfoFilter f) {
304464359a53dc827797917a8d9a0697a91996789801Winson Chung        HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
304564359a53dc827797917a8d9a0697a91996789801Winson Chung        for (ItemInfo i : infos) {
304664359a53dc827797917a8d9a0697a91996789801Winson Chung            if (i instanceof ShortcutInfo) {
304764359a53dc827797917a8d9a0697a91996789801Winson Chung                ShortcutInfo info = (ShortcutInfo) i;
304864359a53dc827797917a8d9a0697a91996789801Winson Chung                ComponentName cn = info.intent.getComponent();
304964359a53dc827797917a8d9a0697a91996789801Winson Chung                if (cn != null && f.filterItem(null, info, cn)) {
305064359a53dc827797917a8d9a0697a91996789801Winson Chung                    filtered.add(info);
305164359a53dc827797917a8d9a0697a91996789801Winson Chung                }
305264359a53dc827797917a8d9a0697a91996789801Winson Chung            } else if (i instanceof FolderInfo) {
305364359a53dc827797917a8d9a0697a91996789801Winson Chung                FolderInfo info = (FolderInfo) i;
305464359a53dc827797917a8d9a0697a91996789801Winson Chung                for (ShortcutInfo s : info.contents) {
305564359a53dc827797917a8d9a0697a91996789801Winson Chung                    ComponentName cn = s.intent.getComponent();
305664359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (cn != null && f.filterItem(info, s, cn)) {
305764359a53dc827797917a8d9a0697a91996789801Winson Chung                        filtered.add(s);
30588a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung                    }
30598a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung                }
306064359a53dc827797917a8d9a0697a91996789801Winson Chung            } else if (i instanceof LauncherAppWidgetInfo) {
306164359a53dc827797917a8d9a0697a91996789801Winson Chung                LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
306264359a53dc827797917a8d9a0697a91996789801Winson Chung                ComponentName cn = info.providerName;
306364359a53dc827797917a8d9a0697a91996789801Winson Chung                if (cn != null && f.filterItem(null, info, cn)) {
306464359a53dc827797917a8d9a0697a91996789801Winson Chung                    filtered.add(info);
306564359a53dc827797917a8d9a0697a91996789801Winson Chung                }
30668a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung            }
30678a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung        }
306864359a53dc827797917a8d9a0697a91996789801Winson Chung        return new ArrayList<ItemInfo>(filtered);
306964359a53dc827797917a8d9a0697a91996789801Winson Chung    }
307064359a53dc827797917a8d9a0697a91996789801Winson Chung
307164359a53dc827797917a8d9a0697a91996789801Winson Chung    private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
307264359a53dc827797917a8d9a0697a91996789801Winson Chung        ItemInfoFilter filter  = new ItemInfoFilter() {
307364359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
307464359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
307564359a53dc827797917a8d9a0697a91996789801Winson Chung                return cn.getPackageName().equals(pn);
307664359a53dc827797917a8d9a0697a91996789801Winson Chung            }
307764359a53dc827797917a8d9a0697a91996789801Winson Chung        };
307864359a53dc827797917a8d9a0697a91996789801Winson Chung        return filterItemInfos(sBgItemsIdMap.values(), filter);
307964359a53dc827797917a8d9a0697a91996789801Winson Chung    }
308064359a53dc827797917a8d9a0697a91996789801Winson Chung
308164359a53dc827797917a8d9a0697a91996789801Winson Chung    private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
308264359a53dc827797917a8d9a0697a91996789801Winson Chung        ItemInfoFilter filter  = new ItemInfoFilter() {
308364359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
308464359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
308564359a53dc827797917a8d9a0697a91996789801Winson Chung                return cn.equals(cname);
308664359a53dc827797917a8d9a0697a91996789801Winson Chung            }
308764359a53dc827797917a8d9a0697a91996789801Winson Chung        };
308864359a53dc827797917a8d9a0697a91996789801Winson Chung        return filterItemInfos(sBgItemsIdMap.values(), filter);
308964359a53dc827797917a8d9a0697a91996789801Winson Chung    }
309064359a53dc827797917a8d9a0697a91996789801Winson Chung
309164359a53dc827797917a8d9a0697a91996789801Winson Chung    public static boolean isShortcutInfoUpdateable(ItemInfo i) {
309264359a53dc827797917a8d9a0697a91996789801Winson Chung        if (i instanceof ShortcutInfo) {
309364359a53dc827797917a8d9a0697a91996789801Winson Chung            ShortcutInfo info = (ShortcutInfo) i;
309464359a53dc827797917a8d9a0697a91996789801Winson Chung            // We need to check for ACTION_MAIN otherwise getComponent() might
309564359a53dc827797917a8d9a0697a91996789801Winson Chung            // return null for some shortcuts (for instance, for shortcuts to
309664359a53dc827797917a8d9a0697a91996789801Winson Chung            // web pages.)
309764359a53dc827797917a8d9a0697a91996789801Winson Chung            Intent intent = info.intent;
309864359a53dc827797917a8d9a0697a91996789801Winson Chung            ComponentName name = intent.getComponent();
309964359a53dc827797917a8d9a0697a91996789801Winson Chung            if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
310064359a53dc827797917a8d9a0697a91996789801Winson Chung                    Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
310164359a53dc827797917a8d9a0697a91996789801Winson Chung                return true;
310264359a53dc827797917a8d9a0697a91996789801Winson Chung            }
3103b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            // placeholder shortcuts get special treatment, let them through too.
3104b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            if (info.getRestoredIntent() != null) {
3105b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren                return true;
3106b6d4c2827a1514432b5eda46ff7d484d6cf244acChris Wren            }
310764359a53dc827797917a8d9a0697a91996789801Winson Chung        }
310864359a53dc827797917a8d9a0697a91996789801Winson Chung        return false;
31098a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung    }
31108a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung
31118a4351063f02c0e8d64ee3ace651b227e9f8321fWinson Chung    /**
31120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato     * Make an ShortcutInfo object for a shortcut that isn't an application.
31139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
31140589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private ShortcutInfo getShortcutInfo(Cursor c, Context context,
311556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
311656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            int titleIndex) {
311731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
311856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        Bitmap icon = null;
3119c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ShortcutInfo info = new ShortcutInfo();
31209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
31217376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
31228ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato        // TODO: If there's an explicit component and we can't install that, delete it.
31238ddc4fdba06cfa786950c306475b7a4e3f6846abJoe Onorato
312456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        info.title = c.getString(titleIndex);
312556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
31269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int iconType = c.getInt(iconTypeIndex);
31279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        switch (iconType) {
31289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
31299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String packageName = c.getString(iconPackageIndex);
31309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            String resourceName = c.getString(iconResourceIndex);
31319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            PackageManager packageManager = context.getPackageManager();
313256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.customIcon = false;
313356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the resource
31349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            try {
31359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                Resources resources = packageManager.getResourcesForApplication(packageName);
313656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                if (resources != null) {
313756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    final int id = resources.getIdentifier(resourceName, null, null);
3138c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
3139c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
314056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                }
31419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            } catch (Exception e) {
314256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                // drop this.  we have other places to look for icons
314356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
314456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the db
314556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
3146931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                icon = getIconFromCursor(c, iconIndex, context);
314756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
314856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            // the fallback icon
314956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
315056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
315156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
31527376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            }
31539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
31549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
3155931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            icon = getIconFromCursor(c, iconIndex, context);
315656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (icon == null) {
315756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                icon = getFallbackIcon();
315856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = false;
315956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.usingFallbackIcon = true;
316056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            } else {
316156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                info.customIcon = true;
31629c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
31639c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
31649c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        default:
3165d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato            icon = getFallbackIcon();
316656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            info.usingFallbackIcon = true;
31679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            info.customIcon = false;
31689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            break;
316931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
3170d8d22da5ca3e8d3e8d2e3616305b038d37486f32Joe Onorato        info.setIcon(icon);
31719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return info;
3172f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
317331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3174931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka    Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
31753a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        @SuppressWarnings("all") // suppress dead code warning
31763a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        final boolean debug = false;
31773a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka        if (debug) {
317856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            Log.d(TAG, "getIconFromCursor app="
317956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
318056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
318156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        byte[] data = c.getBlob(iconIndex);
318256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        try {
3183931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            return Utilities.createIconBitmap(
3184931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                    BitmapFactory.decodeByteArray(data, 0, data.length), context);
318556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        } catch (Exception e) {
318656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return null;
318756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
318856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
318956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
31903d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
31913d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellX, int cellY, boolean notify) {
3192a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
3193d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (info == null) {
3194d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return null;
3195d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
31963d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
31970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
31980589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
31990589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
32000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3201a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    /**
320255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     * Attempts to find an AppWidgetProviderInfo that matches the given component.
320355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung     */
320455cef262f97749300c2f6e764da0b00cbcb78879Winson Chung    AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
320555cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            ComponentName component) {
320655cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        List<AppWidgetProviderInfo> widgets =
320755cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
320855cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        for (AppWidgetProviderInfo info : widgets) {
320955cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            if (info.provider.equals(component)) {
321055cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                return info;
321155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung            }
321255cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        }
321355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung        return null;
3214a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
3215a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
321668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    /**
321768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     * Returns a list of all the widgets that can handle configuration with a particular mimeType.
321868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung     */
321968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
322068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final PackageManager packageManager = context.getPackageManager();
322168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
322268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new ArrayList<WidgetMimeTypeHandlerData>();
322368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
322468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final Intent supportsIntent =
322568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
322668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        supportsIntent.setType(mimeType);
322768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
322868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Create a set of widget configuration components that we can test against
322968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<AppWidgetProviderInfo> widgets =
323068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            AppWidgetManager.getInstance(context).getInstalledProviders();
323168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
323268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            new HashMap<ComponentName, AppWidgetProviderInfo>();
323368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (AppWidgetProviderInfo info : widgets) {
323468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            configurationComponentToWidget.put(info.configure, info);
323568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
323668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
323768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // Run through each of the intents that can handle this type of clip data, and cross
323868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        // reference them with the components that are actual configuration components
323968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
324068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                PackageManager.MATCH_DEFAULT_ONLY);
324168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        for (ResolveInfo info : activities) {
324268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ActivityInfo activityInfo = info.activityInfo;
324368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
324468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                    activityInfo.name);
324568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            if (configurationComponentToWidget.containsKey(infoComponent)) {
324668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                supportedConfigurationActivities.add(
324768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
324868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                                configurationComponentToWidget.get(infoComponent)));
324968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            }
325068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        }
325168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        return supportedConfigurationActivities;
325268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung    }
325368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung
3254a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
32550589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
32560589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
32570589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
32580589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3259d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (intent == null) {
3260d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3261d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3262d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return null;
3263d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
3264d919882006a457d16dea46bd546472e6534b5c19Adam Cohen
32650589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Bitmap icon = null;
32660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        boolean customIcon = false;
32670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        ShortcutIconResource iconResource = null;
32680589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
32690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (bitmap != null && bitmap instanceof Bitmap) {
32700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
32710589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            customIcon = true;
32720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
32730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
32740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (extra != null && extra instanceof ShortcutIconResource) {
32750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                try {
32760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    iconResource = (ShortcutIconResource) extra;
32770589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final PackageManager packageManager = context.getPackageManager();
32780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Resources resources = packageManager.getResourcesForApplication(
32790589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            iconResource.packageName);
32800589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    final int id = resources.getIdentifier(iconResource.resourceName, null, null);
3281c9a961952d1a057029874f8426b90181f6876034Michael Jurka                    icon = Utilities.createIconBitmap(
3282c9a961952d1a057029874f8426b90181f6876034Michael Jurka                            mIconCache.getFullResIcon(resources, id), context);
32830589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                } catch (Exception e) {
32840589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    Log.w(TAG, "Could not load shortcut icon: " + extra);
32850589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                }
32860589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            }
32870589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
32880589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3289c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final ShortcutInfo info = new ShortcutInfo();
329056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
32910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (icon == null) {
3292a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            if (fallbackIcon != null) {
3293a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = fallbackIcon;
3294a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            } else {
3295a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                icon = getFallbackIcon();
3296a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                info.usingFallbackIcon = true;
3297a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            }
32980589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        }
32990589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.setIcon(icon);
330056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
33010589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.title = name;
33020589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.intent = intent;
33030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.customIcon = customIcon;
33040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        info.iconResource = iconResource;
33050589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
33060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        return info;
33070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    }
33080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato
3309aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3310aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            int iconIndex) {
331117a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        // If apps can't be on SD, don't even bother.
3312ee055715cfe5085aa7c1ff1f8a14348bd0cef604Winson Chung        if (!mAppsCanBeOnRemoveableStorage) {
3313aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            return false;
331417a8922e2ab2ad599df3750d398a095473ccd1b4Joe Onorato        }
331556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // If this icon doesn't have a custom icon, check to see
331656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // what's stored in the DB, and if it doesn't match what
331756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // we're going to show, store what we are going to show back
331856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // into the DB.  We do this so when we're loading, if the
331956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // package manager can't find an icon (for example because
332056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        // the app is on SD) then we can use that instead.
3321ddc9c1fb1ab426772add520d277ea9c2cd674094Joe Onorato        if (!info.customIcon && !info.usingFallbackIcon) {
3322aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            cache.put(info, c.getBlob(iconIndex));
3323aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            return true;
3324aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        }
3325aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        return false;
3326aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    }
3327aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung    void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3328aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        boolean needSave = false;
3329aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        try {
3330aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            if (data != null) {
3331aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3332aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                Bitmap loaded = info.getIcon(mIconCache);
3333aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung                needSave = !saved.sameAs(loaded);
3334aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            } else {
333556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                needSave = true;
333656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
3337aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        } catch (Exception e) {
3338aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            needSave = true;
3339aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        }
3340aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung        if (needSave) {
3341aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            Log.d(TAG, "going to save icon bitmap for info=" + info);
3342aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // This is slower than is ideal, but this only happens once
3343aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            // or when the app is updated with a new icon.
3344aac01e1073f3deb3f543fd27cc63fd727101584eWinson Chung            updateItemInDatabase(context, info);
334556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
334656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
334756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
334831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
3349df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     * Return an existing FolderInfo object if we have encountered this ID previously,
33509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * or make a new one.
335131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
3352df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
33539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // See if a placeholder was created for us already
33549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        FolderInfo folderInfo = folders.get(id);
3355df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        if (folderInfo == null) {
33569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            // No placeholder -- create a new instance
3357c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka            folderInfo = new FolderInfo();
33589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            folders.put(id, folderInfo);
335931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
3360df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        return folderInfo;
336131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
336231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3363eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka    public static final Comparator<AppInfo> getAppNameComparator() {
33641190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        final Collator collator = Collator.getInstance();
3365eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        return new Comparator<AppInfo>() {
3366eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            public final int compare(AppInfo a, AppInfo b) {
3367780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                int result = collator.compare(a.title.toString().trim(),
3368780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                        b.title.toString().trim());
33691190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                if (result == 0) {
33701190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                    result = a.componentName.compareTo(b.componentName);
33711190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                }
33721190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung                return result;
33735b1808da75ed3e3528cb31dee56a4e363812cc21Michael Jurka            }
33741190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        };
33751190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    }
3376eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka    public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3377eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka            = new Comparator<AppInfo>() {
3378eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        public final int compare(AppInfo a, AppInfo b) {
337978403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime < b.firstInstallTime) return 1;
338078403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            if (a.firstInstallTime > b.firstInstallTime) return -1;
338178403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung            return 0;
338278403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung        }
338378403feedcf6d61a527cf0c904cb55ca43c0f3cbWinson Chung    };
33841190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
33851190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        final Collator collator = Collator.getInstance();
33861190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        return new Comparator<AppWidgetProviderInfo>() {
33871190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3388780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                return collator.compare(a.label.toString().trim(), b.label.toString().trim());
33891190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            }
33901190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        };
33911190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung    }
33925308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung    static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
33935308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        if (info.activityInfo != null) {
33945308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
33955308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        } else {
33965308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
33975308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung        }
33985308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung    }
3399785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
34001190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        private Collator mCollator;
3401785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        private PackageManager mPackageManager;
3402c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        private HashMap<Object, CharSequence> mLabelCache;
3403785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        ShortcutNameComparator(PackageManager pm) {
3404785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mPackageManager = pm;
3405c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = new HashMap<Object, CharSequence>();
34061190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
3407c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        }
3408c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung        ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3409c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mPackageManager = pm;
3410c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            mLabelCache = labelCache;
34111190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
3412785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3413785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        public final int compare(ResolveInfo a, ResolveInfo b) {
3414c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            CharSequence labelA, labelB;
34155308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
34165308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
34175308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (mLabelCache.containsKey(keyA)) {
34185308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                labelA = mLabelCache.get(keyA);
3419c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3420780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                labelA = a.loadLabel(mPackageManager).toString().trim();
3421c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung
34225308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                mLabelCache.put(keyA, labelA);
3423c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
34245308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung            if (mLabelCache.containsKey(keyB)) {
34255308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                labelB = mLabelCache.get(keyB);
3426c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3427780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                labelB = b.loadLabel(mPackageManager).toString().trim();
3428c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung
34295308f24d4b66b020202b88eff672f5a89096ebb6Winson Chung                mLabelCache.put(keyB, labelB);
3430c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
34311190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            return mCollator.compare(labelA, labelB);
3432785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3433785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    };
34341ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung    public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
34351190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung        private Collator mCollator;
34361ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        private PackageManager mPackageManager;
34371ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        private HashMap<Object, String> mLabelCache;
34381ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        WidgetAndShortcutNameComparator(PackageManager pm) {
34391ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            mPackageManager = pm;
34401ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            mLabelCache = new HashMap<Object, String>();
34411190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            mCollator = Collator.getInstance();
34421ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        }
34431ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        public final int compare(Object a, Object b) {
34441ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung            String labelA, labelB;
3445c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            if (mLabelCache.containsKey(a)) {
3446c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = mLabelCache.get(a);
3447c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3448c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelA = (a instanceof AppWidgetProviderInfo) ?
34491ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((AppWidgetProviderInfo) a).label :
3450780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                    ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
3451c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                mLabelCache.put(a, labelA);
3452c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
3453c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            if (mLabelCache.containsKey(b)) {
3454c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = mLabelCache.get(b);
3455c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            } else {
3456c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                labelB = (b instanceof AppWidgetProviderInfo) ?
34571ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung                    ((AppWidgetProviderInfo) b).label :
3458780fe59a7af8b12fbdcd7f6841edaa7f2c2e019dWinson Chung                    ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
3459c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung                mLabelCache.put(b, labelB);
3460c3eecff9043759926df124483b45fe8f4436ae7cWinson Chung            }
34611190487e15325e371c6c18181b1a5ef7ab2a79e6Winson Chung            return mCollator.compare(labelA, labelB);
34621ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung        }
34631ed747a4c07101793322c13a36dd547df4a3aa50Winson Chung    };
3464be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
3465be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
3466be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "mCallbacks=" + mCallbacks);
3467eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3468eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3469eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3470eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka        AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
34713611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (mLoaderTask != null) {
34723611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mLoaderTask.dumpState();
34733611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        } else {
34743611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            Log.d(TAG, "mLoaderTask=null");
34753611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
3476be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
347731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
3478