blob: 7927593998c9c9b0a0df050082a399039470dd9c [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
26import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070031import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070035import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Resources;
37import android.database.Cursor;
38import android.graphics.Bitmap;
39import android.graphics.BitmapFactory;
40import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080041import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040042import android.os.Handler;
43import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Winson Chung68846fd2010-10-29 11:00:27 -070050import com.android.launcher.R;
51import com.android.launcher2.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;
57import java.util.Collections;
58import java.util.Comparator;
59import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070060import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070061import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070062import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070063import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065/**
66 * Maintains in-memory state of the Launcher. It is expected that there should be only one
67 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070068 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040070public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080071 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070073
Joe Onorato36115782010-06-17 13:28:48 -040074 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080075 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040076 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040077 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040078
Joe Onoratof99f8c12009-10-31 17:27:36 -040079 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040080 private final Object mLock = new Object();
81 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040082 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070083 private boolean mIsLoaderTaskRunning;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070085 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
86 static {
87 sWorkerThread.start();
88 }
89 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
90
Joe Onoratocc67f472010-06-08 10:54:30 -070091 // We start off with everything not loaded. After that, we assume that
92 // our monitoring of the package manager provides all updates and we never
93 // need to do a requery. These are only ever touched from the loader thread.
94 private boolean mWorkspaceLoaded;
95 private boolean mAllAppsLoaded;
96
Winson Chungb8b2a5a2012-07-12 17:55:31 -070097 // When we are loading pages synchronously, we can't just post the binding of items on the side
98 // pages as this delays the rotation process. Instead, we wait for a callback from the first
99 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
100 // a normal load, we also clear this set of Runnables.
101 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
102
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700105 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700106 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800107
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700108 // The lock that must be acquired before referencing any static bg data structures. Unlike
109 // other locks, this one can generally be held long-term because we never expect any of these
110 // static data structures to be referenced outside of the worker thread except on the first
111 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700112 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113
Adam Cohen487f7dd2012-06-28 18:12:10 -0700114 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700115 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700116 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700117
Adam Cohen487f7dd2012-06-28 18:12:10 -0700118 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
119 // created by LauncherModel that are directly on the home screen (however, no widgets or
120 // shortcuts within folders).
121 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
124 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700125 new ArrayList<LauncherAppWidgetInfo>();
126
Adam Cohen487f7dd2012-06-28 18:12:10 -0700127 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
128 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
131 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132 // </ only access in worker thread >
133
134 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800135 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Adam Cohend22015c2010-07-26 22:02:18 -0700137 private static int mCellCountX;
138 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700139
Reena Lee99a73f32011-10-24 17:27:37 -0700140 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700141
Joe Onorato9c1289c2009-08-17 11:03:03 -0400142 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700143 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400144 public int getCurrentWorkspaceScreen();
145 public void startBinding();
146 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500147 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 public void finishBindingItems();
149 public void bindAppWidget(LauncherAppWidgetInfo info);
150 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500151 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
152 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chungcd810732012-06-18 16:45:43 -0700153 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700154 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400155 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800156 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100157 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700158 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Joe Onorato0589f0f2010-02-08 13:44:00 -0800161 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800162 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400163 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800165 mIconCache = iconCache;
166
167 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700168 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400169
Reena Lee93f824a2011-09-23 17:20:28 -0700170 final Resources res = app.getResources();
171 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
172 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700173 Configuration config = res.getConfiguration();
174 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800175 }
176
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700177 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
178 * posted on the main thread handler. */
179 private void runOnMainThread(Runnable r) {
180 if (sWorkerThread.getThreadId() == Process.myTid()) {
181 // If we are on the worker thread, post onto the main handler
182 mHandler.post(r);
183 } else {
184 r.run();
185 }
186 }
187
188 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
189 * posted on the worker thread handler. */
190 private static void runOnWorkerThread(Runnable r) {
191 if (sWorkerThread.getThreadId() == Process.myTid()) {
192 r.run();
193 } else {
194 // If we are not on the worker thread, then post to the worker handler
195 sWorker.post(r);
196 }
197 }
198
Joe Onorato56d82912010-03-07 14:32:10 -0500199 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800200 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chung603bcb92011-09-02 11:45:39 -0700203 public void unbindWorkspaceItems() {
204 sWorker.post(new Runnable() {
205 @Override
206 public void run() {
207 unbindWorkspaceItemsOnMainThread();
208 }
209 });
210 }
211
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700212 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
213 private void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700214 // Ensure that we don't use the same workspace items data structure on the main thread
215 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700216 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
217 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700218 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700219 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
220 tmpAppWidgets.addAll(sBgAppWidgets);
221 }
222 Runnable r = new Runnable() {
223 @Override
224 public void run() {
225 for (ItemInfo item : tmpWorkspaceItems) {
226 item.unbind();
227 }
228 for (ItemInfo item : tmpAppWidgets) {
229 item.unbind();
230 }
231 }
232 };
233 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700234 }
235
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 /**
237 * Adds an item to the DB if it was not created previously, or move it to a new
238 * <container, screen, cellX, cellY>
239 */
240 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
241 int screen, int cellX, int cellY) {
242 if (item.container == ItemInfo.NO_ID) {
243 // From all apps
244 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
245 } else {
246 // From somewhere else
247 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 }
249 }
250
Michael Jurka816474f2012-06-25 14:49:02 -0700251 static void checkItemInfo(final ItemInfo item) {
252 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
253 final long itemId = item.id;
254 Runnable r = new Runnable() {
255 public void run() {
Winson Chung2abf94d2012-07-18 18:16:38 -0700256 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700257 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
258 if (modelItem != null && item != modelItem) {
259 // the modelItem needs to match up perfectly with item if our model is
260 // to be consistent with the database-- for now, just require
261 // modelItem == item
262 String msg = "item: " + ((item != null) ? item.toString() : "null") +
263 "modelItem: " +
264 ((modelItem != null) ? modelItem.toString() : "null") +
265 "Error: ItemInfo passed to checkItemInfo doesn't match original";
266 RuntimeException e = new RuntimeException(msg);
267 e.setStackTrace(stackTrace);
268 throw e;
269 }
Michael Jurka816474f2012-06-25 14:49:02 -0700270 }
271 }
272 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700273 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700274 }
275
Michael Jurkac9d95c52011-08-29 14:03:34 -0700276 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
277 final ItemInfo item, final String callingFunction) {
278 final long itemId = item.id;
279 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
280 final ContentResolver cr = context.getContentResolver();
281
Adam Cohen487f7dd2012-06-28 18:12:10 -0700282 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700283 Runnable r = new Runnable() {
284 public void run() {
285 cr.update(uri, values, null, null);
286
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700287 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700288 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700289 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
290 if (item != modelItem) {
291 // the modelItem needs to match up perfectly with item if our model is to be
292 // consistent with the database-- for now, just require modelItem == item
293 String msg = "item: " + ((item != null) ? item.toString() : "null") +
294 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
295 "Error: ItemInfo passed to " + callingFunction + " doesn't match " +
296 "original";
297 throw new RuntimeException(msg);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700298 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700299
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700300 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
301 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
302 // Item is in a folder, make sure this folder exists
303 if (!sBgFolders.containsKey(item.container)) {
304 // An items container is being set to a that of an item which is not in
305 // the list of Folders.
306 String msg = "item: " + item + " container being set to: " +
307 item.container + ", not in the list of folders";
308 RuntimeException e = new RuntimeException(msg);
309 e.setStackTrace(stackTrace);
310 Launcher.dumpDebugLogsToConsole();
311 throw e;
312 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700313 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700314
315 // Items are added/removed from the corresponding FolderInfo elsewhere, such
316 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
317 // that are on the desktop, as appropriate
318 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
319 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
320 switch (modelItem.itemType) {
321 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
322 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
323 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
324 if (!sBgWorkspaceItems.contains(modelItem)) {
325 sBgWorkspaceItems.add(modelItem);
326 }
327 break;
328 default:
329 break;
330 }
331 } else {
332 sBgWorkspaceItems.remove(modelItem);
333 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700334 }
335 }
336 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700337 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700338 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400341 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700342 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700343 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
344 final int screen, final int cellX, final int cellY) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700345 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
346 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
347 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
348 Launcher.sDumpLogs.add(transaction);
349 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400350 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400351 item.cellX = cellX;
352 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700353
Winson Chung3d503fb2011-07-13 17:25:49 -0700354 // We store hotseat items in canonical form which is this orientation invariant position
355 // in the hotseat
356 if (context instanceof Launcher && screen < 0 &&
357 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
358 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
359 } else {
360 item.screen = screen;
361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362
363 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700365 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
366 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
368
Michael Jurkac9d95c52011-08-29 14:03:34 -0700369 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700370 }
371
372 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700373 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800374 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700375 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
376 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700377 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
378 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
379 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
380 Launcher.sDumpLogs.add(transaction);
381 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800382 item.cellX = cellX;
383 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700384 item.spanX = spanX;
385 item.spanY = spanY;
386
387 // We store hotseat items in canonical form which is this orientation invariant position
388 // in the hotseat
389 if (context instanceof Launcher && screen < 0 &&
390 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
391 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
392 } else {
393 item.screen = screen;
394 }
Adam Cohend4844c32011-02-18 19:25:06 -0800395
Adam Cohend4844c32011-02-18 19:25:06 -0800396 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800397 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700398 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
399 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
400 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
401 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
402 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800403
Michael Jurka816474f2012-06-25 14:49:02 -0700404 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700405 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700406
407 /**
408 * Update an item to the database in a specified container.
409 */
410 static void updateItemInDatabase(Context context, final ItemInfo item) {
411 final ContentValues values = new ContentValues();
412 item.onAddToDatabase(values);
413 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
414 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800415 }
416
417 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400418 * Returns true if the shortcuts already exists in the database.
419 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400421 static boolean shortcutExists(Context context, String title, Intent intent) {
422 final ContentResolver cr = context.getContentResolver();
423 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
424 new String[] { "title", "intent" }, "title=? and intent=?",
425 new String[] { title, intent.toUri(0) }, null);
426 boolean result = false;
427 try {
428 result = c.moveToFirst();
429 } finally {
430 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700433 }
434
Joe Onorato9c1289c2009-08-17 11:03:03 -0400435 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700436 * Returns an ItemInfo array containing all the items in the LauncherModel.
437 * The ItemInfo.id is not set through this function.
438 */
439 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
440 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
441 final ContentResolver cr = context.getContentResolver();
442 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
443 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
444 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
445 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
446
447 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
448 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
449 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
450 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
451 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
452 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
453 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
454
455 try {
456 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700457 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700458 item.cellX = c.getInt(cellXIndex);
459 item.cellY = c.getInt(cellYIndex);
460 item.spanX = c.getInt(spanXIndex);
461 item.spanY = c.getInt(spanYIndex);
462 item.container = c.getInt(containerIndex);
463 item.itemType = c.getInt(itemTypeIndex);
464 item.screen = c.getInt(screenIndex);
465
466 items.add(item);
467 }
468 } catch (Exception e) {
469 items.clear();
470 } finally {
471 c.close();
472 }
473
474 return items;
475 }
476
477 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400478 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
479 */
480 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
481 final ContentResolver cr = context.getContentResolver();
482 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
483 "_id=? and (itemType=? or itemType=?)",
484 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700485 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700486
Joe Onorato9c1289c2009-08-17 11:03:03 -0400487 try {
488 if (c.moveToFirst()) {
489 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
490 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
491 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
492 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
493 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
494 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495
Joe Onorato9c1289c2009-08-17 11:03:03 -0400496 FolderInfo folderInfo = null;
497 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700498 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
499 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400500 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700501 }
502
Joe Onorato9c1289c2009-08-17 11:03:03 -0400503 folderInfo.title = c.getString(titleIndex);
504 folderInfo.id = id;
505 folderInfo.container = c.getInt(containerIndex);
506 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700507 folderInfo.cellX = c.getInt(cellXIndex);
508 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400509
510 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700511 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 } finally {
513 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700514 }
515
516 return null;
517 }
518
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519 /**
520 * Add an item to the database in a specified container. Sets the container, screen, cellX and
521 * cellY fields of the item. Also assigns an ID to the item.
522 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700523 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
524 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526 item.cellX = cellX;
527 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700528 // We store hotseat items in canonical form which is this orientation invariant position
529 // in the hotseat
530 if (context instanceof Launcher && screen < 0 &&
531 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
532 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
533 } else {
534 item.screen = screen;
535 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536
537 final ContentValues values = new ContentValues();
538 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400539 item.onAddToDatabase(values);
540
Michael Jurka7578ec62011-08-03 14:11:54 -0700541 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700542 item.id = app.getLauncherProvider().generateNewId();
543 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700544 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700545
Adam Cohen487f7dd2012-06-28 18:12:10 -0700546 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
547
Michael Jurkac9d95c52011-08-29 14:03:34 -0700548 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700549 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700550 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
551 + item.id + " (" + container + ", " + screen + ", " + cellX + ", "
552 + cellY + ")";
553 Launcher.sDumpLogs.add(transaction);
554 Log.d(TAG, transaction);
555
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700556 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
557 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700559 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700560 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700561 if (sBgItemsIdMap.containsKey(item.id)) {
562 // we should not be adding new items in the db with the same id
563 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
564 "addItemToDatabase already exists." + item.toString());
565 }
566 sBgItemsIdMap.put(item.id, item);
567 switch (item.itemType) {
568 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
569 sBgFolders.put(item.id, (FolderInfo) item);
570 // Fall through
571 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
572 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
573 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
574 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
575 sBgWorkspaceItems.add(item);
576 } else {
577 if (!sBgFolders.containsKey(item.container)) {
578 // Adding an item to a folder that doesn't exist.
579 String msg = "adding item: " + item + " to a folder that " +
580 " doesn't exist";
581 RuntimeException e = new RuntimeException(msg);
582 e.setStackTrace(stackTrace);
583 Launcher.dumpDebugLogsToConsole();
584 throw e;
585 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700586 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700587 break;
588 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
589 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
590 break;
591 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700592 }
593 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700594 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700595 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700596 }
597
Joe Onorato9c1289c2009-08-17 11:03:03 -0400598 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 * Creates a new unique child id, for a given cell span across all layouts.
600 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700601 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700602 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
603 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700604 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700605 }
606
Adam Cohend22015c2010-07-26 22:02:18 -0700607 static int getCellCountX() {
608 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700609 }
610
Adam Cohend22015c2010-07-26 22:02:18 -0700611 static int getCellCountY() {
612 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700613 }
614
615 /**
616 * Updates the model orientation helper to take into account the current layout dimensions
617 * when performing local/canonical coordinate transformations.
618 */
619 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700620 mCellCountX = shortAxisCellCount;
621 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700622 }
623
624 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700625 * Removes the specified item from the database
626 * @param context
627 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700629 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700631 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700632 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
633
Michael Jurka83df1882011-08-31 20:59:26 -0700634 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700635 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700636 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
637 + item.id + " (" + item.container + ", " + item.screen + ", " + item.cellX +
638 ", " + item.cellY + ")";
639 Launcher.sDumpLogs.add(transaction);
640 Log.d(TAG, transaction);
641
Michael Jurkac9d95c52011-08-29 14:03:34 -0700642 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700643
644 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700645 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700646 switch (item.itemType) {
647 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
648 sBgFolders.remove(item.id);
649 for (ItemInfo info: sBgItemsIdMap.values()) {
650 if (info.container == item.id) {
651 // We are deleting a folder which still contains items that
652 // think they are contained by that folder.
653 String msg = "deleting a folder (" + item + ") which still " +
654 "contains items (" + info + ")";
655 RuntimeException e = new RuntimeException(msg);
656 e.setStackTrace(stackTrace);
657 Launcher.dumpDebugLogsToConsole();
658 throw e;
659 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700660 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700661 sBgWorkspaceItems.remove(item);
662 break;
663 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
664 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
665 sBgWorkspaceItems.remove(item);
666 break;
667 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
668 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
669 break;
670 }
671 sBgItemsIdMap.remove(item.id);
672 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700673 }
674 }
Michael Jurka83df1882011-08-31 20:59:26 -0700675 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700676 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 }
678
679 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680 * Remove the contents of the specified folder from the database
681 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700682 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400683 final ContentResolver cr = context.getContentResolver();
684
Michael Jurkac9d95c52011-08-29 14:03:34 -0700685 Runnable r = new Runnable() {
686 public void run() {
687 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700688 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700689 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700690 sBgItemsIdMap.remove(info.id);
691 sBgFolders.remove(info.id);
692 sBgDbIconCache.remove(info);
693 sBgWorkspaceItems.remove(info);
694 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700695
Michael Jurkac9d95c52011-08-29 14:03:34 -0700696 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
697 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700698 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700699 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700700 for (ItemInfo childInfo : info.contents) {
701 sBgItemsIdMap.remove(childInfo.id);
702 sBgDbIconCache.remove(childInfo);
703 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700704 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700705 }
706 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700707 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 }
709
710 /**
711 * Set this as the current Launcher activity object for the loader.
712 */
713 public void initialize(Callbacks callbacks) {
714 synchronized (mLock) {
715 mCallbacks = new WeakReference<Callbacks>(callbacks);
716 }
717 }
718
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
721 * ACTION_PACKAGE_CHANGED.
722 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100723 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400724 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400725 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700726
Joe Onorato36115782010-06-17 13:28:48 -0400727 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400728
Joe Onorato36115782010-06-17 13:28:48 -0400729 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
730 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
731 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
732 final String packageName = intent.getData().getSchemeSpecificPart();
733 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734
Joe Onorato36115782010-06-17 13:28:48 -0400735 int op = PackageUpdatedTask.OP_NONE;
736
737 if (packageName == null || packageName.length() == 0) {
738 // they sent us a bad intent
739 return;
740 }
741
742 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
743 op = PackageUpdatedTask.OP_UPDATE;
744 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
745 if (!replacing) {
746 op = PackageUpdatedTask.OP_REMOVE;
747 }
748 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
749 // later, we will update the package at this time
750 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
751 if (!replacing) {
752 op = PackageUpdatedTask.OP_ADD;
753 } else {
754 op = PackageUpdatedTask.OP_UPDATE;
755 }
756 }
757
758 if (op != PackageUpdatedTask.OP_NONE) {
759 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
760 }
761
762 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400763 // First, schedule to add these apps back in.
764 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
765 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
766 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700767 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400768 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
769 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
770 enqueuePackageUpdated(new PackageUpdatedTask(
771 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700772 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700773 // If we have changed locale we need to clear out the labels in all apps/workspace.
774 forceReload();
775 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
776 // Check if configuration change was an mcc/mnc change which would affect app resources
777 // and we would need to clear out the labels in all apps/workspace. Same handling as
778 // above for ACTION_LOCALE_CHANGED
779 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700780 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700781 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700782 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700783 forceReload();
784 }
785 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700786 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700787 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
788 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700789 if (mCallbacks != null) {
790 Callbacks callbacks = mCallbacks.get();
791 if (callbacks != null) {
792 callbacks.bindSearchablesChanged();
793 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700794 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700795 }
796 }
797
Reena Lee93f824a2011-09-23 17:20:28 -0700798 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700799 resetLoadedState(true, true);
800
Reena Lee93f824a2011-09-23 17:20:28 -0700801 // Do this here because if the launcher activity is running it will be restarted.
802 // If it's not running startLoaderFromBackground will merely tell it that it needs
803 // to reload.
804 startLoaderFromBackground();
805 }
806
Winson Chungf0c6ae02012-03-21 16:10:31 -0700807 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
808 synchronized (mLock) {
809 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
810 // mWorkspaceLoaded to true later
811 stopLoaderLocked();
812 if (resetAllAppsLoaded) mAllAppsLoaded = false;
813 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
814 }
815 }
816
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700817 /**
818 * When the launcher is in the background, it's possible for it to miss paired
819 * configuration changes. So whenever we trigger the loader from the background
820 * tell the launcher that it needs to re-run the loader when it comes back instead
821 * of doing it now.
822 */
823 public void startLoaderFromBackground() {
824 boolean runLoader = false;
825 if (mCallbacks != null) {
826 Callbacks callbacks = mCallbacks.get();
827 if (callbacks != null) {
828 // Only actually run the loader if they're not paused.
829 if (!callbacks.setLoadOnResume()) {
830 runLoader = true;
831 }
832 }
833 }
834 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700835 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -0700836 }
Joe Onorato36115782010-06-17 13:28:48 -0400837 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400838
Reena Lee93f824a2011-09-23 17:20:28 -0700839 // If there is already a loader task running, tell it to stop.
840 // returns true if isLaunching() was true on the old task
841 private boolean stopLoaderLocked() {
842 boolean isLaunching = false;
843 LoaderTask oldTask = mLoaderTask;
844 if (oldTask != null) {
845 if (oldTask.isLaunching()) {
846 isLaunching = true;
847 }
848 oldTask.stopLocked();
849 }
850 return isLaunching;
851 }
852
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700853 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -0400854 synchronized (mLock) {
855 if (DEBUG_LOADERS) {
856 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700859 // Clear any deferred bind-runnables from the synchronized load process
860 // We must do this before any loading/binding is scheduled below.
861 mDeferredBindRunnables.clear();
862
Joe Onorato36115782010-06-17 13:28:48 -0400863 // Don't bother to start the thread if we know it's not going to do anything
864 if (mCallbacks != null && mCallbacks.get() != null) {
865 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700866 // also, don't downgrade isLaunching if we're already running
867 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700868 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700869 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
870 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
871 } else {
872 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
873 sWorker.post(mLoaderTask);
874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876 }
877 }
878
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700879 void bindRemainingSynchronousPages() {
880 // Post the remaining side pages to be loaded
881 if (!mDeferredBindRunnables.isEmpty()) {
882 for (final Runnable r : mDeferredBindRunnables) {
883 mHandler.post(r);
884 }
885 mDeferredBindRunnables.clear();
886 }
887 }
888
Joe Onorato36115782010-06-17 13:28:48 -0400889 public void stopLoader() {
890 synchronized (mLock) {
891 if (mLoaderTask != null) {
892 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893 }
894 }
Joe Onorato36115782010-06-17 13:28:48 -0400895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400896
Michael Jurkac57b7a82011-08-09 22:02:20 -0700897 public boolean isAllAppsLoaded() {
898 return mAllAppsLoaded;
899 }
900
Winson Chung36a62fe2012-05-06 18:04:42 -0700901 boolean isLoadingWorkspace() {
902 synchronized (mLock) {
903 if (mLoaderTask != null) {
904 return mLoaderTask.isLoadingWorkspace();
905 }
906 }
907 return false;
908 }
909
Joe Onorato36115782010-06-17 13:28:48 -0400910 /**
911 * Runnable for the thread that loads the contents of the launcher:
912 * - workspace icons
913 * - widgets
914 * - all apps icons
915 */
916 private class LoaderTask implements Runnable {
917 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -0400918 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700919 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400920 private boolean mStopped;
921 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700922
Winson Chungc3eecff2011-07-11 17:44:15 -0700923 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400924
925 LoaderTask(Context context, boolean isLaunching) {
926 mContext = context;
927 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700928 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 }
930
Joe Onorato36115782010-06-17 13:28:48 -0400931 boolean isLaunching() {
932 return mIsLaunching;
933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934
Winson Chung36a62fe2012-05-06 18:04:42 -0700935 boolean isLoadingWorkspace() {
936 return mIsLoadingAndBindingWorkspace;
937 }
938
Joe Onorato36115782010-06-17 13:28:48 -0400939 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700940 mIsLoadingAndBindingWorkspace = true;
941
Joe Onorato36115782010-06-17 13:28:48 -0400942 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400943 if (DEBUG_LOADERS) {
944 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700946
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700947 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400948 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700949 synchronized (LoaderTask.this) {
950 if (mStopped) {
951 return;
952 }
953 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 }
955 }
956
Joe Onorato36115782010-06-17 13:28:48 -0400957 // Bind the workspace
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700958 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -0400959 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400960
Joe Onorato36115782010-06-17 13:28:48 -0400961 private void waitForIdle() {
962 // Wait until the either we're stopped or the other threads are done.
963 // This way we don't start loading all apps until the workspace has settled
964 // down.
965 synchronized (LoaderTask.this) {
966 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700967
Joe Onorato36115782010-06-17 13:28:48 -0400968 mHandler.postIdle(new Runnable() {
969 public void run() {
970 synchronized (LoaderTask.this) {
971 mLoadAndBindStepFinished = true;
972 if (DEBUG_LOADERS) {
973 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400974 }
Joe Onorato36115782010-06-17 13:28:48 -0400975 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400976 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400977 }
Joe Onorato36115782010-06-17 13:28:48 -0400978 });
979
980 while (!mStopped && !mLoadAndBindStepFinished) {
981 try {
982 this.wait();
983 } catch (InterruptedException ex) {
984 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400985 }
986 }
Joe Onorato36115782010-06-17 13:28:48 -0400987 if (DEBUG_LOADERS) {
988 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700989 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400990 + "ms for previous step to finish binding");
991 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400992 }
Joe Onorato36115782010-06-17 13:28:48 -0400993 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400994
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700995 void runBindSynchronousPage(int synchronousBindPage) {
996 if (synchronousBindPage < 0) {
997 // Ensure that we have a valid page index to load synchronously
998 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
999 "valid page index");
1000 }
1001 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1002 // Ensure that we don't try and bind a specified page when the pages have not been
1003 // loaded already (we should load everything asynchronously in that case)
1004 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1005 }
1006 synchronized (mLock) {
1007 if (mIsLoaderTaskRunning) {
1008 // Ensure that we are never running the background loading at this point since
1009 // we also touch the background collections
1010 throw new RuntimeException("Error! Background loading is already running");
1011 }
1012 }
1013
1014 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1015 // data structures, we can't allow any other thread to touch that data, but because
1016 // this call is synchronous, we can get away with not locking).
1017
Adam Cohena13a2f22012-07-23 14:29:15 -07001018 // The LauncherModel is static in the LauncherApplication and mHandler may have queued
1019 // operations from the previous activity. We need to ensure that all queued operations
1020 // are executed before any synchronous binding work is done.
1021 mHandler.flush();
1022
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 // Divide the set of loaded items into those that we are binding synchronously, and
1024 // everything else that is to be bound normally (asynchronously).
1025 bindWorkspace(synchronousBindPage);
1026 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1027 // arise from that.
1028 onlyBindAllApps();
1029 }
1030
Joe Onorato36115782010-06-17 13:28:48 -04001031 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032 synchronized (mLock) {
1033 mIsLoaderTaskRunning = true;
1034 }
Joe Onorato36115782010-06-17 13:28:48 -04001035 // Optimize for end-user experience: if the Launcher is up and // running with the
1036 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1037 // workspace first (default).
1038 final Callbacks cbk = mCallbacks.get();
1039 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -04001040
Joe Onorato36115782010-06-17 13:28:48 -04001041 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001042 // Elevate priority when Home launches for the first time to avoid
1043 // starving at boot time. Staring at a blank home is not cool.
1044 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001045 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1046 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001047 android.os.Process.setThreadPriority(mIsLaunching
1048 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1049 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001050 if (loadWorkspaceFirst) {
1051 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1052 loadAndBindWorkspace();
1053 } else {
1054 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001055 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001056 }
1057
Joe Onorato36115782010-06-17 13:28:48 -04001058 if (mStopped) {
1059 break keep_running;
1060 }
1061
1062 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1063 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001064 synchronized (mLock) {
1065 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001066 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001067 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1068 }
1069 }
Joe Onorato36115782010-06-17 13:28:48 -04001070 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001071
1072 // second step
1073 if (loadWorkspaceFirst) {
1074 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001075 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001076 } else {
1077 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
1078 loadAndBindWorkspace();
1079 }
Winson Chung7ed37742011-09-08 15:45:51 -07001080
1081 // Restore the default thread priority after we are done loading items
1082 synchronized (mLock) {
1083 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1084 }
Joe Onorato36115782010-06-17 13:28:48 -04001085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086
Winson Chungaac01e12011-08-17 10:37:13 -07001087
1088 // Update the saved icons if necessary
1089 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001090 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001091 for (Object key : sBgDbIconCache.keySet()) {
1092 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1093 }
1094 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001095 }
Winson Chungaac01e12011-08-17 10:37:13 -07001096
Joe Onorato36115782010-06-17 13:28:48 -04001097 // Clear out this reference, otherwise we end up holding it until all of the
1098 // callback runnables are done.
1099 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001100
Joe Onorato36115782010-06-17 13:28:48 -04001101 synchronized (mLock) {
1102 // If we are still the last one to be scheduled, remove ourselves.
1103 if (mLoaderTask == this) {
1104 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001107 }
Joe Onorato36115782010-06-17 13:28:48 -04001108 }
1109
1110 public void stopLocked() {
1111 synchronized (LoaderTask.this) {
1112 mStopped = true;
1113 this.notify();
1114 }
1115 }
1116
1117 /**
1118 * Gets the callbacks object. If we've been stopped, or if the launcher object
1119 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1120 * object that was around when the deferred message was scheduled, and if there's
1121 * a new Callbacks object around then also return null. This will save us from
1122 * calling onto it with data that will be ignored.
1123 */
1124 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1125 synchronized (mLock) {
1126 if (mStopped) {
1127 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001128 }
Joe Onorato36115782010-06-17 13:28:48 -04001129
1130 if (mCallbacks == null) {
1131 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001132 }
Joe Onorato36115782010-06-17 13:28:48 -04001133
1134 final Callbacks callbacks = mCallbacks.get();
1135 if (callbacks != oldCallbacks) {
1136 return null;
1137 }
1138 if (callbacks == null) {
1139 Log.w(TAG, "no mCallbacks");
1140 return null;
1141 }
1142
1143 return callbacks;
1144 }
1145 }
1146
1147 // check & update map of what's occupied; used to discard overlapping/invalid items
1148 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001149 int containerIndex = item.screen;
1150 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001151 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08001152 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001153 return false;
1154 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001155
1156 // We use the last index to refer to the hotseat and the screen as the rank, so
1157 // test and update the occupied state accordingly
1158 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
1159 Log.e(TAG, "Error loading shortcut into hotseat " + item
1160 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
1161 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
1162 return false;
1163 } else {
1164 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
1165 return true;
1166 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001167 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1168 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001169 return true;
1170 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001171
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001172 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001173 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1174 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001175 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001176 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -07001177 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001178 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001179 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -07001180 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001181 return false;
1182 }
1183 }
1184 }
1185 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1186 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001187 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001188 }
1189 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001190
Joe Onorato36115782010-06-17 13:28:48 -04001191 return true;
1192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193
Joe Onorato36115782010-06-17 13:28:48 -04001194 private void loadWorkspace() {
1195 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196
Joe Onorato36115782010-06-17 13:28:48 -04001197 final Context context = mContext;
1198 final ContentResolver contentResolver = context.getContentResolver();
1199 final PackageManager manager = context.getPackageManager();
1200 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1201 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001202
Michael Jurkab85f8a42012-04-25 15:48:32 -07001203 // Make sure the default workspace is loaded, if needed
1204 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary();
1205
Winson Chung2abf94d2012-07-18 18:16:38 -07001206 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001207 sBgWorkspaceItems.clear();
1208 sBgAppWidgets.clear();
1209 sBgFolders.clear();
1210 sBgItemsIdMap.clear();
1211 sBgDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001212
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001213 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001215 final Cursor c = contentResolver.query(
1216 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001217
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001218 // +1 for the hotseat (it can be larger than the workspace)
1219 // Load workspace in reverse order to ensure that latest items are loaded first (and
1220 // before any earlier duplicates)
1221 final ItemInfo occupied[][][] =
1222 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001224 try {
1225 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1226 final int intentIndex = c.getColumnIndexOrThrow
1227 (LauncherSettings.Favorites.INTENT);
1228 final int titleIndex = c.getColumnIndexOrThrow
1229 (LauncherSettings.Favorites.TITLE);
1230 final int iconTypeIndex = c.getColumnIndexOrThrow(
1231 LauncherSettings.Favorites.ICON_TYPE);
1232 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1233 final int iconPackageIndex = c.getColumnIndexOrThrow(
1234 LauncherSettings.Favorites.ICON_PACKAGE);
1235 final int iconResourceIndex = c.getColumnIndexOrThrow(
1236 LauncherSettings.Favorites.ICON_RESOURCE);
1237 final int containerIndex = c.getColumnIndexOrThrow(
1238 LauncherSettings.Favorites.CONTAINER);
1239 final int itemTypeIndex = c.getColumnIndexOrThrow(
1240 LauncherSettings.Favorites.ITEM_TYPE);
1241 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1242 LauncherSettings.Favorites.APPWIDGET_ID);
1243 final int screenIndex = c.getColumnIndexOrThrow(
1244 LauncherSettings.Favorites.SCREEN);
1245 final int cellXIndex = c.getColumnIndexOrThrow
1246 (LauncherSettings.Favorites.CELLX);
1247 final int cellYIndex = c.getColumnIndexOrThrow
1248 (LauncherSettings.Favorites.CELLY);
1249 final int spanXIndex = c.getColumnIndexOrThrow
1250 (LauncherSettings.Favorites.SPANX);
1251 final int spanYIndex = c.getColumnIndexOrThrow(
1252 LauncherSettings.Favorites.SPANY);
1253 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1254 //final int displayModeIndex = c.getColumnIndexOrThrow(
1255 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001257 ShortcutInfo info;
1258 String intentDescription;
1259 LauncherAppWidgetInfo appWidgetInfo;
1260 int container;
1261 long id;
1262 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001264 while (!mStopped && c.moveToNext()) {
1265 try {
1266 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001268 switch (itemType) {
1269 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1270 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1271 intentDescription = c.getString(intentIndex);
1272 try {
1273 intent = Intent.parseUri(intentDescription, 0);
1274 } catch (URISyntaxException e) {
1275 continue;
1276 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001278 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1279 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1280 titleIndex, mLabelCache);
1281 } else {
1282 info = getShortcutInfo(c, context, iconTypeIndex,
1283 iconPackageIndex, iconResourceIndex, iconIndex,
1284 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001285
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001286 // App shortcuts that used to be automatically added to Launcher
1287 // didn't always have the correct intent flags set, so do that
1288 // here
1289 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001290 intent.getCategories() != null &&
1291 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001292 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001293 intent.addFlags(
1294 Intent.FLAG_ACTIVITY_NEW_TASK |
1295 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1296 }
Michael Jurka96879562012-03-22 05:54:33 -07001297 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001299 if (info != null) {
1300 info.intent = intent;
1301 info.id = c.getLong(idIndex);
1302 container = c.getInt(containerIndex);
1303 info.container = container;
1304 info.screen = c.getInt(screenIndex);
1305 info.cellX = c.getInt(cellXIndex);
1306 info.cellY = c.getInt(cellYIndex);
1307
1308 // check & update map of what's occupied
1309 if (!checkItemPlacement(occupied, info)) {
1310 break;
1311 }
1312
1313 switch (container) {
1314 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1315 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1316 sBgWorkspaceItems.add(info);
1317 break;
1318 default:
1319 // Item is in a user folder
1320 FolderInfo folderInfo =
1321 findOrMakeFolder(sBgFolders, container);
1322 folderInfo.add(info);
1323 break;
1324 }
1325 sBgItemsIdMap.put(info.id, info);
1326
1327 // now that we've loaded everthing re-save it with the
1328 // icon in case it disappears somehow.
1329 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
1330 } else {
1331 // Failed to load the shortcut, probably because the
1332 // activity manager couldn't resolve it (maybe the app
1333 // was uninstalled), or the db row was somehow screwed up.
1334 // Delete it.
1335 id = c.getLong(idIndex);
1336 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1337 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1338 id, false), null, null);
1339 }
1340 break;
1341
1342 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1343 id = c.getLong(idIndex);
1344 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1345
1346 folderInfo.title = c.getString(titleIndex);
1347 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001348 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001349 folderInfo.container = container;
1350 folderInfo.screen = c.getInt(screenIndex);
1351 folderInfo.cellX = c.getInt(cellXIndex);
1352 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353
Daniel Sandler8802e962010-05-26 16:28:16 -04001354 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001355 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001356 break;
1357 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001359 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1360 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1361 sBgWorkspaceItems.add(folderInfo);
1362 break;
Joe Onorato36115782010-06-17 13:28:48 -04001363 }
Joe Onorato17a89222011-02-08 17:26:11 -08001364
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001365 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1366 sBgFolders.put(folderInfo.id, folderInfo);
1367 break;
1368
1369 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1370 // Read all Launcher-specific widget details
1371 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001372 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001373
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001374 final AppWidgetProviderInfo provider =
1375 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001376
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001377 if (!isSafeMode && (provider == null || provider.provider == null ||
1378 provider.provider.getPackageName() == null)) {
1379 String log = "Deleting widget that isn't installed anymore: id="
1380 + id + " appWidgetId=" + appWidgetId;
1381 Log.e(TAG, log);
1382 Launcher.sDumpLogs.add(log);
1383 itemsToRemove.add(id);
1384 } else {
1385 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1386 provider.provider);
1387 appWidgetInfo.id = id;
1388 appWidgetInfo.screen = c.getInt(screenIndex);
1389 appWidgetInfo.cellX = c.getInt(cellXIndex);
1390 appWidgetInfo.cellY = c.getInt(cellYIndex);
1391 appWidgetInfo.spanX = c.getInt(spanXIndex);
1392 appWidgetInfo.spanY = c.getInt(spanYIndex);
1393 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1394 appWidgetInfo.minSpanX = minSpan[0];
1395 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001396
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001397 container = c.getInt(containerIndex);
1398 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1399 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1400 Log.e(TAG, "Widget found where container != " +
1401 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1402 continue;
1403 }
1404 appWidgetInfo.container = c.getInt(containerIndex);
1405
1406 // check & update map of what's occupied
1407 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1408 break;
1409 }
1410 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1411 sBgAppWidgets.add(appWidgetInfo);
1412 }
Joe Onorato36115782010-06-17 13:28:48 -04001413 break;
1414 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001415 } catch (Exception e) {
1416 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001417 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 }
1419 } finally {
1420 c.close();
1421 }
1422
1423 if (itemsToRemove.size() > 0) {
1424 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1425 LauncherSettings.Favorites.CONTENT_URI);
1426 // Remove dead items
1427 for (long id : itemsToRemove) {
1428 if (DEBUG_LOADERS) {
1429 Log.d(TAG, "Removed id = " + id);
1430 }
1431 // Don't notify content observers
1432 try {
1433 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1434 null, null);
1435 } catch (RemoteException e) {
1436 Log.w(TAG, "Could not remove id = " + id);
1437 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001438 }
1439 }
1440
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001441 if (DEBUG_LOADERS) {
1442 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1443 Log.d(TAG, "workspace layout: ");
1444 for (int y = 0; y < mCellCountY; y++) {
1445 String line = "";
1446 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1447 if (s > 0) {
1448 line += " | ";
1449 }
1450 for (int x = 0; x < mCellCountX; x++) {
1451 line += ((occupied[s][x][y] != null) ? "#" : ".");
1452 }
Joe Onorato36115782010-06-17 13:28:48 -04001453 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001455 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001456 }
Joe Onorato36115782010-06-17 13:28:48 -04001457 }
1458 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001460 /** Filters the set of items who are directly or indirectly (via another container) on the
1461 * specified screen. */
1462 private void filterCurrentWorkspaceItems(int currentScreen,
1463 ArrayList<ItemInfo> allWorkspaceItems,
1464 ArrayList<ItemInfo> currentScreenItems,
1465 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001466 // Purge any null ItemInfos
1467 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1468 while (iter.hasNext()) {
1469 ItemInfo i = iter.next();
1470 if (i == null) {
1471 iter.remove();
1472 }
1473 }
1474
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001475 // If we aren't filtering on a screen, then the set of items to load is the full set of
1476 // items given.
1477 if (currentScreen < 0) {
1478 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001479 }
1480
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001481 // Order the set of items by their containers first, this allows use to walk through the
1482 // list sequentially, build up a list of containers that are in the specified screen,
1483 // as well as all items in those containers.
1484 Set<Long> itemsOnScreen = new HashSet<Long>();
1485 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1486 @Override
1487 public int compare(ItemInfo lhs, ItemInfo rhs) {
1488 return (int) (lhs.container - rhs.container);
1489 }
1490 });
1491 for (ItemInfo info : allWorkspaceItems) {
1492 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1493 if (info.screen == currentScreen) {
1494 currentScreenItems.add(info);
1495 itemsOnScreen.add(info.id);
1496 } else {
1497 otherScreenItems.add(info);
1498 }
1499 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1500 currentScreenItems.add(info);
1501 itemsOnScreen.add(info.id);
1502 } else {
1503 if (itemsOnScreen.contains(info.container)) {
1504 currentScreenItems.add(info);
1505 itemsOnScreen.add(info.id);
1506 } else {
1507 otherScreenItems.add(info);
1508 }
1509 }
1510 }
1511 }
1512
1513 /** Filters the set of widgets which are on the specified screen. */
1514 private void filterCurrentAppWidgets(int currentScreen,
1515 ArrayList<LauncherAppWidgetInfo> appWidgets,
1516 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1517 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1518 // If we aren't filtering on a screen, then the set of items to load is the full set of
1519 // widgets given.
1520 if (currentScreen < 0) {
1521 currentScreenWidgets.addAll(appWidgets);
1522 }
1523
1524 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001525 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001526 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1527 widget.screen == currentScreen) {
1528 currentScreenWidgets.add(widget);
1529 } else {
1530 otherScreenWidgets.add(widget);
1531 }
1532 }
1533 }
1534
1535 /** Filters the set of folders which are on the specified screen. */
1536 private void filterCurrentFolders(int currentScreen,
1537 HashMap<Long, ItemInfo> itemsIdMap,
1538 HashMap<Long, FolderInfo> folders,
1539 HashMap<Long, FolderInfo> currentScreenFolders,
1540 HashMap<Long, FolderInfo> otherScreenFolders) {
1541 // If we aren't filtering on a screen, then the set of items to load is the full set of
1542 // widgets given.
1543 if (currentScreen < 0) {
1544 currentScreenFolders.putAll(folders);
1545 }
1546
1547 for (long id : folders.keySet()) {
1548 ItemInfo info = itemsIdMap.get(id);
1549 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07001550 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001551 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1552 info.screen == currentScreen) {
1553 currentScreenFolders.put(id, folder);
1554 } else {
1555 otherScreenFolders.put(id, folder);
1556 }
1557 }
1558 }
1559
1560 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1561 * right) */
1562 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1563 // XXX: review this
1564 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001565 @Override
1566 public int compare(ItemInfo lhs, ItemInfo rhs) {
1567 int cellCountX = LauncherModel.getCellCountX();
1568 int cellCountY = LauncherModel.getCellCountY();
1569 int screenOffset = cellCountX * cellCountY;
1570 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1571 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1572 lhs.cellY * cellCountX + lhs.cellX);
1573 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1574 rhs.cellY * cellCountX + rhs.cellX);
1575 return (int) (lr - rr);
1576 }
1577 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001579
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001580 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1581 final ArrayList<ItemInfo> workspaceItems,
1582 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1583 final HashMap<Long, FolderInfo> folders,
1584 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07001585
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001586 final boolean postOnMainThread = (deferredBindRunnables != null);
1587
1588 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001589 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001590 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001591 final int start = i;
1592 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001593 final Runnable r = new Runnable() {
1594 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001596 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001598 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001599 }
1600 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001601 };
1602 if (postOnMainThread) {
1603 deferredBindRunnables.add(r);
1604 } else {
1605 runOnMainThread(r);
1606 }
Joe Onorato36115782010-06-17 13:28:48 -04001607 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001608
1609 // Bind the folders
1610 if (!folders.isEmpty()) {
1611 final Runnable r = new Runnable() {
1612 public void run() {
1613 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1614 if (callbacks != null) {
1615 callbacks.bindFolders(folders);
1616 }
1617 }
1618 };
1619 if (postOnMainThread) {
1620 deferredBindRunnables.add(r);
1621 } else {
1622 runOnMainThread(r);
1623 }
1624 }
1625
1626 // Bind the widgets, one at a time
1627 N = appWidgets.size();
1628 for (int i = 0; i < N; i++) {
1629 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1630 final Runnable r = new Runnable() {
1631 public void run() {
1632 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1633 if (callbacks != null) {
1634 callbacks.bindAppWidget(widget);
1635 }
1636 }
1637 };
1638 if (postOnMainThread) {
1639 deferredBindRunnables.add(r);
1640 } else {
1641 runOnMainThread(r);
1642 }
1643 }
1644 }
1645
1646 /**
1647 * Binds all loaded data to actual views on the main thread.
1648 */
1649 private void bindWorkspace(int synchronizeBindPage) {
1650 final long t = SystemClock.uptimeMillis();
1651 Runnable r;
1652
1653 // Don't use these two variables in any of the callback runnables.
1654 // Otherwise we hold a reference to them.
1655 final Callbacks oldCallbacks = mCallbacks.get();
1656 if (oldCallbacks == null) {
1657 // This launcher has exited and nobody bothered to tell us. Just bail.
1658 Log.w(TAG, "LoaderTask running with no launcher");
1659 return;
1660 }
1661
Winson Chung4a2afa32012-07-19 14:53:05 -07001662 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
1663 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001664 oldCallbacks.getCurrentWorkspaceScreen();
1665
1666 // Load all the items that are on the current page first (and in the process, unbind
1667 // all the existing workspace items before we call startBinding() below.
1668 unbindWorkspaceItemsOnMainThread();
1669 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1670 ArrayList<LauncherAppWidgetInfo> appWidgets =
1671 new ArrayList<LauncherAppWidgetInfo>();
1672 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
1673 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -07001674 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 workspaceItems.addAll(sBgWorkspaceItems);
1676 appWidgets.addAll(sBgAppWidgets);
1677 folders.putAll(sBgFolders);
1678 itemsIdMap.putAll(sBgItemsIdMap);
1679 }
1680
1681 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
1682 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
1683 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
1684 new ArrayList<LauncherAppWidgetInfo>();
1685 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
1686 new ArrayList<LauncherAppWidgetInfo>();
1687 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
1688 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
1689
1690 // Separate the items that are on the current screen, and all the other remaining items
1691 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
1692 otherWorkspaceItems);
1693 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
1694 otherAppWidgets);
1695 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
1696 otherFolders);
1697 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1698 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1699
1700 // Tell the workspace that we're about to start binding items
1701 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001702 public void run() {
1703 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1704 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001705 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001706 }
1707 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 };
1709 runOnMainThread(r);
1710
1711 // Load items on the current page
1712 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
1713 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07001714 if (isLoadingSynchronously) {
1715 r = new Runnable() {
1716 public void run() {
1717 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1718 if (callbacks != null) {
1719 callbacks.onPageBoundSynchronously(currentScreen);
1720 }
1721 }
1722 };
1723 runOnMainThread(r);
1724 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725
Winson Chung4a2afa32012-07-19 14:53:05 -07001726 // Load all the remaining pages (if we are loading synchronously, we want to defer this
1727 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 mDeferredBindRunnables.clear();
1729 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07001730 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731
1732 // Tell the workspace that we're done binding items
1733 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001734 public void run() {
1735 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1736 if (callbacks != null) {
1737 callbacks.finishBindingItems();
1738 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739
Winson Chung98e030b2012-05-07 16:01:11 -07001740 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001741 if (DEBUG_LOADERS) {
1742 Log.d(TAG, "bound workspace in "
1743 + (SystemClock.uptimeMillis()-t) + "ms");
1744 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001745
1746 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001747 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 };
Winson Chung4a2afa32012-07-19 14:53:05 -07001749 if (isLoadingSynchronously) {
1750 mDeferredBindRunnables.add(r);
1751 } else {
1752 runOnMainThread(r);
1753 }
Joe Onorato36115782010-06-17 13:28:48 -04001754 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001755
Joe Onorato36115782010-06-17 13:28:48 -04001756 private void loadAndBindAllApps() {
1757 if (DEBUG_LOADERS) {
1758 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1759 }
1760 if (!mAllAppsLoaded) {
1761 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001762 synchronized (LoaderTask.this) {
1763 if (mStopped) {
1764 return;
1765 }
1766 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001767 }
Joe Onorato36115782010-06-17 13:28:48 -04001768 } else {
1769 onlyBindAllApps();
1770 }
1771 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001772
Joe Onorato36115782010-06-17 13:28:48 -04001773 private void onlyBindAllApps() {
1774 final Callbacks oldCallbacks = mCallbacks.get();
1775 if (oldCallbacks == null) {
1776 // This launcher has exited and nobody bothered to tell us. Just bail.
1777 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1778 return;
1779 }
1780
1781 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001782 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001783 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07001784 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001785 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001786 public void run() {
1787 final long t = SystemClock.uptimeMillis();
1788 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1789 if (callbacks != null) {
1790 callbacks.bindAllApplications(list);
1791 }
1792 if (DEBUG_LOADERS) {
1793 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1794 + (SystemClock.uptimeMillis()-t) + "ms");
1795 }
1796 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001797 };
1798 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
1799 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) {
1800 r.run();
1801 } else {
1802 mHandler.post(r);
1803 }
Joe Onorato36115782010-06-17 13:28:48 -04001804 }
1805
1806 private void loadAllAppsByBatch() {
1807 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1808
1809 // Don't use these two variables in any of the callback runnables.
1810 // Otherwise we hold a reference to them.
1811 final Callbacks oldCallbacks = mCallbacks.get();
1812 if (oldCallbacks == null) {
1813 // This launcher has exited and nobody bothered to tell us. Just bail.
1814 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1815 return;
1816 }
1817
1818 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1819 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1820
1821 final PackageManager packageManager = mContext.getPackageManager();
1822 List<ResolveInfo> apps = null;
1823
1824 int N = Integer.MAX_VALUE;
1825
1826 int startIndex;
1827 int i=0;
1828 int batchSize = -1;
1829 while (i < N && !mStopped) {
1830 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001831 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001832 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1833 apps = packageManager.queryIntentActivities(mainIntent, 0);
1834 if (DEBUG_LOADERS) {
1835 Log.d(TAG, "queryIntentActivities took "
1836 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1837 }
1838 if (apps == null) {
1839 return;
1840 }
1841 N = apps.size();
1842 if (DEBUG_LOADERS) {
1843 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1844 }
1845 if (N == 0) {
1846 // There are no apps?!?
1847 return;
1848 }
1849 if (mBatchSize == 0) {
1850 batchSize = N;
1851 } else {
1852 batchSize = mBatchSize;
1853 }
1854
1855 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1856 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001857 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001858 if (DEBUG_LOADERS) {
1859 Log.d(TAG, "sort took "
1860 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1861 }
1862 }
1863
1864 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1865
1866 startIndex = i;
1867 for (int j=0; i<N && j<batchSize; j++) {
1868 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001869 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001870 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001871 i++;
1872 }
1873
1874 final boolean first = i <= batchSize;
1875 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001876 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
1877 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001878
Joe Onoratocc67f472010-06-08 10:54:30 -07001879 mHandler.post(new Runnable() {
1880 public void run() {
1881 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001882 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001883 if (first) {
1884 callbacks.bindAllApplications(added);
1885 } else {
1886 callbacks.bindAppsAdded(added);
1887 }
1888 if (DEBUG_LOADERS) {
1889 Log.d(TAG, "bound " + added.size() + " apps in "
1890 + (SystemClock.uptimeMillis() - t) + "ms");
1891 }
1892 } else {
1893 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001894 }
1895 }
1896 });
1897
Daniel Sandlerdca66122010-04-13 16:23:58 -04001898 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001899 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1900 + (SystemClock.uptimeMillis()-t2) + "ms");
1901 }
1902
1903 if (mAllAppsLoadDelay > 0 && i < N) {
1904 try {
1905 if (DEBUG_LOADERS) {
1906 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1907 }
1908 Thread.sleep(mAllAppsLoadDelay);
1909 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001910 }
1911 }
1912
Joe Onorato36115782010-06-17 13:28:48 -04001913 if (DEBUG_LOADERS) {
1914 Log.d(TAG, "cached all " + N + " apps in "
1915 + (SystemClock.uptimeMillis()-t) + "ms"
1916 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001917 }
1918 }
1919
1920 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07001921 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1923 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1924 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1925 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1926 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
1927 }
Joe Onorato36115782010-06-17 13:28:48 -04001928 }
1929 }
1930
1931 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001932 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001933 }
1934
1935 private class PackageUpdatedTask implements Runnable {
1936 int mOp;
1937 String[] mPackages;
1938
1939 public static final int OP_NONE = 0;
1940 public static final int OP_ADD = 1;
1941 public static final int OP_UPDATE = 2;
1942 public static final int OP_REMOVE = 3; // uninstlled
1943 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1944
1945
1946 public PackageUpdatedTask(int op, String[] packages) {
1947 mOp = op;
1948 mPackages = packages;
1949 }
1950
1951 public void run() {
1952 final Context context = mApp;
1953
1954 final String[] packages = mPackages;
1955 final int N = packages.length;
1956 switch (mOp) {
1957 case OP_ADD:
1958 for (int i=0; i<N; i++) {
1959 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001960 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001961 }
1962 break;
1963 case OP_UPDATE:
1964 for (int i=0; i<N; i++) {
1965 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001966 mBgAllAppsList.updatePackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001967 }
1968 break;
1969 case OP_REMOVE:
1970 case OP_UNAVAILABLE:
1971 for (int i=0; i<N; i++) {
1972 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001973 mBgAllAppsList.removePackage(packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001974 }
1975 break;
1976 }
1977
1978 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04001979 ArrayList<ApplicationInfo> modified = null;
1980
Adam Cohen487f7dd2012-06-28 18:12:10 -07001981 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07001982 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
1983 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001984 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001985 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07001986 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
1987 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001988 }
Winson Chungcd810732012-06-18 16:45:43 -07001989 // We may be removing packages that have no associated launcher application, so we
1990 // pass through the removed package names directly.
1991 // NOTE: We flush the icon cache aggressively in removePackage() above.
1992 final ArrayList<String> removedPackageNames = new ArrayList<String>();
Winson Chung5d55f332012-07-16 20:45:03 -07001993 if (mBgAllAppsList.removed.size() > 0) {
1994 mBgAllAppsList.removed.clear();
1995
1996 for (int i = 0; i < N; ++i) {
1997 removedPackageNames.add(packages[i]);
1998 }
Winson Chungcd810732012-06-18 16:45:43 -07001999 }
2000
Joe Onorato36115782010-06-17 13:28:48 -04002001 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2002 if (callbacks == null) {
2003 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2004 return;
2005 }
2006
2007 if (added != null) {
2008 final ArrayList<ApplicationInfo> addedFinal = added;
2009 mHandler.post(new Runnable() {
2010 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002011 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2012 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002013 callbacks.bindAppsAdded(addedFinal);
2014 }
2015 }
2016 });
2017 }
2018 if (modified != null) {
2019 final ArrayList<ApplicationInfo> modifiedFinal = modified;
2020 mHandler.post(new Runnable() {
2021 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002022 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2023 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002024 callbacks.bindAppsUpdated(modifiedFinal);
2025 }
2026 }
2027 });
2028 }
Winson Chungcd810732012-06-18 16:45:43 -07002029 if (!removedPackageNames.isEmpty()) {
Joe Onorato36115782010-06-17 13:28:48 -04002030 final boolean permanent = mOp != OP_UNAVAILABLE;
Joe Onorato36115782010-06-17 13:28:48 -04002031 mHandler.post(new Runnable() {
2032 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002033 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2034 if (callbacks == cb && cb != null) {
Winson Chungcd810732012-06-18 16:45:43 -07002035 callbacks.bindAppsRemoved(removedPackageNames, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04002036 }
2037 }
2038 });
Joe Onoratobe386092009-11-17 17:32:16 -08002039 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002040
2041 mHandler.post(new Runnable() {
2042 @Override
2043 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002044 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2045 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002046 callbacks.bindPackagesUpdated();
2047 }
2048 }
2049 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 }
2051 }
2052
2053 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002054 * This is called from the code that adds shortcuts from the intent receiver. This
2055 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002056 */
Joe Onorato56d82912010-03-07 14:32:10 -05002057 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002058 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002059 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002060
Joe Onorato56d82912010-03-07 14:32:10 -05002061 /**
2062 * Make an ShortcutInfo object for a shortcut that is an application.
2063 *
2064 * If c is not null, then it will be used to fill in missing data like the title and icon.
2065 */
2066 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002067 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002068 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002069 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002070
2071 ComponentName componentName = intent.getComponent();
2072 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002073 return null;
2074 }
2075
Adam Cohen00fcb492011-11-02 21:53:47 -07002076 try {
2077 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2078 if (!pi.applicationInfo.enabled) {
2079 // If we return null here, the corresponding item will be removed from the launcher
2080 // db and will not appear in the workspace.
2081 return null;
2082 }
2083 } catch (NameNotFoundException e) {
2084 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
2085 }
2086
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002087 // TODO: See if the PackageManager knows about this case. If it doesn't
2088 // then return null & delete this.
2089
Joe Onorato56d82912010-03-07 14:32:10 -05002090 // the resource -- This may implicitly give us back the fallback icon,
2091 // but don't worry about that. All we're doing with usingFallbackIcon is
2092 // to avoid saving lots of copies of that in the database, and most apps
2093 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002094
2095 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2096 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2097 // via resolveActivity().
2098 ResolveInfo resolveInfo = null;
2099 ComponentName oldComponent = intent.getComponent();
2100 Intent newIntent = new Intent(intent.getAction(), null);
2101 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2102 newIntent.setPackage(oldComponent.getPackageName());
2103 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2104 for (ResolveInfo i : infos) {
2105 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2106 i.activityInfo.name);
2107 if (cn.equals(oldComponent)) {
2108 resolveInfo = i;
2109 }
2110 }
2111 if (resolveInfo == null) {
2112 resolveInfo = manager.resolveActivity(intent, 0);
2113 }
Joe Onorato56d82912010-03-07 14:32:10 -05002114 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002115 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002116 }
Joe Onorato56d82912010-03-07 14:32:10 -05002117 // the db
2118 if (icon == null) {
2119 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002120 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002121 }
2122 }
2123 // the fallback icon
2124 if (icon == null) {
2125 icon = getFallbackIcon();
2126 info.usingFallbackIcon = true;
2127 }
2128 info.setIcon(icon);
2129
2130 // from the resource
2131 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002132 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2133 if (labelCache != null && labelCache.containsKey(key)) {
2134 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002135 } else {
2136 info.title = resolveInfo.activityInfo.loadLabel(manager);
2137 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002138 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002139 }
2140 }
Joe Onorato56d82912010-03-07 14:32:10 -05002141 }
2142 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002144 if (c != null) {
2145 info.title = c.getString(titleIndex);
2146 }
2147 }
2148 // fall back to the class name of the activity
2149 if (info.title == null) {
2150 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002151 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002152 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2153 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002157 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002159 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002160 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2161 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162
Joe Onorato56d82912010-03-07 14:32:10 -05002163 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002164 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002167 // TODO: If there's an explicit component and we can't install that, delete it.
2168
Joe Onorato56d82912010-03-07 14:32:10 -05002169 info.title = c.getString(titleIndex);
2170
Joe Onorato9c1289c2009-08-17 11:03:03 -04002171 int iconType = c.getInt(iconTypeIndex);
2172 switch (iconType) {
2173 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2174 String packageName = c.getString(iconPackageIndex);
2175 String resourceName = c.getString(iconResourceIndex);
2176 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002177 info.customIcon = false;
2178 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002181 if (resources != null) {
2182 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002183 icon = Utilities.createIconBitmap(
2184 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002187 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 }
Joe Onorato56d82912010-03-07 14:32:10 -05002189 // the db
2190 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002191 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002192 }
2193 // the fallback icon
2194 if (icon == null) {
2195 icon = getFallbackIcon();
2196 info.usingFallbackIcon = true;
2197 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198 break;
2199 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002200 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002201 if (icon == null) {
2202 icon = getFallbackIcon();
2203 info.customIcon = false;
2204 info.usingFallbackIcon = true;
2205 } else {
2206 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002207 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002208 break;
2209 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002210 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002211 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002212 info.customIcon = false;
2213 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002215 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 return info;
2217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218
Michael Jurka931dc972011-08-05 15:08:15 -07002219 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002220 @SuppressWarnings("all") // suppress dead code warning
2221 final boolean debug = false;
2222 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002223 Log.d(TAG, "getIconFromCursor app="
2224 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2225 }
2226 byte[] data = c.getBlob(iconIndex);
2227 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002228 return Utilities.createIconBitmap(
2229 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002230 } catch (Exception e) {
2231 return null;
2232 }
2233 }
2234
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2236 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002237 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002238 if (info == null) {
2239 return null;
2240 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002242
2243 return info;
2244 }
2245
Winson Chunga9abd0e2010-10-27 17:18:37 -07002246 /**
Winson Chung55cef262010-10-28 14:14:18 -07002247 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2248 */
2249 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2250 ComponentName component) {
2251 List<AppWidgetProviderInfo> widgets =
2252 AppWidgetManager.getInstance(context).getInstalledProviders();
2253 for (AppWidgetProviderInfo info : widgets) {
2254 if (info.provider.equals(component)) {
2255 return info;
2256 }
2257 }
2258 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002259 }
2260
Winson Chung68846fd2010-10-29 11:00:27 -07002261 /**
2262 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2263 */
2264 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2265 final PackageManager packageManager = context.getPackageManager();
2266 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2267 new ArrayList<WidgetMimeTypeHandlerData>();
2268
2269 final Intent supportsIntent =
2270 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2271 supportsIntent.setType(mimeType);
2272
2273 // Create a set of widget configuration components that we can test against
2274 final List<AppWidgetProviderInfo> widgets =
2275 AppWidgetManager.getInstance(context).getInstalledProviders();
2276 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2277 new HashMap<ComponentName, AppWidgetProviderInfo>();
2278 for (AppWidgetProviderInfo info : widgets) {
2279 configurationComponentToWidget.put(info.configure, info);
2280 }
2281
2282 // Run through each of the intents that can handle this type of clip data, and cross
2283 // reference them with the components that are actual configuration components
2284 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2285 PackageManager.MATCH_DEFAULT_ONLY);
2286 for (ResolveInfo info : activities) {
2287 final ActivityInfo activityInfo = info.activityInfo;
2288 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2289 activityInfo.name);
2290 if (configurationComponentToWidget.containsKey(infoComponent)) {
2291 supportedConfigurationActivities.add(
2292 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2293 configurationComponentToWidget.get(infoComponent)));
2294 }
2295 }
2296 return supportedConfigurationActivities;
2297 }
2298
Winson Chunga9abd0e2010-10-27 17:18:37 -07002299 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002300 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2301 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2302 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2303
Adam Cohend9198822011-11-22 16:42:47 -08002304 if (intent == null) {
2305 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2306 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2307 return null;
2308 }
2309
Joe Onorato0589f0f2010-02-08 13:44:00 -08002310 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002311 boolean customIcon = false;
2312 ShortcutIconResource iconResource = null;
2313
2314 if (bitmap != null && bitmap instanceof Bitmap) {
2315 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002316 customIcon = true;
2317 } else {
2318 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2319 if (extra != null && extra instanceof ShortcutIconResource) {
2320 try {
2321 iconResource = (ShortcutIconResource) extra;
2322 final PackageManager packageManager = context.getPackageManager();
2323 Resources resources = packageManager.getResourcesForApplication(
2324 iconResource.packageName);
2325 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002326 icon = Utilities.createIconBitmap(
2327 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002328 } catch (Exception e) {
2329 Log.w(TAG, "Could not load shortcut icon: " + extra);
2330 }
2331 }
2332 }
2333
Michael Jurkac9d95c52011-08-29 14:03:34 -07002334 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002335
2336 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002337 if (fallbackIcon != null) {
2338 icon = fallbackIcon;
2339 } else {
2340 icon = getFallbackIcon();
2341 info.usingFallbackIcon = true;
2342 }
Joe Onorato56d82912010-03-07 14:32:10 -05002343 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002344 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002345
Joe Onorato0589f0f2010-02-08 13:44:00 -08002346 info.title = name;
2347 info.intent = intent;
2348 info.customIcon = customIcon;
2349 info.iconResource = iconResource;
2350
2351 return info;
2352 }
2353
Winson Chungaac01e12011-08-17 10:37:13 -07002354 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2355 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002356 // If apps can't be on SD, don't even bother.
2357 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002358 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002359 }
Joe Onorato56d82912010-03-07 14:32:10 -05002360 // If this icon doesn't have a custom icon, check to see
2361 // what's stored in the DB, and if it doesn't match what
2362 // we're going to show, store what we are going to show back
2363 // into the DB. We do this so when we're loading, if the
2364 // package manager can't find an icon (for example because
2365 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002366 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002367 cache.put(info, c.getBlob(iconIndex));
2368 return true;
2369 }
2370 return false;
2371 }
2372 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2373 boolean needSave = false;
2374 try {
2375 if (data != null) {
2376 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2377 Bitmap loaded = info.getIcon(mIconCache);
2378 needSave = !saved.sameAs(loaded);
2379 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002380 needSave = true;
2381 }
Winson Chungaac01e12011-08-17 10:37:13 -07002382 } catch (Exception e) {
2383 needSave = true;
2384 }
2385 if (needSave) {
2386 Log.d(TAG, "going to save icon bitmap for info=" + info);
2387 // This is slower than is ideal, but this only happens once
2388 // or when the app is updated with a new icon.
2389 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002390 }
2391 }
2392
Joe Onorato9c1289c2009-08-17 11:03:03 -04002393 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002394 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002395 * or make a new one.
2396 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002397 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002398 // See if a placeholder was created for us already
2399 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002400 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002401 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002402 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002403 folders.put(id, folderInfo);
2404 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002405 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
2407
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08002409 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04002410 = new Comparator<ApplicationInfo>() {
2411 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07002412 int result = sCollator.compare(a.title.toString(), b.title.toString());
2413 if (result == 0) {
2414 result = a.componentName.compareTo(b.componentName);
2415 }
2416 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 };
Winson Chung78403fe2011-01-21 15:38:02 -08002419 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2420 = new Comparator<ApplicationInfo>() {
2421 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2422 if (a.firstInstallTime < b.firstInstallTime) return 1;
2423 if (a.firstInstallTime > b.firstInstallTime) return -1;
2424 return 0;
2425 }
2426 };
Winson Chung785d2eb2011-04-14 16:08:02 -07002427 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
2428 = new Comparator<AppWidgetProviderInfo>() {
2429 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2430 return sCollator.compare(a.label.toString(), b.label.toString());
2431 }
2432 };
Winson Chung5308f242011-08-18 12:12:41 -07002433 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2434 if (info.activityInfo != null) {
2435 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2436 } else {
2437 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2438 }
2439 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002440 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
2441 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002442 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002443 ShortcutNameComparator(PackageManager pm) {
2444 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002445 mLabelCache = new HashMap<Object, CharSequence>();
2446 }
2447 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2448 mPackageManager = pm;
2449 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002450 }
2451 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002452 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002453 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2454 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2455 if (mLabelCache.containsKey(keyA)) {
2456 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002457 } else {
2458 labelA = a.loadLabel(mPackageManager).toString();
2459
Winson Chung5308f242011-08-18 12:12:41 -07002460 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002461 }
Winson Chung5308f242011-08-18 12:12:41 -07002462 if (mLabelCache.containsKey(keyB)) {
2463 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002464 } else {
2465 labelB = b.loadLabel(mPackageManager).toString();
2466
Winson Chung5308f242011-08-18 12:12:41 -07002467 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002468 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002469 return sCollator.compare(labelA, labelB);
2470 }
2471 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002472 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
2473 private PackageManager mPackageManager;
2474 private HashMap<Object, String> mLabelCache;
2475 WidgetAndShortcutNameComparator(PackageManager pm) {
2476 mPackageManager = pm;
2477 mLabelCache = new HashMap<Object, String>();
2478 }
2479 public final int compare(Object a, Object b) {
2480 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002481 if (mLabelCache.containsKey(a)) {
2482 labelA = mLabelCache.get(a);
2483 } else {
2484 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002485 ((AppWidgetProviderInfo) a).label :
2486 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002487 mLabelCache.put(a, labelA);
2488 }
2489 if (mLabelCache.containsKey(b)) {
2490 labelB = mLabelCache.get(b);
2491 } else {
2492 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002493 ((AppWidgetProviderInfo) b).label :
2494 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002495 mLabelCache.put(b, labelB);
2496 }
Winson Chung1ed747a2011-05-03 16:18:34 -07002497 return sCollator.compare(labelA, labelB);
2498 }
2499 };
Joe Onoratobe386092009-11-17 17:32:16 -08002500
2501 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002502 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002503 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2504 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2505 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2506 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002507 if (mLoaderTask != null) {
2508 mLoaderTask.dumpState();
2509 } else {
2510 Log.d(TAG, "mLoaderTask=null");
2511 }
Joe Onoratobe386092009-11-17 17:32:16 -08002512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513}