blob: 269a0fd603c24080bd1f353d338a3672f04f1eff [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
Winson Chung81b52252012-08-27 15:34:29 -070085 // Specific runnable types that are run on the main thread deferred handler, this allows us to
86 // clear all queued binding runnables when the Launcher activity is destroyed.
87 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
88 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
89
90
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070091 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
92 static {
93 sWorkerThread.start();
94 }
95 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
96
Joe Onoratocc67f472010-06-08 10:54:30 -070097 // We start off with everything not loaded. After that, we assume that
98 // our monitoring of the package manager provides all updates and we never
99 // need to do a requery. These are only ever touched from the loader thread.
100 private boolean mWorkspaceLoaded;
101 private boolean mAllAppsLoaded;
102
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700103 // When we are loading pages synchronously, we can't just post the binding of items on the side
104 // pages as this delays the rotation process. Instead, we wait for a callback from the first
105 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
106 // a normal load, we also clear this set of Runnables.
107 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700111 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700112 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800113
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700114 // The lock that must be acquired before referencing any static bg data structures. Unlike
115 // other locks, this one can generally be held long-term because we never expect any of these
116 // static data structures to be referenced outside of the worker thread except on the first
117 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700118 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700119
Adam Cohen487f7dd2012-06-28 18:12:10 -0700120 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700121 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700122 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
125 // created by LauncherModel that are directly on the home screen (however, no widgets or
126 // shortcuts within folders).
127 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700128
Adam Cohen487f7dd2012-06-28 18:12:10 -0700129 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
130 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131 new ArrayList<LauncherAppWidgetInfo>();
132
Adam Cohen487f7dd2012-06-28 18:12:10 -0700133 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
134 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700135
Adam Cohen487f7dd2012-06-28 18:12:10 -0700136 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
137 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138 // </ only access in worker thread >
139
140 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Adam Cohend22015c2010-07-26 22:02:18 -0700143 private static int mCellCountX;
144 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700145
Reena Lee99a73f32011-10-24 17:27:37 -0700146 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700147
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700149 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public int getCurrentWorkspaceScreen();
151 public void startBinding();
152 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500153 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 public void finishBindingItems();
155 public void bindAppWidget(LauncherAppWidgetInfo info);
156 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500157 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
158 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chungcd810732012-06-18 16:45:43 -0700159 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700160 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400161 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800162 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100163 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700164 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Joe Onorato0589f0f2010-02-08 13:44:00 -0800167 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800168 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400169 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700170 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800171 mIconCache = iconCache;
172
173 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700174 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400175
Reena Lee93f824a2011-09-23 17:20:28 -0700176 final Resources res = app.getResources();
177 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
178 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700179 Configuration config = res.getConfiguration();
180 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800181 }
182
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700183 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
184 * posted on the main thread handler. */
185 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700186 runOnMainThread(r, 0);
187 }
188 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700189 if (sWorkerThread.getThreadId() == Process.myTid()) {
190 // If we are on the worker thread, post onto the main handler
191 mHandler.post(r);
192 } else {
193 r.run();
194 }
195 }
196
197 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
198 * posted on the worker thread handler. */
199 private static void runOnWorkerThread(Runnable r) {
200 if (sWorkerThread.getThreadId() == Process.myTid()) {
201 r.run();
202 } else {
203 // If we are not on the worker thread, then post to the worker handler
204 sWorker.post(r);
205 }
206 }
207
Joe Onorato56d82912010-03-07 14:32:10 -0500208 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chung81b52252012-08-27 15:34:29 -0700212 public void unbindItemInfosAndClearQueuedBindRunnables() {
213 if (sWorkerThread.getThreadId() == Process.myTid()) {
214 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
215 "main thread");
216 }
217
218 // Clear any deferred bind runnables
219 mDeferredBindRunnables.clear();
220 // Remove any queued bind runnables
221 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
222 // Unbind all the workspace items
223 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700224 }
225
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700226 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700227 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700228 // Ensure that we don't use the same workspace items data structure on the main thread
229 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700230 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
231 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700232 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700233 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
234 tmpAppWidgets.addAll(sBgAppWidgets);
235 }
236 Runnable r = new Runnable() {
237 @Override
238 public void run() {
239 for (ItemInfo item : tmpWorkspaceItems) {
240 item.unbind();
241 }
242 for (ItemInfo item : tmpAppWidgets) {
243 item.unbind();
244 }
245 }
246 };
247 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700248 }
249
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250 /**
251 * Adds an item to the DB if it was not created previously, or move it to a new
252 * <container, screen, cellX, cellY>
253 */
254 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
255 int screen, int cellX, int cellY) {
256 if (item.container == ItemInfo.NO_ID) {
257 // From all apps
258 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
259 } else {
260 // From somewhere else
261 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 }
263 }
264
Michael Jurka816474f2012-06-25 14:49:02 -0700265 static void checkItemInfo(final ItemInfo item) {
266 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
267 final long itemId = item.id;
268 Runnable r = new Runnable() {
269 public void run() {
Winson Chung2abf94d2012-07-18 18:16:38 -0700270 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700271 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
272 if (modelItem != null && item != modelItem) {
273 // the modelItem needs to match up perfectly with item if our model is
274 // to be consistent with the database-- for now, just require
275 // modelItem == item
276 String msg = "item: " + ((item != null) ? item.toString() : "null") +
277 "modelItem: " +
278 ((modelItem != null) ? modelItem.toString() : "null") +
279 "Error: ItemInfo passed to checkItemInfo doesn't match original";
280 RuntimeException e = new RuntimeException(msg);
281 e.setStackTrace(stackTrace);
282 throw e;
283 }
Michael Jurka816474f2012-06-25 14:49:02 -0700284 }
285 }
286 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700287 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700288 }
289
Michael Jurkac9d95c52011-08-29 14:03:34 -0700290 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
291 final ItemInfo item, final String callingFunction) {
292 final long itemId = item.id;
293 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
294 final ContentResolver cr = context.getContentResolver();
295
Adam Cohen487f7dd2012-06-28 18:12:10 -0700296 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700297 Runnable r = new Runnable() {
298 public void run() {
299 cr.update(uri, values, null, null);
300
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700301 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700302 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700303 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
304 if (item != modelItem) {
305 // the modelItem needs to match up perfectly with item if our model is to be
306 // consistent with the database-- for now, just require modelItem == item
307 String msg = "item: " + ((item != null) ? item.toString() : "null") +
308 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
309 "Error: ItemInfo passed to " + callingFunction + " doesn't match " +
310 "original";
311 throw new RuntimeException(msg);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700312 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700313
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700314 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
315 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
316 // Item is in a folder, make sure this folder exists
317 if (!sBgFolders.containsKey(item.container)) {
318 // An items container is being set to a that of an item which is not in
319 // the list of Folders.
320 String msg = "item: " + item + " container being set to: " +
321 item.container + ", not in the list of folders";
322 RuntimeException e = new RuntimeException(msg);
323 e.setStackTrace(stackTrace);
324 Launcher.dumpDebugLogsToConsole();
325 throw e;
326 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700327 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700328
329 // Items are added/removed from the corresponding FolderInfo elsewhere, such
330 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
331 // that are on the desktop, as appropriate
332 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
333 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
334 switch (modelItem.itemType) {
335 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
336 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
337 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
338 if (!sBgWorkspaceItems.contains(modelItem)) {
339 sBgWorkspaceItems.add(modelItem);
340 }
341 break;
342 default:
343 break;
344 }
345 } else {
346 sBgWorkspaceItems.remove(modelItem);
347 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700348 }
349 }
350 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700351 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700352 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700356 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700357 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
358 final int screen, final int cellX, final int cellY) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700359 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
360 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
361 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
362 Launcher.sDumpLogs.add(transaction);
363 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 item.cellX = cellX;
366 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700367
Winson Chung3d503fb2011-07-13 17:25:49 -0700368 // We store hotseat items in canonical form which is this orientation invariant position
369 // in the hotseat
370 if (context instanceof Launcher && screen < 0 &&
371 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
372 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
373 } else {
374 item.screen = screen;
375 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376
377 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700379 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
380 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
382
Michael Jurkac9d95c52011-08-29 14:03:34 -0700383 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700384 }
385
386 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700387 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800388 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700389 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
390 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700391 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
392 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
393 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
394 Launcher.sDumpLogs.add(transaction);
395 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800396 item.cellX = cellX;
397 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700398 item.spanX = spanX;
399 item.spanY = spanY;
400
401 // We store hotseat items in canonical form which is this orientation invariant position
402 // in the hotseat
403 if (context instanceof Launcher && screen < 0 &&
404 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
405 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
406 } else {
407 item.screen = screen;
408 }
Adam Cohend4844c32011-02-18 19:25:06 -0800409
Adam Cohend4844c32011-02-18 19:25:06 -0800410 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800411 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700412 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
413 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
414 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
415 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
416 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800417
Michael Jurka816474f2012-06-25 14:49:02 -0700418 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700419 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700420
421 /**
422 * Update an item to the database in a specified container.
423 */
424 static void updateItemInDatabase(Context context, final ItemInfo item) {
425 final ContentValues values = new ContentValues();
426 item.onAddToDatabase(values);
427 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
428 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800429 }
430
431 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 * Returns true if the shortcuts already exists in the database.
433 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400435 static boolean shortcutExists(Context context, String title, Intent intent) {
436 final ContentResolver cr = context.getContentResolver();
437 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
438 new String[] { "title", "intent" }, "title=? and intent=?",
439 new String[] { title, intent.toUri(0) }, null);
440 boolean result = false;
441 try {
442 result = c.moveToFirst();
443 } finally {
444 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700447 }
448
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700450 * Returns an ItemInfo array containing all the items in the LauncherModel.
451 * The ItemInfo.id is not set through this function.
452 */
453 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
454 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
455 final ContentResolver cr = context.getContentResolver();
456 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
457 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
458 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
459 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
460
461 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
462 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
463 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
464 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
465 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
466 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
467 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
468
469 try {
470 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700471 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700472 item.cellX = c.getInt(cellXIndex);
473 item.cellY = c.getInt(cellYIndex);
474 item.spanX = c.getInt(spanXIndex);
475 item.spanY = c.getInt(spanYIndex);
476 item.container = c.getInt(containerIndex);
477 item.itemType = c.getInt(itemTypeIndex);
478 item.screen = c.getInt(screenIndex);
479
480 items.add(item);
481 }
482 } catch (Exception e) {
483 items.clear();
484 } finally {
485 c.close();
486 }
487
488 return items;
489 }
490
491 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400492 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
493 */
494 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
495 final ContentResolver cr = context.getContentResolver();
496 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
497 "_id=? and (itemType=? or itemType=?)",
498 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700499 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700500
Joe Onorato9c1289c2009-08-17 11:03:03 -0400501 try {
502 if (c.moveToFirst()) {
503 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
504 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
505 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
506 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
507 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
508 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509
Joe Onorato9c1289c2009-08-17 11:03:03 -0400510 FolderInfo folderInfo = null;
511 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700512 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
513 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700515 }
516
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517 folderInfo.title = c.getString(titleIndex);
518 folderInfo.id = id;
519 folderInfo.container = c.getInt(containerIndex);
520 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700521 folderInfo.cellX = c.getInt(cellXIndex);
522 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400523
524 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700525 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526 } finally {
527 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700528 }
529
530 return null;
531 }
532
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 /**
534 * Add an item to the database in a specified container. Sets the container, screen, cellX and
535 * cellY fields of the item. Also assigns an ID to the item.
536 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700537 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
538 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400539 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400540 item.cellX = cellX;
541 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700542 // We store hotseat items in canonical form which is this orientation invariant position
543 // in the hotseat
544 if (context instanceof Launcher && screen < 0 &&
545 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
546 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
547 } else {
548 item.screen = screen;
549 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550
551 final ContentValues values = new ContentValues();
552 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553 item.onAddToDatabase(values);
554
Michael Jurka7578ec62011-08-03 14:11:54 -0700555 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700556 item.id = app.getLauncherProvider().generateNewId();
557 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700558 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700559
Adam Cohen487f7dd2012-06-28 18:12:10 -0700560 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
561
Michael Jurkac9d95c52011-08-29 14:03:34 -0700562 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700563 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700564 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
565 + item.id + " (" + container + ", " + screen + ", " + cellX + ", "
566 + cellY + ")";
567 Launcher.sDumpLogs.add(transaction);
568 Log.d(TAG, transaction);
569
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700570 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
571 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700573 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700574 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700575 if (sBgItemsIdMap.containsKey(item.id)) {
576 // we should not be adding new items in the db with the same id
577 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
578 "addItemToDatabase already exists." + item.toString());
579 }
580 sBgItemsIdMap.put(item.id, item);
581 switch (item.itemType) {
582 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
583 sBgFolders.put(item.id, (FolderInfo) item);
584 // Fall through
585 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
586 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
587 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
588 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
589 sBgWorkspaceItems.add(item);
590 } else {
591 if (!sBgFolders.containsKey(item.container)) {
592 // Adding an item to a folder that doesn't exist.
593 String msg = "adding item: " + item + " to a folder that " +
594 " doesn't exist";
595 RuntimeException e = new RuntimeException(msg);
596 e.setStackTrace(stackTrace);
597 Launcher.dumpDebugLogsToConsole();
598 throw e;
599 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700600 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700601 break;
602 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
603 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
604 break;
605 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700606 }
607 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700608 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700609 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700610 }
611
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700613 * Creates a new unique child id, for a given cell span across all layouts.
614 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700615 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700616 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
617 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700618 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700619 }
620
Adam Cohend22015c2010-07-26 22:02:18 -0700621 static int getCellCountX() {
622 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700623 }
624
Adam Cohend22015c2010-07-26 22:02:18 -0700625 static int getCellCountY() {
626 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700627 }
628
629 /**
630 * Updates the model orientation helper to take into account the current layout dimensions
631 * when performing local/canonical coordinate transformations.
632 */
633 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700634 mCellCountX = shortAxisCellCount;
635 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 }
637
638 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700639 * Removes the specified item from the database
640 * @param context
641 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700643 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700645 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700646 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
647
Michael Jurka83df1882011-08-31 20:59:26 -0700648 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700649 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700650 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
651 + item.id + " (" + item.container + ", " + item.screen + ", " + item.cellX +
652 ", " + item.cellY + ")";
653 Launcher.sDumpLogs.add(transaction);
654 Log.d(TAG, transaction);
655
Michael Jurkac9d95c52011-08-29 14:03:34 -0700656 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700657
658 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700659 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700660 switch (item.itemType) {
661 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
662 sBgFolders.remove(item.id);
663 for (ItemInfo info: sBgItemsIdMap.values()) {
664 if (info.container == item.id) {
665 // We are deleting a folder which still contains items that
666 // think they are contained by that folder.
667 String msg = "deleting a folder (" + item + ") which still " +
668 "contains items (" + info + ")";
669 RuntimeException e = new RuntimeException(msg);
670 e.setStackTrace(stackTrace);
671 Launcher.dumpDebugLogsToConsole();
672 throw e;
673 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700674 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700675 sBgWorkspaceItems.remove(item);
676 break;
677 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
678 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
679 sBgWorkspaceItems.remove(item);
680 break;
681 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
682 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
683 break;
684 }
685 sBgItemsIdMap.remove(item.id);
686 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700687 }
688 }
Michael Jurka83df1882011-08-31 20:59:26 -0700689 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700690 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 }
692
693 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694 * Remove the contents of the specified folder from the database
695 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700696 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 final ContentResolver cr = context.getContentResolver();
698
Michael Jurkac9d95c52011-08-29 14:03:34 -0700699 Runnable r = new Runnable() {
700 public void run() {
701 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700702 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700703 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700704 sBgItemsIdMap.remove(info.id);
705 sBgFolders.remove(info.id);
706 sBgDbIconCache.remove(info);
707 sBgWorkspaceItems.remove(info);
708 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700709
Michael Jurkac9d95c52011-08-29 14:03:34 -0700710 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
711 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700712 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700713 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700714 for (ItemInfo childInfo : info.contents) {
715 sBgItemsIdMap.remove(childInfo.id);
716 sBgDbIconCache.remove(childInfo);
717 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700718 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700719 }
720 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700721 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 }
723
724 /**
725 * Set this as the current Launcher activity object for the loader.
726 */
727 public void initialize(Callbacks callbacks) {
728 synchronized (mLock) {
729 mCallbacks = new WeakReference<Callbacks>(callbacks);
730 }
731 }
732
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700733 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
735 * ACTION_PACKAGE_CHANGED.
736 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100737 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400738 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400739 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700740
Joe Onorato36115782010-06-17 13:28:48 -0400741 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400742
Joe Onorato36115782010-06-17 13:28:48 -0400743 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
744 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
745 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
746 final String packageName = intent.getData().getSchemeSpecificPart();
747 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748
Joe Onorato36115782010-06-17 13:28:48 -0400749 int op = PackageUpdatedTask.OP_NONE;
750
751 if (packageName == null || packageName.length() == 0) {
752 // they sent us a bad intent
753 return;
754 }
755
756 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
757 op = PackageUpdatedTask.OP_UPDATE;
758 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
759 if (!replacing) {
760 op = PackageUpdatedTask.OP_REMOVE;
761 }
762 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
763 // later, we will update the package at this time
764 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
765 if (!replacing) {
766 op = PackageUpdatedTask.OP_ADD;
767 } else {
768 op = PackageUpdatedTask.OP_UPDATE;
769 }
770 }
771
772 if (op != PackageUpdatedTask.OP_NONE) {
773 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
774 }
775
776 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400777 // First, schedule to add these apps back in.
778 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
779 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
780 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700781 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400782 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
783 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
784 enqueuePackageUpdated(new PackageUpdatedTask(
785 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700786 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700787 // If we have changed locale we need to clear out the labels in all apps/workspace.
788 forceReload();
789 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
790 // Check if configuration change was an mcc/mnc change which would affect app resources
791 // and we would need to clear out the labels in all apps/workspace. Same handling as
792 // above for ACTION_LOCALE_CHANGED
793 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700794 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700795 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700796 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700797 forceReload();
798 }
799 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700800 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700801 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
802 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700803 if (mCallbacks != null) {
804 Callbacks callbacks = mCallbacks.get();
805 if (callbacks != null) {
806 callbacks.bindSearchablesChanged();
807 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700808 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700809 }
810 }
811
Reena Lee93f824a2011-09-23 17:20:28 -0700812 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700813 resetLoadedState(true, true);
814
Reena Lee93f824a2011-09-23 17:20:28 -0700815 // Do this here because if the launcher activity is running it will be restarted.
816 // If it's not running startLoaderFromBackground will merely tell it that it needs
817 // to reload.
818 startLoaderFromBackground();
819 }
820
Winson Chungf0c6ae02012-03-21 16:10:31 -0700821 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
822 synchronized (mLock) {
823 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
824 // mWorkspaceLoaded to true later
825 stopLoaderLocked();
826 if (resetAllAppsLoaded) mAllAppsLoaded = false;
827 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
828 }
829 }
830
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700831 /**
832 * When the launcher is in the background, it's possible for it to miss paired
833 * configuration changes. So whenever we trigger the loader from the background
834 * tell the launcher that it needs to re-run the loader when it comes back instead
835 * of doing it now.
836 */
837 public void startLoaderFromBackground() {
838 boolean runLoader = false;
839 if (mCallbacks != null) {
840 Callbacks callbacks = mCallbacks.get();
841 if (callbacks != null) {
842 // Only actually run the loader if they're not paused.
843 if (!callbacks.setLoadOnResume()) {
844 runLoader = true;
845 }
846 }
847 }
848 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700849 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -0700850 }
Joe Onorato36115782010-06-17 13:28:48 -0400851 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400852
Reena Lee93f824a2011-09-23 17:20:28 -0700853 // If there is already a loader task running, tell it to stop.
854 // returns true if isLaunching() was true on the old task
855 private boolean stopLoaderLocked() {
856 boolean isLaunching = false;
857 LoaderTask oldTask = mLoaderTask;
858 if (oldTask != null) {
859 if (oldTask.isLaunching()) {
860 isLaunching = true;
861 }
862 oldTask.stopLocked();
863 }
864 return isLaunching;
865 }
866
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700867 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -0400868 synchronized (mLock) {
869 if (DEBUG_LOADERS) {
870 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
871 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700873 // Clear any deferred bind-runnables from the synchronized load process
874 // We must do this before any loading/binding is scheduled below.
875 mDeferredBindRunnables.clear();
876
Joe Onorato36115782010-06-17 13:28:48 -0400877 // Don't bother to start the thread if we know it's not going to do anything
878 if (mCallbacks != null && mCallbacks.get() != null) {
879 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700880 // also, don't downgrade isLaunching if we're already running
881 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700882 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
884 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
885 } else {
886 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
887 sWorker.post(mLoaderTask);
888 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890 }
891 }
892
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700893 void bindRemainingSynchronousPages() {
894 // Post the remaining side pages to be loaded
895 if (!mDeferredBindRunnables.isEmpty()) {
896 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -0700897 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700898 }
899 mDeferredBindRunnables.clear();
900 }
901 }
902
Joe Onorato36115782010-06-17 13:28:48 -0400903 public void stopLoader() {
904 synchronized (mLock) {
905 if (mLoaderTask != null) {
906 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907 }
908 }
Joe Onorato36115782010-06-17 13:28:48 -0400909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910
Michael Jurkac57b7a82011-08-09 22:02:20 -0700911 public boolean isAllAppsLoaded() {
912 return mAllAppsLoaded;
913 }
914
Winson Chung36a62fe2012-05-06 18:04:42 -0700915 boolean isLoadingWorkspace() {
916 synchronized (mLock) {
917 if (mLoaderTask != null) {
918 return mLoaderTask.isLoadingWorkspace();
919 }
920 }
921 return false;
922 }
923
Joe Onorato36115782010-06-17 13:28:48 -0400924 /**
925 * Runnable for the thread that loads the contents of the launcher:
926 * - workspace icons
927 * - widgets
928 * - all apps icons
929 */
930 private class LoaderTask implements Runnable {
931 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -0400932 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700933 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400934 private boolean mStopped;
935 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700936
Winson Chungc3eecff2011-07-11 17:44:15 -0700937 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400938
939 LoaderTask(Context context, boolean isLaunching) {
940 mContext = context;
941 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700942 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943 }
944
Joe Onorato36115782010-06-17 13:28:48 -0400945 boolean isLaunching() {
946 return mIsLaunching;
947 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948
Winson Chung36a62fe2012-05-06 18:04:42 -0700949 boolean isLoadingWorkspace() {
950 return mIsLoadingAndBindingWorkspace;
951 }
952
Joe Onorato36115782010-06-17 13:28:48 -0400953 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700954 mIsLoadingAndBindingWorkspace = true;
955
Joe Onorato36115782010-06-17 13:28:48 -0400956 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400957 if (DEBUG_LOADERS) {
958 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400959 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700960
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700961 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400962 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700963 synchronized (LoaderTask.this) {
964 if (mStopped) {
965 return;
966 }
967 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 }
969 }
970
Joe Onorato36115782010-06-17 13:28:48 -0400971 // Bind the workspace
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700972 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -0400973 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400974
Joe Onorato36115782010-06-17 13:28:48 -0400975 private void waitForIdle() {
976 // Wait until the either we're stopped or the other threads are done.
977 // This way we don't start loading all apps until the workspace has settled
978 // down.
979 synchronized (LoaderTask.this) {
980 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700981
Joe Onorato36115782010-06-17 13:28:48 -0400982 mHandler.postIdle(new Runnable() {
983 public void run() {
984 synchronized (LoaderTask.this) {
985 mLoadAndBindStepFinished = true;
986 if (DEBUG_LOADERS) {
987 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400988 }
Joe Onorato36115782010-06-17 13:28:48 -0400989 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400990 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400991 }
Joe Onorato36115782010-06-17 13:28:48 -0400992 });
993
994 while (!mStopped && !mLoadAndBindStepFinished) {
995 try {
996 this.wait();
997 } catch (InterruptedException ex) {
998 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400999 }
1000 }
Joe Onorato36115782010-06-17 13:28:48 -04001001 if (DEBUG_LOADERS) {
1002 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001003 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001004 + "ms for previous step to finish binding");
1005 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001006 }
Joe Onorato36115782010-06-17 13:28:48 -04001007 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001008
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 void runBindSynchronousPage(int synchronousBindPage) {
1010 if (synchronousBindPage < 0) {
1011 // Ensure that we have a valid page index to load synchronously
1012 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1013 "valid page index");
1014 }
1015 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1016 // Ensure that we don't try and bind a specified page when the pages have not been
1017 // loaded already (we should load everything asynchronously in that case)
1018 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1019 }
1020 synchronized (mLock) {
1021 if (mIsLoaderTaskRunning) {
1022 // Ensure that we are never running the background loading at this point since
1023 // we also touch the background collections
1024 throw new RuntimeException("Error! Background loading is already running");
1025 }
1026 }
1027
1028 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1029 // data structures, we can't allow any other thread to touch that data, but because
1030 // this call is synchronous, we can get away with not locking).
1031
Adam Cohena13a2f22012-07-23 14:29:15 -07001032 // The LauncherModel is static in the LauncherApplication and mHandler may have queued
1033 // operations from the previous activity. We need to ensure that all queued operations
1034 // are executed before any synchronous binding work is done.
1035 mHandler.flush();
1036
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001037 // Divide the set of loaded items into those that we are binding synchronously, and
1038 // everything else that is to be bound normally (asynchronously).
1039 bindWorkspace(synchronousBindPage);
1040 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1041 // arise from that.
1042 onlyBindAllApps();
1043 }
1044
Joe Onorato36115782010-06-17 13:28:48 -04001045 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001046 synchronized (mLock) {
1047 mIsLoaderTaskRunning = true;
1048 }
Joe Onorato36115782010-06-17 13:28:48 -04001049 // Optimize for end-user experience: if the Launcher is up and // running with the
1050 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1051 // workspace first (default).
1052 final Callbacks cbk = mCallbacks.get();
1053 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -04001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001056 // Elevate priority when Home launches for the first time to avoid
1057 // starving at boot time. Staring at a blank home is not cool.
1058 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001059 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1060 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001061 android.os.Process.setThreadPriority(mIsLaunching
1062 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1063 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001064 if (loadWorkspaceFirst) {
1065 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1066 loadAndBindWorkspace();
1067 } else {
1068 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001069 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001070 }
1071
Joe Onorato36115782010-06-17 13:28:48 -04001072 if (mStopped) {
1073 break keep_running;
1074 }
1075
1076 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1077 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001078 synchronized (mLock) {
1079 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001080 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001081 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1082 }
1083 }
Joe Onorato36115782010-06-17 13:28:48 -04001084 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001085
1086 // second step
1087 if (loadWorkspaceFirst) {
1088 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001089 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001090 } else {
1091 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
1092 loadAndBindWorkspace();
1093 }
Winson Chung7ed37742011-09-08 15:45:51 -07001094
1095 // Restore the default thread priority after we are done loading items
1096 synchronized (mLock) {
1097 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1098 }
Joe Onorato36115782010-06-17 13:28:48 -04001099 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001100
Winson Chungaac01e12011-08-17 10:37:13 -07001101
1102 // Update the saved icons if necessary
1103 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001104 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001105 for (Object key : sBgDbIconCache.keySet()) {
1106 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1107 }
1108 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001109 }
Winson Chungaac01e12011-08-17 10:37:13 -07001110
Joe Onorato36115782010-06-17 13:28:48 -04001111 // Clear out this reference, otherwise we end up holding it until all of the
1112 // callback runnables are done.
1113 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114
Joe Onorato36115782010-06-17 13:28:48 -04001115 synchronized (mLock) {
1116 // If we are still the last one to be scheduled, remove ourselves.
1117 if (mLoaderTask == this) {
1118 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001121 }
Joe Onorato36115782010-06-17 13:28:48 -04001122 }
1123
1124 public void stopLocked() {
1125 synchronized (LoaderTask.this) {
1126 mStopped = true;
1127 this.notify();
1128 }
1129 }
1130
1131 /**
1132 * Gets the callbacks object. If we've been stopped, or if the launcher object
1133 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1134 * object that was around when the deferred message was scheduled, and if there's
1135 * a new Callbacks object around then also return null. This will save us from
1136 * calling onto it with data that will be ignored.
1137 */
1138 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1139 synchronized (mLock) {
1140 if (mStopped) {
1141 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001142 }
Joe Onorato36115782010-06-17 13:28:48 -04001143
1144 if (mCallbacks == null) {
1145 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001146 }
Joe Onorato36115782010-06-17 13:28:48 -04001147
1148 final Callbacks callbacks = mCallbacks.get();
1149 if (callbacks != oldCallbacks) {
1150 return null;
1151 }
1152 if (callbacks == null) {
1153 Log.w(TAG, "no mCallbacks");
1154 return null;
1155 }
1156
1157 return callbacks;
1158 }
1159 }
1160
1161 // check & update map of what's occupied; used to discard overlapping/invalid items
1162 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001163 int containerIndex = item.screen;
1164 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001165 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08001166 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001167 return false;
1168 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001169
1170 // We use the last index to refer to the hotseat and the screen as the rank, so
1171 // test and update the occupied state accordingly
1172 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
1173 Log.e(TAG, "Error loading shortcut into hotseat " + item
1174 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
1175 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
1176 return false;
1177 } else {
1178 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
1179 return true;
1180 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001181 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1182 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001183 return true;
1184 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001185
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001186 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001187 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1188 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001189 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001190 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -07001191 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001192 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001193 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -07001194 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001195 return false;
1196 }
1197 }
1198 }
1199 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1200 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001201 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001202 }
1203 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001204
Joe Onorato36115782010-06-17 13:28:48 -04001205 return true;
1206 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207
Joe Onorato36115782010-06-17 13:28:48 -04001208 private void loadWorkspace() {
1209 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210
Joe Onorato36115782010-06-17 13:28:48 -04001211 final Context context = mContext;
1212 final ContentResolver contentResolver = context.getContentResolver();
1213 final PackageManager manager = context.getPackageManager();
1214 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1215 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001216
Michael Jurkab85f8a42012-04-25 15:48:32 -07001217 // Make sure the default workspace is loaded, if needed
1218 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary();
1219
Winson Chung2abf94d2012-07-18 18:16:38 -07001220 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001221 sBgWorkspaceItems.clear();
1222 sBgAppWidgets.clear();
1223 sBgFolders.clear();
1224 sBgItemsIdMap.clear();
1225 sBgDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001226
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001227 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001229 final Cursor c = contentResolver.query(
1230 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001231
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001232 // +1 for the hotseat (it can be larger than the workspace)
1233 // Load workspace in reverse order to ensure that latest items are loaded first (and
1234 // before any earlier duplicates)
1235 final ItemInfo occupied[][][] =
1236 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001238 try {
1239 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1240 final int intentIndex = c.getColumnIndexOrThrow
1241 (LauncherSettings.Favorites.INTENT);
1242 final int titleIndex = c.getColumnIndexOrThrow
1243 (LauncherSettings.Favorites.TITLE);
1244 final int iconTypeIndex = c.getColumnIndexOrThrow(
1245 LauncherSettings.Favorites.ICON_TYPE);
1246 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1247 final int iconPackageIndex = c.getColumnIndexOrThrow(
1248 LauncherSettings.Favorites.ICON_PACKAGE);
1249 final int iconResourceIndex = c.getColumnIndexOrThrow(
1250 LauncherSettings.Favorites.ICON_RESOURCE);
1251 final int containerIndex = c.getColumnIndexOrThrow(
1252 LauncherSettings.Favorites.CONTAINER);
1253 final int itemTypeIndex = c.getColumnIndexOrThrow(
1254 LauncherSettings.Favorites.ITEM_TYPE);
1255 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1256 LauncherSettings.Favorites.APPWIDGET_ID);
1257 final int screenIndex = c.getColumnIndexOrThrow(
1258 LauncherSettings.Favorites.SCREEN);
1259 final int cellXIndex = c.getColumnIndexOrThrow
1260 (LauncherSettings.Favorites.CELLX);
1261 final int cellYIndex = c.getColumnIndexOrThrow
1262 (LauncherSettings.Favorites.CELLY);
1263 final int spanXIndex = c.getColumnIndexOrThrow
1264 (LauncherSettings.Favorites.SPANX);
1265 final int spanYIndex = c.getColumnIndexOrThrow(
1266 LauncherSettings.Favorites.SPANY);
1267 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1268 //final int displayModeIndex = c.getColumnIndexOrThrow(
1269 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001270
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001271 ShortcutInfo info;
1272 String intentDescription;
1273 LauncherAppWidgetInfo appWidgetInfo;
1274 int container;
1275 long id;
1276 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001278 while (!mStopped && c.moveToNext()) {
1279 try {
1280 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001281
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001282 switch (itemType) {
1283 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1284 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1285 intentDescription = c.getString(intentIndex);
1286 try {
1287 intent = Intent.parseUri(intentDescription, 0);
1288 } catch (URISyntaxException e) {
1289 continue;
1290 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001291
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001292 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1293 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1294 titleIndex, mLabelCache);
1295 } else {
1296 info = getShortcutInfo(c, context, iconTypeIndex,
1297 iconPackageIndex, iconResourceIndex, iconIndex,
1298 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001299
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001300 // App shortcuts that used to be automatically added to Launcher
1301 // didn't always have the correct intent flags set, so do that
1302 // here
1303 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001304 intent.getCategories() != null &&
1305 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001306 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001307 intent.addFlags(
1308 Intent.FLAG_ACTIVITY_NEW_TASK |
1309 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1310 }
Michael Jurka96879562012-03-22 05:54:33 -07001311 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001312
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001313 if (info != null) {
1314 info.intent = intent;
1315 info.id = c.getLong(idIndex);
1316 container = c.getInt(containerIndex);
1317 info.container = container;
1318 info.screen = c.getInt(screenIndex);
1319 info.cellX = c.getInt(cellXIndex);
1320 info.cellY = c.getInt(cellYIndex);
1321
1322 // check & update map of what's occupied
1323 if (!checkItemPlacement(occupied, info)) {
1324 break;
1325 }
1326
1327 switch (container) {
1328 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1329 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1330 sBgWorkspaceItems.add(info);
1331 break;
1332 default:
1333 // Item is in a user folder
1334 FolderInfo folderInfo =
1335 findOrMakeFolder(sBgFolders, container);
1336 folderInfo.add(info);
1337 break;
1338 }
1339 sBgItemsIdMap.put(info.id, info);
1340
1341 // now that we've loaded everthing re-save it with the
1342 // icon in case it disappears somehow.
1343 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
1344 } else {
1345 // Failed to load the shortcut, probably because the
1346 // activity manager couldn't resolve it (maybe the app
1347 // was uninstalled), or the db row was somehow screwed up.
1348 // Delete it.
1349 id = c.getLong(idIndex);
1350 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1351 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1352 id, false), null, null);
1353 }
1354 break;
1355
1356 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1357 id = c.getLong(idIndex);
1358 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1359
1360 folderInfo.title = c.getString(titleIndex);
1361 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001363 folderInfo.container = container;
1364 folderInfo.screen = c.getInt(screenIndex);
1365 folderInfo.cellX = c.getInt(cellXIndex);
1366 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367
Daniel Sandler8802e962010-05-26 16:28:16 -04001368 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001370 break;
1371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1374 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1375 sBgWorkspaceItems.add(folderInfo);
1376 break;
Joe Onorato36115782010-06-17 13:28:48 -04001377 }
Joe Onorato17a89222011-02-08 17:26:11 -08001378
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1380 sBgFolders.put(folderInfo.id, folderInfo);
1381 break;
1382
1383 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1384 // Read all Launcher-specific widget details
1385 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001386 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001387
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001388 final AppWidgetProviderInfo provider =
1389 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001390
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001391 if (!isSafeMode && (provider == null || provider.provider == null ||
1392 provider.provider.getPackageName() == null)) {
1393 String log = "Deleting widget that isn't installed anymore: id="
1394 + id + " appWidgetId=" + appWidgetId;
1395 Log.e(TAG, log);
1396 Launcher.sDumpLogs.add(log);
1397 itemsToRemove.add(id);
1398 } else {
1399 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1400 provider.provider);
1401 appWidgetInfo.id = id;
1402 appWidgetInfo.screen = c.getInt(screenIndex);
1403 appWidgetInfo.cellX = c.getInt(cellXIndex);
1404 appWidgetInfo.cellY = c.getInt(cellYIndex);
1405 appWidgetInfo.spanX = c.getInt(spanXIndex);
1406 appWidgetInfo.spanY = c.getInt(spanYIndex);
1407 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1408 appWidgetInfo.minSpanX = minSpan[0];
1409 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001410
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001411 container = c.getInt(containerIndex);
1412 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1413 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1414 Log.e(TAG, "Widget found where container != " +
1415 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1416 continue;
1417 }
1418 appWidgetInfo.container = c.getInt(containerIndex);
1419
1420 // check & update map of what's occupied
1421 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1422 break;
1423 }
1424 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1425 sBgAppWidgets.add(appWidgetInfo);
1426 }
Joe Onorato36115782010-06-17 13:28:48 -04001427 break;
1428 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001429 } catch (Exception e) {
1430 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001431 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432 }
1433 } finally {
1434 c.close();
1435 }
1436
1437 if (itemsToRemove.size() > 0) {
1438 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1439 LauncherSettings.Favorites.CONTENT_URI);
1440 // Remove dead items
1441 for (long id : itemsToRemove) {
1442 if (DEBUG_LOADERS) {
1443 Log.d(TAG, "Removed id = " + id);
1444 }
1445 // Don't notify content observers
1446 try {
1447 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1448 null, null);
1449 } catch (RemoteException e) {
1450 Log.w(TAG, "Could not remove id = " + id);
1451 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001452 }
1453 }
1454
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001455 if (DEBUG_LOADERS) {
1456 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1457 Log.d(TAG, "workspace layout: ");
1458 for (int y = 0; y < mCellCountY; y++) {
1459 String line = "";
1460 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1461 if (s > 0) {
1462 line += " | ";
1463 }
1464 for (int x = 0; x < mCellCountX; x++) {
1465 line += ((occupied[s][x][y] != null) ? "#" : ".");
1466 }
Joe Onorato36115782010-06-17 13:28:48 -04001467 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001469 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001470 }
Joe Onorato36115782010-06-17 13:28:48 -04001471 }
1472 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001473
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001474 /** Filters the set of items who are directly or indirectly (via another container) on the
1475 * specified screen. */
1476 private void filterCurrentWorkspaceItems(int currentScreen,
1477 ArrayList<ItemInfo> allWorkspaceItems,
1478 ArrayList<ItemInfo> currentScreenItems,
1479 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001480 // Purge any null ItemInfos
1481 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1482 while (iter.hasNext()) {
1483 ItemInfo i = iter.next();
1484 if (i == null) {
1485 iter.remove();
1486 }
1487 }
1488
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001489 // If we aren't filtering on a screen, then the set of items to load is the full set of
1490 // items given.
1491 if (currentScreen < 0) {
1492 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001493 }
1494
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001495 // Order the set of items by their containers first, this allows use to walk through the
1496 // list sequentially, build up a list of containers that are in the specified screen,
1497 // as well as all items in those containers.
1498 Set<Long> itemsOnScreen = new HashSet<Long>();
1499 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1500 @Override
1501 public int compare(ItemInfo lhs, ItemInfo rhs) {
1502 return (int) (lhs.container - rhs.container);
1503 }
1504 });
1505 for (ItemInfo info : allWorkspaceItems) {
1506 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1507 if (info.screen == currentScreen) {
1508 currentScreenItems.add(info);
1509 itemsOnScreen.add(info.id);
1510 } else {
1511 otherScreenItems.add(info);
1512 }
1513 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1514 currentScreenItems.add(info);
1515 itemsOnScreen.add(info.id);
1516 } else {
1517 if (itemsOnScreen.contains(info.container)) {
1518 currentScreenItems.add(info);
1519 itemsOnScreen.add(info.id);
1520 } else {
1521 otherScreenItems.add(info);
1522 }
1523 }
1524 }
1525 }
1526
1527 /** Filters the set of widgets which are on the specified screen. */
1528 private void filterCurrentAppWidgets(int currentScreen,
1529 ArrayList<LauncherAppWidgetInfo> appWidgets,
1530 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1531 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1532 // If we aren't filtering on a screen, then the set of items to load is the full set of
1533 // widgets given.
1534 if (currentScreen < 0) {
1535 currentScreenWidgets.addAll(appWidgets);
1536 }
1537
1538 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001539 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001540 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1541 widget.screen == currentScreen) {
1542 currentScreenWidgets.add(widget);
1543 } else {
1544 otherScreenWidgets.add(widget);
1545 }
1546 }
1547 }
1548
1549 /** Filters the set of folders which are on the specified screen. */
1550 private void filterCurrentFolders(int currentScreen,
1551 HashMap<Long, ItemInfo> itemsIdMap,
1552 HashMap<Long, FolderInfo> folders,
1553 HashMap<Long, FolderInfo> currentScreenFolders,
1554 HashMap<Long, FolderInfo> otherScreenFolders) {
1555 // If we aren't filtering on a screen, then the set of items to load is the full set of
1556 // widgets given.
1557 if (currentScreen < 0) {
1558 currentScreenFolders.putAll(folders);
1559 }
1560
1561 for (long id : folders.keySet()) {
1562 ItemInfo info = itemsIdMap.get(id);
1563 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07001564 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001565 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1566 info.screen == currentScreen) {
1567 currentScreenFolders.put(id, folder);
1568 } else {
1569 otherScreenFolders.put(id, folder);
1570 }
1571 }
1572 }
1573
1574 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1575 * right) */
1576 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1577 // XXX: review this
1578 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001579 @Override
1580 public int compare(ItemInfo lhs, ItemInfo rhs) {
1581 int cellCountX = LauncherModel.getCellCountX();
1582 int cellCountY = LauncherModel.getCellCountY();
1583 int screenOffset = cellCountX * cellCountY;
1584 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1585 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1586 lhs.cellY * cellCountX + lhs.cellX);
1587 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1588 rhs.cellY * cellCountX + rhs.cellX);
1589 return (int) (lr - rr);
1590 }
1591 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001592 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001593
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001594 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1595 final ArrayList<ItemInfo> workspaceItems,
1596 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1597 final HashMap<Long, FolderInfo> folders,
1598 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07001599
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001600 final boolean postOnMainThread = (deferredBindRunnables != null);
1601
1602 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001603 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001604 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001605 final int start = i;
1606 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001607 final Runnable r = new Runnable() {
1608 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001609 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001610 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001611 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001612 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613 }
1614 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001615 };
1616 if (postOnMainThread) {
1617 deferredBindRunnables.add(r);
1618 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001619 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 }
Joe Onorato36115782010-06-17 13:28:48 -04001621 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001622
1623 // Bind the folders
1624 if (!folders.isEmpty()) {
1625 final Runnable r = new Runnable() {
1626 public void run() {
1627 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1628 if (callbacks != null) {
1629 callbacks.bindFolders(folders);
1630 }
1631 }
1632 };
1633 if (postOnMainThread) {
1634 deferredBindRunnables.add(r);
1635 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001636 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 }
1638 }
1639
1640 // Bind the widgets, one at a time
1641 N = appWidgets.size();
1642 for (int i = 0; i < N; i++) {
1643 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1644 final Runnable r = new Runnable() {
1645 public void run() {
1646 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1647 if (callbacks != null) {
1648 callbacks.bindAppWidget(widget);
1649 }
1650 }
1651 };
1652 if (postOnMainThread) {
1653 deferredBindRunnables.add(r);
1654 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001655 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001656 }
1657 }
1658 }
1659
1660 /**
1661 * Binds all loaded data to actual views on the main thread.
1662 */
1663 private void bindWorkspace(int synchronizeBindPage) {
1664 final long t = SystemClock.uptimeMillis();
1665 Runnable r;
1666
1667 // Don't use these two variables in any of the callback runnables.
1668 // Otherwise we hold a reference to them.
1669 final Callbacks oldCallbacks = mCallbacks.get();
1670 if (oldCallbacks == null) {
1671 // This launcher has exited and nobody bothered to tell us. Just bail.
1672 Log.w(TAG, "LoaderTask running with no launcher");
1673 return;
1674 }
1675
Winson Chung4a2afa32012-07-19 14:53:05 -07001676 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
1677 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001678 oldCallbacks.getCurrentWorkspaceScreen();
1679
1680 // Load all the items that are on the current page first (and in the process, unbind
1681 // all the existing workspace items before we call startBinding() below.
1682 unbindWorkspaceItemsOnMainThread();
1683 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1684 ArrayList<LauncherAppWidgetInfo> appWidgets =
1685 new ArrayList<LauncherAppWidgetInfo>();
1686 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
1687 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -07001688 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001689 workspaceItems.addAll(sBgWorkspaceItems);
1690 appWidgets.addAll(sBgAppWidgets);
1691 folders.putAll(sBgFolders);
1692 itemsIdMap.putAll(sBgItemsIdMap);
1693 }
1694
1695 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
1696 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
1697 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
1698 new ArrayList<LauncherAppWidgetInfo>();
1699 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
1700 new ArrayList<LauncherAppWidgetInfo>();
1701 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
1702 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
1703
1704 // Separate the items that are on the current screen, and all the other remaining items
1705 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
1706 otherWorkspaceItems);
1707 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
1708 otherAppWidgets);
1709 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
1710 otherFolders);
1711 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1712 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1713
1714 // Tell the workspace that we're about to start binding items
1715 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001716 public void run() {
1717 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1718 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001720 }
1721 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001722 };
Winson Chung81b52252012-08-27 15:34:29 -07001723 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001724
1725 // Load items on the current page
1726 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
1727 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07001728 if (isLoadingSynchronously) {
1729 r = new Runnable() {
1730 public void run() {
1731 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1732 if (callbacks != null) {
1733 callbacks.onPageBoundSynchronously(currentScreen);
1734 }
1735 }
1736 };
Winson Chung81b52252012-08-27 15:34:29 -07001737 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07001738 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739
Winson Chung4a2afa32012-07-19 14:53:05 -07001740 // Load all the remaining pages (if we are loading synchronously, we want to defer this
1741 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001742 mDeferredBindRunnables.clear();
1743 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07001744 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745
1746 // Tell the workspace that we're done binding items
1747 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001748 public void run() {
1749 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1750 if (callbacks != null) {
1751 callbacks.finishBindingItems();
1752 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753
Winson Chung98e030b2012-05-07 16:01:11 -07001754 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001755 if (DEBUG_LOADERS) {
1756 Log.d(TAG, "bound workspace in "
1757 + (SystemClock.uptimeMillis()-t) + "ms");
1758 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001759
1760 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001761 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 };
Winson Chung4a2afa32012-07-19 14:53:05 -07001763 if (isLoadingSynchronously) {
1764 mDeferredBindRunnables.add(r);
1765 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001766 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07001767 }
Joe Onorato36115782010-06-17 13:28:48 -04001768 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001769
Joe Onorato36115782010-06-17 13:28:48 -04001770 private void loadAndBindAllApps() {
1771 if (DEBUG_LOADERS) {
1772 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1773 }
1774 if (!mAllAppsLoaded) {
1775 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001776 synchronized (LoaderTask.this) {
1777 if (mStopped) {
1778 return;
1779 }
1780 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001781 }
Joe Onorato36115782010-06-17 13:28:48 -04001782 } else {
1783 onlyBindAllApps();
1784 }
1785 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001786
Joe Onorato36115782010-06-17 13:28:48 -04001787 private void onlyBindAllApps() {
1788 final Callbacks oldCallbacks = mCallbacks.get();
1789 if (oldCallbacks == null) {
1790 // This launcher has exited and nobody bothered to tell us. Just bail.
1791 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1792 return;
1793 }
1794
1795 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001796 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001797 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07001798 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001799 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001800 public void run() {
1801 final long t = SystemClock.uptimeMillis();
1802 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1803 if (callbacks != null) {
1804 callbacks.bindAllApplications(list);
1805 }
1806 if (DEBUG_LOADERS) {
1807 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1808 + (SystemClock.uptimeMillis()-t) + "ms");
1809 }
1810 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001811 };
1812 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
1813 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) {
1814 r.run();
1815 } else {
1816 mHandler.post(r);
1817 }
Joe Onorato36115782010-06-17 13:28:48 -04001818 }
1819
1820 private void loadAllAppsByBatch() {
1821 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1822
1823 // Don't use these two variables in any of the callback runnables.
1824 // Otherwise we hold a reference to them.
1825 final Callbacks oldCallbacks = mCallbacks.get();
1826 if (oldCallbacks == null) {
1827 // This launcher has exited and nobody bothered to tell us. Just bail.
1828 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1829 return;
1830 }
1831
1832 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1833 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1834
1835 final PackageManager packageManager = mContext.getPackageManager();
1836 List<ResolveInfo> apps = null;
1837
1838 int N = Integer.MAX_VALUE;
1839
1840 int startIndex;
1841 int i=0;
1842 int batchSize = -1;
1843 while (i < N && !mStopped) {
1844 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001845 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001846 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1847 apps = packageManager.queryIntentActivities(mainIntent, 0);
1848 if (DEBUG_LOADERS) {
1849 Log.d(TAG, "queryIntentActivities took "
1850 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1851 }
1852 if (apps == null) {
1853 return;
1854 }
1855 N = apps.size();
1856 if (DEBUG_LOADERS) {
1857 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1858 }
1859 if (N == 0) {
1860 // There are no apps?!?
1861 return;
1862 }
1863 if (mBatchSize == 0) {
1864 batchSize = N;
1865 } else {
1866 batchSize = mBatchSize;
1867 }
1868
1869 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1870 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001871 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001872 if (DEBUG_LOADERS) {
1873 Log.d(TAG, "sort took "
1874 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1875 }
1876 }
1877
1878 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1879
1880 startIndex = i;
1881 for (int j=0; i<N && j<batchSize; j++) {
1882 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001883 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001884 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001885 i++;
1886 }
1887
1888 final boolean first = i <= batchSize;
1889 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001890 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
1891 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001892
Joe Onoratocc67f472010-06-08 10:54:30 -07001893 mHandler.post(new Runnable() {
1894 public void run() {
1895 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001896 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001897 if (first) {
1898 callbacks.bindAllApplications(added);
1899 } else {
1900 callbacks.bindAppsAdded(added);
1901 }
1902 if (DEBUG_LOADERS) {
1903 Log.d(TAG, "bound " + added.size() + " apps in "
1904 + (SystemClock.uptimeMillis() - t) + "ms");
1905 }
1906 } else {
1907 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001908 }
1909 }
1910 });
1911
Daniel Sandlerdca66122010-04-13 16:23:58 -04001912 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001913 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1914 + (SystemClock.uptimeMillis()-t2) + "ms");
1915 }
1916
1917 if (mAllAppsLoadDelay > 0 && i < N) {
1918 try {
1919 if (DEBUG_LOADERS) {
1920 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1921 }
1922 Thread.sleep(mAllAppsLoadDelay);
1923 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 }
1925 }
1926
Joe Onorato36115782010-06-17 13:28:48 -04001927 if (DEBUG_LOADERS) {
1928 Log.d(TAG, "cached all " + N + " apps in "
1929 + (SystemClock.uptimeMillis()-t) + "ms"
1930 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001931 }
1932 }
1933
1934 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07001935 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001936 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1937 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1938 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1939 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1940 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
1941 }
Joe Onorato36115782010-06-17 13:28:48 -04001942 }
1943 }
1944
1945 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001946 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001947 }
1948
1949 private class PackageUpdatedTask implements Runnable {
1950 int mOp;
1951 String[] mPackages;
1952
1953 public static final int OP_NONE = 0;
1954 public static final int OP_ADD = 1;
1955 public static final int OP_UPDATE = 2;
1956 public static final int OP_REMOVE = 3; // uninstlled
1957 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1958
1959
1960 public PackageUpdatedTask(int op, String[] packages) {
1961 mOp = op;
1962 mPackages = packages;
1963 }
1964
1965 public void run() {
1966 final Context context = mApp;
1967
1968 final String[] packages = mPackages;
1969 final int N = packages.length;
1970 switch (mOp) {
1971 case OP_ADD:
1972 for (int i=0; i<N; i++) {
1973 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001974 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001975 }
1976 break;
1977 case OP_UPDATE:
1978 for (int i=0; i<N; i++) {
1979 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001980 mBgAllAppsList.updatePackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001981 }
1982 break;
1983 case OP_REMOVE:
1984 case OP_UNAVAILABLE:
1985 for (int i=0; i<N; i++) {
1986 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001987 mBgAllAppsList.removePackage(packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001988 }
1989 break;
1990 }
1991
1992 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04001993 ArrayList<ApplicationInfo> modified = null;
1994
Adam Cohen487f7dd2012-06-28 18:12:10 -07001995 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07001996 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
1997 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001998 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001999 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002000 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2001 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002002 }
Winson Chungcd810732012-06-18 16:45:43 -07002003 // We may be removing packages that have no associated launcher application, so we
2004 // pass through the removed package names directly.
2005 // NOTE: We flush the icon cache aggressively in removePackage() above.
2006 final ArrayList<String> removedPackageNames = new ArrayList<String>();
Winson Chung5d55f332012-07-16 20:45:03 -07002007 if (mBgAllAppsList.removed.size() > 0) {
2008 mBgAllAppsList.removed.clear();
2009
2010 for (int i = 0; i < N; ++i) {
2011 removedPackageNames.add(packages[i]);
2012 }
Winson Chungcd810732012-06-18 16:45:43 -07002013 }
2014
Joe Onorato36115782010-06-17 13:28:48 -04002015 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2016 if (callbacks == null) {
2017 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2018 return;
2019 }
2020
2021 if (added != null) {
2022 final ArrayList<ApplicationInfo> addedFinal = added;
2023 mHandler.post(new Runnable() {
2024 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002025 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2026 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002027 callbacks.bindAppsAdded(addedFinal);
2028 }
2029 }
2030 });
2031 }
2032 if (modified != null) {
2033 final ArrayList<ApplicationInfo> modifiedFinal = modified;
2034 mHandler.post(new Runnable() {
2035 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002036 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2037 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002038 callbacks.bindAppsUpdated(modifiedFinal);
2039 }
2040 }
2041 });
2042 }
Winson Chungcd810732012-06-18 16:45:43 -07002043 if (!removedPackageNames.isEmpty()) {
Joe Onorato36115782010-06-17 13:28:48 -04002044 final boolean permanent = mOp != OP_UNAVAILABLE;
Joe Onorato36115782010-06-17 13:28:48 -04002045 mHandler.post(new Runnable() {
2046 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002047 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2048 if (callbacks == cb && cb != null) {
Winson Chungcd810732012-06-18 16:45:43 -07002049 callbacks.bindAppsRemoved(removedPackageNames, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04002050 }
2051 }
2052 });
Joe Onoratobe386092009-11-17 17:32:16 -08002053 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002054
2055 mHandler.post(new Runnable() {
2056 @Override
2057 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002058 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2059 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002060 callbacks.bindPackagesUpdated();
2061 }
2062 }
2063 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002064 }
2065 }
2066
2067 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002068 * This is called from the code that adds shortcuts from the intent receiver. This
2069 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070 */
Joe Onorato56d82912010-03-07 14:32:10 -05002071 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002072 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074
Joe Onorato56d82912010-03-07 14:32:10 -05002075 /**
2076 * Make an ShortcutInfo object for a shortcut that is an application.
2077 *
2078 * If c is not null, then it will be used to fill in missing data like the title and icon.
2079 */
2080 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002081 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002082 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002083 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002084
2085 ComponentName componentName = intent.getComponent();
2086 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002087 return null;
2088 }
2089
Adam Cohen00fcb492011-11-02 21:53:47 -07002090 try {
2091 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2092 if (!pi.applicationInfo.enabled) {
2093 // If we return null here, the corresponding item will be removed from the launcher
2094 // db and will not appear in the workspace.
2095 return null;
2096 }
2097 } catch (NameNotFoundException e) {
2098 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
2099 }
2100
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002101 // TODO: See if the PackageManager knows about this case. If it doesn't
2102 // then return null & delete this.
2103
Joe Onorato56d82912010-03-07 14:32:10 -05002104 // the resource -- This may implicitly give us back the fallback icon,
2105 // but don't worry about that. All we're doing with usingFallbackIcon is
2106 // to avoid saving lots of copies of that in the database, and most apps
2107 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002108
2109 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2110 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2111 // via resolveActivity().
2112 ResolveInfo resolveInfo = null;
2113 ComponentName oldComponent = intent.getComponent();
2114 Intent newIntent = new Intent(intent.getAction(), null);
2115 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2116 newIntent.setPackage(oldComponent.getPackageName());
2117 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2118 for (ResolveInfo i : infos) {
2119 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2120 i.activityInfo.name);
2121 if (cn.equals(oldComponent)) {
2122 resolveInfo = i;
2123 }
2124 }
2125 if (resolveInfo == null) {
2126 resolveInfo = manager.resolveActivity(intent, 0);
2127 }
Joe Onorato56d82912010-03-07 14:32:10 -05002128 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002129 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002130 }
Joe Onorato56d82912010-03-07 14:32:10 -05002131 // the db
2132 if (icon == null) {
2133 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002134 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002135 }
2136 }
2137 // the fallback icon
2138 if (icon == null) {
2139 icon = getFallbackIcon();
2140 info.usingFallbackIcon = true;
2141 }
2142 info.setIcon(icon);
2143
2144 // from the resource
2145 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002146 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2147 if (labelCache != null && labelCache.containsKey(key)) {
2148 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002149 } else {
2150 info.title = resolveInfo.activityInfo.loadLabel(manager);
2151 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002152 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002153 }
2154 }
Joe Onorato56d82912010-03-07 14:32:10 -05002155 }
2156 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002158 if (c != null) {
2159 info.title = c.getString(titleIndex);
2160 }
2161 }
2162 // fall back to the class name of the activity
2163 if (info.title == null) {
2164 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2167 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002171 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002173 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002174 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2175 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176
Joe Onorato56d82912010-03-07 14:32:10 -05002177 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002178 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002181 // TODO: If there's an explicit component and we can't install that, delete it.
2182
Joe Onorato56d82912010-03-07 14:32:10 -05002183 info.title = c.getString(titleIndex);
2184
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185 int iconType = c.getInt(iconTypeIndex);
2186 switch (iconType) {
2187 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2188 String packageName = c.getString(iconPackageIndex);
2189 String resourceName = c.getString(iconResourceIndex);
2190 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002191 info.customIcon = false;
2192 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002195 if (resources != null) {
2196 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002197 icon = Utilities.createIconBitmap(
2198 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002199 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002200 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002201 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 }
Joe Onorato56d82912010-03-07 14:32:10 -05002203 // the db
2204 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002205 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002206 }
2207 // the fallback icon
2208 if (icon == null) {
2209 icon = getFallbackIcon();
2210 info.usingFallbackIcon = true;
2211 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002212 break;
2213 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002214 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002215 if (icon == null) {
2216 icon = getFallbackIcon();
2217 info.customIcon = false;
2218 info.usingFallbackIcon = true;
2219 } else {
2220 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002222 break;
2223 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002224 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002225 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002226 info.customIcon = false;
2227 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002229 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 return info;
2231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232
Michael Jurka931dc972011-08-05 15:08:15 -07002233 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002234 @SuppressWarnings("all") // suppress dead code warning
2235 final boolean debug = false;
2236 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002237 Log.d(TAG, "getIconFromCursor app="
2238 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2239 }
2240 byte[] data = c.getBlob(iconIndex);
2241 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002242 return Utilities.createIconBitmap(
2243 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002244 } catch (Exception e) {
2245 return null;
2246 }
2247 }
2248
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2250 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002251 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002252 if (info == null) {
2253 return null;
2254 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002255 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002256
2257 return info;
2258 }
2259
Winson Chunga9abd0e2010-10-27 17:18:37 -07002260 /**
Winson Chung55cef262010-10-28 14:14:18 -07002261 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2262 */
2263 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2264 ComponentName component) {
2265 List<AppWidgetProviderInfo> widgets =
2266 AppWidgetManager.getInstance(context).getInstalledProviders();
2267 for (AppWidgetProviderInfo info : widgets) {
2268 if (info.provider.equals(component)) {
2269 return info;
2270 }
2271 }
2272 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002273 }
2274
Winson Chung68846fd2010-10-29 11:00:27 -07002275 /**
2276 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2277 */
2278 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2279 final PackageManager packageManager = context.getPackageManager();
2280 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2281 new ArrayList<WidgetMimeTypeHandlerData>();
2282
2283 final Intent supportsIntent =
2284 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2285 supportsIntent.setType(mimeType);
2286
2287 // Create a set of widget configuration components that we can test against
2288 final List<AppWidgetProviderInfo> widgets =
2289 AppWidgetManager.getInstance(context).getInstalledProviders();
2290 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2291 new HashMap<ComponentName, AppWidgetProviderInfo>();
2292 for (AppWidgetProviderInfo info : widgets) {
2293 configurationComponentToWidget.put(info.configure, info);
2294 }
2295
2296 // Run through each of the intents that can handle this type of clip data, and cross
2297 // reference them with the components that are actual configuration components
2298 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2299 PackageManager.MATCH_DEFAULT_ONLY);
2300 for (ResolveInfo info : activities) {
2301 final ActivityInfo activityInfo = info.activityInfo;
2302 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2303 activityInfo.name);
2304 if (configurationComponentToWidget.containsKey(infoComponent)) {
2305 supportedConfigurationActivities.add(
2306 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2307 configurationComponentToWidget.get(infoComponent)));
2308 }
2309 }
2310 return supportedConfigurationActivities;
2311 }
2312
Winson Chunga9abd0e2010-10-27 17:18:37 -07002313 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002314 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2315 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2316 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2317
Adam Cohend9198822011-11-22 16:42:47 -08002318 if (intent == null) {
2319 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2320 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2321 return null;
2322 }
2323
Joe Onorato0589f0f2010-02-08 13:44:00 -08002324 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002325 boolean customIcon = false;
2326 ShortcutIconResource iconResource = null;
2327
2328 if (bitmap != null && bitmap instanceof Bitmap) {
2329 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002330 customIcon = true;
2331 } else {
2332 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2333 if (extra != null && extra instanceof ShortcutIconResource) {
2334 try {
2335 iconResource = (ShortcutIconResource) extra;
2336 final PackageManager packageManager = context.getPackageManager();
2337 Resources resources = packageManager.getResourcesForApplication(
2338 iconResource.packageName);
2339 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002340 icon = Utilities.createIconBitmap(
2341 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002342 } catch (Exception e) {
2343 Log.w(TAG, "Could not load shortcut icon: " + extra);
2344 }
2345 }
2346 }
2347
Michael Jurkac9d95c52011-08-29 14:03:34 -07002348 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002349
2350 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002351 if (fallbackIcon != null) {
2352 icon = fallbackIcon;
2353 } else {
2354 icon = getFallbackIcon();
2355 info.usingFallbackIcon = true;
2356 }
Joe Onorato56d82912010-03-07 14:32:10 -05002357 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002358 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002359
Joe Onorato0589f0f2010-02-08 13:44:00 -08002360 info.title = name;
2361 info.intent = intent;
2362 info.customIcon = customIcon;
2363 info.iconResource = iconResource;
2364
2365 return info;
2366 }
2367
Winson Chungaac01e12011-08-17 10:37:13 -07002368 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2369 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002370 // If apps can't be on SD, don't even bother.
2371 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002372 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002373 }
Joe Onorato56d82912010-03-07 14:32:10 -05002374 // If this icon doesn't have a custom icon, check to see
2375 // what's stored in the DB, and if it doesn't match what
2376 // we're going to show, store what we are going to show back
2377 // into the DB. We do this so when we're loading, if the
2378 // package manager can't find an icon (for example because
2379 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002380 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002381 cache.put(info, c.getBlob(iconIndex));
2382 return true;
2383 }
2384 return false;
2385 }
2386 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2387 boolean needSave = false;
2388 try {
2389 if (data != null) {
2390 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2391 Bitmap loaded = info.getIcon(mIconCache);
2392 needSave = !saved.sameAs(loaded);
2393 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002394 needSave = true;
2395 }
Winson Chungaac01e12011-08-17 10:37:13 -07002396 } catch (Exception e) {
2397 needSave = true;
2398 }
2399 if (needSave) {
2400 Log.d(TAG, "going to save icon bitmap for info=" + info);
2401 // This is slower than is ideal, but this only happens once
2402 // or when the app is updated with a new icon.
2403 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002404 }
2405 }
2406
Joe Onorato9c1289c2009-08-17 11:03:03 -04002407 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002408 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002409 * or make a new one.
2410 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002411 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002412 // See if a placeholder was created for us already
2413 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002414 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002415 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002416 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002417 folders.put(id, folderInfo);
2418 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002419 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 }
2421
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08002423 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04002424 = new Comparator<ApplicationInfo>() {
2425 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07002426 int result = sCollator.compare(a.title.toString(), b.title.toString());
2427 if (result == 0) {
2428 result = a.componentName.compareTo(b.componentName);
2429 }
2430 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002432 };
Winson Chung78403fe2011-01-21 15:38:02 -08002433 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2434 = new Comparator<ApplicationInfo>() {
2435 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2436 if (a.firstInstallTime < b.firstInstallTime) return 1;
2437 if (a.firstInstallTime > b.firstInstallTime) return -1;
2438 return 0;
2439 }
2440 };
Winson Chung785d2eb2011-04-14 16:08:02 -07002441 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
2442 = new Comparator<AppWidgetProviderInfo>() {
2443 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2444 return sCollator.compare(a.label.toString(), b.label.toString());
2445 }
2446 };
Winson Chung5308f242011-08-18 12:12:41 -07002447 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2448 if (info.activityInfo != null) {
2449 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2450 } else {
2451 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2452 }
2453 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002454 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
2455 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002456 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002457 ShortcutNameComparator(PackageManager pm) {
2458 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002459 mLabelCache = new HashMap<Object, CharSequence>();
2460 }
2461 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2462 mPackageManager = pm;
2463 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002464 }
2465 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002466 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002467 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2468 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2469 if (mLabelCache.containsKey(keyA)) {
2470 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002471 } else {
2472 labelA = a.loadLabel(mPackageManager).toString();
2473
Winson Chung5308f242011-08-18 12:12:41 -07002474 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002475 }
Winson Chung5308f242011-08-18 12:12:41 -07002476 if (mLabelCache.containsKey(keyB)) {
2477 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002478 } else {
2479 labelB = b.loadLabel(mPackageManager).toString();
2480
Winson Chung5308f242011-08-18 12:12:41 -07002481 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002482 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002483 return sCollator.compare(labelA, labelB);
2484 }
2485 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002486 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
2487 private PackageManager mPackageManager;
2488 private HashMap<Object, String> mLabelCache;
2489 WidgetAndShortcutNameComparator(PackageManager pm) {
2490 mPackageManager = pm;
2491 mLabelCache = new HashMap<Object, String>();
2492 }
2493 public final int compare(Object a, Object b) {
2494 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002495 if (mLabelCache.containsKey(a)) {
2496 labelA = mLabelCache.get(a);
2497 } else {
2498 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002499 ((AppWidgetProviderInfo) a).label :
2500 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002501 mLabelCache.put(a, labelA);
2502 }
2503 if (mLabelCache.containsKey(b)) {
2504 labelB = mLabelCache.get(b);
2505 } else {
2506 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002507 ((AppWidgetProviderInfo) b).label :
2508 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002509 mLabelCache.put(b, labelB);
2510 }
Winson Chung1ed747a2011-05-03 16:18:34 -07002511 return sCollator.compare(labelA, labelB);
2512 }
2513 };
Joe Onoratobe386092009-11-17 17:32:16 -08002514
2515 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002516 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002517 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2518 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2519 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2520 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002521 if (mLoaderTask != null) {
2522 mLoaderTask.dumpState();
2523 } else {
2524 Log.d(TAG, "mLoaderTask=null");
2525 }
Joe Onoratobe386092009-11-17 17:32:16 -08002526 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527}