blob: 041882fd95bf2368221e66ac99950e3d624daaa1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040022import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080024import android.content.ContentProviderClient;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070025import android.content.ContentProviderOperation;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ContentResolver;
27import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080030import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070032import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070034import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070036import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Resources;
38import android.database.Cursor;
39import android.graphics.Bitmap;
40import android.graphics.BitmapFactory;
41import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080042import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040043import android.os.Handler;
44import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080045import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040048import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080052
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.lang.ref.WeakReference;
54import java.net.URISyntaxException;
55import java.text.Collator;
56import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070057import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070058import java.util.Collections;
59import java.util.Comparator;
60import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070061import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070062import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070063import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070064import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070065import java.util.TreeMap;
Michael Jurkac2f801e2011-07-12 14:19:46 -070066
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067/**
68 * Maintains in-memory state of the Launcher. It is expected that there should be only one
69 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070070 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040072public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080073 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070075
Joe Onorato36115782010-06-17 13:28:48 -040076 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080077 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040078 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040079 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040080
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040082 private final Object mLock = new Object();
83 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040084 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070085 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020086 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087
Winson Chung81b52252012-08-27 15:34:29 -070088 // Specific runnable types that are run on the main thread deferred handler, this allows us to
89 // clear all queued binding runnables when the Launcher activity is destroyed.
90 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
91 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
92
93
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070094 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
95 static {
96 sWorkerThread.start();
97 }
98 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
99
Joe Onoratocc67f472010-06-08 10:54:30 -0700100 // We start off with everything not loaded. After that, we assume that
101 // our monitoring of the package manager provides all updates and we never
102 // need to do a requery. These are only ever touched from the loader thread.
103 private boolean mWorkspaceLoaded;
104 private boolean mAllAppsLoaded;
105
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700106 // When we are loading pages synchronously, we can't just post the binding of items on the side
107 // pages as this delays the rotation process. Instead, we wait for a callback from the first
108 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
109 // a normal load, we also clear this set of Runnables.
110 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
111
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700114 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700115 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800116
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700117 // The lock that must be acquired before referencing any static bg data structures. Unlike
118 // other locks, this one can generally be held long-term because we never expect any of these
119 // static data structures to be referenced outside of the worker thread except on the first
120 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700121 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700122
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700124 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700125 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700126
Adam Cohen487f7dd2012-06-28 18:12:10 -0700127 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
128 // created by LauncherModel that are directly on the home screen (however, no widgets or
129 // shortcuts within folders).
130 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
133 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700134 new ArrayList<LauncherAppWidgetInfo>();
135
Adam Cohen487f7dd2012-06-28 18:12:10 -0700136 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
137 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700138
Adam Cohen487f7dd2012-06-28 18:12:10 -0700139 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
140 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700141
142 // sBgWorkspaceScreens is the ordered set of workspace screens.
143 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
144
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145 // </ only access in worker thread >
146
147 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Adam Cohend22015c2010-07-26 22:02:18 -0700150 private static int mCellCountX;
151 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700152
Reena Lee99a73f32011-10-24 17:27:37 -0700153 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700156 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400157 public int getCurrentWorkspaceScreen();
158 public void startBinding();
159 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Adam Cohendcd297f2013-06-18 13:13:40 -0700160 public void bindScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500161 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700162 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400163 public void bindAppWidget(LauncherAppWidgetInfo info);
164 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500165 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
166 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700167 public void bindComponentsRemoved(ArrayList<String> packageNames,
168 ArrayList<ApplicationInfo> appInfos,
169 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200170 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Daniel Sandler843e8602010-06-07 14:59:01 -0400171 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800172 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100173 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700174 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400177 LauncherModel(Context context, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800178 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400179 mApp = LauncherAppState.getInstance();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700180 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800181 mIconCache = iconCache;
182
183 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400184 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400185
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400186 final Resources res = context.getResources();
Reena Lee93f824a2011-09-23 17:20:28 -0700187 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
188 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700189 Configuration config = res.getConfiguration();
190 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800191 }
192
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700193 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
194 * posted on the main thread handler. */
195 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700196 runOnMainThread(r, 0);
197 }
198 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700199 if (sWorkerThread.getThreadId() == Process.myTid()) {
200 // If we are on the worker thread, post onto the main handler
201 mHandler.post(r);
202 } else {
203 r.run();
204 }
205 }
206
207 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
208 * posted on the worker thread handler. */
209 private static void runOnWorkerThread(Runnable r) {
210 if (sWorkerThread.getThreadId() == Process.myTid()) {
211 r.run();
212 } else {
213 // If we are not on the worker thread, then post to the worker handler
214 sWorker.post(r);
215 }
216 }
217
Joe Onorato56d82912010-03-07 14:32:10 -0500218 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
Winson Chung81b52252012-08-27 15:34:29 -0700222 public void unbindItemInfosAndClearQueuedBindRunnables() {
223 if (sWorkerThread.getThreadId() == Process.myTid()) {
224 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
225 "main thread");
226 }
227
228 // Clear any deferred bind runnables
229 mDeferredBindRunnables.clear();
230 // Remove any queued bind runnables
231 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
232 // Unbind all the workspace items
233 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700234 }
235
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700236 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700237 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700238 // Ensure that we don't use the same workspace items data structure on the main thread
239 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700240 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
241 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700242 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700243 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
244 tmpAppWidgets.addAll(sBgAppWidgets);
245 }
246 Runnable r = new Runnable() {
247 @Override
248 public void run() {
249 for (ItemInfo item : tmpWorkspaceItems) {
250 item.unbind();
251 }
252 for (ItemInfo item : tmpAppWidgets) {
253 item.unbind();
254 }
255 }
256 };
257 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700258 }
259
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260 /**
261 * Adds an item to the DB if it was not created previously, or move it to a new
262 * <container, screen, cellX, cellY>
263 */
264 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700265 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266 if (item.container == ItemInfo.NO_ID) {
267 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700268 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 } else {
270 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700271 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 }
273 }
274
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700275 static void checkItemInfoLocked(
276 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
277 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
278 if (modelItem != null && item != modelItem) {
279 // check all the data is consistent
280 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
281 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
282 ShortcutInfo shortcut = (ShortcutInfo) item;
283 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
284 modelShortcut.intent.filterEquals(shortcut.intent) &&
285 modelShortcut.id == shortcut.id &&
286 modelShortcut.itemType == shortcut.itemType &&
287 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700288 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700289 modelShortcut.cellX == shortcut.cellX &&
290 modelShortcut.cellY == shortcut.cellY &&
291 modelShortcut.spanX == shortcut.spanX &&
292 modelShortcut.spanY == shortcut.spanY &&
293 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
294 (modelShortcut.dropPos != null &&
295 shortcut.dropPos != null &&
296 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
297 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
298 // For all intents and purposes, this is the same object
299 return;
300 }
301 }
302
303 // the modelItem needs to match up perfectly with item if our model is
304 // to be consistent with the database-- for now, just require
305 // modelItem == item or the equality check above
306 String msg = "item: " + ((item != null) ? item.toString() : "null") +
307 "modelItem: " +
308 ((modelItem != null) ? modelItem.toString() : "null") +
309 "Error: ItemInfo passed to checkItemInfo doesn't match original";
310 RuntimeException e = new RuntimeException(msg);
311 if (stackTrace != null) {
312 e.setStackTrace(stackTrace);
313 }
Adam Cohene25af792013-06-06 23:08:25 -0700314 // TODO: something breaks this in the upgrade path
315 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700316 }
317 }
318
Michael Jurka816474f2012-06-25 14:49:02 -0700319 static void checkItemInfo(final ItemInfo item) {
320 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
321 final long itemId = item.id;
322 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700323 public void run() {
324 synchronized (sBgLock) {
325 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700326 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700327 }
328 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700329 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700330 }
331
Michael Jurkac9d95c52011-08-29 14:03:34 -0700332 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
333 final ItemInfo item, final String callingFunction) {
334 final long itemId = item.id;
335 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
336 final ContentResolver cr = context.getContentResolver();
337
Adam Cohen487f7dd2012-06-28 18:12:10 -0700338 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700339 Runnable r = new Runnable() {
340 public void run() {
341 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700342 updateItemArrays(item, itemId, stackTrace);
343 }
344 };
345 runOnWorkerThread(r);
346 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700347
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700348 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
349 final ArrayList<ItemInfo> items, final String callingFunction) {
350 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700351
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700352 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
353 Runnable r = new Runnable() {
354 public void run() {
355 ArrayList<ContentProviderOperation> ops =
356 new ArrayList<ContentProviderOperation>();
357 int count = items.size();
358 for (int i = 0; i < count; i++) {
359 ItemInfo item = items.get(i);
360 final long itemId = item.id;
361 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
362 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700363
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700364 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
365 updateItemArrays(item, itemId, stackTrace);
366
367 }
368 try {
369 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
370 } catch (Exception e) {
371 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700372 }
373 }
374 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700375 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700376 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700377
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700378 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
379 // Lock on mBgLock *after* the db operation
380 synchronized (sBgLock) {
381 checkItemInfoLocked(itemId, item, stackTrace);
382
383 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
384 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
385 // Item is in a folder, make sure this folder exists
386 if (!sBgFolders.containsKey(item.container)) {
387 // An items container is being set to a that of an item which is not in
388 // the list of Folders.
389 String msg = "item: " + item + " container being set to: " +
390 item.container + ", not in the list of folders";
391 Log.e(TAG, msg);
392 Launcher.dumpDebugLogsToConsole();
393 }
394 }
395
396 // Items are added/removed from the corresponding FolderInfo elsewhere, such
397 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
398 // that are on the desktop, as appropriate
399 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
400 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
401 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
402 switch (modelItem.itemType) {
403 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
404 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
405 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
406 if (!sBgWorkspaceItems.contains(modelItem)) {
407 sBgWorkspaceItems.add(modelItem);
408 }
409 break;
410 default:
411 break;
412 }
413 } else {
414 sBgWorkspaceItems.remove(modelItem);
415 }
416 }
417 }
418
Michael Jurkac7700af2013-05-14 20:17:58 +0200419 public void flushWorkerThread() {
420 mFlushingWorkerThread = true;
421 Runnable waiter = new Runnable() {
422 public void run() {
423 synchronized (this) {
424 notifyAll();
425 mFlushingWorkerThread = false;
426 }
427 }
428 };
429
430 synchronized(waiter) {
431 runOnWorkerThread(waiter);
432 if (mLoaderTask != null) {
433 synchronized(mLoaderTask) {
434 mLoaderTask.notify();
435 }
436 }
437 boolean success = false;
438 while (!success) {
439 try {
440 waiter.wait();
441 success = true;
442 } catch (InterruptedException e) {
443 }
444 }
445 }
446 }
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700450 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700451 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700452 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700453 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700454 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
455 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700456 Launcher.sDumpLogs.add(transaction);
457 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400458 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400459 item.cellX = cellX;
460 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700461
Winson Chung3d503fb2011-07-13 17:25:49 -0700462 // We store hotseat items in canonical form which is this orientation invariant position
463 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700464 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700465 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700466 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700467 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700468 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700469 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400470
471 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400472 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700473 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
474 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700475 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400476
Michael Jurkac9d95c52011-08-29 14:03:34 -0700477 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700478 }
479
480 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700481 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
482 * cellX, cellY have already been updated on the ItemInfos.
483 */
484 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
485 final long container, final int screen) {
486
487 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
488 int count = items.size();
489
490 for (int i = 0; i < count; i++) {
491 ItemInfo item = items.get(i);
492 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700493 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700494 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
495 + item.cellX + ", " + item.cellY + ")";
496 Launcher.sDumpLogs.add(transaction);
497 item.container = container;
498
499 // We store hotseat items in canonical form which is this orientation invariant position
500 // in the hotseat
501 if (context instanceof Launcher && screen < 0 &&
502 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700503 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700504 item.cellY);
505 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700506 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700507 }
508
509 final ContentValues values = new ContentValues();
510 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
511 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
512 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700513 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700514
515 contentValues.add(values);
516 }
517 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
518 }
519
520 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700521 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800522 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700523 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700524 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700525 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700526 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
527 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700528 Launcher.sDumpLogs.add(transaction);
529 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800530 item.cellX = cellX;
531 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700532 item.spanX = spanX;
533 item.spanY = spanY;
534
535 // We store hotseat items in canonical form which is this orientation invariant position
536 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700537 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700538 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700539 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700540 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700541 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700542 }
Adam Cohend4844c32011-02-18 19:25:06 -0800543
Adam Cohend4844c32011-02-18 19:25:06 -0800544 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800545 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700546 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
547 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
548 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
549 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700550 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800551
Michael Jurka816474f2012-06-25 14:49:02 -0700552 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700553 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700554
555 /**
556 * Update an item to the database in a specified container.
557 */
558 static void updateItemInDatabase(Context context, final ItemInfo item) {
559 final ContentValues values = new ContentValues();
560 item.onAddToDatabase(values);
561 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
562 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800563 }
564
565 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 * Returns true if the shortcuts already exists in the database.
567 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400569 static boolean shortcutExists(Context context, String title, Intent intent) {
570 final ContentResolver cr = context.getContentResolver();
571 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
572 new String[] { "title", "intent" }, "title=? and intent=?",
573 new String[] { title, intent.toUri(0) }, null);
574 boolean result = false;
575 try {
576 result = c.moveToFirst();
577 } finally {
578 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400580 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700581 }
582
Joe Onorato9c1289c2009-08-17 11:03:03 -0400583 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 * Returns an ItemInfo array containing all the items in the LauncherModel.
585 * The ItemInfo.id is not set through this function.
586 */
587 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
588 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
589 final ContentResolver cr = context.getContentResolver();
590 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
591 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
592 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
593 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
594
595 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
596 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
597 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
598 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
599 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
600 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
601 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
602
603 try {
604 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700605 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700606 item.cellX = c.getInt(cellXIndex);
607 item.cellY = c.getInt(cellYIndex);
608 item.spanX = c.getInt(spanXIndex);
609 item.spanY = c.getInt(spanYIndex);
610 item.container = c.getInt(containerIndex);
611 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700612 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700613
614 items.add(item);
615 }
616 } catch (Exception e) {
617 items.clear();
618 } finally {
619 c.close();
620 }
621
622 return items;
623 }
624
625 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
627 */
628 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
629 final ContentResolver cr = context.getContentResolver();
630 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
631 "_id=? and (itemType=? or itemType=?)",
632 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700633 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700634
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 try {
636 if (c.moveToFirst()) {
637 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
638 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
639 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
640 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
641 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
642 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 FolderInfo folderInfo = null;
645 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700646 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
647 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700649 }
650
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 folderInfo.title = c.getString(titleIndex);
652 folderInfo.id = id;
653 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700654 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700655 folderInfo.cellX = c.getInt(cellXIndex);
656 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657
658 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700659 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 } finally {
661 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700662 }
663
664 return null;
665 }
666
Joe Onorato9c1289c2009-08-17 11:03:03 -0400667 /**
668 * Add an item to the database in a specified container. Sets the container, screen, cellX and
669 * cellY fields of the item. Also assigns an ID to the item.
670 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700671 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700672 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400673 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 item.cellX = cellX;
675 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700676 // We store hotseat items in canonical form which is this orientation invariant position
677 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700679 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700680 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700681 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700683 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684
685 final ContentValues values = new ContentValues();
686 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687 item.onAddToDatabase(values);
688
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400689 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohendcd297f2013-06-18 13:13:40 -0700690 item.id = app.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700691 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700692 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700693
Michael Jurkac9d95c52011-08-29 14:03:34 -0700694 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700695 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700696 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700697 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700698 + cellY + ")";
699 Launcher.sDumpLogs.add(transaction);
700 Log.d(TAG, transaction);
701
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700702 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
703 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700705 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700706 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700707 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700708 sBgItemsIdMap.put(item.id, item);
709 switch (item.itemType) {
710 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
711 sBgFolders.put(item.id, (FolderInfo) item);
712 // Fall through
713 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
714 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
715 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
716 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
717 sBgWorkspaceItems.add(item);
718 } else {
719 if (!sBgFolders.containsKey(item.container)) {
720 // Adding an item to a folder that doesn't exist.
721 String msg = "adding item: " + item + " to a folder that " +
722 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700723 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700724 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700725 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700726 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700727 break;
728 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
729 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
730 break;
731 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700732 }
733 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700734 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700735 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700736 }
737
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700739 * Creates a new unique child id, for a given cell span across all layouts.
740 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700741 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700743 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700744 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700745 }
746
Adam Cohend22015c2010-07-26 22:02:18 -0700747 static int getCellCountX() {
748 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700749 }
750
Adam Cohend22015c2010-07-26 22:02:18 -0700751 static int getCellCountY() {
752 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700753 }
754
755 /**
756 * Updates the model orientation helper to take into account the current layout dimensions
757 * when performing local/canonical coordinate transformations.
758 */
759 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700760 mCellCountX = shortAxisCellCount;
761 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700762 }
763
764 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700765 * Removes the specified item from the database
766 * @param context
767 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700769 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700771 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700772
Michael Jurka83df1882011-08-31 20:59:26 -0700773 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700774 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700775 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700777 ", " + item.cellY + ")";
778 Launcher.sDumpLogs.add(transaction);
779 Log.d(TAG, transaction);
780
Michael Jurkac9d95c52011-08-29 14:03:34 -0700781 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700782
783 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700784 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700785 switch (item.itemType) {
786 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
787 sBgFolders.remove(item.id);
788 for (ItemInfo info: sBgItemsIdMap.values()) {
789 if (info.container == item.id) {
790 // We are deleting a folder which still contains items that
791 // think they are contained by that folder.
792 String msg = "deleting a folder (" + item + ") which still " +
793 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700794 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700795 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700796 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700797 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700798 sBgWorkspaceItems.remove(item);
799 break;
800 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
801 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
802 sBgWorkspaceItems.remove(item);
803 break;
804 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
805 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
806 break;
807 }
808 sBgItemsIdMap.remove(item.id);
809 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700810 }
811 }
Michael Jurka83df1882011-08-31 20:59:26 -0700812 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700813 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 }
815
816 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700817 * Update the order of the workspace screens in the database. The array list contains
818 * a list of screen ids in the order that they should appear.
819 */
820 static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
821 final ContentResolver cr = context.getContentResolver();
822 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
823
824 // Remove any negative screen ids -- these aren't persisted
825 Iterator<Long> iter = screens.iterator();
826 while (iter.hasNext()) {
827 long id = iter.next();
828 if (id < 0) {
829 iter.remove();
830 }
831 }
832
833 Runnable r = new Runnable() {
834 @Override
835 public void run() {
836 final ArrayList<Long> screensCopy = new ArrayList<Long>();
837
838 // Clear the table
839 cr.delete(uri, null, null);
840 int count = screens.size();
841 ContentValues[] values = new ContentValues[count];
842 for (int i = 0; i < count; i++) {
843 ContentValues v = new ContentValues();
844 long screenId = screens.get(i);
845 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
846 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
847 screensCopy.add(screenId);
848 values[i] = v;
849 }
850 cr.bulkInsert(uri, values);
851 sBgWorkspaceScreens.clear();
852 sBgWorkspaceScreens.addAll(screensCopy);
853 }
854 };
855 runOnWorkerThread(r);
856 }
857
858 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 * Remove the contents of the specified folder from the database
860 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700861 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862 final ContentResolver cr = context.getContentResolver();
863
Michael Jurkac9d95c52011-08-29 14:03:34 -0700864 Runnable r = new Runnable() {
865 public void run() {
866 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700867 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700868 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700869 sBgItemsIdMap.remove(info.id);
870 sBgFolders.remove(info.id);
871 sBgDbIconCache.remove(info);
872 sBgWorkspaceItems.remove(info);
873 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700874
Michael Jurkac9d95c52011-08-29 14:03:34 -0700875 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
876 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700877 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700878 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700879 for (ItemInfo childInfo : info.contents) {
880 sBgItemsIdMap.remove(childInfo.id);
881 sBgDbIconCache.remove(childInfo);
882 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700883 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700884 }
885 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887 }
888
889 /**
890 * Set this as the current Launcher activity object for the loader.
891 */
892 public void initialize(Callbacks callbacks) {
893 synchronized (mLock) {
894 mCallbacks = new WeakReference<Callbacks>(callbacks);
895 }
896 }
897
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700898 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
900 * ACTION_PACKAGE_CHANGED.
901 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100902 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400903 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400904 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700905
Joe Onorato36115782010-06-17 13:28:48 -0400906 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400907
Joe Onorato36115782010-06-17 13:28:48 -0400908 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
909 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
910 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
911 final String packageName = intent.getData().getSchemeSpecificPart();
912 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913
Joe Onorato36115782010-06-17 13:28:48 -0400914 int op = PackageUpdatedTask.OP_NONE;
915
916 if (packageName == null || packageName.length() == 0) {
917 // they sent us a bad intent
918 return;
919 }
920
921 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
922 op = PackageUpdatedTask.OP_UPDATE;
923 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
924 if (!replacing) {
925 op = PackageUpdatedTask.OP_REMOVE;
926 }
927 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
928 // later, we will update the package at this time
929 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
930 if (!replacing) {
931 op = PackageUpdatedTask.OP_ADD;
932 } else {
933 op = PackageUpdatedTask.OP_UPDATE;
934 }
935 }
936
937 if (op != PackageUpdatedTask.OP_NONE) {
938 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
939 }
940
941 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400942 // First, schedule to add these apps back in.
943 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
944 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
945 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700946 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400947 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
948 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
949 enqueuePackageUpdated(new PackageUpdatedTask(
950 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700951 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700952 // If we have changed locale we need to clear out the labels in all apps/workspace.
953 forceReload();
954 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
955 // Check if configuration change was an mcc/mnc change which would affect app resources
956 // and we would need to clear out the labels in all apps/workspace. Same handling as
957 // above for ACTION_LOCALE_CHANGED
958 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700959 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700960 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700961 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700962 forceReload();
963 }
964 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700965 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700966 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
967 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700968 if (mCallbacks != null) {
969 Callbacks callbacks = mCallbacks.get();
970 if (callbacks != null) {
971 callbacks.bindSearchablesChanged();
972 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700973 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700974 }
975 }
976
Reena Lee93f824a2011-09-23 17:20:28 -0700977 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700978 resetLoadedState(true, true);
979
Reena Lee93f824a2011-09-23 17:20:28 -0700980 // Do this here because if the launcher activity is running it will be restarted.
981 // If it's not running startLoaderFromBackground will merely tell it that it needs
982 // to reload.
983 startLoaderFromBackground();
984 }
985
Winson Chungf0c6ae02012-03-21 16:10:31 -0700986 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
987 synchronized (mLock) {
988 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
989 // mWorkspaceLoaded to true later
990 stopLoaderLocked();
991 if (resetAllAppsLoaded) mAllAppsLoaded = false;
992 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
993 }
994 }
995
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700996 /**
997 * When the launcher is in the background, it's possible for it to miss paired
998 * configuration changes. So whenever we trigger the loader from the background
999 * tell the launcher that it needs to re-run the loader when it comes back instead
1000 * of doing it now.
1001 */
1002 public void startLoaderFromBackground() {
1003 boolean runLoader = false;
1004 if (mCallbacks != null) {
1005 Callbacks callbacks = mCallbacks.get();
1006 if (callbacks != null) {
1007 // Only actually run the loader if they're not paused.
1008 if (!callbacks.setLoadOnResume()) {
1009 runLoader = true;
1010 }
1011 }
1012 }
1013 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001015 }
Joe Onorato36115782010-06-17 13:28:48 -04001016 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001017
Reena Lee93f824a2011-09-23 17:20:28 -07001018 // If there is already a loader task running, tell it to stop.
1019 // returns true if isLaunching() was true on the old task
1020 private boolean stopLoaderLocked() {
1021 boolean isLaunching = false;
1022 LoaderTask oldTask = mLoaderTask;
1023 if (oldTask != null) {
1024 if (oldTask.isLaunching()) {
1025 isLaunching = true;
1026 }
1027 oldTask.stopLocked();
1028 }
1029 return isLaunching;
1030 }
1031
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001033 synchronized (mLock) {
1034 if (DEBUG_LOADERS) {
1035 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1036 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 // Clear any deferred bind-runnables from the synchronized load process
1039 // We must do this before any loading/binding is scheduled below.
1040 mDeferredBindRunnables.clear();
1041
Joe Onorato36115782010-06-17 13:28:48 -04001042 // Don't bother to start the thread if we know it's not going to do anything
1043 if (mCallbacks != null && mCallbacks.get() != null) {
1044 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001045 // also, don't downgrade isLaunching if we're already running
1046 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001047 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1049 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1050 } else {
1051 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1052 sWorker.post(mLoaderTask);
1053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001055 }
1056 }
1057
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001058 void bindRemainingSynchronousPages() {
1059 // Post the remaining side pages to be loaded
1060 if (!mDeferredBindRunnables.isEmpty()) {
1061 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001062 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 }
1064 mDeferredBindRunnables.clear();
1065 }
1066 }
1067
Joe Onorato36115782010-06-17 13:28:48 -04001068 public void stopLoader() {
1069 synchronized (mLock) {
1070 if (mLoaderTask != null) {
1071 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 }
1073 }
Joe Onorato36115782010-06-17 13:28:48 -04001074 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075
Michael Jurkac57b7a82011-08-09 22:02:20 -07001076 public boolean isAllAppsLoaded() {
1077 return mAllAppsLoaded;
1078 }
1079
Winson Chung36a62fe2012-05-06 18:04:42 -07001080 boolean isLoadingWorkspace() {
1081 synchronized (mLock) {
1082 if (mLoaderTask != null) {
1083 return mLoaderTask.isLoadingWorkspace();
1084 }
1085 }
1086 return false;
1087 }
1088
Joe Onorato36115782010-06-17 13:28:48 -04001089 /**
1090 * Runnable for the thread that loads the contents of the launcher:
1091 * - workspace icons
1092 * - widgets
1093 * - all apps icons
1094 */
1095 private class LoaderTask implements Runnable {
1096 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001097 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001098 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001099 private boolean mStopped;
1100 private boolean mLoadAndBindStepFinished;
Adam Cohene25af792013-06-06 23:08:25 -07001101 private boolean mIsUpgradePath;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001102
Winson Chungc3eecff2011-07-11 17:44:15 -07001103 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001104
1105 LoaderTask(Context context, boolean isLaunching) {
1106 mContext = context;
1107 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001108 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001109 }
1110
Joe Onorato36115782010-06-17 13:28:48 -04001111 boolean isLaunching() {
1112 return mIsLaunching;
1113 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114
Winson Chung36a62fe2012-05-06 18:04:42 -07001115 boolean isLoadingWorkspace() {
1116 return mIsLoadingAndBindingWorkspace;
1117 }
1118
Joe Onorato36115782010-06-17 13:28:48 -04001119 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001120 mIsLoadingAndBindingWorkspace = true;
1121
Joe Onorato36115782010-06-17 13:28:48 -04001122 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001123 if (DEBUG_LOADERS) {
1124 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001126
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001127 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -04001128 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001129 synchronized (LoaderTask.this) {
1130 if (mStopped) {
1131 return;
1132 }
1133 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 }
1135 }
1136
Joe Onorato36115782010-06-17 13:28:48 -04001137 // Bind the workspace
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001138 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001139 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001140
Joe Onorato36115782010-06-17 13:28:48 -04001141 private void waitForIdle() {
1142 // Wait until the either we're stopped or the other threads are done.
1143 // This way we don't start loading all apps until the workspace has settled
1144 // down.
1145 synchronized (LoaderTask.this) {
1146 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001147
Joe Onorato36115782010-06-17 13:28:48 -04001148 mHandler.postIdle(new Runnable() {
1149 public void run() {
1150 synchronized (LoaderTask.this) {
1151 mLoadAndBindStepFinished = true;
1152 if (DEBUG_LOADERS) {
1153 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001154 }
Joe Onorato36115782010-06-17 13:28:48 -04001155 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001156 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001157 }
Joe Onorato36115782010-06-17 13:28:48 -04001158 });
1159
Michael Jurkac7700af2013-05-14 20:17:58 +02001160 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001161 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001162 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1163 // wait no longer than 1sec at a time
1164 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001165 } catch (InterruptedException ex) {
1166 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001167 }
1168 }
Joe Onorato36115782010-06-17 13:28:48 -04001169 if (DEBUG_LOADERS) {
1170 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001171 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001172 + "ms for previous step to finish binding");
1173 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001174 }
Joe Onorato36115782010-06-17 13:28:48 -04001175 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001176
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001177 void runBindSynchronousPage(int synchronousBindPage) {
1178 if (synchronousBindPage < 0) {
1179 // Ensure that we have a valid page index to load synchronously
1180 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1181 "valid page index");
1182 }
1183 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1184 // Ensure that we don't try and bind a specified page when the pages have not been
1185 // loaded already (we should load everything asynchronously in that case)
1186 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1187 }
1188 synchronized (mLock) {
1189 if (mIsLoaderTaskRunning) {
1190 // Ensure that we are never running the background loading at this point since
1191 // we also touch the background collections
1192 throw new RuntimeException("Error! Background loading is already running");
1193 }
1194 }
1195
1196 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1197 // data structures, we can't allow any other thread to touch that data, but because
1198 // this call is synchronous, we can get away with not locking).
1199
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001200 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001201 // operations from the previous activity. We need to ensure that all queued operations
1202 // are executed before any synchronous binding work is done.
1203 mHandler.flush();
1204
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205 // Divide the set of loaded items into those that we are binding synchronously, and
1206 // everything else that is to be bound normally (asynchronously).
1207 bindWorkspace(synchronousBindPage);
1208 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1209 // arise from that.
1210 onlyBindAllApps();
1211 }
1212
Joe Onorato36115782010-06-17 13:28:48 -04001213 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001214 synchronized (mLock) {
1215 mIsLoaderTaskRunning = true;
1216 }
Joe Onorato36115782010-06-17 13:28:48 -04001217 // Optimize for end-user experience: if the Launcher is up and // running with the
1218 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1219 // workspace first (default).
1220 final Callbacks cbk = mCallbacks.get();
1221 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -04001222
Joe Onorato36115782010-06-17 13:28:48 -04001223 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001224 // Elevate priority when Home launches for the first time to avoid
1225 // starving at boot time. Staring at a blank home is not cool.
1226 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001227 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1228 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001229 android.os.Process.setThreadPriority(mIsLaunching
1230 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1231 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001232 if (loadWorkspaceFirst) {
1233 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1234 loadAndBindWorkspace();
1235 } else {
1236 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001237 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001238 }
1239
Joe Onorato36115782010-06-17 13:28:48 -04001240 if (mStopped) {
1241 break keep_running;
1242 }
1243
1244 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1245 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001246 synchronized (mLock) {
1247 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001248 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001249 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1250 }
1251 }
Joe Onorato36115782010-06-17 13:28:48 -04001252 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001253
1254 // second step
1255 if (loadWorkspaceFirst) {
1256 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001257 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001258 } else {
1259 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
1260 loadAndBindWorkspace();
1261 }
Winson Chung7ed37742011-09-08 15:45:51 -07001262
1263 // Restore the default thread priority after we are done loading items
1264 synchronized (mLock) {
1265 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1266 }
Joe Onorato36115782010-06-17 13:28:48 -04001267 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268
Winson Chungaac01e12011-08-17 10:37:13 -07001269 // Update the saved icons if necessary
1270 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001271 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001272 for (Object key : sBgDbIconCache.keySet()) {
1273 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1274 }
1275 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001276 }
Winson Chungaac01e12011-08-17 10:37:13 -07001277
Joe Onorato36115782010-06-17 13:28:48 -04001278 // Clear out this reference, otherwise we end up holding it until all of the
1279 // callback runnables are done.
1280 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001281
Joe Onorato36115782010-06-17 13:28:48 -04001282 synchronized (mLock) {
1283 // If we are still the last one to be scheduled, remove ourselves.
1284 if (mLoaderTask == this) {
1285 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001286 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001287 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001288 }
Joe Onorato36115782010-06-17 13:28:48 -04001289 }
1290
1291 public void stopLocked() {
1292 synchronized (LoaderTask.this) {
1293 mStopped = true;
1294 this.notify();
1295 }
1296 }
1297
1298 /**
1299 * Gets the callbacks object. If we've been stopped, or if the launcher object
1300 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1301 * object that was around when the deferred message was scheduled, and if there's
1302 * a new Callbacks object around then also return null. This will save us from
1303 * calling onto it with data that will be ignored.
1304 */
1305 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1306 synchronized (mLock) {
1307 if (mStopped) {
1308 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001309 }
Joe Onorato36115782010-06-17 13:28:48 -04001310
1311 if (mCallbacks == null) {
1312 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001313 }
Joe Onorato36115782010-06-17 13:28:48 -04001314
1315 final Callbacks callbacks = mCallbacks.get();
1316 if (callbacks != oldCallbacks) {
1317 return null;
1318 }
1319 if (callbacks == null) {
1320 Log.w(TAG, "no mCallbacks");
1321 return null;
1322 }
1323
1324 return callbacks;
1325 }
1326 }
1327
1328 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001329 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
1330 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001331 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001332 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1333 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1334 [(int) item.screenId][0] != null) {
1335 Log.e(TAG, "Error loading shortcut into hotseat " + item
1336 + " into position (" + item.screenId + ":" + item.cellX + ","
1337 + item.cellY + ") occupied by "
1338 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1339 [(int) item.screenId][0]);
1340 return false;
1341 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001342 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001343 ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1344 items[(int) item.screenId][0] = item;
1345 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001346 return true;
1347 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001348 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1349 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001350 return true;
1351 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001352
Adam Cohendcd297f2013-06-18 13:13:40 -07001353 if (!occupied.containsKey(item.screenId)) {
1354 ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1355 occupied.put(item.screenId, items);
1356 }
1357
1358 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001359 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001360 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1361 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001362 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001363 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001364 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001365 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001366 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001367 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001368 return false;
1369 }
1370 }
1371 }
1372 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1373 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001374 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001375 }
1376 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001377
Joe Onorato36115782010-06-17 13:28:48 -04001378 return true;
1379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380
Joe Onorato36115782010-06-17 13:28:48 -04001381 private void loadWorkspace() {
1382 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383
Joe Onorato36115782010-06-17 13:28:48 -04001384 final Context context = mContext;
1385 final ContentResolver contentResolver = context.getContentResolver();
1386 final PackageManager manager = context.getPackageManager();
1387 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1388 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001389
Michael Jurkab85f8a42012-04-25 15:48:32 -07001390 // Make sure the default workspace is loaded, if needed
Adam Cohene25af792013-06-06 23:08:25 -07001391 boolean loadOldDb = mApp.getLauncherProvider().shouldLoadOldDb();
1392 Uri contentUri = loadOldDb ? LauncherSettings.Favorites.OLD_CONTENT_URI :
1393 LauncherSettings.Favorites.CONTENT_URI;
1394
1395 mIsUpgradePath = loadOldDb;
Michael Jurkab85f8a42012-04-25 15:48:32 -07001396
Winson Chung2abf94d2012-07-18 18:16:38 -07001397 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001398 sBgWorkspaceItems.clear();
1399 sBgAppWidgets.clear();
1400 sBgFolders.clear();
1401 sBgItemsIdMap.clear();
1402 sBgDbIconCache.clear();
Adam Cohendcd297f2013-06-18 13:13:40 -07001403 sBgWorkspaceScreens.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001404
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001405 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001406
Adam Cohene25af792013-06-06 23:08:25 -07001407 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001408
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409 // +1 for the hotseat (it can be larger than the workspace)
1410 // Load workspace in reverse order to ensure that latest items are loaded first (and
1411 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001412 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001413
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001414 try {
1415 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1416 final int intentIndex = c.getColumnIndexOrThrow
1417 (LauncherSettings.Favorites.INTENT);
1418 final int titleIndex = c.getColumnIndexOrThrow
1419 (LauncherSettings.Favorites.TITLE);
1420 final int iconTypeIndex = c.getColumnIndexOrThrow(
1421 LauncherSettings.Favorites.ICON_TYPE);
1422 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1423 final int iconPackageIndex = c.getColumnIndexOrThrow(
1424 LauncherSettings.Favorites.ICON_PACKAGE);
1425 final int iconResourceIndex = c.getColumnIndexOrThrow(
1426 LauncherSettings.Favorites.ICON_RESOURCE);
1427 final int containerIndex = c.getColumnIndexOrThrow(
1428 LauncherSettings.Favorites.CONTAINER);
1429 final int itemTypeIndex = c.getColumnIndexOrThrow(
1430 LauncherSettings.Favorites.ITEM_TYPE);
1431 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1432 LauncherSettings.Favorites.APPWIDGET_ID);
1433 final int screenIndex = c.getColumnIndexOrThrow(
1434 LauncherSettings.Favorites.SCREEN);
1435 final int cellXIndex = c.getColumnIndexOrThrow
1436 (LauncherSettings.Favorites.CELLX);
1437 final int cellYIndex = c.getColumnIndexOrThrow
1438 (LauncherSettings.Favorites.CELLY);
1439 final int spanXIndex = c.getColumnIndexOrThrow
1440 (LauncherSettings.Favorites.SPANX);
1441 final int spanYIndex = c.getColumnIndexOrThrow(
1442 LauncherSettings.Favorites.SPANY);
1443 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1444 //final int displayModeIndex = c.getColumnIndexOrThrow(
1445 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447 ShortcutInfo info;
1448 String intentDescription;
1449 LauncherAppWidgetInfo appWidgetInfo;
1450 int container;
1451 long id;
1452 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 while (!mStopped && c.moveToNext()) {
1455 try {
1456 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001457
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001458 switch (itemType) {
1459 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1460 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1461 intentDescription = c.getString(intentIndex);
1462 try {
1463 intent = Intent.parseUri(intentDescription, 0);
1464 } catch (URISyntaxException e) {
1465 continue;
1466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1469 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1470 titleIndex, mLabelCache);
1471 } else {
1472 info = getShortcutInfo(c, context, iconTypeIndex,
1473 iconPackageIndex, iconResourceIndex, iconIndex,
1474 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001475
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001476 // App shortcuts that used to be automatically added to Launcher
1477 // didn't always have the correct intent flags set, so do that
1478 // here
1479 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001480 intent.getCategories() != null &&
1481 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001482 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001483 intent.addFlags(
1484 Intent.FLAG_ACTIVITY_NEW_TASK |
1485 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1486 }
Michael Jurka96879562012-03-22 05:54:33 -07001487 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001489 if (info != null) {
1490 info.intent = intent;
1491 info.id = c.getLong(idIndex);
1492 container = c.getInt(containerIndex);
1493 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001494 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001495 info.cellX = c.getInt(cellXIndex);
1496 info.cellY = c.getInt(cellYIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001497 // check & update map of what's occupied
1498 if (!checkItemPlacement(occupied, info)) {
1499 break;
1500 }
1501
1502 switch (container) {
1503 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1504 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1505 sBgWorkspaceItems.add(info);
1506 break;
1507 default:
1508 // Item is in a user folder
1509 FolderInfo folderInfo =
1510 findOrMakeFolder(sBgFolders, container);
1511 folderInfo.add(info);
1512 break;
1513 }
Adam Cohene25af792013-06-06 23:08:25 -07001514 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1515 loadOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 info.screenId = permuteScreens(info.screenId);
Adam Cohene25af792013-06-06 23:08:25 -07001517 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001518 sBgItemsIdMap.put(info.id, info);
1519
1520 // now that we've loaded everthing re-save it with the
1521 // icon in case it disappears somehow.
1522 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
1523 } else {
1524 // Failed to load the shortcut, probably because the
1525 // activity manager couldn't resolve it (maybe the app
1526 // was uninstalled), or the db row was somehow screwed up.
1527 // Delete it.
1528 id = c.getLong(idIndex);
1529 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1530 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1531 id, false), null, null);
1532 }
1533 break;
1534
1535 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1536 id = c.getLong(idIndex);
1537 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1538
1539 folderInfo.title = c.getString(titleIndex);
1540 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001541 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001542 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001543 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001544 folderInfo.cellX = c.getInt(cellXIndex);
1545 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001546
Daniel Sandler8802e962010-05-26 16:28:16 -04001547 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001548 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001549 break;
1550 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001551 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001552 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1553 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1554 sBgWorkspaceItems.add(folderInfo);
1555 break;
Joe Onorato36115782010-06-17 13:28:48 -04001556 }
Adam Cohene25af792013-06-06 23:08:25 -07001557 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1558 loadOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 folderInfo.screenId = permuteScreens(folderInfo.screenId);
Adam Cohene25af792013-06-06 23:08:25 -07001560 }
Joe Onorato17a89222011-02-08 17:26:11 -08001561
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001562 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1563 sBgFolders.put(folderInfo.id, folderInfo);
1564 break;
1565
1566 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1567 // Read all Launcher-specific widget details
1568 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001569 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001570
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001571 final AppWidgetProviderInfo provider =
1572 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001573
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001574 if (!isSafeMode && (provider == null || provider.provider == null ||
1575 provider.provider.getPackageName() == null)) {
1576 String log = "Deleting widget that isn't installed anymore: id="
1577 + id + " appWidgetId=" + appWidgetId;
1578 Log.e(TAG, log);
1579 Launcher.sDumpLogs.add(log);
1580 itemsToRemove.add(id);
1581 } else {
1582 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1583 provider.provider);
1584 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001586 appWidgetInfo.cellX = c.getInt(cellXIndex);
1587 appWidgetInfo.cellY = c.getInt(cellYIndex);
1588 appWidgetInfo.spanX = c.getInt(spanXIndex);
1589 appWidgetInfo.spanY = c.getInt(spanYIndex);
1590 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1591 appWidgetInfo.minSpanX = minSpan[0];
1592 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001593
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001594 container = c.getInt(containerIndex);
1595 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1596 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1597 Log.e(TAG, "Widget found where container != " +
1598 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1599 continue;
1600 }
Adam Cohene25af792013-06-06 23:08:25 -07001601 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1602 loadOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001603 appWidgetInfo.screenId =
1604 permuteScreens(appWidgetInfo.screenId);
Adam Cohene25af792013-06-06 23:08:25 -07001605 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001606
Adam Cohene25af792013-06-06 23:08:25 -07001607 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001608 // check & update map of what's occupied
1609 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1610 break;
1611 }
1612 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1613 sBgAppWidgets.add(appWidgetInfo);
1614 }
Joe Onorato36115782010-06-17 13:28:48 -04001615 break;
1616 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001617 } catch (Exception e) {
1618 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001619 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 }
1621 } finally {
1622 c.close();
1623 }
1624
1625 if (itemsToRemove.size() > 0) {
1626 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1627 LauncherSettings.Favorites.CONTENT_URI);
1628 // Remove dead items
1629 for (long id : itemsToRemove) {
1630 if (DEBUG_LOADERS) {
1631 Log.d(TAG, "Removed id = " + id);
1632 }
1633 // Don't notify content observers
1634 try {
1635 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1636 null, null);
1637 } catch (RemoteException e) {
1638 Log.w(TAG, "Could not remove id = " + id);
1639 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001640 }
1641 }
1642
Adam Cohendcd297f2013-06-18 13:13:40 -07001643 if (loadOldDb) {
1644 long maxScreenId = 0;
1645 // If we're importing we use the old screen order.
1646 for (ItemInfo item: sBgItemsIdMap.values()) {
1647 long screenId = item.screenId;
1648 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1649 !sBgWorkspaceScreens.contains(screenId)) {
1650 sBgWorkspaceScreens.add(screenId);
1651 if (screenId > maxScreenId) {
1652 maxScreenId = screenId;
1653 }
1654 }
1655 }
1656 Collections.sort(sBgWorkspaceScreens);
1657 mApp.getLauncherProvider().updateMaxScreenId(maxScreenId);
1658 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1659 } else {
1660 Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1661 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1662 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1663
1664 try {
1665 final int idIndex = sc.getColumnIndexOrThrow(
1666 LauncherSettings.WorkspaceScreens._ID);
1667 final int rankIndex = sc.getColumnIndexOrThrow(
1668 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1669 while (sc.moveToNext()) {
1670 try {
1671 long screenId = sc.getLong(idIndex);
1672 int rank = sc.getInt(rankIndex);
1673
1674 orderedScreens.put(rank, screenId);
1675 } catch (Exception e) {
1676 Log.w(TAG, "Desktop items loading interrupted:", e);
1677 }
1678 }
1679 } finally {
1680 sc.close();
1681 }
1682
1683 Iterator<Integer> iter = orderedScreens.keySet().iterator();
1684 while (iter.hasNext()) {
1685 sBgWorkspaceScreens.add(orderedScreens.get(iter.next()));
1686 }
1687
1688 // Remove any empty screens
1689 ArrayList<Long> unusedScreens = new ArrayList<Long>();
1690 unusedScreens.addAll(sBgWorkspaceScreens);
1691
1692 for (ItemInfo item: sBgItemsIdMap.values()) {
1693 long screenId = item.screenId;
1694
1695 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1696 unusedScreens.contains(screenId)) {
1697 unusedScreens.remove(screenId);
1698 }
1699 }
1700
1701 // If there are any empty screens remove them, and update.
1702 if (unusedScreens.size() != 0) {
1703 sBgWorkspaceScreens.removeAll(unusedScreens);
1704 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1705 }
1706 }
1707
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 if (DEBUG_LOADERS) {
1709 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1710 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001711 Iterator<Long> iter = occupied.keySet().iterator();
1712 int nScreens = occupied.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 for (int y = 0; y < mCellCountY; y++) {
1714 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001715
1716 for (int s = 0; s < nScreens; s++) {
1717 long screenId = iter.next();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 if (s > 0) {
1719 line += " | ";
1720 }
1721 for (int x = 0; x < mCellCountX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001722 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001723 }
Joe Onorato36115782010-06-17 13:28:48 -04001724 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727 }
Joe Onorato36115782010-06-17 13:28:48 -04001728 }
1729 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001730
Adam Cohene25af792013-06-06 23:08:25 -07001731 // We rearrange the screens from the old launcher
1732 // 12345 -> 34512
Adam Cohendcd297f2013-06-18 13:13:40 -07001733 private long permuteScreens(long screen) {
Adam Cohene25af792013-06-06 23:08:25 -07001734 if (screen >= 2) {
1735 return screen - 2;
1736 } else {
1737 return screen + 3;
1738 }
1739 }
1740
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001741 /** Filters the set of items who are directly or indirectly (via another container) on the
1742 * specified screen. */
1743 private void filterCurrentWorkspaceItems(int currentScreen,
1744 ArrayList<ItemInfo> allWorkspaceItems,
1745 ArrayList<ItemInfo> currentScreenItems,
1746 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001747 // Purge any null ItemInfos
1748 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1749 while (iter.hasNext()) {
1750 ItemInfo i = iter.next();
1751 if (i == null) {
1752 iter.remove();
1753 }
1754 }
1755
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001756 // If we aren't filtering on a screen, then the set of items to load is the full set of
1757 // items given.
1758 if (currentScreen < 0) {
1759 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001760 }
1761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 // Order the set of items by their containers first, this allows use to walk through the
1763 // list sequentially, build up a list of containers that are in the specified screen,
1764 // as well as all items in those containers.
1765 Set<Long> itemsOnScreen = new HashSet<Long>();
1766 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1767 @Override
1768 public int compare(ItemInfo lhs, ItemInfo rhs) {
1769 return (int) (lhs.container - rhs.container);
1770 }
1771 });
1772 for (ItemInfo info : allWorkspaceItems) {
1773 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001774 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 currentScreenItems.add(info);
1776 itemsOnScreen.add(info.id);
1777 } else {
1778 otherScreenItems.add(info);
1779 }
1780 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1781 currentScreenItems.add(info);
1782 itemsOnScreen.add(info.id);
1783 } else {
1784 if (itemsOnScreen.contains(info.container)) {
1785 currentScreenItems.add(info);
1786 itemsOnScreen.add(info.id);
1787 } else {
1788 otherScreenItems.add(info);
1789 }
1790 }
1791 }
1792 }
1793
1794 /** Filters the set of widgets which are on the specified screen. */
1795 private void filterCurrentAppWidgets(int currentScreen,
1796 ArrayList<LauncherAppWidgetInfo> appWidgets,
1797 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1798 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1799 // If we aren't filtering on a screen, then the set of items to load is the full set of
1800 // widgets given.
1801 if (currentScreen < 0) {
1802 currentScreenWidgets.addAll(appWidgets);
1803 }
1804
1805 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001806 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07001808 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 currentScreenWidgets.add(widget);
1810 } else {
1811 otherScreenWidgets.add(widget);
1812 }
1813 }
1814 }
1815
1816 /** Filters the set of folders which are on the specified screen. */
1817 private void filterCurrentFolders(int currentScreen,
1818 HashMap<Long, ItemInfo> itemsIdMap,
1819 HashMap<Long, FolderInfo> folders,
1820 HashMap<Long, FolderInfo> currentScreenFolders,
1821 HashMap<Long, FolderInfo> otherScreenFolders) {
1822 // If we aren't filtering on a screen, then the set of items to load is the full set of
1823 // widgets given.
1824 if (currentScreen < 0) {
1825 currentScreenFolders.putAll(folders);
1826 }
1827
1828 for (long id : folders.keySet()) {
1829 ItemInfo info = itemsIdMap.get(id);
1830 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07001831 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07001833 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 currentScreenFolders.put(id, folder);
1835 } else {
1836 otherScreenFolders.put(id, folder);
1837 }
1838 }
1839 }
1840
1841 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1842 * right) */
1843 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1844 // XXX: review this
1845 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001846 @Override
1847 public int compare(ItemInfo lhs, ItemInfo rhs) {
1848 int cellCountX = LauncherModel.getCellCountX();
1849 int cellCountY = LauncherModel.getCellCountY();
1850 int screenOffset = cellCountX * cellCountY;
1851 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001852 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07001853 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07001854 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07001855 rhs.cellY * cellCountX + rhs.cellX);
1856 return (int) (lr - rr);
1857 }
1858 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001860
Adam Cohendcd297f2013-06-18 13:13:40 -07001861 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1862 final ArrayList<Long> orderedScreens) {
1863
1864 final Runnable r = new Runnable() {
1865 @Override
1866 public void run() {
1867 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1868 if (callbacks != null) {
1869 callbacks.bindScreens(orderedScreens);
1870 }
1871 }
1872 };
1873 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
1874 }
1875
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1877 final ArrayList<ItemInfo> workspaceItems,
1878 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1879 final HashMap<Long, FolderInfo> folders,
1880 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07001881
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 final boolean postOnMainThread = (deferredBindRunnables != null);
1883
1884 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001885 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001887 final int start = i;
1888 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 final Runnable r = new Runnable() {
1890 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001891 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001892 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001893 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001894 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001895 }
1896 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001897 };
1898 if (postOnMainThread) {
1899 deferredBindRunnables.add(r);
1900 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001901 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001902 }
Joe Onorato36115782010-06-17 13:28:48 -04001903 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904
1905 // Bind the folders
1906 if (!folders.isEmpty()) {
1907 final Runnable r = new Runnable() {
1908 public void run() {
1909 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1910 if (callbacks != null) {
1911 callbacks.bindFolders(folders);
1912 }
1913 }
1914 };
1915 if (postOnMainThread) {
1916 deferredBindRunnables.add(r);
1917 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001918 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 }
1920 }
1921
1922 // Bind the widgets, one at a time
1923 N = appWidgets.size();
1924 for (int i = 0; i < N; i++) {
1925 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1926 final Runnable r = new Runnable() {
1927 public void run() {
1928 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1929 if (callbacks != null) {
1930 callbacks.bindAppWidget(widget);
1931 }
1932 }
1933 };
1934 if (postOnMainThread) {
1935 deferredBindRunnables.add(r);
1936 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001937 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001938 }
1939 }
1940 }
1941
1942 /**
1943 * Binds all loaded data to actual views on the main thread.
1944 */
1945 private void bindWorkspace(int synchronizeBindPage) {
1946 final long t = SystemClock.uptimeMillis();
1947 Runnable r;
1948
1949 // Don't use these two variables in any of the callback runnables.
1950 // Otherwise we hold a reference to them.
1951 final Callbacks oldCallbacks = mCallbacks.get();
1952 if (oldCallbacks == null) {
1953 // This launcher has exited and nobody bothered to tell us. Just bail.
1954 Log.w(TAG, "LoaderTask running with no launcher");
1955 return;
1956 }
1957
Winson Chung4a2afa32012-07-19 14:53:05 -07001958 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
1959 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001960 oldCallbacks.getCurrentWorkspaceScreen();
1961
1962 // Load all the items that are on the current page first (and in the process, unbind
1963 // all the existing workspace items before we call startBinding() below.
1964 unbindWorkspaceItemsOnMainThread();
1965 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1966 ArrayList<LauncherAppWidgetInfo> appWidgets =
1967 new ArrayList<LauncherAppWidgetInfo>();
1968 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
1969 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001970 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07001971 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 workspaceItems.addAll(sBgWorkspaceItems);
1973 appWidgets.addAll(sBgAppWidgets);
1974 folders.putAll(sBgFolders);
1975 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 }
1978
1979 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
1980 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
1981 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
1982 new ArrayList<LauncherAppWidgetInfo>();
1983 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
1984 new ArrayList<LauncherAppWidgetInfo>();
1985 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
1986 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
1987
1988 // Separate the items that are on the current screen, and all the other remaining items
1989 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
1990 otherWorkspaceItems);
1991 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
1992 otherAppWidgets);
1993 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
1994 otherFolders);
1995 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1996 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1997
1998 // Tell the workspace that we're about to start binding items
1999 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002000 public void run() {
2001 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2002 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002004 }
2005 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 };
Winson Chung81b52252012-08-27 15:34:29 -07002007 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008
Adam Cohendcd297f2013-06-18 13:13:40 -07002009 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2010
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 // Load items on the current page
2012 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2013 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002014 if (isLoadingSynchronously) {
2015 r = new Runnable() {
2016 public void run() {
2017 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2018 if (callbacks != null) {
2019 callbacks.onPageBoundSynchronously(currentScreen);
2020 }
2021 }
2022 };
Winson Chung81b52252012-08-27 15:34:29 -07002023 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002024 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025
Winson Chung4a2afa32012-07-19 14:53:05 -07002026 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2027 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 mDeferredBindRunnables.clear();
2029 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002030 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031
2032 // Tell the workspace that we're done binding items
2033 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002034 public void run() {
2035 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2036 if (callbacks != null) {
Adam Cohene25af792013-06-06 23:08:25 -07002037 callbacks.finishBindingItems(mIsUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002038 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039
Winson Chung98e030b2012-05-07 16:01:11 -07002040 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002041 if (DEBUG_LOADERS) {
2042 Log.d(TAG, "bound workspace in "
2043 + (SystemClock.uptimeMillis()-t) + "ms");
2044 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002045
2046 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002047 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002049 if (isLoadingSynchronously) {
2050 mDeferredBindRunnables.add(r);
2051 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002052 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002053 }
Joe Onorato36115782010-06-17 13:28:48 -04002054 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002055
Joe Onorato36115782010-06-17 13:28:48 -04002056 private void loadAndBindAllApps() {
2057 if (DEBUG_LOADERS) {
2058 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2059 }
2060 if (!mAllAppsLoaded) {
2061 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07002062 synchronized (LoaderTask.this) {
2063 if (mStopped) {
2064 return;
2065 }
2066 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002067 }
Joe Onorato36115782010-06-17 13:28:48 -04002068 } else {
2069 onlyBindAllApps();
2070 }
2071 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002072
Joe Onorato36115782010-06-17 13:28:48 -04002073 private void onlyBindAllApps() {
2074 final Callbacks oldCallbacks = mCallbacks.get();
2075 if (oldCallbacks == null) {
2076 // This launcher has exited and nobody bothered to tell us. Just bail.
2077 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2078 return;
2079 }
2080
2081 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002082 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04002083 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07002084 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002085 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002086 public void run() {
2087 final long t = SystemClock.uptimeMillis();
2088 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2089 if (callbacks != null) {
2090 callbacks.bindAllApplications(list);
2091 }
2092 if (DEBUG_LOADERS) {
2093 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2094 + (SystemClock.uptimeMillis()-t) + "ms");
2095 }
2096 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002097 };
2098 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
2099 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) {
2100 r.run();
2101 } else {
2102 mHandler.post(r);
2103 }
Joe Onorato36115782010-06-17 13:28:48 -04002104 }
2105
2106 private void loadAllAppsByBatch() {
2107 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2108
2109 // Don't use these two variables in any of the callback runnables.
2110 // Otherwise we hold a reference to them.
2111 final Callbacks oldCallbacks = mCallbacks.get();
2112 if (oldCallbacks == null) {
2113 // This launcher has exited and nobody bothered to tell us. Just bail.
2114 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
2115 return;
2116 }
2117
2118 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2119 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2120
2121 final PackageManager packageManager = mContext.getPackageManager();
2122 List<ResolveInfo> apps = null;
2123
2124 int N = Integer.MAX_VALUE;
2125
2126 int startIndex;
2127 int i=0;
2128 int batchSize = -1;
2129 while (i < N && !mStopped) {
2130 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002131 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002132 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2133 apps = packageManager.queryIntentActivities(mainIntent, 0);
2134 if (DEBUG_LOADERS) {
2135 Log.d(TAG, "queryIntentActivities took "
2136 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2137 }
2138 if (apps == null) {
2139 return;
2140 }
2141 N = apps.size();
2142 if (DEBUG_LOADERS) {
2143 Log.d(TAG, "queryIntentActivities got " + N + " apps");
2144 }
2145 if (N == 0) {
2146 // There are no apps?!?
2147 return;
2148 }
2149 if (mBatchSize == 0) {
2150 batchSize = N;
2151 } else {
2152 batchSize = mBatchSize;
2153 }
2154
2155 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2156 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07002157 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04002158 if (DEBUG_LOADERS) {
2159 Log.d(TAG, "sort took "
2160 + (SystemClock.uptimeMillis()-sortTime) + "ms");
2161 }
2162 }
2163
2164 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2165
2166 startIndex = i;
2167 for (int j=0; i<N && j<batchSize; j++) {
2168 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07002169 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07002170 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04002171 i++;
2172 }
2173
2174 final boolean first = i <= batchSize;
2175 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002176 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
2177 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002178
Joe Onoratocc67f472010-06-08 10:54:30 -07002179 mHandler.post(new Runnable() {
2180 public void run() {
2181 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07002182 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002183 if (first) {
2184 callbacks.bindAllApplications(added);
2185 } else {
2186 callbacks.bindAppsAdded(added);
2187 }
2188 if (DEBUG_LOADERS) {
2189 Log.d(TAG, "bound " + added.size() + " apps in "
2190 + (SystemClock.uptimeMillis() - t) + "ms");
2191 }
2192 } else {
2193 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07002194 }
2195 }
2196 });
2197
Daniel Sandlerdca66122010-04-13 16:23:58 -04002198 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04002199 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
2200 + (SystemClock.uptimeMillis()-t2) + "ms");
2201 }
2202
2203 if (mAllAppsLoadDelay > 0 && i < N) {
2204 try {
2205 if (DEBUG_LOADERS) {
2206 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
2207 }
2208 Thread.sleep(mAllAppsLoadDelay);
2209 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 }
2211 }
2212
Joe Onorato36115782010-06-17 13:28:48 -04002213 if (DEBUG_LOADERS) {
2214 Log.d(TAG, "cached all " + N + " apps in "
2215 + (SystemClock.uptimeMillis()-t) + "ms"
2216 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08002217 }
2218 }
2219
2220 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002221 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002222 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2223 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2224 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2225 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2226 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2227 }
Joe Onorato36115782010-06-17 13:28:48 -04002228 }
2229 }
2230
2231 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002232 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002233 }
2234
2235 private class PackageUpdatedTask implements Runnable {
2236 int mOp;
2237 String[] mPackages;
2238
2239 public static final int OP_NONE = 0;
2240 public static final int OP_ADD = 1;
2241 public static final int OP_UPDATE = 2;
2242 public static final int OP_REMOVE = 3; // uninstlled
2243 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2244
2245
2246 public PackageUpdatedTask(int op, String[] packages) {
2247 mOp = op;
2248 mPackages = packages;
2249 }
2250
2251 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002252 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002253
2254 final String[] packages = mPackages;
2255 final int N = packages.length;
2256 switch (mOp) {
2257 case OP_ADD:
2258 for (int i=0; i<N; i++) {
2259 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002260 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002261 }
2262 break;
2263 case OP_UPDATE:
2264 for (int i=0; i<N; i++) {
2265 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002266 mBgAllAppsList.updatePackage(context, packages[i]);
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002267 LauncherAppState app =
2268 LauncherAppState.getInstance();
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002269 WidgetPreviewLoader.removeFromDb(
2270 app.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002271 }
2272 break;
2273 case OP_REMOVE:
2274 case OP_UNAVAILABLE:
2275 for (int i=0; i<N; i++) {
2276 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002277 mBgAllAppsList.removePackage(packages[i]);
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002278 LauncherAppState app =
2279 LauncherAppState.getInstance();
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002280 WidgetPreviewLoader.removeFromDb(
2281 app.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002282 }
2283 break;
2284 }
2285
2286 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002287 ArrayList<ApplicationInfo> modified = null;
Winson Chung83892cc2013-05-01 16:53:33 -07002288 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002289
Adam Cohen487f7dd2012-06-28 18:12:10 -07002290 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002291 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2292 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002293 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002294 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002295 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2296 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002297 }
Winson Chung5d55f332012-07-16 20:45:03 -07002298 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002299 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002300 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002301 }
2302
Joe Onorato36115782010-06-17 13:28:48 -04002303 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2304 if (callbacks == null) {
2305 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2306 return;
2307 }
2308
2309 if (added != null) {
2310 final ArrayList<ApplicationInfo> addedFinal = added;
2311 mHandler.post(new Runnable() {
2312 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002313 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2314 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002315 callbacks.bindAppsAdded(addedFinal);
2316 }
2317 }
2318 });
2319 }
2320 if (modified != null) {
2321 final ArrayList<ApplicationInfo> modifiedFinal = modified;
2322 mHandler.post(new Runnable() {
2323 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002324 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2325 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002326 callbacks.bindAppsUpdated(modifiedFinal);
2327 }
2328 }
2329 });
2330 }
Winson Chung83892cc2013-05-01 16:53:33 -07002331 // If a package has been removed, or an app has been removed as a result of
2332 // an update (for example), make the removed callback.
2333 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
2334 final boolean permanent = (mOp == OP_REMOVE);
2335 final ArrayList<String> removedPackageNames =
2336 new ArrayList<String>(Arrays.asList(packages));
2337
Joe Onorato36115782010-06-17 13:28:48 -04002338 mHandler.post(new Runnable() {
2339 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002340 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2341 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002342 callbacks.bindComponentsRemoved(removedPackageNames,
2343 removedApps, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04002344 }
2345 }
2346 });
Joe Onoratobe386092009-11-17 17:32:16 -08002347 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002348
Michael Jurkac402cd92013-05-20 15:49:32 +02002349 final ArrayList<Object> widgetsAndShortcuts =
2350 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002351 mHandler.post(new Runnable() {
2352 @Override
2353 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002354 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2355 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002356 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002357 }
2358 }
2359 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002360 }
2361 }
2362
Michael Jurkac402cd92013-05-20 15:49:32 +02002363 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2364 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2365 PackageManager packageManager = context.getPackageManager();
2366 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2367 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2368 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2369 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2370 Collections.sort(widgetsAndShortcuts,
2371 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2372 return widgetsAndShortcuts;
2373 }
2374
Joe Onorato9c1289c2009-08-17 11:03:03 -04002375 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002376 * This is called from the code that adds shortcuts from the intent receiver. This
2377 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002378 */
Joe Onorato56d82912010-03-07 14:32:10 -05002379 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002380 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002381 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002382
Joe Onorato56d82912010-03-07 14:32:10 -05002383 /**
2384 * Make an ShortcutInfo object for a shortcut that is an application.
2385 *
2386 * If c is not null, then it will be used to fill in missing data like the title and icon.
2387 */
2388 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002389 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002390 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002391 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002392
2393 ComponentName componentName = intent.getComponent();
2394 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002395 return null;
2396 }
2397
Adam Cohen00fcb492011-11-02 21:53:47 -07002398 try {
2399 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2400 if (!pi.applicationInfo.enabled) {
2401 // If we return null here, the corresponding item will be removed from the launcher
2402 // db and will not appear in the workspace.
2403 return null;
2404 }
2405 } catch (NameNotFoundException e) {
2406 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
2407 }
2408
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002409 // TODO: See if the PackageManager knows about this case. If it doesn't
2410 // then return null & delete this.
2411
Joe Onorato56d82912010-03-07 14:32:10 -05002412 // the resource -- This may implicitly give us back the fallback icon,
2413 // but don't worry about that. All we're doing with usingFallbackIcon is
2414 // to avoid saving lots of copies of that in the database, and most apps
2415 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002416
2417 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2418 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2419 // via resolveActivity().
2420 ResolveInfo resolveInfo = null;
2421 ComponentName oldComponent = intent.getComponent();
2422 Intent newIntent = new Intent(intent.getAction(), null);
2423 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2424 newIntent.setPackage(oldComponent.getPackageName());
2425 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2426 for (ResolveInfo i : infos) {
2427 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2428 i.activityInfo.name);
2429 if (cn.equals(oldComponent)) {
2430 resolveInfo = i;
2431 }
2432 }
2433 if (resolveInfo == null) {
2434 resolveInfo = manager.resolveActivity(intent, 0);
2435 }
Joe Onorato56d82912010-03-07 14:32:10 -05002436 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002437 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002438 }
Joe Onorato56d82912010-03-07 14:32:10 -05002439 // the db
2440 if (icon == null) {
2441 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002442 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002443 }
2444 }
2445 // the fallback icon
2446 if (icon == null) {
2447 icon = getFallbackIcon();
2448 info.usingFallbackIcon = true;
2449 }
2450 info.setIcon(icon);
2451
2452 // from the resource
2453 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002454 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2455 if (labelCache != null && labelCache.containsKey(key)) {
2456 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002457 } else {
2458 info.title = resolveInfo.activityInfo.loadLabel(manager);
2459 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002460 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002461 }
2462 }
Joe Onorato56d82912010-03-07 14:32:10 -05002463 }
2464 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002465 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002466 if (c != null) {
2467 info.title = c.getString(titleIndex);
2468 }
2469 }
2470 // fall back to the class name of the activity
2471 if (info.title == null) {
2472 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002473 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002474 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2475 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 /**
Winson Chung8a435102012-08-30 17:16:53 -07002479 * Returns the set of workspace ShortcutInfos with the specified intent.
2480 */
2481 static ArrayList<ItemInfo> getWorkspaceShortcutItemInfosWithIntent(Intent intent) {
2482 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
2483 synchronized (sBgLock) {
2484 for (ItemInfo info : sBgWorkspaceItems) {
2485 if (info instanceof ShortcutInfo) {
2486 ShortcutInfo shortcut = (ShortcutInfo) info;
2487 if (shortcut.intent.toUri(0).equals(intent.toUri(0))) {
2488 items.add(shortcut);
2489 }
2490 }
2491 }
2492 }
2493 return items;
2494 }
2495
2496 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002497 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002499 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002500 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2501 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502
Joe Onorato56d82912010-03-07 14:32:10 -05002503 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002504 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002507 // TODO: If there's an explicit component and we can't install that, delete it.
2508
Joe Onorato56d82912010-03-07 14:32:10 -05002509 info.title = c.getString(titleIndex);
2510
Joe Onorato9c1289c2009-08-17 11:03:03 -04002511 int iconType = c.getInt(iconTypeIndex);
2512 switch (iconType) {
2513 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2514 String packageName = c.getString(iconPackageIndex);
2515 String resourceName = c.getString(iconResourceIndex);
2516 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002517 info.customIcon = false;
2518 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002520 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002521 if (resources != null) {
2522 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002523 icon = Utilities.createIconBitmap(
2524 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002525 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002526 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002527 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 }
Joe Onorato56d82912010-03-07 14:32:10 -05002529 // the db
2530 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002531 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002532 }
2533 // the fallback icon
2534 if (icon == null) {
2535 icon = getFallbackIcon();
2536 info.usingFallbackIcon = true;
2537 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002538 break;
2539 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002540 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002541 if (icon == null) {
2542 icon = getFallbackIcon();
2543 info.customIcon = false;
2544 info.usingFallbackIcon = true;
2545 } else {
2546 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002548 break;
2549 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002550 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002551 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002552 info.customIcon = false;
2553 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002555 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002556 return info;
2557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558
Michael Jurka931dc972011-08-05 15:08:15 -07002559 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002560 @SuppressWarnings("all") // suppress dead code warning
2561 final boolean debug = false;
2562 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002563 Log.d(TAG, "getIconFromCursor app="
2564 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2565 }
2566 byte[] data = c.getBlob(iconIndex);
2567 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002568 return Utilities.createIconBitmap(
2569 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002570 } catch (Exception e) {
2571 return null;
2572 }
2573 }
2574
Winson Chung3d503fb2011-07-13 17:25:49 -07002575 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2576 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002577 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002578 if (info == null) {
2579 return null;
2580 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002581 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002582
2583 return info;
2584 }
2585
Winson Chunga9abd0e2010-10-27 17:18:37 -07002586 /**
Winson Chung55cef262010-10-28 14:14:18 -07002587 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2588 */
2589 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2590 ComponentName component) {
2591 List<AppWidgetProviderInfo> widgets =
2592 AppWidgetManager.getInstance(context).getInstalledProviders();
2593 for (AppWidgetProviderInfo info : widgets) {
2594 if (info.provider.equals(component)) {
2595 return info;
2596 }
2597 }
2598 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002599 }
2600
Winson Chung68846fd2010-10-29 11:00:27 -07002601 /**
2602 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2603 */
2604 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2605 final PackageManager packageManager = context.getPackageManager();
2606 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2607 new ArrayList<WidgetMimeTypeHandlerData>();
2608
2609 final Intent supportsIntent =
2610 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2611 supportsIntent.setType(mimeType);
2612
2613 // Create a set of widget configuration components that we can test against
2614 final List<AppWidgetProviderInfo> widgets =
2615 AppWidgetManager.getInstance(context).getInstalledProviders();
2616 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2617 new HashMap<ComponentName, AppWidgetProviderInfo>();
2618 for (AppWidgetProviderInfo info : widgets) {
2619 configurationComponentToWidget.put(info.configure, info);
2620 }
2621
2622 // Run through each of the intents that can handle this type of clip data, and cross
2623 // reference them with the components that are actual configuration components
2624 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2625 PackageManager.MATCH_DEFAULT_ONLY);
2626 for (ResolveInfo info : activities) {
2627 final ActivityInfo activityInfo = info.activityInfo;
2628 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2629 activityInfo.name);
2630 if (configurationComponentToWidget.containsKey(infoComponent)) {
2631 supportedConfigurationActivities.add(
2632 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2633 configurationComponentToWidget.get(infoComponent)));
2634 }
2635 }
2636 return supportedConfigurationActivities;
2637 }
2638
Winson Chunga9abd0e2010-10-27 17:18:37 -07002639 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002640 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2641 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2642 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2643
Adam Cohend9198822011-11-22 16:42:47 -08002644 if (intent == null) {
2645 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2646 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2647 return null;
2648 }
2649
Joe Onorato0589f0f2010-02-08 13:44:00 -08002650 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002651 boolean customIcon = false;
2652 ShortcutIconResource iconResource = null;
2653
2654 if (bitmap != null && bitmap instanceof Bitmap) {
2655 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002656 customIcon = true;
2657 } else {
2658 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2659 if (extra != null && extra instanceof ShortcutIconResource) {
2660 try {
2661 iconResource = (ShortcutIconResource) extra;
2662 final PackageManager packageManager = context.getPackageManager();
2663 Resources resources = packageManager.getResourcesForApplication(
2664 iconResource.packageName);
2665 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002666 icon = Utilities.createIconBitmap(
2667 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002668 } catch (Exception e) {
2669 Log.w(TAG, "Could not load shortcut icon: " + extra);
2670 }
2671 }
2672 }
2673
Michael Jurkac9d95c52011-08-29 14:03:34 -07002674 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002675
2676 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002677 if (fallbackIcon != null) {
2678 icon = fallbackIcon;
2679 } else {
2680 icon = getFallbackIcon();
2681 info.usingFallbackIcon = true;
2682 }
Joe Onorato56d82912010-03-07 14:32:10 -05002683 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002684 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002685
Joe Onorato0589f0f2010-02-08 13:44:00 -08002686 info.title = name;
2687 info.intent = intent;
2688 info.customIcon = customIcon;
2689 info.iconResource = iconResource;
2690
2691 return info;
2692 }
2693
Winson Chungaac01e12011-08-17 10:37:13 -07002694 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2695 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002696 // If apps can't be on SD, don't even bother.
2697 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002698 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002699 }
Joe Onorato56d82912010-03-07 14:32:10 -05002700 // If this icon doesn't have a custom icon, check to see
2701 // what's stored in the DB, and if it doesn't match what
2702 // we're going to show, store what we are going to show back
2703 // into the DB. We do this so when we're loading, if the
2704 // package manager can't find an icon (for example because
2705 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002706 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002707 cache.put(info, c.getBlob(iconIndex));
2708 return true;
2709 }
2710 return false;
2711 }
2712 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2713 boolean needSave = false;
2714 try {
2715 if (data != null) {
2716 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2717 Bitmap loaded = info.getIcon(mIconCache);
2718 needSave = !saved.sameAs(loaded);
2719 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002720 needSave = true;
2721 }
Winson Chungaac01e12011-08-17 10:37:13 -07002722 } catch (Exception e) {
2723 needSave = true;
2724 }
2725 if (needSave) {
2726 Log.d(TAG, "going to save icon bitmap for info=" + info);
2727 // This is slower than is ideal, but this only happens once
2728 // or when the app is updated with a new icon.
2729 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002730 }
2731 }
2732
Joe Onorato9c1289c2009-08-17 11:03:03 -04002733 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002734 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002735 * or make a new one.
2736 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002737 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002738 // See if a placeholder was created for us already
2739 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002740 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002742 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002743 folders.put(id, folderInfo);
2744 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002745 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002746 }
2747
Winson Chung11904872012-09-17 16:58:46 -07002748 public static final Comparator<ApplicationInfo> getAppNameComparator() {
2749 final Collator collator = Collator.getInstance();
2750 return new Comparator<ApplicationInfo>() {
2751 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2752 int result = collator.compare(a.title.toString(), b.title.toString());
2753 if (result == 0) {
2754 result = a.componentName.compareTo(b.componentName);
2755 }
2756 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07002757 }
Winson Chung11904872012-09-17 16:58:46 -07002758 };
2759 }
Winson Chung78403fe2011-01-21 15:38:02 -08002760 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2761 = new Comparator<ApplicationInfo>() {
2762 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2763 if (a.firstInstallTime < b.firstInstallTime) return 1;
2764 if (a.firstInstallTime > b.firstInstallTime) return -1;
2765 return 0;
2766 }
2767 };
Winson Chung11904872012-09-17 16:58:46 -07002768 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
2769 final Collator collator = Collator.getInstance();
2770 return new Comparator<AppWidgetProviderInfo>() {
2771 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2772 return collator.compare(a.label.toString(), b.label.toString());
2773 }
2774 };
2775 }
Winson Chung5308f242011-08-18 12:12:41 -07002776 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2777 if (info.activityInfo != null) {
2778 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2779 } else {
2780 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2781 }
2782 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002783 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07002784 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07002785 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002786 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002787 ShortcutNameComparator(PackageManager pm) {
2788 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002789 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07002790 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07002791 }
2792 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2793 mPackageManager = pm;
2794 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07002795 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07002796 }
2797 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002798 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002799 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2800 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2801 if (mLabelCache.containsKey(keyA)) {
2802 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002803 } else {
2804 labelA = a.loadLabel(mPackageManager).toString();
2805
Winson Chung5308f242011-08-18 12:12:41 -07002806 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002807 }
Winson Chung5308f242011-08-18 12:12:41 -07002808 if (mLabelCache.containsKey(keyB)) {
2809 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002810 } else {
2811 labelB = b.loadLabel(mPackageManager).toString();
2812
Winson Chung5308f242011-08-18 12:12:41 -07002813 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002814 }
Winson Chung11904872012-09-17 16:58:46 -07002815 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07002816 }
2817 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002818 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07002819 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07002820 private PackageManager mPackageManager;
2821 private HashMap<Object, String> mLabelCache;
2822 WidgetAndShortcutNameComparator(PackageManager pm) {
2823 mPackageManager = pm;
2824 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07002825 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07002826 }
2827 public final int compare(Object a, Object b) {
2828 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002829 if (mLabelCache.containsKey(a)) {
2830 labelA = mLabelCache.get(a);
2831 } else {
2832 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002833 ((AppWidgetProviderInfo) a).label :
2834 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002835 mLabelCache.put(a, labelA);
2836 }
2837 if (mLabelCache.containsKey(b)) {
2838 labelB = mLabelCache.get(b);
2839 } else {
2840 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002841 ((AppWidgetProviderInfo) b).label :
2842 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002843 mLabelCache.put(b, labelB);
2844 }
Winson Chung11904872012-09-17 16:58:46 -07002845 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07002846 }
2847 };
Joe Onoratobe386092009-11-17 17:32:16 -08002848
2849 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002850 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002851 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2852 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2853 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2854 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002855 if (mLoaderTask != null) {
2856 mLoaderTask.dumpState();
2857 } else {
2858 Log.d(TAG, "mLoaderTask=null");
2859 }
Joe Onoratobe386092009-11-17 17:32:16 -08002860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861}