blob: e197b3501cadf5809fa5ad9293417018d94e3b88 [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";
344 RuntimeException e = new RuntimeException(msg);
345 e.setStackTrace(stackTrace);
346 Launcher.dumpDebugLogsToConsole();
347 throw e;
348 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700349 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700350
351 // Items are added/removed from the corresponding FolderInfo elsewhere, such
352 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
353 // that are on the desktop, as appropriate
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700354 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700355 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
356 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
357 switch (modelItem.itemType) {
358 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
359 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
360 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
361 if (!sBgWorkspaceItems.contains(modelItem)) {
362 sBgWorkspaceItems.add(modelItem);
363 }
364 break;
365 default:
366 break;
367 }
368 } else {
369 sBgWorkspaceItems.remove(modelItem);
370 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700371 }
372 }
373 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700374 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700375 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700379 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700380 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
381 final int screen, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700382 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
383 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700384 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
385 Launcher.sDumpLogs.add(transaction);
386 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400388 item.cellX = cellX;
389 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700390
Winson Chung3d503fb2011-07-13 17:25:49 -0700391 // We store hotseat items in canonical form which is this orientation invariant position
392 // in the hotseat
393 if (context instanceof Launcher && screen < 0 &&
394 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
395 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
396 } else {
397 item.screen = screen;
398 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399
400 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400401 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700402 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
403 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400404 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
405
Michael Jurkac9d95c52011-08-29 14:03:34 -0700406 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700407 }
408
409 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700410 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800411 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700412 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
413 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700414 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
415 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700416 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
417 Launcher.sDumpLogs.add(transaction);
418 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800419 item.cellX = cellX;
420 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700421 item.spanX = spanX;
422 item.spanY = spanY;
423
424 // We store hotseat items in canonical form which is this orientation invariant position
425 // in the hotseat
426 if (context instanceof Launcher && screen < 0 &&
427 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
428 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
429 } else {
430 item.screen = screen;
431 }
Adam Cohend4844c32011-02-18 19:25:06 -0800432
Adam Cohend4844c32011-02-18 19:25:06 -0800433 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800434 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700435 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
436 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
437 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
438 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
439 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800440
Michael Jurka816474f2012-06-25 14:49:02 -0700441 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700442 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700443
444 /**
445 * Update an item to the database in a specified container.
446 */
447 static void updateItemInDatabase(Context context, final ItemInfo item) {
448 final ContentValues values = new ContentValues();
449 item.onAddToDatabase(values);
450 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
451 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800452 }
453
454 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455 * Returns true if the shortcuts already exists in the database.
456 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400458 static boolean shortcutExists(Context context, String title, Intent intent) {
459 final ContentResolver cr = context.getContentResolver();
460 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
461 new String[] { "title", "intent" }, "title=? and intent=?",
462 new String[] { title, intent.toUri(0) }, null);
463 boolean result = false;
464 try {
465 result = c.moveToFirst();
466 } finally {
467 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400469 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700470 }
471
Joe Onorato9c1289c2009-08-17 11:03:03 -0400472 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700473 * Returns an ItemInfo array containing all the items in the LauncherModel.
474 * The ItemInfo.id is not set through this function.
475 */
476 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
477 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
478 final ContentResolver cr = context.getContentResolver();
479 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
480 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
481 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
482 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
483
484 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
485 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
486 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
487 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
488 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
489 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
490 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
491
492 try {
493 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700494 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700495 item.cellX = c.getInt(cellXIndex);
496 item.cellY = c.getInt(cellYIndex);
497 item.spanX = c.getInt(spanXIndex);
498 item.spanY = c.getInt(spanYIndex);
499 item.container = c.getInt(containerIndex);
500 item.itemType = c.getInt(itemTypeIndex);
501 item.screen = c.getInt(screenIndex);
502
503 items.add(item);
504 }
505 } catch (Exception e) {
506 items.clear();
507 } finally {
508 c.close();
509 }
510
511 return items;
512 }
513
514 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
516 */
517 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
518 final ContentResolver cr = context.getContentResolver();
519 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
520 "_id=? and (itemType=? or itemType=?)",
521 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700523
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 try {
525 if (c.moveToFirst()) {
526 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
527 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
528 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
529 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
530 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
531 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 FolderInfo folderInfo = null;
534 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700535 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
536 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700538 }
539
Joe Onorato9c1289c2009-08-17 11:03:03 -0400540 folderInfo.title = c.getString(titleIndex);
541 folderInfo.id = id;
542 folderInfo.container = c.getInt(containerIndex);
543 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700544 folderInfo.cellX = c.getInt(cellXIndex);
545 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546
547 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700548 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549 } finally {
550 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700551 }
552
553 return null;
554 }
555
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 /**
557 * Add an item to the database in a specified container. Sets the container, screen, cellX and
558 * cellY fields of the item. Also assigns an ID to the item.
559 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700560 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
561 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 item.cellX = cellX;
564 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700565 // We store hotseat items in canonical form which is this orientation invariant position
566 // in the hotseat
567 if (context instanceof Launcher && screen < 0 &&
568 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
569 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
570 } else {
571 item.screen = screen;
572 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573
574 final ContentValues values = new ContentValues();
575 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576 item.onAddToDatabase(values);
577
Michael Jurka7578ec62011-08-03 14:11:54 -0700578 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700579 item.id = app.getLauncherProvider().generateNewId();
580 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700581 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700582
Adam Cohen487f7dd2012-06-28 18:12:10 -0700583 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
584
Michael Jurkac9d95c52011-08-29 14:03:34 -0700585 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700586 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700587 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
588 + item.id + " (" + container + ", " + screen + ", " + cellX + ", "
589 + cellY + ")";
590 Launcher.sDumpLogs.add(transaction);
591 Log.d(TAG, transaction);
592
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700593 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
594 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400595
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700596 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700597 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700598 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700599 sBgItemsIdMap.put(item.id, item);
600 switch (item.itemType) {
601 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
602 sBgFolders.put(item.id, (FolderInfo) item);
603 // Fall through
604 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
605 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
606 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
607 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
608 sBgWorkspaceItems.add(item);
609 } else {
610 if (!sBgFolders.containsKey(item.container)) {
611 // Adding an item to a folder that doesn't exist.
612 String msg = "adding item: " + item + " to a folder that " +
613 " doesn't exist";
614 RuntimeException e = new RuntimeException(msg);
615 e.setStackTrace(stackTrace);
616 Launcher.dumpDebugLogsToConsole();
617 throw e;
618 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700619 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700620 break;
621 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
622 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
623 break;
624 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700625 }
626 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700627 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700628 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700629 }
630
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 * Creates a new unique child id, for a given cell span across all layouts.
633 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700634 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700635 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
636 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700637 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 }
639
Adam Cohend22015c2010-07-26 22:02:18 -0700640 static int getCellCountX() {
641 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700642 }
643
Adam Cohend22015c2010-07-26 22:02:18 -0700644 static int getCellCountY() {
645 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700646 }
647
648 /**
649 * Updates the model orientation helper to take into account the current layout dimensions
650 * when performing local/canonical coordinate transformations.
651 */
652 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700653 mCellCountX = shortAxisCellCount;
654 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700655 }
656
657 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700658 * Removes the specified item from the database
659 * @param context
660 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700662 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700664 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700665 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
666
Michael Jurka83df1882011-08-31 20:59:26 -0700667 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700668 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700669 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
670 + item.id + " (" + item.container + ", " + item.screen + ", " + item.cellX +
671 ", " + item.cellY + ")";
672 Launcher.sDumpLogs.add(transaction);
673 Log.d(TAG, transaction);
674
Michael Jurkac9d95c52011-08-29 14:03:34 -0700675 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700676
677 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700678 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700679 switch (item.itemType) {
680 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
681 sBgFolders.remove(item.id);
682 for (ItemInfo info: sBgItemsIdMap.values()) {
683 if (info.container == item.id) {
684 // We are deleting a folder which still contains items that
685 // think they are contained by that folder.
686 String msg = "deleting a folder (" + item + ") which still " +
687 "contains items (" + info + ")";
688 RuntimeException e = new RuntimeException(msg);
689 e.setStackTrace(stackTrace);
690 Launcher.dumpDebugLogsToConsole();
691 throw e;
692 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700693 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700694 sBgWorkspaceItems.remove(item);
695 break;
696 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
697 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
698 sBgWorkspaceItems.remove(item);
699 break;
700 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
701 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
702 break;
703 }
704 sBgItemsIdMap.remove(item.id);
705 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700706 }
707 }
Michael Jurka83df1882011-08-31 20:59:26 -0700708 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700709 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 }
711
712 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713 * Remove the contents of the specified folder from the database
714 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700715 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 final ContentResolver cr = context.getContentResolver();
717
Michael Jurkac9d95c52011-08-29 14:03:34 -0700718 Runnable r = new Runnable() {
719 public void run() {
720 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700721 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700722 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700723 sBgItemsIdMap.remove(info.id);
724 sBgFolders.remove(info.id);
725 sBgDbIconCache.remove(info);
726 sBgWorkspaceItems.remove(info);
727 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700728
Michael Jurkac9d95c52011-08-29 14:03:34 -0700729 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
730 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700731 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700732 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700733 for (ItemInfo childInfo : info.contents) {
734 sBgItemsIdMap.remove(childInfo.id);
735 sBgDbIconCache.remove(childInfo);
736 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700737 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700738 }
739 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700740 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 }
742
743 /**
744 * Set this as the current Launcher activity object for the loader.
745 */
746 public void initialize(Callbacks callbacks) {
747 synchronized (mLock) {
748 mCallbacks = new WeakReference<Callbacks>(callbacks);
749 }
750 }
751
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700752 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
754 * ACTION_PACKAGE_CHANGED.
755 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100756 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400757 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400758 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700759
Joe Onorato36115782010-06-17 13:28:48 -0400760 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400761
Joe Onorato36115782010-06-17 13:28:48 -0400762 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
763 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
764 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
765 final String packageName = intent.getData().getSchemeSpecificPart();
766 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400767
Joe Onorato36115782010-06-17 13:28:48 -0400768 int op = PackageUpdatedTask.OP_NONE;
769
770 if (packageName == null || packageName.length() == 0) {
771 // they sent us a bad intent
772 return;
773 }
774
775 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
776 op = PackageUpdatedTask.OP_UPDATE;
777 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
778 if (!replacing) {
779 op = PackageUpdatedTask.OP_REMOVE;
780 }
781 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
782 // later, we will update the package at this time
783 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
784 if (!replacing) {
785 op = PackageUpdatedTask.OP_ADD;
786 } else {
787 op = PackageUpdatedTask.OP_UPDATE;
788 }
789 }
790
791 if (op != PackageUpdatedTask.OP_NONE) {
792 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
793 }
794
795 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400796 // First, schedule to add these apps back in.
797 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
798 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
799 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700800 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400801 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
802 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
803 enqueuePackageUpdated(new PackageUpdatedTask(
804 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700805 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700806 // If we have changed locale we need to clear out the labels in all apps/workspace.
807 forceReload();
808 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
809 // Check if configuration change was an mcc/mnc change which would affect app resources
810 // and we would need to clear out the labels in all apps/workspace. Same handling as
811 // above for ACTION_LOCALE_CHANGED
812 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700813 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700814 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700815 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700816 forceReload();
817 }
818 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700819 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700820 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
821 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700822 if (mCallbacks != null) {
823 Callbacks callbacks = mCallbacks.get();
824 if (callbacks != null) {
825 callbacks.bindSearchablesChanged();
826 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700827 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700828 }
829 }
830
Reena Lee93f824a2011-09-23 17:20:28 -0700831 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700832 resetLoadedState(true, true);
833
Reena Lee93f824a2011-09-23 17:20:28 -0700834 // Do this here because if the launcher activity is running it will be restarted.
835 // If it's not running startLoaderFromBackground will merely tell it that it needs
836 // to reload.
837 startLoaderFromBackground();
838 }
839
Winson Chungf0c6ae02012-03-21 16:10:31 -0700840 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
841 synchronized (mLock) {
842 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
843 // mWorkspaceLoaded to true later
844 stopLoaderLocked();
845 if (resetAllAppsLoaded) mAllAppsLoaded = false;
846 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
847 }
848 }
849
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700850 /**
851 * When the launcher is in the background, it's possible for it to miss paired
852 * configuration changes. So whenever we trigger the loader from the background
853 * tell the launcher that it needs to re-run the loader when it comes back instead
854 * of doing it now.
855 */
856 public void startLoaderFromBackground() {
857 boolean runLoader = false;
858 if (mCallbacks != null) {
859 Callbacks callbacks = mCallbacks.get();
860 if (callbacks != null) {
861 // Only actually run the loader if they're not paused.
862 if (!callbacks.setLoadOnResume()) {
863 runLoader = true;
864 }
865 }
866 }
867 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700868 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -0700869 }
Joe Onorato36115782010-06-17 13:28:48 -0400870 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400871
Reena Lee93f824a2011-09-23 17:20:28 -0700872 // If there is already a loader task running, tell it to stop.
873 // returns true if isLaunching() was true on the old task
874 private boolean stopLoaderLocked() {
875 boolean isLaunching = false;
876 LoaderTask oldTask = mLoaderTask;
877 if (oldTask != null) {
878 if (oldTask.isLaunching()) {
879 isLaunching = true;
880 }
881 oldTask.stopLocked();
882 }
883 return isLaunching;
884 }
885
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -0400887 synchronized (mLock) {
888 if (DEBUG_LOADERS) {
889 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
890 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700892 // Clear any deferred bind-runnables from the synchronized load process
893 // We must do this before any loading/binding is scheduled below.
894 mDeferredBindRunnables.clear();
895
Joe Onorato36115782010-06-17 13:28:48 -0400896 // Don't bother to start the thread if we know it's not going to do anything
897 if (mCallbacks != null && mCallbacks.get() != null) {
898 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700899 // also, don't downgrade isLaunching if we're already running
900 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700901 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700902 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
903 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
904 } else {
905 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
906 sWorker.post(mLoaderTask);
907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909 }
910 }
911
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700912 void bindRemainingSynchronousPages() {
913 // Post the remaining side pages to be loaded
914 if (!mDeferredBindRunnables.isEmpty()) {
915 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -0700916 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700917 }
918 mDeferredBindRunnables.clear();
919 }
920 }
921
Joe Onorato36115782010-06-17 13:28:48 -0400922 public void stopLoader() {
923 synchronized (mLock) {
924 if (mLoaderTask != null) {
925 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 }
927 }
Joe Onorato36115782010-06-17 13:28:48 -0400928 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929
Michael Jurkac57b7a82011-08-09 22:02:20 -0700930 public boolean isAllAppsLoaded() {
931 return mAllAppsLoaded;
932 }
933
Winson Chung36a62fe2012-05-06 18:04:42 -0700934 boolean isLoadingWorkspace() {
935 synchronized (mLock) {
936 if (mLoaderTask != null) {
937 return mLoaderTask.isLoadingWorkspace();
938 }
939 }
940 return false;
941 }
942
Joe Onorato36115782010-06-17 13:28:48 -0400943 /**
944 * Runnable for the thread that loads the contents of the launcher:
945 * - workspace icons
946 * - widgets
947 * - all apps icons
948 */
949 private class LoaderTask implements Runnable {
950 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -0400951 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700952 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400953 private boolean mStopped;
954 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955
Winson Chungc3eecff2011-07-11 17:44:15 -0700956 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400957
958 LoaderTask(Context context, boolean isLaunching) {
959 mContext = context;
960 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700961 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400962 }
963
Joe Onorato36115782010-06-17 13:28:48 -0400964 boolean isLaunching() {
965 return mIsLaunching;
966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967
Winson Chung36a62fe2012-05-06 18:04:42 -0700968 boolean isLoadingWorkspace() {
969 return mIsLoadingAndBindingWorkspace;
970 }
971
Joe Onorato36115782010-06-17 13:28:48 -0400972 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700973 mIsLoadingAndBindingWorkspace = true;
974
Joe Onorato36115782010-06-17 13:28:48 -0400975 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400976 if (DEBUG_LOADERS) {
977 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700979
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700980 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400981 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700982 synchronized (LoaderTask.this) {
983 if (mStopped) {
984 return;
985 }
986 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 }
988 }
989
Joe Onorato36115782010-06-17 13:28:48 -0400990 // Bind the workspace
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700991 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -0400992 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400993
Joe Onorato36115782010-06-17 13:28:48 -0400994 private void waitForIdle() {
995 // Wait until the either we're stopped or the other threads are done.
996 // This way we don't start loading all apps until the workspace has settled
997 // down.
998 synchronized (LoaderTask.this) {
999 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001000
Joe Onorato36115782010-06-17 13:28:48 -04001001 mHandler.postIdle(new Runnable() {
1002 public void run() {
1003 synchronized (LoaderTask.this) {
1004 mLoadAndBindStepFinished = true;
1005 if (DEBUG_LOADERS) {
1006 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001007 }
Joe Onorato36115782010-06-17 13:28:48 -04001008 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001009 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001010 }
Joe Onorato36115782010-06-17 13:28:48 -04001011 });
1012
1013 while (!mStopped && !mLoadAndBindStepFinished) {
1014 try {
1015 this.wait();
1016 } catch (InterruptedException ex) {
1017 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001018 }
1019 }
Joe Onorato36115782010-06-17 13:28:48 -04001020 if (DEBUG_LOADERS) {
1021 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001022 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001023 + "ms for previous step to finish binding");
1024 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001025 }
Joe Onorato36115782010-06-17 13:28:48 -04001026 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001027
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001028 void runBindSynchronousPage(int synchronousBindPage) {
1029 if (synchronousBindPage < 0) {
1030 // Ensure that we have a valid page index to load synchronously
1031 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1032 "valid page index");
1033 }
1034 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1035 // Ensure that we don't try and bind a specified page when the pages have not been
1036 // loaded already (we should load everything asynchronously in that case)
1037 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1038 }
1039 synchronized (mLock) {
1040 if (mIsLoaderTaskRunning) {
1041 // Ensure that we are never running the background loading at this point since
1042 // we also touch the background collections
1043 throw new RuntimeException("Error! Background loading is already running");
1044 }
1045 }
1046
1047 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1048 // data structures, we can't allow any other thread to touch that data, but because
1049 // this call is synchronous, we can get away with not locking).
1050
Adam Cohena13a2f22012-07-23 14:29:15 -07001051 // The LauncherModel is static in the LauncherApplication and mHandler may have queued
1052 // operations from the previous activity. We need to ensure that all queued operations
1053 // are executed before any synchronous binding work is done.
1054 mHandler.flush();
1055
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 // Divide the set of loaded items into those that we are binding synchronously, and
1057 // everything else that is to be bound normally (asynchronously).
1058 bindWorkspace(synchronousBindPage);
1059 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1060 // arise from that.
1061 onlyBindAllApps();
1062 }
1063
Joe Onorato36115782010-06-17 13:28:48 -04001064 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001065 synchronized (mLock) {
1066 mIsLoaderTaskRunning = true;
1067 }
Joe Onorato36115782010-06-17 13:28:48 -04001068 // Optimize for end-user experience: if the Launcher is up and // running with the
1069 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1070 // workspace first (default).
1071 final Callbacks cbk = mCallbacks.get();
1072 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -04001073
Joe Onorato36115782010-06-17 13:28:48 -04001074 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001075 // Elevate priority when Home launches for the first time to avoid
1076 // starving at boot time. Staring at a blank home is not cool.
1077 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001078 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1079 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001080 android.os.Process.setThreadPriority(mIsLaunching
1081 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1082 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001083 if (loadWorkspaceFirst) {
1084 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1085 loadAndBindWorkspace();
1086 } else {
1087 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001088 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001089 }
1090
Joe Onorato36115782010-06-17 13:28:48 -04001091 if (mStopped) {
1092 break keep_running;
1093 }
1094
1095 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1096 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001097 synchronized (mLock) {
1098 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001099 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001100 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1101 }
1102 }
Joe Onorato36115782010-06-17 13:28:48 -04001103 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001104
1105 // second step
1106 if (loadWorkspaceFirst) {
1107 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001108 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001109 } else {
1110 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
1111 loadAndBindWorkspace();
1112 }
Winson Chung7ed37742011-09-08 15:45:51 -07001113
1114 // Restore the default thread priority after we are done loading items
1115 synchronized (mLock) {
1116 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1117 }
Joe Onorato36115782010-06-17 13:28:48 -04001118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119
Winson Chungaac01e12011-08-17 10:37:13 -07001120
1121 // Update the saved icons if necessary
1122 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001123 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 for (Object key : sBgDbIconCache.keySet()) {
1125 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1126 }
1127 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001128 }
Winson Chungaac01e12011-08-17 10:37:13 -07001129
Joe Onorato36115782010-06-17 13:28:48 -04001130 // Clear out this reference, otherwise we end up holding it until all of the
1131 // callback runnables are done.
1132 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133
Joe Onorato36115782010-06-17 13:28:48 -04001134 synchronized (mLock) {
1135 // If we are still the last one to be scheduled, remove ourselves.
1136 if (mLoaderTask == this) {
1137 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001139 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001140 }
Joe Onorato36115782010-06-17 13:28:48 -04001141 }
1142
1143 public void stopLocked() {
1144 synchronized (LoaderTask.this) {
1145 mStopped = true;
1146 this.notify();
1147 }
1148 }
1149
1150 /**
1151 * Gets the callbacks object. If we've been stopped, or if the launcher object
1152 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1153 * object that was around when the deferred message was scheduled, and if there's
1154 * a new Callbacks object around then also return null. This will save us from
1155 * calling onto it with data that will be ignored.
1156 */
1157 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1158 synchronized (mLock) {
1159 if (mStopped) {
1160 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001161 }
Joe Onorato36115782010-06-17 13:28:48 -04001162
1163 if (mCallbacks == null) {
1164 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001165 }
Joe Onorato36115782010-06-17 13:28:48 -04001166
1167 final Callbacks callbacks = mCallbacks.get();
1168 if (callbacks != oldCallbacks) {
1169 return null;
1170 }
1171 if (callbacks == null) {
1172 Log.w(TAG, "no mCallbacks");
1173 return null;
1174 }
1175
1176 return callbacks;
1177 }
1178 }
1179
1180 // check & update map of what's occupied; used to discard overlapping/invalid items
1181 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001182 int containerIndex = item.screen;
1183 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001184 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08001185 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001186 return false;
1187 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001188
1189 // We use the last index to refer to the hotseat and the screen as the rank, so
1190 // test and update the occupied state accordingly
1191 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
1192 Log.e(TAG, "Error loading shortcut into hotseat " + item
1193 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
1194 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
1195 return false;
1196 } else {
1197 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
1198 return true;
1199 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001200 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1201 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001202 return true;
1203 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001204
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001205 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001206 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1207 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001208 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001209 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -07001210 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001211 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001212 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -07001213 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001214 return false;
1215 }
1216 }
1217 }
1218 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1219 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001220 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001221 }
1222 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001223
Joe Onorato36115782010-06-17 13:28:48 -04001224 return true;
1225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001226
Joe Onorato36115782010-06-17 13:28:48 -04001227 private void loadWorkspace() {
1228 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001229
Joe Onorato36115782010-06-17 13:28:48 -04001230 final Context context = mContext;
1231 final ContentResolver contentResolver = context.getContentResolver();
1232 final PackageManager manager = context.getPackageManager();
1233 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1234 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001235
Michael Jurkab85f8a42012-04-25 15:48:32 -07001236 // Make sure the default workspace is loaded, if needed
Brian Muramatsu5524b492012-10-02 16:55:54 -07001237 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001238
Winson Chung2abf94d2012-07-18 18:16:38 -07001239 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001240 sBgWorkspaceItems.clear();
1241 sBgAppWidgets.clear();
1242 sBgFolders.clear();
1243 sBgItemsIdMap.clear();
1244 sBgDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001245
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001246 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001248 final Cursor c = contentResolver.query(
1249 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001250
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001251 // +1 for the hotseat (it can be larger than the workspace)
1252 // Load workspace in reverse order to ensure that latest items are loaded first (and
1253 // before any earlier duplicates)
1254 final ItemInfo occupied[][][] =
1255 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001257 try {
1258 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1259 final int intentIndex = c.getColumnIndexOrThrow
1260 (LauncherSettings.Favorites.INTENT);
1261 final int titleIndex = c.getColumnIndexOrThrow
1262 (LauncherSettings.Favorites.TITLE);
1263 final int iconTypeIndex = c.getColumnIndexOrThrow(
1264 LauncherSettings.Favorites.ICON_TYPE);
1265 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1266 final int iconPackageIndex = c.getColumnIndexOrThrow(
1267 LauncherSettings.Favorites.ICON_PACKAGE);
1268 final int iconResourceIndex = c.getColumnIndexOrThrow(
1269 LauncherSettings.Favorites.ICON_RESOURCE);
1270 final int containerIndex = c.getColumnIndexOrThrow(
1271 LauncherSettings.Favorites.CONTAINER);
1272 final int itemTypeIndex = c.getColumnIndexOrThrow(
1273 LauncherSettings.Favorites.ITEM_TYPE);
1274 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1275 LauncherSettings.Favorites.APPWIDGET_ID);
1276 final int screenIndex = c.getColumnIndexOrThrow(
1277 LauncherSettings.Favorites.SCREEN);
1278 final int cellXIndex = c.getColumnIndexOrThrow
1279 (LauncherSettings.Favorites.CELLX);
1280 final int cellYIndex = c.getColumnIndexOrThrow
1281 (LauncherSettings.Favorites.CELLY);
1282 final int spanXIndex = c.getColumnIndexOrThrow
1283 (LauncherSettings.Favorites.SPANX);
1284 final int spanYIndex = c.getColumnIndexOrThrow(
1285 LauncherSettings.Favorites.SPANY);
1286 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1287 //final int displayModeIndex = c.getColumnIndexOrThrow(
1288 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001290 ShortcutInfo info;
1291 String intentDescription;
1292 LauncherAppWidgetInfo appWidgetInfo;
1293 int container;
1294 long id;
1295 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001296
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001297 while (!mStopped && c.moveToNext()) {
1298 try {
1299 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001300
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001301 switch (itemType) {
1302 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1303 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1304 intentDescription = c.getString(intentIndex);
1305 try {
1306 intent = Intent.parseUri(intentDescription, 0);
1307 } catch (URISyntaxException e) {
1308 continue;
1309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001311 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1312 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1313 titleIndex, mLabelCache);
1314 } else {
1315 info = getShortcutInfo(c, context, iconTypeIndex,
1316 iconPackageIndex, iconResourceIndex, iconIndex,
1317 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001318
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001319 // App shortcuts that used to be automatically added to Launcher
1320 // didn't always have the correct intent flags set, so do that
1321 // here
1322 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001323 intent.getCategories() != null &&
1324 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001325 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 intent.addFlags(
1327 Intent.FLAG_ACTIVITY_NEW_TASK |
1328 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1329 }
Michael Jurka96879562012-03-22 05:54:33 -07001330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001332 if (info != null) {
1333 info.intent = intent;
1334 info.id = c.getLong(idIndex);
1335 container = c.getInt(containerIndex);
1336 info.container = container;
1337 info.screen = c.getInt(screenIndex);
1338 info.cellX = c.getInt(cellXIndex);
1339 info.cellY = c.getInt(cellYIndex);
1340
1341 // check & update map of what's occupied
1342 if (!checkItemPlacement(occupied, info)) {
1343 break;
1344 }
1345
1346 switch (container) {
1347 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1348 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1349 sBgWorkspaceItems.add(info);
1350 break;
1351 default:
1352 // Item is in a user folder
1353 FolderInfo folderInfo =
1354 findOrMakeFolder(sBgFolders, container);
1355 folderInfo.add(info);
1356 break;
1357 }
1358 sBgItemsIdMap.put(info.id, info);
1359
1360 // now that we've loaded everthing re-save it with the
1361 // icon in case it disappears somehow.
1362 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
1363 } else {
1364 // Failed to load the shortcut, probably because the
1365 // activity manager couldn't resolve it (maybe the app
1366 // was uninstalled), or the db row was somehow screwed up.
1367 // Delete it.
1368 id = c.getLong(idIndex);
1369 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1370 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1371 id, false), null, null);
1372 }
1373 break;
1374
1375 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1376 id = c.getLong(idIndex);
1377 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1378
1379 folderInfo.title = c.getString(titleIndex);
1380 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001382 folderInfo.container = container;
1383 folderInfo.screen = c.getInt(screenIndex);
1384 folderInfo.cellX = c.getInt(cellXIndex);
1385 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386
Daniel Sandler8802e962010-05-26 16:28:16 -04001387 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001388 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001389 break;
1390 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001392 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1393 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1394 sBgWorkspaceItems.add(folderInfo);
1395 break;
Joe Onorato36115782010-06-17 13:28:48 -04001396 }
Joe Onorato17a89222011-02-08 17:26:11 -08001397
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001398 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1399 sBgFolders.put(folderInfo.id, folderInfo);
1400 break;
1401
1402 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1403 // Read all Launcher-specific widget details
1404 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001405 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001406
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001407 final AppWidgetProviderInfo provider =
1408 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001409
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001410 if (!isSafeMode && (provider == null || provider.provider == null ||
1411 provider.provider.getPackageName() == null)) {
1412 String log = "Deleting widget that isn't installed anymore: id="
1413 + id + " appWidgetId=" + appWidgetId;
1414 Log.e(TAG, log);
1415 Launcher.sDumpLogs.add(log);
1416 itemsToRemove.add(id);
1417 } else {
1418 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1419 provider.provider);
1420 appWidgetInfo.id = id;
1421 appWidgetInfo.screen = c.getInt(screenIndex);
1422 appWidgetInfo.cellX = c.getInt(cellXIndex);
1423 appWidgetInfo.cellY = c.getInt(cellYIndex);
1424 appWidgetInfo.spanX = c.getInt(spanXIndex);
1425 appWidgetInfo.spanY = c.getInt(spanYIndex);
1426 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1427 appWidgetInfo.minSpanX = minSpan[0];
1428 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001429
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001430 container = c.getInt(containerIndex);
1431 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1432 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1433 Log.e(TAG, "Widget found where container != " +
1434 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1435 continue;
1436 }
1437 appWidgetInfo.container = c.getInt(containerIndex);
1438
1439 // check & update map of what's occupied
1440 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1441 break;
1442 }
1443 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1444 sBgAppWidgets.add(appWidgetInfo);
1445 }
Joe Onorato36115782010-06-17 13:28:48 -04001446 break;
1447 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001448 } catch (Exception e) {
1449 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 }
1452 } finally {
1453 c.close();
1454 }
1455
1456 if (itemsToRemove.size() > 0) {
1457 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1458 LauncherSettings.Favorites.CONTENT_URI);
1459 // Remove dead items
1460 for (long id : itemsToRemove) {
1461 if (DEBUG_LOADERS) {
1462 Log.d(TAG, "Removed id = " + id);
1463 }
1464 // Don't notify content observers
1465 try {
1466 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1467 null, null);
1468 } catch (RemoteException e) {
1469 Log.w(TAG, "Could not remove id = " + id);
1470 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001471 }
1472 }
1473
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001474 if (DEBUG_LOADERS) {
1475 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1476 Log.d(TAG, "workspace layout: ");
1477 for (int y = 0; y < mCellCountY; y++) {
1478 String line = "";
1479 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1480 if (s > 0) {
1481 line += " | ";
1482 }
1483 for (int x = 0; x < mCellCountX; x++) {
1484 line += ((occupied[s][x][y] != null) ? "#" : ".");
1485 }
Joe Onorato36115782010-06-17 13:28:48 -04001486 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001487 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001488 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
Joe Onorato36115782010-06-17 13:28:48 -04001490 }
1491 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001492
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001493 /** Filters the set of items who are directly or indirectly (via another container) on the
1494 * specified screen. */
1495 private void filterCurrentWorkspaceItems(int currentScreen,
1496 ArrayList<ItemInfo> allWorkspaceItems,
1497 ArrayList<ItemInfo> currentScreenItems,
1498 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001499 // Purge any null ItemInfos
1500 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1501 while (iter.hasNext()) {
1502 ItemInfo i = iter.next();
1503 if (i == null) {
1504 iter.remove();
1505 }
1506 }
1507
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 // If we aren't filtering on a screen, then the set of items to load is the full set of
1509 // items given.
1510 if (currentScreen < 0) {
1511 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
1513
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001514 // Order the set of items by their containers first, this allows use to walk through the
1515 // list sequentially, build up a list of containers that are in the specified screen,
1516 // as well as all items in those containers.
1517 Set<Long> itemsOnScreen = new HashSet<Long>();
1518 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1519 @Override
1520 public int compare(ItemInfo lhs, ItemInfo rhs) {
1521 return (int) (lhs.container - rhs.container);
1522 }
1523 });
1524 for (ItemInfo info : allWorkspaceItems) {
1525 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1526 if (info.screen == currentScreen) {
1527 currentScreenItems.add(info);
1528 itemsOnScreen.add(info.id);
1529 } else {
1530 otherScreenItems.add(info);
1531 }
1532 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1533 currentScreenItems.add(info);
1534 itemsOnScreen.add(info.id);
1535 } else {
1536 if (itemsOnScreen.contains(info.container)) {
1537 currentScreenItems.add(info);
1538 itemsOnScreen.add(info.id);
1539 } else {
1540 otherScreenItems.add(info);
1541 }
1542 }
1543 }
1544 }
1545
1546 /** Filters the set of widgets which are on the specified screen. */
1547 private void filterCurrentAppWidgets(int currentScreen,
1548 ArrayList<LauncherAppWidgetInfo> appWidgets,
1549 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1550 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1551 // If we aren't filtering on a screen, then the set of items to load is the full set of
1552 // widgets given.
1553 if (currentScreen < 0) {
1554 currentScreenWidgets.addAll(appWidgets);
1555 }
1556
1557 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001558 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001559 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1560 widget.screen == currentScreen) {
1561 currentScreenWidgets.add(widget);
1562 } else {
1563 otherScreenWidgets.add(widget);
1564 }
1565 }
1566 }
1567
1568 /** Filters the set of folders which are on the specified screen. */
1569 private void filterCurrentFolders(int currentScreen,
1570 HashMap<Long, ItemInfo> itemsIdMap,
1571 HashMap<Long, FolderInfo> folders,
1572 HashMap<Long, FolderInfo> currentScreenFolders,
1573 HashMap<Long, FolderInfo> otherScreenFolders) {
1574 // If we aren't filtering on a screen, then the set of items to load is the full set of
1575 // widgets given.
1576 if (currentScreen < 0) {
1577 currentScreenFolders.putAll(folders);
1578 }
1579
1580 for (long id : folders.keySet()) {
1581 ItemInfo info = itemsIdMap.get(id);
1582 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07001583 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001584 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1585 info.screen == currentScreen) {
1586 currentScreenFolders.put(id, folder);
1587 } else {
1588 otherScreenFolders.put(id, folder);
1589 }
1590 }
1591 }
1592
1593 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1594 * right) */
1595 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1596 // XXX: review this
1597 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001598 @Override
1599 public int compare(ItemInfo lhs, ItemInfo rhs) {
1600 int cellCountX = LauncherModel.getCellCountX();
1601 int cellCountY = LauncherModel.getCellCountY();
1602 int screenOffset = cellCountX * cellCountY;
1603 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1604 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1605 lhs.cellY * cellCountX + lhs.cellX);
1606 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1607 rhs.cellY * cellCountX + rhs.cellX);
1608 return (int) (lr - rr);
1609 }
1610 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001611 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001612
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001613 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1614 final ArrayList<ItemInfo> workspaceItems,
1615 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1616 final HashMap<Long, FolderInfo> folders,
1617 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07001618
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001619 final boolean postOnMainThread = (deferredBindRunnables != null);
1620
1621 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001622 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001623 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001624 final int start = i;
1625 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001626 final Runnable r = new Runnable() {
1627 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001628 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001629 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001631 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001632 }
1633 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001634 };
1635 if (postOnMainThread) {
1636 deferredBindRunnables.add(r);
1637 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001638 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001639 }
Joe Onorato36115782010-06-17 13:28:48 -04001640 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001641
1642 // Bind the folders
1643 if (!folders.isEmpty()) {
1644 final Runnable r = new Runnable() {
1645 public void run() {
1646 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1647 if (callbacks != null) {
1648 callbacks.bindFolders(folders);
1649 }
1650 }
1651 };
1652 if (postOnMainThread) {
1653 deferredBindRunnables.add(r);
1654 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001655 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001656 }
1657 }
1658
1659 // Bind the widgets, one at a time
1660 N = appWidgets.size();
1661 for (int i = 0; i < N; i++) {
1662 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1663 final Runnable r = new Runnable() {
1664 public void run() {
1665 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1666 if (callbacks != null) {
1667 callbacks.bindAppWidget(widget);
1668 }
1669 }
1670 };
1671 if (postOnMainThread) {
1672 deferredBindRunnables.add(r);
1673 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001674 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 }
1676 }
1677 }
1678
1679 /**
1680 * Binds all loaded data to actual views on the main thread.
1681 */
1682 private void bindWorkspace(int synchronizeBindPage) {
1683 final long t = SystemClock.uptimeMillis();
1684 Runnable r;
1685
1686 // Don't use these two variables in any of the callback runnables.
1687 // Otherwise we hold a reference to them.
1688 final Callbacks oldCallbacks = mCallbacks.get();
1689 if (oldCallbacks == null) {
1690 // This launcher has exited and nobody bothered to tell us. Just bail.
1691 Log.w(TAG, "LoaderTask running with no launcher");
1692 return;
1693 }
1694
Winson Chung4a2afa32012-07-19 14:53:05 -07001695 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
1696 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001697 oldCallbacks.getCurrentWorkspaceScreen();
1698
1699 // Load all the items that are on the current page first (and in the process, unbind
1700 // all the existing workspace items before we call startBinding() below.
1701 unbindWorkspaceItemsOnMainThread();
1702 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1703 ArrayList<LauncherAppWidgetInfo> appWidgets =
1704 new ArrayList<LauncherAppWidgetInfo>();
1705 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
1706 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -07001707 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 workspaceItems.addAll(sBgWorkspaceItems);
1709 appWidgets.addAll(sBgAppWidgets);
1710 folders.putAll(sBgFolders);
1711 itemsIdMap.putAll(sBgItemsIdMap);
1712 }
1713
1714 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
1715 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
1716 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
1717 new ArrayList<LauncherAppWidgetInfo>();
1718 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
1719 new ArrayList<LauncherAppWidgetInfo>();
1720 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
1721 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
1722
1723 // Separate the items that are on the current screen, and all the other remaining items
1724 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
1725 otherWorkspaceItems);
1726 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
1727 otherAppWidgets);
1728 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
1729 otherFolders);
1730 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1731 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1732
1733 // Tell the workspace that we're about to start binding items
1734 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001735 public void run() {
1736 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1737 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001738 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001739 }
1740 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001741 };
Winson Chung81b52252012-08-27 15:34:29 -07001742 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743
1744 // Load items on the current page
1745 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
1746 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07001747 if (isLoadingSynchronously) {
1748 r = new Runnable() {
1749 public void run() {
1750 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1751 if (callbacks != null) {
1752 callbacks.onPageBoundSynchronously(currentScreen);
1753 }
1754 }
1755 };
Winson Chung81b52252012-08-27 15:34:29 -07001756 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07001757 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001758
Winson Chung4a2afa32012-07-19 14:53:05 -07001759 // Load all the remaining pages (if we are loading synchronously, we want to defer this
1760 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 mDeferredBindRunnables.clear();
1762 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07001763 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764
1765 // Tell the workspace that we're done binding items
1766 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001767 public void run() {
1768 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1769 if (callbacks != null) {
1770 callbacks.finishBindingItems();
1771 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001772
Winson Chung98e030b2012-05-07 16:01:11 -07001773 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001774 if (DEBUG_LOADERS) {
1775 Log.d(TAG, "bound workspace in "
1776 + (SystemClock.uptimeMillis()-t) + "ms");
1777 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001778
1779 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001780 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 };
Winson Chung4a2afa32012-07-19 14:53:05 -07001782 if (isLoadingSynchronously) {
1783 mDeferredBindRunnables.add(r);
1784 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001785 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07001786 }
Joe Onorato36115782010-06-17 13:28:48 -04001787 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001788
Joe Onorato36115782010-06-17 13:28:48 -04001789 private void loadAndBindAllApps() {
1790 if (DEBUG_LOADERS) {
1791 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1792 }
1793 if (!mAllAppsLoaded) {
1794 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001795 synchronized (LoaderTask.this) {
1796 if (mStopped) {
1797 return;
1798 }
1799 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001800 }
Joe Onorato36115782010-06-17 13:28:48 -04001801 } else {
1802 onlyBindAllApps();
1803 }
1804 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001805
Joe Onorato36115782010-06-17 13:28:48 -04001806 private void onlyBindAllApps() {
1807 final Callbacks oldCallbacks = mCallbacks.get();
1808 if (oldCallbacks == null) {
1809 // This launcher has exited and nobody bothered to tell us. Just bail.
1810 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1811 return;
1812 }
1813
1814 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001815 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001816 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07001817 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001818 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001819 public void run() {
1820 final long t = SystemClock.uptimeMillis();
1821 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1822 if (callbacks != null) {
1823 callbacks.bindAllApplications(list);
1824 }
1825 if (DEBUG_LOADERS) {
1826 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1827 + (SystemClock.uptimeMillis()-t) + "ms");
1828 }
1829 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001830 };
1831 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
1832 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) {
1833 r.run();
1834 } else {
1835 mHandler.post(r);
1836 }
Joe Onorato36115782010-06-17 13:28:48 -04001837 }
1838
1839 private void loadAllAppsByBatch() {
1840 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1841
1842 // Don't use these two variables in any of the callback runnables.
1843 // Otherwise we hold a reference to them.
1844 final Callbacks oldCallbacks = mCallbacks.get();
1845 if (oldCallbacks == null) {
1846 // This launcher has exited and nobody bothered to tell us. Just bail.
1847 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1848 return;
1849 }
1850
1851 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1852 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1853
1854 final PackageManager packageManager = mContext.getPackageManager();
1855 List<ResolveInfo> apps = null;
1856
1857 int N = Integer.MAX_VALUE;
1858
1859 int startIndex;
1860 int i=0;
1861 int batchSize = -1;
1862 while (i < N && !mStopped) {
1863 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001864 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001865 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1866 apps = packageManager.queryIntentActivities(mainIntent, 0);
1867 if (DEBUG_LOADERS) {
1868 Log.d(TAG, "queryIntentActivities took "
1869 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1870 }
1871 if (apps == null) {
1872 return;
1873 }
1874 N = apps.size();
1875 if (DEBUG_LOADERS) {
1876 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1877 }
1878 if (N == 0) {
1879 // There are no apps?!?
1880 return;
1881 }
1882 if (mBatchSize == 0) {
1883 batchSize = N;
1884 } else {
1885 batchSize = mBatchSize;
1886 }
1887
1888 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1889 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001890 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001891 if (DEBUG_LOADERS) {
1892 Log.d(TAG, "sort took "
1893 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1894 }
1895 }
1896
1897 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1898
1899 startIndex = i;
1900 for (int j=0; i<N && j<batchSize; j++) {
1901 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001902 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001903 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001904 i++;
1905 }
1906
1907 final boolean first = i <= batchSize;
1908 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001909 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
1910 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001911
Joe Onoratocc67f472010-06-08 10:54:30 -07001912 mHandler.post(new Runnable() {
1913 public void run() {
1914 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001915 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001916 if (first) {
1917 callbacks.bindAllApplications(added);
1918 } else {
1919 callbacks.bindAppsAdded(added);
1920 }
1921 if (DEBUG_LOADERS) {
1922 Log.d(TAG, "bound " + added.size() + " apps in "
1923 + (SystemClock.uptimeMillis() - t) + "ms");
1924 }
1925 } else {
1926 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001927 }
1928 }
1929 });
1930
Daniel Sandlerdca66122010-04-13 16:23:58 -04001931 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001932 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1933 + (SystemClock.uptimeMillis()-t2) + "ms");
1934 }
1935
1936 if (mAllAppsLoadDelay > 0 && i < N) {
1937 try {
1938 if (DEBUG_LOADERS) {
1939 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1940 }
1941 Thread.sleep(mAllAppsLoadDelay);
1942 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001943 }
1944 }
1945
Joe Onorato36115782010-06-17 13:28:48 -04001946 if (DEBUG_LOADERS) {
1947 Log.d(TAG, "cached all " + N + " apps in "
1948 + (SystemClock.uptimeMillis()-t) + "ms"
1949 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001950 }
1951 }
1952
1953 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07001954 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001955 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1956 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1957 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1958 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1959 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
1960 }
Joe Onorato36115782010-06-17 13:28:48 -04001961 }
1962 }
1963
1964 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001965 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001966 }
1967
1968 private class PackageUpdatedTask implements Runnable {
1969 int mOp;
1970 String[] mPackages;
1971
1972 public static final int OP_NONE = 0;
1973 public static final int OP_ADD = 1;
1974 public static final int OP_UPDATE = 2;
1975 public static final int OP_REMOVE = 3; // uninstlled
1976 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1977
1978
1979 public PackageUpdatedTask(int op, String[] packages) {
1980 mOp = op;
1981 mPackages = packages;
1982 }
1983
1984 public void run() {
1985 final Context context = mApp;
1986
1987 final String[] packages = mPackages;
1988 final int N = packages.length;
1989 switch (mOp) {
1990 case OP_ADD:
1991 for (int i=0; i<N; i++) {
1992 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001993 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001994 }
1995 break;
1996 case OP_UPDATE:
1997 for (int i=0; i<N; i++) {
1998 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001999 mBgAllAppsList.updatePackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002000 }
2001 break;
2002 case OP_REMOVE:
2003 case OP_UNAVAILABLE:
2004 for (int i=0; i<N; i++) {
2005 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002006 mBgAllAppsList.removePackage(packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002007 }
2008 break;
2009 }
2010
2011 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002012 ArrayList<ApplicationInfo> modified = null;
2013
Adam Cohen487f7dd2012-06-28 18:12:10 -07002014 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002015 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2016 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002017 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002018 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002019 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2020 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002021 }
Winson Chungcd810732012-06-18 16:45:43 -07002022 // We may be removing packages that have no associated launcher application, so we
2023 // pass through the removed package names directly.
2024 // NOTE: We flush the icon cache aggressively in removePackage() above.
2025 final ArrayList<String> removedPackageNames = new ArrayList<String>();
Winson Chung5d55f332012-07-16 20:45:03 -07002026 if (mBgAllAppsList.removed.size() > 0) {
2027 mBgAllAppsList.removed.clear();
2028
2029 for (int i = 0; i < N; ++i) {
2030 removedPackageNames.add(packages[i]);
2031 }
Winson Chungcd810732012-06-18 16:45:43 -07002032 }
2033
Joe Onorato36115782010-06-17 13:28:48 -04002034 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2035 if (callbacks == null) {
2036 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2037 return;
2038 }
2039
2040 if (added != null) {
2041 final ArrayList<ApplicationInfo> addedFinal = added;
2042 mHandler.post(new Runnable() {
2043 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002044 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2045 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002046 callbacks.bindAppsAdded(addedFinal);
2047 }
2048 }
2049 });
2050 }
2051 if (modified != null) {
2052 final ArrayList<ApplicationInfo> modifiedFinal = modified;
2053 mHandler.post(new Runnable() {
2054 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002055 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2056 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002057 callbacks.bindAppsUpdated(modifiedFinal);
2058 }
2059 }
2060 });
2061 }
Winson Chungcd810732012-06-18 16:45:43 -07002062 if (!removedPackageNames.isEmpty()) {
Joe Onorato36115782010-06-17 13:28:48 -04002063 final boolean permanent = mOp != OP_UNAVAILABLE;
Joe Onorato36115782010-06-17 13:28:48 -04002064 mHandler.post(new Runnable() {
2065 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002066 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2067 if (callbacks == cb && cb != null) {
Winson Chungcd810732012-06-18 16:45:43 -07002068 callbacks.bindAppsRemoved(removedPackageNames, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04002069 }
2070 }
2071 });
Joe Onoratobe386092009-11-17 17:32:16 -08002072 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002073
2074 mHandler.post(new Runnable() {
2075 @Override
2076 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002077 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2078 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002079 callbacks.bindPackagesUpdated();
2080 }
2081 }
2082 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083 }
2084 }
2085
2086 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002087 * This is called from the code that adds shortcuts from the intent receiver. This
2088 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002089 */
Joe Onorato56d82912010-03-07 14:32:10 -05002090 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002091 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002092 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002093
Joe Onorato56d82912010-03-07 14:32:10 -05002094 /**
2095 * Make an ShortcutInfo object for a shortcut that is an application.
2096 *
2097 * If c is not null, then it will be used to fill in missing data like the title and icon.
2098 */
2099 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002100 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002101 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002102 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002103
2104 ComponentName componentName = intent.getComponent();
2105 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002106 return null;
2107 }
2108
Adam Cohen00fcb492011-11-02 21:53:47 -07002109 try {
2110 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2111 if (!pi.applicationInfo.enabled) {
2112 // If we return null here, the corresponding item will be removed from the launcher
2113 // db and will not appear in the workspace.
2114 return null;
2115 }
2116 } catch (NameNotFoundException e) {
2117 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
2118 }
2119
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002120 // TODO: See if the PackageManager knows about this case. If it doesn't
2121 // then return null & delete this.
2122
Joe Onorato56d82912010-03-07 14:32:10 -05002123 // the resource -- This may implicitly give us back the fallback icon,
2124 // but don't worry about that. All we're doing with usingFallbackIcon is
2125 // to avoid saving lots of copies of that in the database, and most apps
2126 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002127
2128 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2129 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2130 // via resolveActivity().
2131 ResolveInfo resolveInfo = null;
2132 ComponentName oldComponent = intent.getComponent();
2133 Intent newIntent = new Intent(intent.getAction(), null);
2134 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2135 newIntent.setPackage(oldComponent.getPackageName());
2136 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2137 for (ResolveInfo i : infos) {
2138 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2139 i.activityInfo.name);
2140 if (cn.equals(oldComponent)) {
2141 resolveInfo = i;
2142 }
2143 }
2144 if (resolveInfo == null) {
2145 resolveInfo = manager.resolveActivity(intent, 0);
2146 }
Joe Onorato56d82912010-03-07 14:32:10 -05002147 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002148 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002149 }
Joe Onorato56d82912010-03-07 14:32:10 -05002150 // the db
2151 if (icon == null) {
2152 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002153 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002154 }
2155 }
2156 // the fallback icon
2157 if (icon == null) {
2158 icon = getFallbackIcon();
2159 info.usingFallbackIcon = true;
2160 }
2161 info.setIcon(icon);
2162
2163 // from the resource
2164 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002165 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2166 if (labelCache != null && labelCache.containsKey(key)) {
2167 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002168 } else {
2169 info.title = resolveInfo.activityInfo.loadLabel(manager);
2170 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002171 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002172 }
2173 }
Joe Onorato56d82912010-03-07 14:32:10 -05002174 }
2175 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002176 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002177 if (c != null) {
2178 info.title = c.getString(titleIndex);
2179 }
2180 }
2181 // fall back to the class name of the activity
2182 if (info.title == null) {
2183 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002184 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2186 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 /**
Winson Chung8a435102012-08-30 17:16:53 -07002190 * Returns the set of workspace ShortcutInfos with the specified intent.
2191 */
2192 static ArrayList<ItemInfo> getWorkspaceShortcutItemInfosWithIntent(Intent intent) {
2193 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
2194 synchronized (sBgLock) {
2195 for (ItemInfo info : sBgWorkspaceItems) {
2196 if (info instanceof ShortcutInfo) {
2197 ShortcutInfo shortcut = (ShortcutInfo) info;
2198 if (shortcut.intent.toUri(0).equals(intent.toUri(0))) {
2199 items.add(shortcut);
2200 }
2201 }
2202 }
2203 }
2204 return items;
2205 }
2206
2207 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002208 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002210 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002211 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2212 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213
Joe Onorato56d82912010-03-07 14:32:10 -05002214 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002215 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002218 // TODO: If there's an explicit component and we can't install that, delete it.
2219
Joe Onorato56d82912010-03-07 14:32:10 -05002220 info.title = c.getString(titleIndex);
2221
Joe Onorato9c1289c2009-08-17 11:03:03 -04002222 int iconType = c.getInt(iconTypeIndex);
2223 switch (iconType) {
2224 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2225 String packageName = c.getString(iconPackageIndex);
2226 String resourceName = c.getString(iconResourceIndex);
2227 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002228 info.customIcon = false;
2229 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002231 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002232 if (resources != null) {
2233 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002234 icon = Utilities.createIconBitmap(
2235 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002238 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 }
Joe Onorato56d82912010-03-07 14:32:10 -05002240 // the db
2241 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002242 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002243 }
2244 // the fallback icon
2245 if (icon == null) {
2246 icon = getFallbackIcon();
2247 info.usingFallbackIcon = true;
2248 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002249 break;
2250 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002251 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002252 if (icon == null) {
2253 icon = getFallbackIcon();
2254 info.customIcon = false;
2255 info.usingFallbackIcon = true;
2256 } else {
2257 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002259 break;
2260 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002261 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002262 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002263 info.customIcon = false;
2264 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002266 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002267 return info;
2268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269
Michael Jurka931dc972011-08-05 15:08:15 -07002270 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002271 @SuppressWarnings("all") // suppress dead code warning
2272 final boolean debug = false;
2273 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002274 Log.d(TAG, "getIconFromCursor app="
2275 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2276 }
2277 byte[] data = c.getBlob(iconIndex);
2278 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002279 return Utilities.createIconBitmap(
2280 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002281 } catch (Exception e) {
2282 return null;
2283 }
2284 }
2285
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2287 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002288 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002289 if (info == null) {
2290 return null;
2291 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002292 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002293
2294 return info;
2295 }
2296
Winson Chunga9abd0e2010-10-27 17:18:37 -07002297 /**
Winson Chung55cef262010-10-28 14:14:18 -07002298 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2299 */
2300 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2301 ComponentName component) {
2302 List<AppWidgetProviderInfo> widgets =
2303 AppWidgetManager.getInstance(context).getInstalledProviders();
2304 for (AppWidgetProviderInfo info : widgets) {
2305 if (info.provider.equals(component)) {
2306 return info;
2307 }
2308 }
2309 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002310 }
2311
Winson Chung68846fd2010-10-29 11:00:27 -07002312 /**
2313 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2314 */
2315 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2316 final PackageManager packageManager = context.getPackageManager();
2317 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2318 new ArrayList<WidgetMimeTypeHandlerData>();
2319
2320 final Intent supportsIntent =
2321 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2322 supportsIntent.setType(mimeType);
2323
2324 // Create a set of widget configuration components that we can test against
2325 final List<AppWidgetProviderInfo> widgets =
2326 AppWidgetManager.getInstance(context).getInstalledProviders();
2327 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2328 new HashMap<ComponentName, AppWidgetProviderInfo>();
2329 for (AppWidgetProviderInfo info : widgets) {
2330 configurationComponentToWidget.put(info.configure, info);
2331 }
2332
2333 // Run through each of the intents that can handle this type of clip data, and cross
2334 // reference them with the components that are actual configuration components
2335 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2336 PackageManager.MATCH_DEFAULT_ONLY);
2337 for (ResolveInfo info : activities) {
2338 final ActivityInfo activityInfo = info.activityInfo;
2339 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2340 activityInfo.name);
2341 if (configurationComponentToWidget.containsKey(infoComponent)) {
2342 supportedConfigurationActivities.add(
2343 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2344 configurationComponentToWidget.get(infoComponent)));
2345 }
2346 }
2347 return supportedConfigurationActivities;
2348 }
2349
Winson Chunga9abd0e2010-10-27 17:18:37 -07002350 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002351 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2352 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2353 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2354
Adam Cohend9198822011-11-22 16:42:47 -08002355 if (intent == null) {
2356 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2357 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2358 return null;
2359 }
2360
Joe Onorato0589f0f2010-02-08 13:44:00 -08002361 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002362 boolean customIcon = false;
2363 ShortcutIconResource iconResource = null;
2364
2365 if (bitmap != null && bitmap instanceof Bitmap) {
2366 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002367 customIcon = true;
2368 } else {
2369 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2370 if (extra != null && extra instanceof ShortcutIconResource) {
2371 try {
2372 iconResource = (ShortcutIconResource) extra;
2373 final PackageManager packageManager = context.getPackageManager();
2374 Resources resources = packageManager.getResourcesForApplication(
2375 iconResource.packageName);
2376 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002377 icon = Utilities.createIconBitmap(
2378 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002379 } catch (Exception e) {
2380 Log.w(TAG, "Could not load shortcut icon: " + extra);
2381 }
2382 }
2383 }
2384
Michael Jurkac9d95c52011-08-29 14:03:34 -07002385 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002386
2387 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002388 if (fallbackIcon != null) {
2389 icon = fallbackIcon;
2390 } else {
2391 icon = getFallbackIcon();
2392 info.usingFallbackIcon = true;
2393 }
Joe Onorato56d82912010-03-07 14:32:10 -05002394 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002395 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002396
Joe Onorato0589f0f2010-02-08 13:44:00 -08002397 info.title = name;
2398 info.intent = intent;
2399 info.customIcon = customIcon;
2400 info.iconResource = iconResource;
2401
2402 return info;
2403 }
2404
Winson Chungaac01e12011-08-17 10:37:13 -07002405 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2406 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002407 // If apps can't be on SD, don't even bother.
2408 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002409 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002410 }
Joe Onorato56d82912010-03-07 14:32:10 -05002411 // If this icon doesn't have a custom icon, check to see
2412 // what's stored in the DB, and if it doesn't match what
2413 // we're going to show, store what we are going to show back
2414 // into the DB. We do this so when we're loading, if the
2415 // package manager can't find an icon (for example because
2416 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002417 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002418 cache.put(info, c.getBlob(iconIndex));
2419 return true;
2420 }
2421 return false;
2422 }
2423 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2424 boolean needSave = false;
2425 try {
2426 if (data != null) {
2427 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2428 Bitmap loaded = info.getIcon(mIconCache);
2429 needSave = !saved.sameAs(loaded);
2430 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002431 needSave = true;
2432 }
Winson Chungaac01e12011-08-17 10:37:13 -07002433 } catch (Exception e) {
2434 needSave = true;
2435 }
2436 if (needSave) {
2437 Log.d(TAG, "going to save icon bitmap for info=" + info);
2438 // This is slower than is ideal, but this only happens once
2439 // or when the app is updated with a new icon.
2440 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002441 }
2442 }
2443
Joe Onorato9c1289c2009-08-17 11:03:03 -04002444 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002445 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002446 * or make a new one.
2447 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002448 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002449 // See if a placeholder was created for us already
2450 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002451 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002452 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002453 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002454 folders.put(id, folderInfo);
2455 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002456 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 }
2458
Winson Chung11904872012-09-17 16:58:46 -07002459 public static final Comparator<ApplicationInfo> getAppNameComparator() {
2460 final Collator collator = Collator.getInstance();
2461 return new Comparator<ApplicationInfo>() {
2462 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2463 int result = collator.compare(a.title.toString(), b.title.toString());
2464 if (result == 0) {
2465 result = a.componentName.compareTo(b.componentName);
2466 }
2467 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07002468 }
Winson Chung11904872012-09-17 16:58:46 -07002469 };
2470 }
Winson Chung78403fe2011-01-21 15:38:02 -08002471 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2472 = new Comparator<ApplicationInfo>() {
2473 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2474 if (a.firstInstallTime < b.firstInstallTime) return 1;
2475 if (a.firstInstallTime > b.firstInstallTime) return -1;
2476 return 0;
2477 }
2478 };
Winson Chung11904872012-09-17 16:58:46 -07002479 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
2480 final Collator collator = Collator.getInstance();
2481 return new Comparator<AppWidgetProviderInfo>() {
2482 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2483 return collator.compare(a.label.toString(), b.label.toString());
2484 }
2485 };
2486 }
Winson Chung5308f242011-08-18 12:12:41 -07002487 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2488 if (info.activityInfo != null) {
2489 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2490 } else {
2491 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2492 }
2493 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002494 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07002495 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07002496 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002497 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002498 ShortcutNameComparator(PackageManager pm) {
2499 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002500 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07002501 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07002502 }
2503 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2504 mPackageManager = pm;
2505 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07002506 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07002507 }
2508 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002509 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002510 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2511 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2512 if (mLabelCache.containsKey(keyA)) {
2513 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002514 } else {
2515 labelA = a.loadLabel(mPackageManager).toString();
2516
Winson Chung5308f242011-08-18 12:12:41 -07002517 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002518 }
Winson Chung5308f242011-08-18 12:12:41 -07002519 if (mLabelCache.containsKey(keyB)) {
2520 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002521 } else {
2522 labelB = b.loadLabel(mPackageManager).toString();
2523
Winson Chung5308f242011-08-18 12:12:41 -07002524 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002525 }
Winson Chung11904872012-09-17 16:58:46 -07002526 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07002527 }
2528 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002529 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07002530 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07002531 private PackageManager mPackageManager;
2532 private HashMap<Object, String> mLabelCache;
2533 WidgetAndShortcutNameComparator(PackageManager pm) {
2534 mPackageManager = pm;
2535 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07002536 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07002537 }
2538 public final int compare(Object a, Object b) {
2539 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002540 if (mLabelCache.containsKey(a)) {
2541 labelA = mLabelCache.get(a);
2542 } else {
2543 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002544 ((AppWidgetProviderInfo) a).label :
2545 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002546 mLabelCache.put(a, labelA);
2547 }
2548 if (mLabelCache.containsKey(b)) {
2549 labelB = mLabelCache.get(b);
2550 } else {
2551 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002552 ((AppWidgetProviderInfo) b).label :
2553 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002554 mLabelCache.put(b, labelB);
2555 }
Winson Chung11904872012-09-17 16:58:46 -07002556 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07002557 }
2558 };
Joe Onoratobe386092009-11-17 17:32:16 -08002559
2560 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002561 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002562 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2563 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2564 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2565 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002566 if (mLoaderTask != null) {
2567 mLoaderTask.dumpState();
2568 } else {
2569 Log.d(TAG, "mLoaderTask=null");
2570 }
Joe Onoratobe386092009-11-17 17:32:16 -08002571 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002572}