blob: 9a5bd47b304bf961e43f2bf8db75a7319bc940ab [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 Jurkab2ae8ac2012-09-21 12:06:06 -0700265 static void checkItemInfoLocked(
266 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
267 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
268 if (modelItem != null && item != modelItem) {
269 // check all the data is consistent
270 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
271 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
272 ShortcutInfo shortcut = (ShortcutInfo) item;
273 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
274 modelShortcut.intent.filterEquals(shortcut.intent) &&
275 modelShortcut.id == shortcut.id &&
276 modelShortcut.itemType == shortcut.itemType &&
277 modelShortcut.container == shortcut.container &&
278 modelShortcut.screen == shortcut.screen &&
279 modelShortcut.cellX == shortcut.cellX &&
280 modelShortcut.cellY == shortcut.cellY &&
281 modelShortcut.spanX == shortcut.spanX &&
282 modelShortcut.spanY == shortcut.spanY &&
283 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
284 (modelShortcut.dropPos != null &&
285 shortcut.dropPos != null &&
286 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
287 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
288 // For all intents and purposes, this is the same object
289 return;
290 }
291 }
292
293 // the modelItem needs to match up perfectly with item if our model is
294 // to be consistent with the database-- for now, just require
295 // modelItem == item or the equality check above
296 String msg = "item: " + ((item != null) ? item.toString() : "null") +
297 "modelItem: " +
298 ((modelItem != null) ? modelItem.toString() : "null") +
299 "Error: ItemInfo passed to checkItemInfo doesn't match original";
300 RuntimeException e = new RuntimeException(msg);
301 if (stackTrace != null) {
302 e.setStackTrace(stackTrace);
303 }
304 throw e;
305 }
306 }
307
Michael Jurka816474f2012-06-25 14:49:02 -0700308 static void checkItemInfo(final ItemInfo item) {
309 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
310 final long itemId = item.id;
311 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700312 public void run() {
313 synchronized (sBgLock) {
314 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700315 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700316 }
317 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700318 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700319 }
320
Michael Jurkac9d95c52011-08-29 14:03:34 -0700321 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
322 final ItemInfo item, final String callingFunction) {
323 final long itemId = item.id;
324 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
325 final ContentResolver cr = context.getContentResolver();
326
Adam Cohen487f7dd2012-06-28 18:12:10 -0700327 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700328 Runnable r = new Runnable() {
329 public void run() {
330 cr.update(uri, values, null, null);
331
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700332 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700333 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700334 checkItemInfoLocked(itemId, item, stackTrace);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700335
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700336 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
337 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
338 // Item is in a folder, make sure this folder exists
339 if (!sBgFolders.containsKey(item.container)) {
340 // An items container is being set to a that of an item which is not in
341 // the list of Folders.
342 String msg = "item: " + item + " container being set to: " +
343 item.container + ", not in the list of folders";
Adam Cohen28b3e102012-10-04 17:21:33 -0700344 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700345 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700346 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700347 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700348
349 // Items are added/removed from the corresponding FolderInfo elsewhere, such
350 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
351 // that are on the desktop, as appropriate
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700352 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700353 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
354 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
355 switch (modelItem.itemType) {
356 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
357 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
358 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
359 if (!sBgWorkspaceItems.contains(modelItem)) {
360 sBgWorkspaceItems.add(modelItem);
361 }
362 break;
363 default:
364 break;
365 }
366 } else {
367 sBgWorkspaceItems.remove(modelItem);
368 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700369 }
370 }
371 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700372 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700373 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700377 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700378 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
379 final int screen, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700380 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
381 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700382 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
383 Launcher.sDumpLogs.add(transaction);
384 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400385 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386 item.cellX = cellX;
387 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700388
Winson Chung3d503fb2011-07-13 17:25:49 -0700389 // We store hotseat items in canonical form which is this orientation invariant position
390 // in the hotseat
391 if (context instanceof Launcher && screen < 0 &&
392 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
393 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
394 } else {
395 item.screen = screen;
396 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400397
398 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700400 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
401 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400402 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
403
Michael Jurkac9d95c52011-08-29 14:03:34 -0700404 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700405 }
406
407 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700408 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800409 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700410 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
411 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700412 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
413 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700414 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
415 Launcher.sDumpLogs.add(transaction);
416 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800417 item.cellX = cellX;
418 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700419 item.spanX = spanX;
420 item.spanY = spanY;
421
422 // We store hotseat items in canonical form which is this orientation invariant position
423 // in the hotseat
424 if (context instanceof Launcher && screen < 0 &&
425 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
426 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
427 } else {
428 item.screen = screen;
429 }
Adam Cohend4844c32011-02-18 19:25:06 -0800430
Adam Cohend4844c32011-02-18 19:25:06 -0800431 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800432 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700433 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
434 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
435 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
436 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
437 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800438
Michael Jurka816474f2012-06-25 14:49:02 -0700439 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700440 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700441
442 /**
443 * Update an item to the database in a specified container.
444 */
445 static void updateItemInDatabase(Context context, final ItemInfo item) {
446 final ContentValues values = new ContentValues();
447 item.onAddToDatabase(values);
448 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
449 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800450 }
451
452 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400453 * Returns true if the shortcuts already exists in the database.
454 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400456 static boolean shortcutExists(Context context, String title, Intent intent) {
457 final ContentResolver cr = context.getContentResolver();
458 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
459 new String[] { "title", "intent" }, "title=? and intent=?",
460 new String[] { title, intent.toUri(0) }, null);
461 boolean result = false;
462 try {
463 result = c.moveToFirst();
464 } finally {
465 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400467 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700468 }
469
Joe Onorato9c1289c2009-08-17 11:03:03 -0400470 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700471 * Returns an ItemInfo array containing all the items in the LauncherModel.
472 * The ItemInfo.id is not set through this function.
473 */
474 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
475 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
476 final ContentResolver cr = context.getContentResolver();
477 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
478 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
479 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
480 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
481
482 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
483 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
484 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
485 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
486 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
487 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
488 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
489
490 try {
491 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700492 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700493 item.cellX = c.getInt(cellXIndex);
494 item.cellY = c.getInt(cellYIndex);
495 item.spanX = c.getInt(spanXIndex);
496 item.spanY = c.getInt(spanYIndex);
497 item.container = c.getInt(containerIndex);
498 item.itemType = c.getInt(itemTypeIndex);
499 item.screen = c.getInt(screenIndex);
500
501 items.add(item);
502 }
503 } catch (Exception e) {
504 items.clear();
505 } finally {
506 c.close();
507 }
508
509 return items;
510 }
511
512 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400513 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
514 */
515 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
516 final ContentResolver cr = context.getContentResolver();
517 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
518 "_id=? and (itemType=? or itemType=?)",
519 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700520 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700521
Joe Onorato9c1289c2009-08-17 11:03:03 -0400522 try {
523 if (c.moveToFirst()) {
524 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
525 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
526 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
527 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
528 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
529 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
Joe Onorato9c1289c2009-08-17 11:03:03 -0400531 FolderInfo folderInfo = null;
532 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
534 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700536 }
537
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 folderInfo.title = c.getString(titleIndex);
539 folderInfo.id = id;
540 folderInfo.container = c.getInt(containerIndex);
541 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700542 folderInfo.cellX = c.getInt(cellXIndex);
543 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544
545 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700546 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 } finally {
548 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700549 }
550
551 return null;
552 }
553
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 /**
555 * Add an item to the database in a specified container. Sets the container, screen, cellX and
556 * cellY fields of the item. Also assigns an ID to the item.
557 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700558 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
559 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400561 item.cellX = cellX;
562 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700563 // We store hotseat items in canonical form which is this orientation invariant position
564 // in the hotseat
565 if (context instanceof Launcher && screen < 0 &&
566 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
567 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
568 } else {
569 item.screen = screen;
570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571
572 final ContentValues values = new ContentValues();
573 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 item.onAddToDatabase(values);
575
Michael Jurka7578ec62011-08-03 14:11:54 -0700576 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700577 item.id = app.getLauncherProvider().generateNewId();
578 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700579 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700580
Michael Jurkac9d95c52011-08-29 14:03:34 -0700581 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700582 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700583 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
584 + item.id + " (" + container + ", " + screen + ", " + cellX + ", "
585 + cellY + ")";
586 Launcher.sDumpLogs.add(transaction);
587 Log.d(TAG, transaction);
588
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700589 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
590 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400591
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700592 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700593 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700594 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700595 sBgItemsIdMap.put(item.id, item);
596 switch (item.itemType) {
597 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
598 sBgFolders.put(item.id, (FolderInfo) item);
599 // Fall through
600 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
601 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
602 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
603 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
604 sBgWorkspaceItems.add(item);
605 } else {
606 if (!sBgFolders.containsKey(item.container)) {
607 // Adding an item to a folder that doesn't exist.
608 String msg = "adding item: " + item + " to a folder that " +
609 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700610 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700611 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700612 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700613 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700614 break;
615 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
616 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
617 break;
618 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700619 }
620 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700621 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700622 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700623 }
624
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 * Creates a new unique child id, for a given cell span across all layouts.
627 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700628 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700629 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
630 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700631 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 }
633
Adam Cohend22015c2010-07-26 22:02:18 -0700634 static int getCellCountX() {
635 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 }
637
Adam Cohend22015c2010-07-26 22:02:18 -0700638 static int getCellCountY() {
639 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700640 }
641
642 /**
643 * Updates the model orientation helper to take into account the current layout dimensions
644 * when performing local/canonical coordinate transformations.
645 */
646 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700647 mCellCountX = shortAxisCellCount;
648 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 }
650
651 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700652 * Removes the specified item from the database
653 * @param context
654 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700656 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700658 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700659
Michael Jurka83df1882011-08-31 20:59:26 -0700660 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700661 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700662 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
663 + item.id + " (" + item.container + ", " + item.screen + ", " + item.cellX +
664 ", " + item.cellY + ")";
665 Launcher.sDumpLogs.add(transaction);
666 Log.d(TAG, transaction);
667
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700669
670 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700671 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700672 switch (item.itemType) {
673 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
674 sBgFolders.remove(item.id);
675 for (ItemInfo info: sBgItemsIdMap.values()) {
676 if (info.container == item.id) {
677 // We are deleting a folder which still contains items that
678 // think they are contained by that folder.
679 String msg = "deleting a folder (" + item + ") which still " +
680 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700681 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700682 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700683 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700684 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700685 sBgWorkspaceItems.remove(item);
686 break;
687 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
688 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
689 sBgWorkspaceItems.remove(item);
690 break;
691 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
692 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
693 break;
694 }
695 sBgItemsIdMap.remove(item.id);
696 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700697 }
698 }
Michael Jurka83df1882011-08-31 20:59:26 -0700699 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700700 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 }
702
703 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 * Remove the contents of the specified folder from the database
705 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700706 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 final ContentResolver cr = context.getContentResolver();
708
Michael Jurkac9d95c52011-08-29 14:03:34 -0700709 Runnable r = new Runnable() {
710 public void run() {
711 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, 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 sBgItemsIdMap.remove(info.id);
715 sBgFolders.remove(info.id);
716 sBgDbIconCache.remove(info);
717 sBgWorkspaceItems.remove(info);
718 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700719
Michael Jurkac9d95c52011-08-29 14:03:34 -0700720 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
721 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700722 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700723 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700724 for (ItemInfo childInfo : info.contents) {
725 sBgItemsIdMap.remove(childInfo.id);
726 sBgDbIconCache.remove(childInfo);
727 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700728 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700729 }
730 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700731 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732 }
733
734 /**
735 * Set this as the current Launcher activity object for the loader.
736 */
737 public void initialize(Callbacks callbacks) {
738 synchronized (mLock) {
739 mCallbacks = new WeakReference<Callbacks>(callbacks);
740 }
741 }
742
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700743 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
745 * ACTION_PACKAGE_CHANGED.
746 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100747 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400748 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400749 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700750
Joe Onorato36115782010-06-17 13:28:48 -0400751 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400752
Joe Onorato36115782010-06-17 13:28:48 -0400753 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
754 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
755 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
756 final String packageName = intent.getData().getSchemeSpecificPart();
757 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758
Joe Onorato36115782010-06-17 13:28:48 -0400759 int op = PackageUpdatedTask.OP_NONE;
760
761 if (packageName == null || packageName.length() == 0) {
762 // they sent us a bad intent
763 return;
764 }
765
766 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
767 op = PackageUpdatedTask.OP_UPDATE;
768 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
769 if (!replacing) {
770 op = PackageUpdatedTask.OP_REMOVE;
771 }
772 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
773 // later, we will update the package at this time
774 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
775 if (!replacing) {
776 op = PackageUpdatedTask.OP_ADD;
777 } else {
778 op = PackageUpdatedTask.OP_UPDATE;
779 }
780 }
781
782 if (op != PackageUpdatedTask.OP_NONE) {
783 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
784 }
785
786 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400787 // First, schedule to add these apps back in.
788 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
789 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
790 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700791 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400792 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
793 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
794 enqueuePackageUpdated(new PackageUpdatedTask(
795 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700796 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700797 // If we have changed locale we need to clear out the labels in all apps/workspace.
798 forceReload();
799 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
800 // Check if configuration change was an mcc/mnc change which would affect app resources
801 // and we would need to clear out the labels in all apps/workspace. Same handling as
802 // above for ACTION_LOCALE_CHANGED
803 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700804 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700805 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700806 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700807 forceReload();
808 }
809 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700810 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700811 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
812 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700813 if (mCallbacks != null) {
814 Callbacks callbacks = mCallbacks.get();
815 if (callbacks != null) {
816 callbacks.bindSearchablesChanged();
817 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700818 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700819 }
820 }
821
Reena Lee93f824a2011-09-23 17:20:28 -0700822 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700823 resetLoadedState(true, true);
824
Reena Lee93f824a2011-09-23 17:20:28 -0700825 // Do this here because if the launcher activity is running it will be restarted.
826 // If it's not running startLoaderFromBackground will merely tell it that it needs
827 // to reload.
828 startLoaderFromBackground();
829 }
830
Winson Chungf0c6ae02012-03-21 16:10:31 -0700831 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
832 synchronized (mLock) {
833 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
834 // mWorkspaceLoaded to true later
835 stopLoaderLocked();
836 if (resetAllAppsLoaded) mAllAppsLoaded = false;
837 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
838 }
839 }
840
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700841 /**
842 * When the launcher is in the background, it's possible for it to miss paired
843 * configuration changes. So whenever we trigger the loader from the background
844 * tell the launcher that it needs to re-run the loader when it comes back instead
845 * of doing it now.
846 */
847 public void startLoaderFromBackground() {
848 boolean runLoader = false;
849 if (mCallbacks != null) {
850 Callbacks callbacks = mCallbacks.get();
851 if (callbacks != null) {
852 // Only actually run the loader if they're not paused.
853 if (!callbacks.setLoadOnResume()) {
854 runLoader = true;
855 }
856 }
857 }
858 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700859 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -0700860 }
Joe Onorato36115782010-06-17 13:28:48 -0400861 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400862
Reena Lee93f824a2011-09-23 17:20:28 -0700863 // If there is already a loader task running, tell it to stop.
864 // returns true if isLaunching() was true on the old task
865 private boolean stopLoaderLocked() {
866 boolean isLaunching = false;
867 LoaderTask oldTask = mLoaderTask;
868 if (oldTask != null) {
869 if (oldTask.isLaunching()) {
870 isLaunching = true;
871 }
872 oldTask.stopLocked();
873 }
874 return isLaunching;
875 }
876
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700877 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -0400878 synchronized (mLock) {
879 if (DEBUG_LOADERS) {
880 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 // Clear any deferred bind-runnables from the synchronized load process
884 // We must do this before any loading/binding is scheduled below.
885 mDeferredBindRunnables.clear();
886
Joe Onorato36115782010-06-17 13:28:48 -0400887 // Don't bother to start the thread if we know it's not going to do anything
888 if (mCallbacks != null && mCallbacks.get() != null) {
889 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700890 // also, don't downgrade isLaunching if we're already running
891 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700892 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700893 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
894 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
895 } else {
896 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
897 sWorker.post(mLoaderTask);
898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400900 }
901 }
902
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 void bindRemainingSynchronousPages() {
904 // Post the remaining side pages to be loaded
905 if (!mDeferredBindRunnables.isEmpty()) {
906 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -0700907 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700908 }
909 mDeferredBindRunnables.clear();
910 }
911 }
912
Joe Onorato36115782010-06-17 13:28:48 -0400913 public void stopLoader() {
914 synchronized (mLock) {
915 if (mLoaderTask != null) {
916 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 }
918 }
Joe Onorato36115782010-06-17 13:28:48 -0400919 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920
Michael Jurkac57b7a82011-08-09 22:02:20 -0700921 public boolean isAllAppsLoaded() {
922 return mAllAppsLoaded;
923 }
924
Winson Chung36a62fe2012-05-06 18:04:42 -0700925 boolean isLoadingWorkspace() {
926 synchronized (mLock) {
927 if (mLoaderTask != null) {
928 return mLoaderTask.isLoadingWorkspace();
929 }
930 }
931 return false;
932 }
933
Joe Onorato36115782010-06-17 13:28:48 -0400934 /**
935 * Runnable for the thread that loads the contents of the launcher:
936 * - workspace icons
937 * - widgets
938 * - all apps icons
939 */
940 private class LoaderTask implements Runnable {
941 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -0400942 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700943 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400944 private boolean mStopped;
945 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700946
Winson Chungc3eecff2011-07-11 17:44:15 -0700947 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400948
949 LoaderTask(Context context, boolean isLaunching) {
950 mContext = context;
951 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700952 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953 }
954
Joe Onorato36115782010-06-17 13:28:48 -0400955 boolean isLaunching() {
956 return mIsLaunching;
957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958
Winson Chung36a62fe2012-05-06 18:04:42 -0700959 boolean isLoadingWorkspace() {
960 return mIsLoadingAndBindingWorkspace;
961 }
962
Joe Onorato36115782010-06-17 13:28:48 -0400963 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700964 mIsLoadingAndBindingWorkspace = true;
965
Joe Onorato36115782010-06-17 13:28:48 -0400966 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400967 if (DEBUG_LOADERS) {
968 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700970
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700971 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400972 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700973 synchronized (LoaderTask.this) {
974 if (mStopped) {
975 return;
976 }
977 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 }
979 }
980
Joe Onorato36115782010-06-17 13:28:48 -0400981 // Bind the workspace
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700982 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -0400983 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400984
Joe Onorato36115782010-06-17 13:28:48 -0400985 private void waitForIdle() {
986 // Wait until the either we're stopped or the other threads are done.
987 // This way we don't start loading all apps until the workspace has settled
988 // down.
989 synchronized (LoaderTask.this) {
990 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700991
Joe Onorato36115782010-06-17 13:28:48 -0400992 mHandler.postIdle(new Runnable() {
993 public void run() {
994 synchronized (LoaderTask.this) {
995 mLoadAndBindStepFinished = true;
996 if (DEBUG_LOADERS) {
997 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400998 }
Joe Onorato36115782010-06-17 13:28:48 -0400999 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001000 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001001 }
Joe Onorato36115782010-06-17 13:28:48 -04001002 });
1003
1004 while (!mStopped && !mLoadAndBindStepFinished) {
1005 try {
1006 this.wait();
1007 } catch (InterruptedException ex) {
1008 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001009 }
1010 }
Joe Onorato36115782010-06-17 13:28:48 -04001011 if (DEBUG_LOADERS) {
1012 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001013 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001014 + "ms for previous step to finish binding");
1015 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001016 }
Joe Onorato36115782010-06-17 13:28:48 -04001017 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001018
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 void runBindSynchronousPage(int synchronousBindPage) {
1020 if (synchronousBindPage < 0) {
1021 // Ensure that we have a valid page index to load synchronously
1022 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1023 "valid page index");
1024 }
1025 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1026 // Ensure that we don't try and bind a specified page when the pages have not been
1027 // loaded already (we should load everything asynchronously in that case)
1028 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1029 }
1030 synchronized (mLock) {
1031 if (mIsLoaderTaskRunning) {
1032 // Ensure that we are never running the background loading at this point since
1033 // we also touch the background collections
1034 throw new RuntimeException("Error! Background loading is already running");
1035 }
1036 }
1037
1038 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1039 // data structures, we can't allow any other thread to touch that data, but because
1040 // this call is synchronous, we can get away with not locking).
1041
Adam Cohena13a2f22012-07-23 14:29:15 -07001042 // The LauncherModel is static in the LauncherApplication and mHandler may have queued
1043 // operations from the previous activity. We need to ensure that all queued operations
1044 // are executed before any synchronous binding work is done.
1045 mHandler.flush();
1046
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001047 // Divide the set of loaded items into those that we are binding synchronously, and
1048 // everything else that is to be bound normally (asynchronously).
1049 bindWorkspace(synchronousBindPage);
1050 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1051 // arise from that.
1052 onlyBindAllApps();
1053 }
1054
Joe Onorato36115782010-06-17 13:28:48 -04001055 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 synchronized (mLock) {
1057 mIsLoaderTaskRunning = true;
1058 }
Joe Onorato36115782010-06-17 13:28:48 -04001059 // Optimize for end-user experience: if the Launcher is up and // running with the
1060 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1061 // workspace first (default).
1062 final Callbacks cbk = mCallbacks.get();
1063 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -04001064
Joe Onorato36115782010-06-17 13:28:48 -04001065 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001066 // Elevate priority when Home launches for the first time to avoid
1067 // starving at boot time. Staring at a blank home is not cool.
1068 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001069 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1070 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001071 android.os.Process.setThreadPriority(mIsLaunching
1072 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1073 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001074 if (loadWorkspaceFirst) {
1075 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1076 loadAndBindWorkspace();
1077 } else {
1078 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001079 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001080 }
1081
Joe Onorato36115782010-06-17 13:28:48 -04001082 if (mStopped) {
1083 break keep_running;
1084 }
1085
1086 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1087 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001088 synchronized (mLock) {
1089 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001090 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001091 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1092 }
1093 }
Joe Onorato36115782010-06-17 13:28:48 -04001094 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001095
1096 // second step
1097 if (loadWorkspaceFirst) {
1098 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001099 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001100 } else {
1101 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
1102 loadAndBindWorkspace();
1103 }
Winson Chung7ed37742011-09-08 15:45:51 -07001104
1105 // Restore the default thread priority after we are done loading items
1106 synchronized (mLock) {
1107 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1108 }
Joe Onorato36115782010-06-17 13:28:48 -04001109 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110
Winson Chungaac01e12011-08-17 10:37:13 -07001111
1112 // Update the saved icons if necessary
1113 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001114 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 for (Object key : sBgDbIconCache.keySet()) {
1116 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1117 }
1118 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001119 }
Winson Chungaac01e12011-08-17 10:37:13 -07001120
Joe Onorato36115782010-06-17 13:28:48 -04001121 // Clear out this reference, otherwise we end up holding it until all of the
1122 // callback runnables are done.
1123 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124
Joe Onorato36115782010-06-17 13:28:48 -04001125 synchronized (mLock) {
1126 // If we are still the last one to be scheduled, remove ourselves.
1127 if (mLoaderTask == this) {
1128 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001131 }
Joe Onorato36115782010-06-17 13:28:48 -04001132 }
1133
1134 public void stopLocked() {
1135 synchronized (LoaderTask.this) {
1136 mStopped = true;
1137 this.notify();
1138 }
1139 }
1140
1141 /**
1142 * Gets the callbacks object. If we've been stopped, or if the launcher object
1143 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1144 * object that was around when the deferred message was scheduled, and if there's
1145 * a new Callbacks object around then also return null. This will save us from
1146 * calling onto it with data that will be ignored.
1147 */
1148 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1149 synchronized (mLock) {
1150 if (mStopped) {
1151 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001152 }
Joe Onorato36115782010-06-17 13:28:48 -04001153
1154 if (mCallbacks == null) {
1155 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001156 }
Joe Onorato36115782010-06-17 13:28:48 -04001157
1158 final Callbacks callbacks = mCallbacks.get();
1159 if (callbacks != oldCallbacks) {
1160 return null;
1161 }
1162 if (callbacks == null) {
1163 Log.w(TAG, "no mCallbacks");
1164 return null;
1165 }
1166
1167 return callbacks;
1168 }
1169 }
1170
1171 // check & update map of what's occupied; used to discard overlapping/invalid items
1172 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001173 int containerIndex = item.screen;
1174 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001175 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08001176 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001177 return false;
1178 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001179
1180 // We use the last index to refer to the hotseat and the screen as the rank, so
1181 // test and update the occupied state accordingly
1182 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
1183 Log.e(TAG, "Error loading shortcut into hotseat " + item
1184 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
1185 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
1186 return false;
1187 } else {
1188 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
1189 return true;
1190 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001191 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1192 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001193 return true;
1194 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001195
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001196 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001197 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1198 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001199 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001200 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -07001201 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001202 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001203 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -07001204 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001205 return false;
1206 }
1207 }
1208 }
1209 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1210 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001211 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001212 }
1213 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001214
Joe Onorato36115782010-06-17 13:28:48 -04001215 return true;
1216 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217
Joe Onorato36115782010-06-17 13:28:48 -04001218 private void loadWorkspace() {
1219 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220
Joe Onorato36115782010-06-17 13:28:48 -04001221 final Context context = mContext;
1222 final ContentResolver contentResolver = context.getContentResolver();
1223 final PackageManager manager = context.getPackageManager();
1224 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1225 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001226
Michael Jurkab85f8a42012-04-25 15:48:32 -07001227 // Make sure the default workspace is loaded, if needed
Brian Muramatsu5524b492012-10-02 16:55:54 -07001228 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001229
Winson Chung2abf94d2012-07-18 18:16:38 -07001230 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001231 sBgWorkspaceItems.clear();
1232 sBgAppWidgets.clear();
1233 sBgFolders.clear();
1234 sBgItemsIdMap.clear();
1235 sBgDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001236
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001237 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001239 final Cursor c = contentResolver.query(
1240 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001241
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001242 // +1 for the hotseat (it can be larger than the workspace)
1243 // Load workspace in reverse order to ensure that latest items are loaded first (and
1244 // before any earlier duplicates)
1245 final ItemInfo occupied[][][] =
1246 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001248 try {
1249 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1250 final int intentIndex = c.getColumnIndexOrThrow
1251 (LauncherSettings.Favorites.INTENT);
1252 final int titleIndex = c.getColumnIndexOrThrow
1253 (LauncherSettings.Favorites.TITLE);
1254 final int iconTypeIndex = c.getColumnIndexOrThrow(
1255 LauncherSettings.Favorites.ICON_TYPE);
1256 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1257 final int iconPackageIndex = c.getColumnIndexOrThrow(
1258 LauncherSettings.Favorites.ICON_PACKAGE);
1259 final int iconResourceIndex = c.getColumnIndexOrThrow(
1260 LauncherSettings.Favorites.ICON_RESOURCE);
1261 final int containerIndex = c.getColumnIndexOrThrow(
1262 LauncherSettings.Favorites.CONTAINER);
1263 final int itemTypeIndex = c.getColumnIndexOrThrow(
1264 LauncherSettings.Favorites.ITEM_TYPE);
1265 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1266 LauncherSettings.Favorites.APPWIDGET_ID);
1267 final int screenIndex = c.getColumnIndexOrThrow(
1268 LauncherSettings.Favorites.SCREEN);
1269 final int cellXIndex = c.getColumnIndexOrThrow
1270 (LauncherSettings.Favorites.CELLX);
1271 final int cellYIndex = c.getColumnIndexOrThrow
1272 (LauncherSettings.Favorites.CELLY);
1273 final int spanXIndex = c.getColumnIndexOrThrow
1274 (LauncherSettings.Favorites.SPANX);
1275 final int spanYIndex = c.getColumnIndexOrThrow(
1276 LauncherSettings.Favorites.SPANY);
1277 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1278 //final int displayModeIndex = c.getColumnIndexOrThrow(
1279 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001280
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001281 ShortcutInfo info;
1282 String intentDescription;
1283 LauncherAppWidgetInfo appWidgetInfo;
1284 int container;
1285 long id;
1286 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001287
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001288 while (!mStopped && c.moveToNext()) {
1289 try {
1290 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001291
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001292 switch (itemType) {
1293 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1294 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1295 intentDescription = c.getString(intentIndex);
1296 try {
1297 intent = Intent.parseUri(intentDescription, 0);
1298 } catch (URISyntaxException e) {
1299 continue;
1300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001301
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001302 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1303 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1304 titleIndex, mLabelCache);
1305 } else {
1306 info = getShortcutInfo(c, context, iconTypeIndex,
1307 iconPackageIndex, iconResourceIndex, iconIndex,
1308 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001309
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001310 // App shortcuts that used to be automatically added to Launcher
1311 // didn't always have the correct intent flags set, so do that
1312 // here
1313 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001314 intent.getCategories() != null &&
1315 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001316 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001317 intent.addFlags(
1318 Intent.FLAG_ACTIVITY_NEW_TASK |
1319 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1320 }
Michael Jurka96879562012-03-22 05:54:33 -07001321 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 if (info != null) {
1324 info.intent = intent;
1325 info.id = c.getLong(idIndex);
1326 container = c.getInt(containerIndex);
1327 info.container = container;
1328 info.screen = c.getInt(screenIndex);
1329 info.cellX = c.getInt(cellXIndex);
1330 info.cellY = c.getInt(cellYIndex);
1331
1332 // check & update map of what's occupied
1333 if (!checkItemPlacement(occupied, info)) {
1334 break;
1335 }
1336
1337 switch (container) {
1338 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1339 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1340 sBgWorkspaceItems.add(info);
1341 break;
1342 default:
1343 // Item is in a user folder
1344 FolderInfo folderInfo =
1345 findOrMakeFolder(sBgFolders, container);
1346 folderInfo.add(info);
1347 break;
1348 }
1349 sBgItemsIdMap.put(info.id, info);
1350
1351 // now that we've loaded everthing re-save it with the
1352 // icon in case it disappears somehow.
1353 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
1354 } else {
1355 // Failed to load the shortcut, probably because the
1356 // activity manager couldn't resolve it (maybe the app
1357 // was uninstalled), or the db row was somehow screwed up.
1358 // Delete it.
1359 id = c.getLong(idIndex);
1360 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1361 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1362 id, false), null, null);
1363 }
1364 break;
1365
1366 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1367 id = c.getLong(idIndex);
1368 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1369
1370 folderInfo.title = c.getString(titleIndex);
1371 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 folderInfo.container = container;
1374 folderInfo.screen = c.getInt(screenIndex);
1375 folderInfo.cellX = c.getInt(cellXIndex);
1376 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377
Daniel Sandler8802e962010-05-26 16:28:16 -04001378 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001380 break;
1381 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001382 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1384 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1385 sBgWorkspaceItems.add(folderInfo);
1386 break;
Joe Onorato36115782010-06-17 13:28:48 -04001387 }
Joe Onorato17a89222011-02-08 17:26:11 -08001388
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1390 sBgFolders.put(folderInfo.id, folderInfo);
1391 break;
1392
1393 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1394 // Read all Launcher-specific widget details
1395 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001396 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001397
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001398 final AppWidgetProviderInfo provider =
1399 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001400
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001401 if (!isSafeMode && (provider == null || provider.provider == null ||
1402 provider.provider.getPackageName() == null)) {
1403 String log = "Deleting widget that isn't installed anymore: id="
1404 + id + " appWidgetId=" + appWidgetId;
1405 Log.e(TAG, log);
1406 Launcher.sDumpLogs.add(log);
1407 itemsToRemove.add(id);
1408 } else {
1409 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1410 provider.provider);
1411 appWidgetInfo.id = id;
1412 appWidgetInfo.screen = c.getInt(screenIndex);
1413 appWidgetInfo.cellX = c.getInt(cellXIndex);
1414 appWidgetInfo.cellY = c.getInt(cellYIndex);
1415 appWidgetInfo.spanX = c.getInt(spanXIndex);
1416 appWidgetInfo.spanY = c.getInt(spanYIndex);
1417 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1418 appWidgetInfo.minSpanX = minSpan[0];
1419 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001420
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001421 container = c.getInt(containerIndex);
1422 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1423 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1424 Log.e(TAG, "Widget found where container != " +
1425 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1426 continue;
1427 }
1428 appWidgetInfo.container = c.getInt(containerIndex);
1429
1430 // check & update map of what's occupied
1431 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1432 break;
1433 }
1434 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1435 sBgAppWidgets.add(appWidgetInfo);
1436 }
Joe Onorato36115782010-06-17 13:28:48 -04001437 break;
1438 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 } catch (Exception e) {
1440 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001441 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442 }
1443 } finally {
1444 c.close();
1445 }
1446
1447 if (itemsToRemove.size() > 0) {
1448 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1449 LauncherSettings.Favorites.CONTENT_URI);
1450 // Remove dead items
1451 for (long id : itemsToRemove) {
1452 if (DEBUG_LOADERS) {
1453 Log.d(TAG, "Removed id = " + id);
1454 }
1455 // Don't notify content observers
1456 try {
1457 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1458 null, null);
1459 } catch (RemoteException e) {
1460 Log.w(TAG, "Could not remove id = " + id);
1461 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001462 }
1463 }
1464
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465 if (DEBUG_LOADERS) {
1466 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1467 Log.d(TAG, "workspace layout: ");
1468 for (int y = 0; y < mCellCountY; y++) {
1469 String line = "";
1470 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1471 if (s > 0) {
1472 line += " | ";
1473 }
1474 for (int x = 0; x < mCellCountX; x++) {
1475 line += ((occupied[s][x][y] != null) ? "#" : ".");
1476 }
Joe Onorato36115782010-06-17 13:28:48 -04001477 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001478 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001479 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480 }
Joe Onorato36115782010-06-17 13:28:48 -04001481 }
1482 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001484 /** Filters the set of items who are directly or indirectly (via another container) on the
1485 * specified screen. */
1486 private void filterCurrentWorkspaceItems(int currentScreen,
1487 ArrayList<ItemInfo> allWorkspaceItems,
1488 ArrayList<ItemInfo> currentScreenItems,
1489 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001490 // Purge any null ItemInfos
1491 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1492 while (iter.hasNext()) {
1493 ItemInfo i = iter.next();
1494 if (i == null) {
1495 iter.remove();
1496 }
1497 }
1498
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001499 // If we aren't filtering on a screen, then the set of items to load is the full set of
1500 // items given.
1501 if (currentScreen < 0) {
1502 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001503 }
1504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001505 // Order the set of items by their containers first, this allows use to walk through the
1506 // list sequentially, build up a list of containers that are in the specified screen,
1507 // as well as all items in those containers.
1508 Set<Long> itemsOnScreen = new HashSet<Long>();
1509 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1510 @Override
1511 public int compare(ItemInfo lhs, ItemInfo rhs) {
1512 return (int) (lhs.container - rhs.container);
1513 }
1514 });
1515 for (ItemInfo info : allWorkspaceItems) {
1516 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1517 if (info.screen == currentScreen) {
1518 currentScreenItems.add(info);
1519 itemsOnScreen.add(info.id);
1520 } else {
1521 otherScreenItems.add(info);
1522 }
1523 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1524 currentScreenItems.add(info);
1525 itemsOnScreen.add(info.id);
1526 } else {
1527 if (itemsOnScreen.contains(info.container)) {
1528 currentScreenItems.add(info);
1529 itemsOnScreen.add(info.id);
1530 } else {
1531 otherScreenItems.add(info);
1532 }
1533 }
1534 }
1535 }
1536
1537 /** Filters the set of widgets which are on the specified screen. */
1538 private void filterCurrentAppWidgets(int currentScreen,
1539 ArrayList<LauncherAppWidgetInfo> appWidgets,
1540 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1541 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1542 // If we aren't filtering on a screen, then the set of items to load is the full set of
1543 // widgets given.
1544 if (currentScreen < 0) {
1545 currentScreenWidgets.addAll(appWidgets);
1546 }
1547
1548 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001549 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1551 widget.screen == currentScreen) {
1552 currentScreenWidgets.add(widget);
1553 } else {
1554 otherScreenWidgets.add(widget);
1555 }
1556 }
1557 }
1558
1559 /** Filters the set of folders which are on the specified screen. */
1560 private void filterCurrentFolders(int currentScreen,
1561 HashMap<Long, ItemInfo> itemsIdMap,
1562 HashMap<Long, FolderInfo> folders,
1563 HashMap<Long, FolderInfo> currentScreenFolders,
1564 HashMap<Long, FolderInfo> otherScreenFolders) {
1565 // If we aren't filtering on a screen, then the set of items to load is the full set of
1566 // widgets given.
1567 if (currentScreen < 0) {
1568 currentScreenFolders.putAll(folders);
1569 }
1570
1571 for (long id : folders.keySet()) {
1572 ItemInfo info = itemsIdMap.get(id);
1573 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07001574 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001575 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1576 info.screen == currentScreen) {
1577 currentScreenFolders.put(id, folder);
1578 } else {
1579 otherScreenFolders.put(id, folder);
1580 }
1581 }
1582 }
1583
1584 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1585 * right) */
1586 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1587 // XXX: review this
1588 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001589 @Override
1590 public int compare(ItemInfo lhs, ItemInfo rhs) {
1591 int cellCountX = LauncherModel.getCellCountX();
1592 int cellCountY = LauncherModel.getCellCountY();
1593 int screenOffset = cellCountX * cellCountY;
1594 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1595 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1596 lhs.cellY * cellCountX + lhs.cellX);
1597 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1598 rhs.cellY * cellCountX + rhs.cellX);
1599 return (int) (lr - rr);
1600 }
1601 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001602 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001603
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001604 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1605 final ArrayList<ItemInfo> workspaceItems,
1606 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1607 final HashMap<Long, FolderInfo> folders,
1608 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07001609
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001610 final boolean postOnMainThread = (deferredBindRunnables != null);
1611
1612 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001613 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001614 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001615 final int start = i;
1616 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001617 final Runnable r = new Runnable() {
1618 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001619 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001620 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001621 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001622 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001623 }
1624 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001625 };
1626 if (postOnMainThread) {
1627 deferredBindRunnables.add(r);
1628 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001629 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001630 }
Joe Onorato36115782010-06-17 13:28:48 -04001631 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001632
1633 // Bind the folders
1634 if (!folders.isEmpty()) {
1635 final Runnable r = new Runnable() {
1636 public void run() {
1637 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1638 if (callbacks != null) {
1639 callbacks.bindFolders(folders);
1640 }
1641 }
1642 };
1643 if (postOnMainThread) {
1644 deferredBindRunnables.add(r);
1645 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001646 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001647 }
1648 }
1649
1650 // Bind the widgets, one at a time
1651 N = appWidgets.size();
1652 for (int i = 0; i < N; i++) {
1653 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1654 final Runnable r = new Runnable() {
1655 public void run() {
1656 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1657 if (callbacks != null) {
1658 callbacks.bindAppWidget(widget);
1659 }
1660 }
1661 };
1662 if (postOnMainThread) {
1663 deferredBindRunnables.add(r);
1664 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001665 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001666 }
1667 }
1668 }
1669
1670 /**
1671 * Binds all loaded data to actual views on the main thread.
1672 */
1673 private void bindWorkspace(int synchronizeBindPage) {
1674 final long t = SystemClock.uptimeMillis();
1675 Runnable r;
1676
1677 // Don't use these two variables in any of the callback runnables.
1678 // Otherwise we hold a reference to them.
1679 final Callbacks oldCallbacks = mCallbacks.get();
1680 if (oldCallbacks == null) {
1681 // This launcher has exited and nobody bothered to tell us. Just bail.
1682 Log.w(TAG, "LoaderTask running with no launcher");
1683 return;
1684 }
1685
Winson Chung4a2afa32012-07-19 14:53:05 -07001686 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
1687 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001688 oldCallbacks.getCurrentWorkspaceScreen();
1689
1690 // Load all the items that are on the current page first (and in the process, unbind
1691 // all the existing workspace items before we call startBinding() below.
1692 unbindWorkspaceItemsOnMainThread();
1693 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1694 ArrayList<LauncherAppWidgetInfo> appWidgets =
1695 new ArrayList<LauncherAppWidgetInfo>();
1696 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
1697 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -07001698 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001699 workspaceItems.addAll(sBgWorkspaceItems);
1700 appWidgets.addAll(sBgAppWidgets);
1701 folders.putAll(sBgFolders);
1702 itemsIdMap.putAll(sBgItemsIdMap);
1703 }
1704
1705 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
1706 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
1707 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
1708 new ArrayList<LauncherAppWidgetInfo>();
1709 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
1710 new ArrayList<LauncherAppWidgetInfo>();
1711 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
1712 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
1713
1714 // Separate the items that are on the current screen, and all the other remaining items
1715 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
1716 otherWorkspaceItems);
1717 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
1718 otherAppWidgets);
1719 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
1720 otherFolders);
1721 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1722 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1723
1724 // Tell the workspace that we're about to start binding items
1725 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001726 public void run() {
1727 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1728 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001730 }
1731 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 };
Winson Chung81b52252012-08-27 15:34:29 -07001733 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001734
1735 // Load items on the current page
1736 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
1737 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07001738 if (isLoadingSynchronously) {
1739 r = new Runnable() {
1740 public void run() {
1741 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1742 if (callbacks != null) {
1743 callbacks.onPageBoundSynchronously(currentScreen);
1744 }
1745 }
1746 };
Winson Chung81b52252012-08-27 15:34:29 -07001747 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07001748 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749
Winson Chung4a2afa32012-07-19 14:53:05 -07001750 // Load all the remaining pages (if we are loading synchronously, we want to defer this
1751 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752 mDeferredBindRunnables.clear();
1753 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07001754 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755
1756 // Tell the workspace that we're done binding items
1757 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001758 public void run() {
1759 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1760 if (callbacks != null) {
1761 callbacks.finishBindingItems();
1762 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001763
Winson Chung98e030b2012-05-07 16:01:11 -07001764 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001765 if (DEBUG_LOADERS) {
1766 Log.d(TAG, "bound workspace in "
1767 + (SystemClock.uptimeMillis()-t) + "ms");
1768 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001769
1770 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001771 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001772 };
Winson Chung4a2afa32012-07-19 14:53:05 -07001773 if (isLoadingSynchronously) {
1774 mDeferredBindRunnables.add(r);
1775 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001776 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07001777 }
Joe Onorato36115782010-06-17 13:28:48 -04001778 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001779
Joe Onorato36115782010-06-17 13:28:48 -04001780 private void loadAndBindAllApps() {
1781 if (DEBUG_LOADERS) {
1782 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1783 }
1784 if (!mAllAppsLoaded) {
1785 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001786 synchronized (LoaderTask.this) {
1787 if (mStopped) {
1788 return;
1789 }
1790 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001791 }
Joe Onorato36115782010-06-17 13:28:48 -04001792 } else {
1793 onlyBindAllApps();
1794 }
1795 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001796
Joe Onorato36115782010-06-17 13:28:48 -04001797 private void onlyBindAllApps() {
1798 final Callbacks oldCallbacks = mCallbacks.get();
1799 if (oldCallbacks == null) {
1800 // This launcher has exited and nobody bothered to tell us. Just bail.
1801 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1802 return;
1803 }
1804
1805 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001806 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001807 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07001808 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001809 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001810 public void run() {
1811 final long t = SystemClock.uptimeMillis();
1812 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1813 if (callbacks != null) {
1814 callbacks.bindAllApplications(list);
1815 }
1816 if (DEBUG_LOADERS) {
1817 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1818 + (SystemClock.uptimeMillis()-t) + "ms");
1819 }
1820 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001821 };
1822 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
1823 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) {
1824 r.run();
1825 } else {
1826 mHandler.post(r);
1827 }
Joe Onorato36115782010-06-17 13:28:48 -04001828 }
1829
1830 private void loadAllAppsByBatch() {
1831 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1832
1833 // Don't use these two variables in any of the callback runnables.
1834 // Otherwise we hold a reference to them.
1835 final Callbacks oldCallbacks = mCallbacks.get();
1836 if (oldCallbacks == null) {
1837 // This launcher has exited and nobody bothered to tell us. Just bail.
1838 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1839 return;
1840 }
1841
1842 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1843 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1844
1845 final PackageManager packageManager = mContext.getPackageManager();
1846 List<ResolveInfo> apps = null;
1847
1848 int N = Integer.MAX_VALUE;
1849
1850 int startIndex;
1851 int i=0;
1852 int batchSize = -1;
1853 while (i < N && !mStopped) {
1854 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001855 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001856 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1857 apps = packageManager.queryIntentActivities(mainIntent, 0);
1858 if (DEBUG_LOADERS) {
1859 Log.d(TAG, "queryIntentActivities took "
1860 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1861 }
1862 if (apps == null) {
1863 return;
1864 }
1865 N = apps.size();
1866 if (DEBUG_LOADERS) {
1867 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1868 }
1869 if (N == 0) {
1870 // There are no apps?!?
1871 return;
1872 }
1873 if (mBatchSize == 0) {
1874 batchSize = N;
1875 } else {
1876 batchSize = mBatchSize;
1877 }
1878
1879 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1880 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001881 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001882 if (DEBUG_LOADERS) {
1883 Log.d(TAG, "sort took "
1884 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1885 }
1886 }
1887
1888 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1889
1890 startIndex = i;
1891 for (int j=0; i<N && j<batchSize; j++) {
1892 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001893 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001894 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001895 i++;
1896 }
1897
1898 final boolean first = i <= batchSize;
1899 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001900 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
1901 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001902
Joe Onoratocc67f472010-06-08 10:54:30 -07001903 mHandler.post(new Runnable() {
1904 public void run() {
1905 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001906 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001907 if (first) {
1908 callbacks.bindAllApplications(added);
1909 } else {
1910 callbacks.bindAppsAdded(added);
1911 }
1912 if (DEBUG_LOADERS) {
1913 Log.d(TAG, "bound " + added.size() + " apps in "
1914 + (SystemClock.uptimeMillis() - t) + "ms");
1915 }
1916 } else {
1917 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001918 }
1919 }
1920 });
1921
Daniel Sandlerdca66122010-04-13 16:23:58 -04001922 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001923 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1924 + (SystemClock.uptimeMillis()-t2) + "ms");
1925 }
1926
1927 if (mAllAppsLoadDelay > 0 && i < N) {
1928 try {
1929 if (DEBUG_LOADERS) {
1930 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1931 }
1932 Thread.sleep(mAllAppsLoadDelay);
1933 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 }
1935 }
1936
Joe Onorato36115782010-06-17 13:28:48 -04001937 if (DEBUG_LOADERS) {
1938 Log.d(TAG, "cached all " + N + " apps in "
1939 + (SystemClock.uptimeMillis()-t) + "ms"
1940 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001941 }
1942 }
1943
1944 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07001945 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001946 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1947 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1948 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1949 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1950 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
1951 }
Joe Onorato36115782010-06-17 13:28:48 -04001952 }
1953 }
1954
1955 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001956 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001957 }
1958
1959 private class PackageUpdatedTask implements Runnable {
1960 int mOp;
1961 String[] mPackages;
1962
1963 public static final int OP_NONE = 0;
1964 public static final int OP_ADD = 1;
1965 public static final int OP_UPDATE = 2;
1966 public static final int OP_REMOVE = 3; // uninstlled
1967 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1968
1969
1970 public PackageUpdatedTask(int op, String[] packages) {
1971 mOp = op;
1972 mPackages = packages;
1973 }
1974
1975 public void run() {
1976 final Context context = mApp;
1977
1978 final String[] packages = mPackages;
1979 final int N = packages.length;
1980 switch (mOp) {
1981 case OP_ADD:
1982 for (int i=0; i<N; i++) {
1983 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001984 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001985 }
1986 break;
1987 case OP_UPDATE:
1988 for (int i=0; i<N; i++) {
1989 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001990 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurka05713af2013-01-23 12:39:24 +01001991 WidgetPreviewLoader.removeFromDb(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001992 }
1993 break;
1994 case OP_REMOVE:
1995 case OP_UNAVAILABLE:
1996 for (int i=0; i<N; i++) {
1997 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001998 mBgAllAppsList.removePackage(packages[i]);
Michael Jurka05713af2013-01-23 12:39:24 +01001999 WidgetPreviewLoader.removeFromDb(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002000 }
2001 break;
2002 }
2003
2004 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002005 ArrayList<ApplicationInfo> modified = null;
2006
Adam Cohen487f7dd2012-06-28 18:12:10 -07002007 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002008 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2009 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002010 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002011 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002012 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2013 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002014 }
Winson Chungcd810732012-06-18 16:45:43 -07002015 // We may be removing packages that have no associated launcher application, so we
2016 // pass through the removed package names directly.
2017 // NOTE: We flush the icon cache aggressively in removePackage() above.
2018 final ArrayList<String> removedPackageNames = new ArrayList<String>();
Winson Chung5d55f332012-07-16 20:45:03 -07002019 if (mBgAllAppsList.removed.size() > 0) {
2020 mBgAllAppsList.removed.clear();
2021
2022 for (int i = 0; i < N; ++i) {
2023 removedPackageNames.add(packages[i]);
2024 }
Winson Chungcd810732012-06-18 16:45:43 -07002025 }
2026
Joe Onorato36115782010-06-17 13:28:48 -04002027 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2028 if (callbacks == null) {
2029 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2030 return;
2031 }
2032
2033 if (added != null) {
2034 final ArrayList<ApplicationInfo> addedFinal = added;
2035 mHandler.post(new Runnable() {
2036 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002037 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2038 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002039 callbacks.bindAppsAdded(addedFinal);
2040 }
2041 }
2042 });
2043 }
2044 if (modified != null) {
2045 final ArrayList<ApplicationInfo> modifiedFinal = modified;
2046 mHandler.post(new Runnable() {
2047 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002048 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2049 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002050 callbacks.bindAppsUpdated(modifiedFinal);
2051 }
2052 }
2053 });
2054 }
Winson Chungcd810732012-06-18 16:45:43 -07002055 if (!removedPackageNames.isEmpty()) {
Joe Onorato36115782010-06-17 13:28:48 -04002056 final boolean permanent = mOp != OP_UNAVAILABLE;
Joe Onorato36115782010-06-17 13:28:48 -04002057 mHandler.post(new Runnable() {
2058 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002059 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2060 if (callbacks == cb && cb != null) {
Winson Chungcd810732012-06-18 16:45:43 -07002061 callbacks.bindAppsRemoved(removedPackageNames, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04002062 }
2063 }
2064 });
Joe Onoratobe386092009-11-17 17:32:16 -08002065 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002066
2067 mHandler.post(new Runnable() {
2068 @Override
2069 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002070 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2071 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002072 callbacks.bindPackagesUpdated();
2073 }
2074 }
2075 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002076 }
2077 }
2078
2079 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002080 * This is called from the code that adds shortcuts from the intent receiver. This
2081 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 */
Joe Onorato56d82912010-03-07 14:32:10 -05002083 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002084 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002086
Joe Onorato56d82912010-03-07 14:32:10 -05002087 /**
2088 * Make an ShortcutInfo object for a shortcut that is an application.
2089 *
2090 * If c is not null, then it will be used to fill in missing data like the title and icon.
2091 */
2092 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002093 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002094 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002095 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002096
2097 ComponentName componentName = intent.getComponent();
2098 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002099 return null;
2100 }
2101
Adam Cohen00fcb492011-11-02 21:53:47 -07002102 try {
2103 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2104 if (!pi.applicationInfo.enabled) {
2105 // If we return null here, the corresponding item will be removed from the launcher
2106 // db and will not appear in the workspace.
2107 return null;
2108 }
2109 } catch (NameNotFoundException e) {
2110 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
2111 }
2112
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002113 // TODO: See if the PackageManager knows about this case. If it doesn't
2114 // then return null & delete this.
2115
Joe Onorato56d82912010-03-07 14:32:10 -05002116 // the resource -- This may implicitly give us back the fallback icon,
2117 // but don't worry about that. All we're doing with usingFallbackIcon is
2118 // to avoid saving lots of copies of that in the database, and most apps
2119 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002120
2121 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2122 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2123 // via resolveActivity().
2124 ResolveInfo resolveInfo = null;
2125 ComponentName oldComponent = intent.getComponent();
2126 Intent newIntent = new Intent(intent.getAction(), null);
2127 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2128 newIntent.setPackage(oldComponent.getPackageName());
2129 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2130 for (ResolveInfo i : infos) {
2131 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2132 i.activityInfo.name);
2133 if (cn.equals(oldComponent)) {
2134 resolveInfo = i;
2135 }
2136 }
2137 if (resolveInfo == null) {
2138 resolveInfo = manager.resolveActivity(intent, 0);
2139 }
Joe Onorato56d82912010-03-07 14:32:10 -05002140 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002141 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002142 }
Joe Onorato56d82912010-03-07 14:32:10 -05002143 // the db
2144 if (icon == null) {
2145 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002146 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002147 }
2148 }
2149 // the fallback icon
2150 if (icon == null) {
2151 icon = getFallbackIcon();
2152 info.usingFallbackIcon = true;
2153 }
2154 info.setIcon(icon);
2155
2156 // from the resource
2157 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002158 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2159 if (labelCache != null && labelCache.containsKey(key)) {
2160 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002161 } else {
2162 info.title = resolveInfo.activityInfo.loadLabel(manager);
2163 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002164 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002165 }
2166 }
Joe Onorato56d82912010-03-07 14:32:10 -05002167 }
2168 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002169 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002170 if (c != null) {
2171 info.title = c.getString(titleIndex);
2172 }
2173 }
2174 // fall back to the class name of the activity
2175 if (info.title == null) {
2176 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002177 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002178 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2179 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 /**
Winson Chung8a435102012-08-30 17:16:53 -07002183 * Returns the set of workspace ShortcutInfos with the specified intent.
2184 */
2185 static ArrayList<ItemInfo> getWorkspaceShortcutItemInfosWithIntent(Intent intent) {
2186 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
2187 synchronized (sBgLock) {
2188 for (ItemInfo info : sBgWorkspaceItems) {
2189 if (info instanceof ShortcutInfo) {
2190 ShortcutInfo shortcut = (ShortcutInfo) info;
2191 if (shortcut.intent.toUri(0).equals(intent.toUri(0))) {
2192 items.add(shortcut);
2193 }
2194 }
2195 }
2196 }
2197 return items;
2198 }
2199
2200 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002201 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002203 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002204 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2205 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206
Joe Onorato56d82912010-03-07 14:32:10 -05002207 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002208 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002209 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002211 // TODO: If there's an explicit component and we can't install that, delete it.
2212
Joe Onorato56d82912010-03-07 14:32:10 -05002213 info.title = c.getString(titleIndex);
2214
Joe Onorato9c1289c2009-08-17 11:03:03 -04002215 int iconType = c.getInt(iconTypeIndex);
2216 switch (iconType) {
2217 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2218 String packageName = c.getString(iconPackageIndex);
2219 String resourceName = c.getString(iconResourceIndex);
2220 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002221 info.customIcon = false;
2222 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002224 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002225 if (resources != null) {
2226 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002227 icon = Utilities.createIconBitmap(
2228 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002229 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002231 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
Joe Onorato56d82912010-03-07 14:32:10 -05002233 // the db
2234 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002235 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002236 }
2237 // the fallback icon
2238 if (icon == null) {
2239 icon = getFallbackIcon();
2240 info.usingFallbackIcon = true;
2241 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002242 break;
2243 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002244 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002245 if (icon == null) {
2246 icon = getFallbackIcon();
2247 info.customIcon = false;
2248 info.usingFallbackIcon = true;
2249 } else {
2250 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002251 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002252 break;
2253 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002254 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002255 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002256 info.customIcon = false;
2257 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002259 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002260 return info;
2261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262
Michael Jurka931dc972011-08-05 15:08:15 -07002263 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002264 @SuppressWarnings("all") // suppress dead code warning
2265 final boolean debug = false;
2266 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002267 Log.d(TAG, "getIconFromCursor app="
2268 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2269 }
2270 byte[] data = c.getBlob(iconIndex);
2271 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002272 return Utilities.createIconBitmap(
2273 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002274 } catch (Exception e) {
2275 return null;
2276 }
2277 }
2278
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2280 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002281 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002282 if (info == null) {
2283 return null;
2284 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002286
2287 return info;
2288 }
2289
Winson Chunga9abd0e2010-10-27 17:18:37 -07002290 /**
Winson Chung55cef262010-10-28 14:14:18 -07002291 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2292 */
2293 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2294 ComponentName component) {
2295 List<AppWidgetProviderInfo> widgets =
2296 AppWidgetManager.getInstance(context).getInstalledProviders();
2297 for (AppWidgetProviderInfo info : widgets) {
2298 if (info.provider.equals(component)) {
2299 return info;
2300 }
2301 }
2302 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002303 }
2304
Winson Chung68846fd2010-10-29 11:00:27 -07002305 /**
2306 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2307 */
2308 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2309 final PackageManager packageManager = context.getPackageManager();
2310 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2311 new ArrayList<WidgetMimeTypeHandlerData>();
2312
2313 final Intent supportsIntent =
2314 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2315 supportsIntent.setType(mimeType);
2316
2317 // Create a set of widget configuration components that we can test against
2318 final List<AppWidgetProviderInfo> widgets =
2319 AppWidgetManager.getInstance(context).getInstalledProviders();
2320 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2321 new HashMap<ComponentName, AppWidgetProviderInfo>();
2322 for (AppWidgetProviderInfo info : widgets) {
2323 configurationComponentToWidget.put(info.configure, info);
2324 }
2325
2326 // Run through each of the intents that can handle this type of clip data, and cross
2327 // reference them with the components that are actual configuration components
2328 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2329 PackageManager.MATCH_DEFAULT_ONLY);
2330 for (ResolveInfo info : activities) {
2331 final ActivityInfo activityInfo = info.activityInfo;
2332 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2333 activityInfo.name);
2334 if (configurationComponentToWidget.containsKey(infoComponent)) {
2335 supportedConfigurationActivities.add(
2336 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2337 configurationComponentToWidget.get(infoComponent)));
2338 }
2339 }
2340 return supportedConfigurationActivities;
2341 }
2342
Winson Chunga9abd0e2010-10-27 17:18:37 -07002343 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002344 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2345 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2346 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2347
Adam Cohend9198822011-11-22 16:42:47 -08002348 if (intent == null) {
2349 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2350 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2351 return null;
2352 }
2353
Joe Onorato0589f0f2010-02-08 13:44:00 -08002354 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002355 boolean customIcon = false;
2356 ShortcutIconResource iconResource = null;
2357
2358 if (bitmap != null && bitmap instanceof Bitmap) {
2359 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002360 customIcon = true;
2361 } else {
2362 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2363 if (extra != null && extra instanceof ShortcutIconResource) {
2364 try {
2365 iconResource = (ShortcutIconResource) extra;
2366 final PackageManager packageManager = context.getPackageManager();
2367 Resources resources = packageManager.getResourcesForApplication(
2368 iconResource.packageName);
2369 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002370 icon = Utilities.createIconBitmap(
2371 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002372 } catch (Exception e) {
2373 Log.w(TAG, "Could not load shortcut icon: " + extra);
2374 }
2375 }
2376 }
2377
Michael Jurkac9d95c52011-08-29 14:03:34 -07002378 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002379
2380 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002381 if (fallbackIcon != null) {
2382 icon = fallbackIcon;
2383 } else {
2384 icon = getFallbackIcon();
2385 info.usingFallbackIcon = true;
2386 }
Joe Onorato56d82912010-03-07 14:32:10 -05002387 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002388 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002389
Joe Onorato0589f0f2010-02-08 13:44:00 -08002390 info.title = name;
2391 info.intent = intent;
2392 info.customIcon = customIcon;
2393 info.iconResource = iconResource;
2394
2395 return info;
2396 }
2397
Winson Chungaac01e12011-08-17 10:37:13 -07002398 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2399 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002400 // If apps can't be on SD, don't even bother.
2401 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002402 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002403 }
Joe Onorato56d82912010-03-07 14:32:10 -05002404 // If this icon doesn't have a custom icon, check to see
2405 // what's stored in the DB, and if it doesn't match what
2406 // we're going to show, store what we are going to show back
2407 // into the DB. We do this so when we're loading, if the
2408 // package manager can't find an icon (for example because
2409 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002410 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002411 cache.put(info, c.getBlob(iconIndex));
2412 return true;
2413 }
2414 return false;
2415 }
2416 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2417 boolean needSave = false;
2418 try {
2419 if (data != null) {
2420 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2421 Bitmap loaded = info.getIcon(mIconCache);
2422 needSave = !saved.sameAs(loaded);
2423 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002424 needSave = true;
2425 }
Winson Chungaac01e12011-08-17 10:37:13 -07002426 } catch (Exception e) {
2427 needSave = true;
2428 }
2429 if (needSave) {
2430 Log.d(TAG, "going to save icon bitmap for info=" + info);
2431 // This is slower than is ideal, but this only happens once
2432 // or when the app is updated with a new icon.
2433 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002434 }
2435 }
2436
Joe Onorato9c1289c2009-08-17 11:03:03 -04002437 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002438 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002439 * or make a new one.
2440 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002441 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002442 // See if a placeholder was created for us already
2443 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002444 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002445 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002446 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002447 folders.put(id, folderInfo);
2448 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002449 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 }
2451
Winson Chung11904872012-09-17 16:58:46 -07002452 public static final Comparator<ApplicationInfo> getAppNameComparator() {
2453 final Collator collator = Collator.getInstance();
2454 return new Comparator<ApplicationInfo>() {
2455 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2456 int result = collator.compare(a.title.toString(), b.title.toString());
2457 if (result == 0) {
2458 result = a.componentName.compareTo(b.componentName);
2459 }
2460 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07002461 }
Winson Chung11904872012-09-17 16:58:46 -07002462 };
2463 }
Winson Chung78403fe2011-01-21 15:38:02 -08002464 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2465 = new Comparator<ApplicationInfo>() {
2466 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2467 if (a.firstInstallTime < b.firstInstallTime) return 1;
2468 if (a.firstInstallTime > b.firstInstallTime) return -1;
2469 return 0;
2470 }
2471 };
Winson Chung11904872012-09-17 16:58:46 -07002472 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
2473 final Collator collator = Collator.getInstance();
2474 return new Comparator<AppWidgetProviderInfo>() {
2475 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2476 return collator.compare(a.label.toString(), b.label.toString());
2477 }
2478 };
2479 }
Winson Chung5308f242011-08-18 12:12:41 -07002480 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2481 if (info.activityInfo != null) {
2482 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2483 } else {
2484 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2485 }
2486 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002487 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07002488 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07002489 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002490 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002491 ShortcutNameComparator(PackageManager pm) {
2492 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002493 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07002494 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07002495 }
2496 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2497 mPackageManager = pm;
2498 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07002499 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07002500 }
2501 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002502 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002503 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2504 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2505 if (mLabelCache.containsKey(keyA)) {
2506 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002507 } else {
2508 labelA = a.loadLabel(mPackageManager).toString();
2509
Winson Chung5308f242011-08-18 12:12:41 -07002510 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002511 }
Winson Chung5308f242011-08-18 12:12:41 -07002512 if (mLabelCache.containsKey(keyB)) {
2513 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002514 } else {
2515 labelB = b.loadLabel(mPackageManager).toString();
2516
Winson Chung5308f242011-08-18 12:12:41 -07002517 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002518 }
Winson Chung11904872012-09-17 16:58:46 -07002519 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07002520 }
2521 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002522 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07002523 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07002524 private PackageManager mPackageManager;
2525 private HashMap<Object, String> mLabelCache;
2526 WidgetAndShortcutNameComparator(PackageManager pm) {
2527 mPackageManager = pm;
2528 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07002529 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07002530 }
2531 public final int compare(Object a, Object b) {
2532 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002533 if (mLabelCache.containsKey(a)) {
2534 labelA = mLabelCache.get(a);
2535 } else {
2536 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002537 ((AppWidgetProviderInfo) a).label :
2538 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002539 mLabelCache.put(a, labelA);
2540 }
2541 if (mLabelCache.containsKey(b)) {
2542 labelB = mLabelCache.get(b);
2543 } else {
2544 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002545 ((AppWidgetProviderInfo) b).label :
2546 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002547 mLabelCache.put(b, labelB);
2548 }
Winson Chung11904872012-09-17 16:58:46 -07002549 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07002550 }
2551 };
Joe Onoratobe386092009-11-17 17:32:16 -08002552
2553 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002554 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002555 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2556 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2557 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2558 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002559 if (mLoaderTask != null) {
2560 mLoaderTask.dumpState();
2561 } else {
2562 Log.d(TAG, "mLoaderTask=null");
2563 }
Joe Onoratobe386092009-11-17 17:32:16 -08002564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565}