blob: 00770b28acf594e834c916764410065869e9fe57 [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;
Winson Chung83892cc2013-05-01 16:53:33 -070056import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070057import java.util.ArrayList;
58import java.util.Collections;
59import java.util.Comparator;
60import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070061import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070062import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070063import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070064import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070065
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066/**
67 * Maintains in-memory state of the Launcher. It is expected that there should be only one
68 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070069 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040071public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080072 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040073 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070074
Joe Onorato36115782010-06-17 13:28:48 -040075 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080076 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040077 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040078 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040079
Joe Onoratof99f8c12009-10-31 17:27:36 -040080 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 private final Object mLock = new Object();
82 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040083 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070084 private boolean mIsLoaderTaskRunning;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Winson Chung81b52252012-08-27 15:34:29 -070086 // Specific runnable types that are run on the main thread deferred handler, this allows us to
87 // clear all queued binding runnables when the Launcher activity is destroyed.
88 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
89 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
90
91
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070092 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
93 static {
94 sWorkerThread.start();
95 }
96 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
97
Joe Onoratocc67f472010-06-08 10:54:30 -070098 // We start off with everything not loaded. After that, we assume that
99 // our monitoring of the package manager provides all updates and we never
100 // need to do a requery. These are only ever touched from the loader thread.
101 private boolean mWorkspaceLoaded;
102 private boolean mAllAppsLoaded;
103
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700104 // When we are loading pages synchronously, we can't just post the binding of items on the side
105 // pages as this delays the rotation process. Instead, we wait for a callback from the first
106 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
107 // a normal load, we also clear this set of Runnables.
108 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700112 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700113 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700115 // The lock that must be acquired before referencing any static bg data structures. Unlike
116 // other locks, this one can generally be held long-term because we never expect any of these
117 // static data structures to be referenced outside of the worker thread except on the first
118 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700119 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700120
Adam Cohen487f7dd2012-06-28 18:12:10 -0700121 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700124
Adam Cohen487f7dd2012-06-28 18:12:10 -0700125 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
126 // created by LauncherModel that are directly on the home screen (however, no widgets or
127 // shortcuts within folders).
128 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
131 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132 new ArrayList<LauncherAppWidgetInfo>();
133
Adam Cohen487f7dd2012-06-28 18:12:10 -0700134 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
135 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700136
Adam Cohen487f7dd2012-06-28 18:12:10 -0700137 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
138 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // </ only access in worker thread >
140
141 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800142 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Adam Cohend22015c2010-07-26 22:02:18 -0700144 private static int mCellCountX;
145 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700146
Reena Lee99a73f32011-10-24 17:27:37 -0700147 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700148
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700150 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 public int getCurrentWorkspaceScreen();
152 public void startBinding();
153 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500154 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 public void finishBindingItems();
156 public void bindAppWidget(LauncherAppWidgetInfo info);
157 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500158 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
159 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700160 public void bindComponentsRemoved(ArrayList<String> packageNames,
161 ArrayList<ApplicationInfo> appInfos,
162 boolean matchPackageNamesOnly);
Winson Chung80baf5a2010-08-09 16:03:15 -0700163 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400164 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800165 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100166 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700167 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Joe Onorato0589f0f2010-02-08 13:44:00 -0800170 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800171 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400172 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700173 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800174 mIconCache = iconCache;
175
176 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700177 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400178
Reena Lee93f824a2011-09-23 17:20:28 -0700179 final Resources res = app.getResources();
180 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
181 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700182 Configuration config = res.getConfiguration();
183 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800184 }
185
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700186 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
187 * posted on the main thread handler. */
188 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700189 runOnMainThread(r, 0);
190 }
191 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700192 if (sWorkerThread.getThreadId() == Process.myTid()) {
193 // If we are on the worker thread, post onto the main handler
194 mHandler.post(r);
195 } else {
196 r.run();
197 }
198 }
199
200 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
201 * posted on the worker thread handler. */
202 private static void runOnWorkerThread(Runnable r) {
203 if (sWorkerThread.getThreadId() == Process.myTid()) {
204 r.run();
205 } else {
206 // If we are not on the worker thread, then post to the worker handler
207 sWorker.post(r);
208 }
209 }
210
Joe Onorato56d82912010-03-07 14:32:10 -0500211 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800212 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chung81b52252012-08-27 15:34:29 -0700215 public void unbindItemInfosAndClearQueuedBindRunnables() {
216 if (sWorkerThread.getThreadId() == Process.myTid()) {
217 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
218 "main thread");
219 }
220
221 // Clear any deferred bind runnables
222 mDeferredBindRunnables.clear();
223 // Remove any queued bind runnables
224 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
225 // Unbind all the workspace items
226 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700227 }
228
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700229 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700230 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700231 // Ensure that we don't use the same workspace items data structure on the main thread
232 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700233 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
234 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700235 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700236 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
237 tmpAppWidgets.addAll(sBgAppWidgets);
238 }
239 Runnable r = new Runnable() {
240 @Override
241 public void run() {
242 for (ItemInfo item : tmpWorkspaceItems) {
243 item.unbind();
244 }
245 for (ItemInfo item : tmpAppWidgets) {
246 item.unbind();
247 }
248 }
249 };
250 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700251 }
252
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 /**
254 * Adds an item to the DB if it was not created previously, or move it to a new
255 * <container, screen, cellX, cellY>
256 */
257 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
258 int screen, int cellX, int cellY) {
259 if (item.container == ItemInfo.NO_ID) {
260 // From all apps
261 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
262 } else {
263 // From somewhere else
264 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
266 }
267
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700268 static void checkItemInfoLocked(
269 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
270 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
271 if (modelItem != null && item != modelItem) {
272 // check all the data is consistent
273 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
274 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
275 ShortcutInfo shortcut = (ShortcutInfo) item;
276 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
277 modelShortcut.intent.filterEquals(shortcut.intent) &&
278 modelShortcut.id == shortcut.id &&
279 modelShortcut.itemType == shortcut.itemType &&
280 modelShortcut.container == shortcut.container &&
281 modelShortcut.screen == shortcut.screen &&
282 modelShortcut.cellX == shortcut.cellX &&
283 modelShortcut.cellY == shortcut.cellY &&
284 modelShortcut.spanX == shortcut.spanX &&
285 modelShortcut.spanY == shortcut.spanY &&
286 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
287 (modelShortcut.dropPos != null &&
288 shortcut.dropPos != null &&
289 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
290 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
291 // For all intents and purposes, this is the same object
292 return;
293 }
294 }
295
296 // the modelItem needs to match up perfectly with item if our model is
297 // to be consistent with the database-- for now, just require
298 // modelItem == item or the equality check above
299 String msg = "item: " + ((item != null) ? item.toString() : "null") +
300 "modelItem: " +
301 ((modelItem != null) ? modelItem.toString() : "null") +
302 "Error: ItemInfo passed to checkItemInfo doesn't match original";
303 RuntimeException e = new RuntimeException(msg);
304 if (stackTrace != null) {
305 e.setStackTrace(stackTrace);
306 }
307 throw e;
308 }
309 }
310
Michael Jurka816474f2012-06-25 14:49:02 -0700311 static void checkItemInfo(final ItemInfo item) {
312 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
313 final long itemId = item.id;
314 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700315 public void run() {
316 synchronized (sBgLock) {
317 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700318 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700319 }
320 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700321 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700322 }
323
Michael Jurkac9d95c52011-08-29 14:03:34 -0700324 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
325 final ItemInfo item, final String callingFunction) {
326 final long itemId = item.id;
327 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
328 final ContentResolver cr = context.getContentResolver();
329
Adam Cohen487f7dd2012-06-28 18:12:10 -0700330 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700331 Runnable r = new Runnable() {
332 public void run() {
333 cr.update(uri, values, null, null);
334
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700335 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700336 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700337 checkItemInfoLocked(itemId, item, stackTrace);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700338
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700339 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
340 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
341 // Item is in a folder, make sure this folder exists
342 if (!sBgFolders.containsKey(item.container)) {
343 // An items container is being set to a that of an item which is not in
344 // the list of Folders.
345 String msg = "item: " + item + " container being set to: " +
346 item.container + ", not in the list of folders";
Adam Cohen28b3e102012-10-04 17:21:33 -0700347 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700348 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700349 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700350 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700351
352 // Items are added/removed from the corresponding FolderInfo elsewhere, such
353 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
354 // that are on the desktop, as appropriate
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700355 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700356 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
357 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
358 switch (modelItem.itemType) {
359 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
360 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
361 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
362 if (!sBgWorkspaceItems.contains(modelItem)) {
363 sBgWorkspaceItems.add(modelItem);
364 }
365 break;
366 default:
367 break;
368 }
369 } else {
370 sBgWorkspaceItems.remove(modelItem);
371 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700372 }
373 }
374 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700375 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700376 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700380 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700381 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
382 final int screen, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700383 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
384 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700385 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
386 Launcher.sDumpLogs.add(transaction);
387 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400388 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400389 item.cellX = cellX;
390 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700391
Winson Chung3d503fb2011-07-13 17:25:49 -0700392 // We store hotseat items in canonical form which is this orientation invariant position
393 // in the hotseat
394 if (context instanceof Launcher && screen < 0 &&
395 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
396 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
397 } else {
398 item.screen = screen;
399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400400
401 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400402 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700403 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
404 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
406
Michael Jurkac9d95c52011-08-29 14:03:34 -0700407 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700408 }
409
410 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700411 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800412 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700413 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
414 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700415 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
416 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700417 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
418 Launcher.sDumpLogs.add(transaction);
419 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800420 item.cellX = cellX;
421 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700422 item.spanX = spanX;
423 item.spanY = spanY;
424
425 // We store hotseat items in canonical form which is this orientation invariant position
426 // in the hotseat
427 if (context instanceof Launcher && screen < 0 &&
428 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
429 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
430 } else {
431 item.screen = screen;
432 }
Adam Cohend4844c32011-02-18 19:25:06 -0800433
Adam Cohend4844c32011-02-18 19:25:06 -0800434 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800435 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700436 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
437 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
438 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
439 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
440 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800441
Michael Jurka816474f2012-06-25 14:49:02 -0700442 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700443 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700444
445 /**
446 * Update an item to the database in a specified container.
447 */
448 static void updateItemInDatabase(Context context, final ItemInfo item) {
449 final ContentValues values = new ContentValues();
450 item.onAddToDatabase(values);
451 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
452 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800453 }
454
455 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400456 * Returns true if the shortcuts already exists in the database.
457 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400459 static boolean shortcutExists(Context context, String title, Intent intent) {
460 final ContentResolver cr = context.getContentResolver();
461 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
462 new String[] { "title", "intent" }, "title=? and intent=?",
463 new String[] { title, intent.toUri(0) }, null);
464 boolean result = false;
465 try {
466 result = c.moveToFirst();
467 } finally {
468 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400470 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700471 }
472
Joe Onorato9c1289c2009-08-17 11:03:03 -0400473 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700474 * Returns an ItemInfo array containing all the items in the LauncherModel.
475 * The ItemInfo.id is not set through this function.
476 */
477 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
478 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
479 final ContentResolver cr = context.getContentResolver();
480 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
481 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
482 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
483 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
484
485 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
486 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
487 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
488 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
489 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
490 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
491 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
492
493 try {
494 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700495 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700496 item.cellX = c.getInt(cellXIndex);
497 item.cellY = c.getInt(cellYIndex);
498 item.spanX = c.getInt(spanXIndex);
499 item.spanY = c.getInt(spanYIndex);
500 item.container = c.getInt(containerIndex);
501 item.itemType = c.getInt(itemTypeIndex);
502 item.screen = c.getInt(screenIndex);
503
504 items.add(item);
505 }
506 } catch (Exception e) {
507 items.clear();
508 } finally {
509 c.close();
510 }
511
512 return items;
513 }
514
515 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
517 */
518 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
519 final ContentResolver cr = context.getContentResolver();
520 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
521 "_id=? and (itemType=? or itemType=?)",
522 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700523 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700524
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525 try {
526 if (c.moveToFirst()) {
527 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
528 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
529 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
530 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
531 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
532 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533
Joe Onorato9c1289c2009-08-17 11:03:03 -0400534 FolderInfo folderInfo = null;
535 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
537 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700539 }
540
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541 folderInfo.title = c.getString(titleIndex);
542 folderInfo.id = id;
543 folderInfo.container = c.getInt(containerIndex);
544 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700545 folderInfo.cellX = c.getInt(cellXIndex);
546 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547
548 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700549 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550 } finally {
551 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700552 }
553
554 return null;
555 }
556
Joe Onorato9c1289c2009-08-17 11:03:03 -0400557 /**
558 * Add an item to the database in a specified container. Sets the container, screen, cellX and
559 * cellY fields of the item. Also assigns an ID to the item.
560 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700561 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
562 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 item.cellX = cellX;
565 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700566 // We store hotseat items in canonical form which is this orientation invariant position
567 // in the hotseat
568 if (context instanceof Launcher && screen < 0 &&
569 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
570 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
571 } else {
572 item.screen = screen;
573 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574
575 final ContentValues values = new ContentValues();
576 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577 item.onAddToDatabase(values);
578
Michael Jurka7578ec62011-08-03 14:11:54 -0700579 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700580 item.id = app.getLauncherProvider().generateNewId();
581 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700582 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700583
Michael Jurkac9d95c52011-08-29 14:03:34 -0700584 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700585 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700586 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
587 + item.id + " (" + container + ", " + screen + ", " + cellX + ", "
588 + cellY + ")";
589 Launcher.sDumpLogs.add(transaction);
590 Log.d(TAG, transaction);
591
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700592 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
593 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400594
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700595 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700596 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700597 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700598 sBgItemsIdMap.put(item.id, item);
599 switch (item.itemType) {
600 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
601 sBgFolders.put(item.id, (FolderInfo) item);
602 // Fall through
603 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
604 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
605 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
606 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
607 sBgWorkspaceItems.add(item);
608 } else {
609 if (!sBgFolders.containsKey(item.container)) {
610 // Adding an item to a folder that doesn't exist.
611 String msg = "adding item: " + item + " to a folder that " +
612 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700613 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700614 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700615 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700616 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700617 break;
618 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
619 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
620 break;
621 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700622 }
623 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700624 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700625 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700626 }
627
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700629 * Creates a new unique child id, for a given cell span across all layouts.
630 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700631 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700632 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
633 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700634 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 }
636
Adam Cohend22015c2010-07-26 22:02:18 -0700637 static int getCellCountX() {
638 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 }
640
Adam Cohend22015c2010-07-26 22:02:18 -0700641 static int getCellCountY() {
642 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 }
644
645 /**
646 * Updates the model orientation helper to take into account the current layout dimensions
647 * when performing local/canonical coordinate transformations.
648 */
649 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700650 mCellCountX = shortAxisCellCount;
651 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700652 }
653
654 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700655 * Removes the specified item from the database
656 * @param context
657 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700659 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700661 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700662
Michael Jurka83df1882011-08-31 20:59:26 -0700663 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700664 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700665 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
666 + item.id + " (" + item.container + ", " + item.screen + ", " + item.cellX +
667 ", " + item.cellY + ")";
668 Launcher.sDumpLogs.add(transaction);
669 Log.d(TAG, transaction);
670
Michael Jurkac9d95c52011-08-29 14:03:34 -0700671 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700672
673 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700674 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700675 switch (item.itemType) {
676 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
677 sBgFolders.remove(item.id);
678 for (ItemInfo info: sBgItemsIdMap.values()) {
679 if (info.container == item.id) {
680 // We are deleting a folder which still contains items that
681 // think they are contained by that folder.
682 String msg = "deleting a folder (" + item + ") which still " +
683 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700684 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700685 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700686 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700687 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700688 sBgWorkspaceItems.remove(item);
689 break;
690 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
691 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
692 sBgWorkspaceItems.remove(item);
693 break;
694 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
695 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
696 break;
697 }
698 sBgItemsIdMap.remove(item.id);
699 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700700 }
701 }
Michael Jurka83df1882011-08-31 20:59:26 -0700702 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700703 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 }
705
706 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 * Remove the contents of the specified folder from the database
708 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700709 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 final ContentResolver cr = context.getContentResolver();
711
Michael Jurkac9d95c52011-08-29 14:03:34 -0700712 Runnable r = new Runnable() {
713 public void run() {
714 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700715 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700716 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700717 sBgItemsIdMap.remove(info.id);
718 sBgFolders.remove(info.id);
719 sBgDbIconCache.remove(info);
720 sBgWorkspaceItems.remove(info);
721 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700722
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
724 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700725 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700726 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700727 for (ItemInfo childInfo : info.contents) {
728 sBgItemsIdMap.remove(childInfo.id);
729 sBgDbIconCache.remove(childInfo);
730 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700731 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700732 }
733 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700734 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 }
736
737 /**
738 * Set this as the current Launcher activity object for the loader.
739 */
740 public void initialize(Callbacks callbacks) {
741 synchronized (mLock) {
742 mCallbacks = new WeakReference<Callbacks>(callbacks);
743 }
744 }
745
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700746 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
748 * ACTION_PACKAGE_CHANGED.
749 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100750 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400751 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400752 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700753
Joe Onorato36115782010-06-17 13:28:48 -0400754 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400755
Joe Onorato36115782010-06-17 13:28:48 -0400756 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
757 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
758 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
759 final String packageName = intent.getData().getSchemeSpecificPart();
760 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761
Joe Onorato36115782010-06-17 13:28:48 -0400762 int op = PackageUpdatedTask.OP_NONE;
763
764 if (packageName == null || packageName.length() == 0) {
765 // they sent us a bad intent
766 return;
767 }
768
769 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
770 op = PackageUpdatedTask.OP_UPDATE;
771 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
772 if (!replacing) {
773 op = PackageUpdatedTask.OP_REMOVE;
774 }
775 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
776 // later, we will update the package at this time
777 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
778 if (!replacing) {
779 op = PackageUpdatedTask.OP_ADD;
780 } else {
781 op = PackageUpdatedTask.OP_UPDATE;
782 }
783 }
784
785 if (op != PackageUpdatedTask.OP_NONE) {
786 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
787 }
788
789 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400790 // First, schedule to add these apps back in.
791 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
792 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
793 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700794 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400795 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
796 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
797 enqueuePackageUpdated(new PackageUpdatedTask(
798 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700799 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700800 // If we have changed locale we need to clear out the labels in all apps/workspace.
801 forceReload();
802 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
803 // Check if configuration change was an mcc/mnc change which would affect app resources
804 // and we would need to clear out the labels in all apps/workspace. Same handling as
805 // above for ACTION_LOCALE_CHANGED
806 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700807 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700808 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700809 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700810 forceReload();
811 }
812 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700813 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700814 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
815 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700816 if (mCallbacks != null) {
817 Callbacks callbacks = mCallbacks.get();
818 if (callbacks != null) {
819 callbacks.bindSearchablesChanged();
820 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700821 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700822 }
823 }
824
Reena Lee93f824a2011-09-23 17:20:28 -0700825 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700826 resetLoadedState(true, true);
827
Reena Lee93f824a2011-09-23 17:20:28 -0700828 // Do this here because if the launcher activity is running it will be restarted.
829 // If it's not running startLoaderFromBackground will merely tell it that it needs
830 // to reload.
831 startLoaderFromBackground();
832 }
833
Winson Chungf0c6ae02012-03-21 16:10:31 -0700834 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
835 synchronized (mLock) {
836 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
837 // mWorkspaceLoaded to true later
838 stopLoaderLocked();
839 if (resetAllAppsLoaded) mAllAppsLoaded = false;
840 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
841 }
842 }
843
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700844 /**
845 * When the launcher is in the background, it's possible for it to miss paired
846 * configuration changes. So whenever we trigger the loader from the background
847 * tell the launcher that it needs to re-run the loader when it comes back instead
848 * of doing it now.
849 */
850 public void startLoaderFromBackground() {
851 boolean runLoader = false;
852 if (mCallbacks != null) {
853 Callbacks callbacks = mCallbacks.get();
854 if (callbacks != null) {
855 // Only actually run the loader if they're not paused.
856 if (!callbacks.setLoadOnResume()) {
857 runLoader = true;
858 }
859 }
860 }
861 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700862 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -0700863 }
Joe Onorato36115782010-06-17 13:28:48 -0400864 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400865
Reena Lee93f824a2011-09-23 17:20:28 -0700866 // If there is already a loader task running, tell it to stop.
867 // returns true if isLaunching() was true on the old task
868 private boolean stopLoaderLocked() {
869 boolean isLaunching = false;
870 LoaderTask oldTask = mLoaderTask;
871 if (oldTask != null) {
872 if (oldTask.isLaunching()) {
873 isLaunching = true;
874 }
875 oldTask.stopLocked();
876 }
877 return isLaunching;
878 }
879
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700880 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -0400881 synchronized (mLock) {
882 if (DEBUG_LOADERS) {
883 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 // Clear any deferred bind-runnables from the synchronized load process
887 // We must do this before any loading/binding is scheduled below.
888 mDeferredBindRunnables.clear();
889
Joe Onorato36115782010-06-17 13:28:48 -0400890 // Don't bother to start the thread if we know it's not going to do anything
891 if (mCallbacks != null && mCallbacks.get() != null) {
892 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700893 // also, don't downgrade isLaunching if we're already running
894 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700895 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700896 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
897 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
898 } else {
899 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
900 sWorker.post(mLoaderTask);
901 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 }
904 }
905
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906 void bindRemainingSynchronousPages() {
907 // Post the remaining side pages to be loaded
908 if (!mDeferredBindRunnables.isEmpty()) {
909 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -0700910 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700911 }
912 mDeferredBindRunnables.clear();
913 }
914 }
915
Joe Onorato36115782010-06-17 13:28:48 -0400916 public void stopLoader() {
917 synchronized (mLock) {
918 if (mLoaderTask != null) {
919 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 }
921 }
Joe Onorato36115782010-06-17 13:28:48 -0400922 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923
Michael Jurkac57b7a82011-08-09 22:02:20 -0700924 public boolean isAllAppsLoaded() {
925 return mAllAppsLoaded;
926 }
927
Winson Chung36a62fe2012-05-06 18:04:42 -0700928 boolean isLoadingWorkspace() {
929 synchronized (mLock) {
930 if (mLoaderTask != null) {
931 return mLoaderTask.isLoadingWorkspace();
932 }
933 }
934 return false;
935 }
936
Joe Onorato36115782010-06-17 13:28:48 -0400937 /**
938 * Runnable for the thread that loads the contents of the launcher:
939 * - workspace icons
940 * - widgets
941 * - all apps icons
942 */
943 private class LoaderTask implements Runnable {
944 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -0400945 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700946 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400947 private boolean mStopped;
948 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700949
Winson Chungc3eecff2011-07-11 17:44:15 -0700950 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400951
952 LoaderTask(Context context, boolean isLaunching) {
953 mContext = context;
954 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700955 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 }
957
Joe Onorato36115782010-06-17 13:28:48 -0400958 boolean isLaunching() {
959 return mIsLaunching;
960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961
Winson Chung36a62fe2012-05-06 18:04:42 -0700962 boolean isLoadingWorkspace() {
963 return mIsLoadingAndBindingWorkspace;
964 }
965
Joe Onorato36115782010-06-17 13:28:48 -0400966 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700967 mIsLoadingAndBindingWorkspace = true;
968
Joe Onorato36115782010-06-17 13:28:48 -0400969 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400970 if (DEBUG_LOADERS) {
971 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700973
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700974 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400975 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700976 synchronized (LoaderTask.this) {
977 if (mStopped) {
978 return;
979 }
980 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 }
982 }
983
Joe Onorato36115782010-06-17 13:28:48 -0400984 // Bind the workspace
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -0400986 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400987
Joe Onorato36115782010-06-17 13:28:48 -0400988 private void waitForIdle() {
989 // Wait until the either we're stopped or the other threads are done.
990 // This way we don't start loading all apps until the workspace has settled
991 // down.
992 synchronized (LoaderTask.this) {
993 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700994
Joe Onorato36115782010-06-17 13:28:48 -0400995 mHandler.postIdle(new Runnable() {
996 public void run() {
997 synchronized (LoaderTask.this) {
998 mLoadAndBindStepFinished = true;
999 if (DEBUG_LOADERS) {
1000 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001001 }
Joe Onorato36115782010-06-17 13:28:48 -04001002 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001003 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001004 }
Joe Onorato36115782010-06-17 13:28:48 -04001005 });
1006
1007 while (!mStopped && !mLoadAndBindStepFinished) {
1008 try {
1009 this.wait();
1010 } catch (InterruptedException ex) {
1011 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001012 }
1013 }
Joe Onorato36115782010-06-17 13:28:48 -04001014 if (DEBUG_LOADERS) {
1015 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001016 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001017 + "ms for previous step to finish binding");
1018 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001019 }
Joe Onorato36115782010-06-17 13:28:48 -04001020 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001021
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 void runBindSynchronousPage(int synchronousBindPage) {
1023 if (synchronousBindPage < 0) {
1024 // Ensure that we have a valid page index to load synchronously
1025 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1026 "valid page index");
1027 }
1028 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1029 // Ensure that we don't try and bind a specified page when the pages have not been
1030 // loaded already (we should load everything asynchronously in that case)
1031 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1032 }
1033 synchronized (mLock) {
1034 if (mIsLoaderTaskRunning) {
1035 // Ensure that we are never running the background loading at this point since
1036 // we also touch the background collections
1037 throw new RuntimeException("Error! Background loading is already running");
1038 }
1039 }
1040
1041 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1042 // data structures, we can't allow any other thread to touch that data, but because
1043 // this call is synchronous, we can get away with not locking).
1044
Adam Cohena13a2f22012-07-23 14:29:15 -07001045 // The LauncherModel is static in the LauncherApplication and mHandler may have queued
1046 // operations from the previous activity. We need to ensure that all queued operations
1047 // are executed before any synchronous binding work is done.
1048 mHandler.flush();
1049
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050 // Divide the set of loaded items into those that we are binding synchronously, and
1051 // everything else that is to be bound normally (asynchronously).
1052 bindWorkspace(synchronousBindPage);
1053 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1054 // arise from that.
1055 onlyBindAllApps();
1056 }
1057
Joe Onorato36115782010-06-17 13:28:48 -04001058 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001059 synchronized (mLock) {
1060 mIsLoaderTaskRunning = true;
1061 }
Joe Onorato36115782010-06-17 13:28:48 -04001062 // Optimize for end-user experience: if the Launcher is up and // running with the
1063 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1064 // workspace first (default).
1065 final Callbacks cbk = mCallbacks.get();
1066 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -04001067
Joe Onorato36115782010-06-17 13:28:48 -04001068 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001069 // Elevate priority when Home launches for the first time to avoid
1070 // starving at boot time. Staring at a blank home is not cool.
1071 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001072 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1073 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001074 android.os.Process.setThreadPriority(mIsLaunching
1075 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1076 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001077 if (loadWorkspaceFirst) {
1078 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1079 loadAndBindWorkspace();
1080 } else {
1081 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001082 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001083 }
1084
Joe Onorato36115782010-06-17 13:28:48 -04001085 if (mStopped) {
1086 break keep_running;
1087 }
1088
1089 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1090 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001091 synchronized (mLock) {
1092 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001093 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001094 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1095 }
1096 }
Joe Onorato36115782010-06-17 13:28:48 -04001097 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001098
1099 // second step
1100 if (loadWorkspaceFirst) {
1101 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001102 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001103 } else {
1104 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
1105 loadAndBindWorkspace();
1106 }
Winson Chung7ed37742011-09-08 15:45:51 -07001107
1108 // Restore the default thread priority after we are done loading items
1109 synchronized (mLock) {
1110 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1111 }
Joe Onorato36115782010-06-17 13:28:48 -04001112 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113
Winson Chungaac01e12011-08-17 10:37:13 -07001114
1115 // Update the saved icons if necessary
1116 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001117 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 for (Object key : sBgDbIconCache.keySet()) {
1119 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1120 }
1121 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001122 }
Winson Chungaac01e12011-08-17 10:37:13 -07001123
Joe Onorato36115782010-06-17 13:28:48 -04001124 // Clear out this reference, otherwise we end up holding it until all of the
1125 // callback runnables are done.
1126 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127
Joe Onorato36115782010-06-17 13:28:48 -04001128 synchronized (mLock) {
1129 // If we are still the last one to be scheduled, remove ourselves.
1130 if (mLoaderTask == this) {
1131 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001133 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001134 }
Joe Onorato36115782010-06-17 13:28:48 -04001135 }
1136
1137 public void stopLocked() {
1138 synchronized (LoaderTask.this) {
1139 mStopped = true;
1140 this.notify();
1141 }
1142 }
1143
1144 /**
1145 * Gets the callbacks object. If we've been stopped, or if the launcher object
1146 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1147 * object that was around when the deferred message was scheduled, and if there's
1148 * a new Callbacks object around then also return null. This will save us from
1149 * calling onto it with data that will be ignored.
1150 */
1151 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1152 synchronized (mLock) {
1153 if (mStopped) {
1154 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001155 }
Joe Onorato36115782010-06-17 13:28:48 -04001156
1157 if (mCallbacks == null) {
1158 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001159 }
Joe Onorato36115782010-06-17 13:28:48 -04001160
1161 final Callbacks callbacks = mCallbacks.get();
1162 if (callbacks != oldCallbacks) {
1163 return null;
1164 }
1165 if (callbacks == null) {
1166 Log.w(TAG, "no mCallbacks");
1167 return null;
1168 }
1169
1170 return callbacks;
1171 }
1172 }
1173
1174 // check & update map of what's occupied; used to discard overlapping/invalid items
1175 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001176 int containerIndex = item.screen;
1177 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001178 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08001179 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001180 return false;
1181 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001182
1183 // We use the last index to refer to the hotseat and the screen as the rank, so
1184 // test and update the occupied state accordingly
1185 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
1186 Log.e(TAG, "Error loading shortcut into hotseat " + item
1187 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
1188 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
1189 return false;
1190 } else {
1191 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
1192 return true;
1193 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001194 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1195 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001196 return true;
1197 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001198
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001199 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001200 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1201 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001202 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001203 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -07001204 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001205 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001206 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -07001207 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001208 return false;
1209 }
1210 }
1211 }
1212 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1213 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001214 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001215 }
1216 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001217
Joe Onorato36115782010-06-17 13:28:48 -04001218 return true;
1219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220
Joe Onorato36115782010-06-17 13:28:48 -04001221 private void loadWorkspace() {
1222 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223
Joe Onorato36115782010-06-17 13:28:48 -04001224 final Context context = mContext;
1225 final ContentResolver contentResolver = context.getContentResolver();
1226 final PackageManager manager = context.getPackageManager();
1227 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1228 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001229
Michael Jurkab85f8a42012-04-25 15:48:32 -07001230 // Make sure the default workspace is loaded, if needed
Brian Muramatsu5524b492012-10-02 16:55:54 -07001231 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001232
Winson Chung2abf94d2012-07-18 18:16:38 -07001233 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001234 sBgWorkspaceItems.clear();
1235 sBgAppWidgets.clear();
1236 sBgFolders.clear();
1237 sBgItemsIdMap.clear();
1238 sBgDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001239
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001240 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001242 final Cursor c = contentResolver.query(
1243 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001244
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001245 // +1 for the hotseat (it can be larger than the workspace)
1246 // Load workspace in reverse order to ensure that latest items are loaded first (and
1247 // before any earlier duplicates)
1248 final ItemInfo occupied[][][] =
1249 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001251 try {
1252 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1253 final int intentIndex = c.getColumnIndexOrThrow
1254 (LauncherSettings.Favorites.INTENT);
1255 final int titleIndex = c.getColumnIndexOrThrow
1256 (LauncherSettings.Favorites.TITLE);
1257 final int iconTypeIndex = c.getColumnIndexOrThrow(
1258 LauncherSettings.Favorites.ICON_TYPE);
1259 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1260 final int iconPackageIndex = c.getColumnIndexOrThrow(
1261 LauncherSettings.Favorites.ICON_PACKAGE);
1262 final int iconResourceIndex = c.getColumnIndexOrThrow(
1263 LauncherSettings.Favorites.ICON_RESOURCE);
1264 final int containerIndex = c.getColumnIndexOrThrow(
1265 LauncherSettings.Favorites.CONTAINER);
1266 final int itemTypeIndex = c.getColumnIndexOrThrow(
1267 LauncherSettings.Favorites.ITEM_TYPE);
1268 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1269 LauncherSettings.Favorites.APPWIDGET_ID);
1270 final int screenIndex = c.getColumnIndexOrThrow(
1271 LauncherSettings.Favorites.SCREEN);
1272 final int cellXIndex = c.getColumnIndexOrThrow
1273 (LauncherSettings.Favorites.CELLX);
1274 final int cellYIndex = c.getColumnIndexOrThrow
1275 (LauncherSettings.Favorites.CELLY);
1276 final int spanXIndex = c.getColumnIndexOrThrow
1277 (LauncherSettings.Favorites.SPANX);
1278 final int spanYIndex = c.getColumnIndexOrThrow(
1279 LauncherSettings.Favorites.SPANY);
1280 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1281 //final int displayModeIndex = c.getColumnIndexOrThrow(
1282 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001283
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001284 ShortcutInfo info;
1285 String intentDescription;
1286 LauncherAppWidgetInfo appWidgetInfo;
1287 int container;
1288 long id;
1289 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001290
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001291 while (!mStopped && c.moveToNext()) {
1292 try {
1293 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001294
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001295 switch (itemType) {
1296 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1297 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1298 intentDescription = c.getString(intentIndex);
1299 try {
1300 intent = Intent.parseUri(intentDescription, 0);
1301 } catch (URISyntaxException e) {
1302 continue;
1303 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001304
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001305 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1306 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1307 titleIndex, mLabelCache);
1308 } else {
1309 info = getShortcutInfo(c, context, iconTypeIndex,
1310 iconPackageIndex, iconResourceIndex, iconIndex,
1311 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001312
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001313 // App shortcuts that used to be automatically added to Launcher
1314 // didn't always have the correct intent flags set, so do that
1315 // here
1316 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001317 intent.getCategories() != null &&
1318 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001319 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001320 intent.addFlags(
1321 Intent.FLAG_ACTIVITY_NEW_TASK |
1322 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1323 }
Michael Jurka96879562012-03-22 05:54:33 -07001324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 if (info != null) {
1327 info.intent = intent;
1328 info.id = c.getLong(idIndex);
1329 container = c.getInt(containerIndex);
1330 info.container = container;
1331 info.screen = c.getInt(screenIndex);
1332 info.cellX = c.getInt(cellXIndex);
1333 info.cellY = c.getInt(cellYIndex);
1334
1335 // check & update map of what's occupied
1336 if (!checkItemPlacement(occupied, info)) {
1337 break;
1338 }
1339
1340 switch (container) {
1341 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1342 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1343 sBgWorkspaceItems.add(info);
1344 break;
1345 default:
1346 // Item is in a user folder
1347 FolderInfo folderInfo =
1348 findOrMakeFolder(sBgFolders, container);
1349 folderInfo.add(info);
1350 break;
1351 }
1352 sBgItemsIdMap.put(info.id, info);
1353
1354 // now that we've loaded everthing re-save it with the
1355 // icon in case it disappears somehow.
1356 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
1357 } else {
1358 // Failed to load the shortcut, probably because the
1359 // activity manager couldn't resolve it (maybe the app
1360 // was uninstalled), or the db row was somehow screwed up.
1361 // Delete it.
1362 id = c.getLong(idIndex);
1363 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1364 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1365 id, false), null, null);
1366 }
1367 break;
1368
1369 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1370 id = c.getLong(idIndex);
1371 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1372
1373 folderInfo.title = c.getString(titleIndex);
1374 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376 folderInfo.container = container;
1377 folderInfo.screen = c.getInt(screenIndex);
1378 folderInfo.cellX = c.getInt(cellXIndex);
1379 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380
Daniel Sandler8802e962010-05-26 16:28:16 -04001381 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001382 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001383 break;
1384 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001385 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1387 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1388 sBgWorkspaceItems.add(folderInfo);
1389 break;
Joe Onorato36115782010-06-17 13:28:48 -04001390 }
Joe Onorato17a89222011-02-08 17:26:11 -08001391
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001392 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1393 sBgFolders.put(folderInfo.id, folderInfo);
1394 break;
1395
1396 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1397 // Read all Launcher-specific widget details
1398 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001399 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001400
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001401 final AppWidgetProviderInfo provider =
1402 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001403
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001404 if (!isSafeMode && (provider == null || provider.provider == null ||
1405 provider.provider.getPackageName() == null)) {
1406 String log = "Deleting widget that isn't installed anymore: id="
1407 + id + " appWidgetId=" + appWidgetId;
1408 Log.e(TAG, log);
1409 Launcher.sDumpLogs.add(log);
1410 itemsToRemove.add(id);
1411 } else {
1412 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1413 provider.provider);
1414 appWidgetInfo.id = id;
1415 appWidgetInfo.screen = c.getInt(screenIndex);
1416 appWidgetInfo.cellX = c.getInt(cellXIndex);
1417 appWidgetInfo.cellY = c.getInt(cellYIndex);
1418 appWidgetInfo.spanX = c.getInt(spanXIndex);
1419 appWidgetInfo.spanY = c.getInt(spanYIndex);
1420 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1421 appWidgetInfo.minSpanX = minSpan[0];
1422 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001423
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001424 container = c.getInt(containerIndex);
1425 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1426 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1427 Log.e(TAG, "Widget found where container != " +
1428 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1429 continue;
1430 }
1431 appWidgetInfo.container = c.getInt(containerIndex);
1432
1433 // check & update map of what's occupied
1434 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1435 break;
1436 }
1437 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1438 sBgAppWidgets.add(appWidgetInfo);
1439 }
Joe Onorato36115782010-06-17 13:28:48 -04001440 break;
1441 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442 } catch (Exception e) {
1443 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001444 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001445 }
1446 } finally {
1447 c.close();
1448 }
1449
1450 if (itemsToRemove.size() > 0) {
1451 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1452 LauncherSettings.Favorites.CONTENT_URI);
1453 // Remove dead items
1454 for (long id : itemsToRemove) {
1455 if (DEBUG_LOADERS) {
1456 Log.d(TAG, "Removed id = " + id);
1457 }
1458 // Don't notify content observers
1459 try {
1460 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1461 null, null);
1462 } catch (RemoteException e) {
1463 Log.w(TAG, "Could not remove id = " + id);
1464 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001465 }
1466 }
1467
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 if (DEBUG_LOADERS) {
1469 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1470 Log.d(TAG, "workspace layout: ");
1471 for (int y = 0; y < mCellCountY; y++) {
1472 String line = "";
1473 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1474 if (s > 0) {
1475 line += " | ";
1476 }
1477 for (int x = 0; x < mCellCountX; x++) {
1478 line += ((occupied[s][x][y] != null) ? "#" : ".");
1479 }
Joe Onorato36115782010-06-17 13:28:48 -04001480 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001481 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001482 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483 }
Joe Onorato36115782010-06-17 13:28:48 -04001484 }
1485 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001486
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001487 /** Filters the set of items who are directly or indirectly (via another container) on the
1488 * specified screen. */
1489 private void filterCurrentWorkspaceItems(int currentScreen,
1490 ArrayList<ItemInfo> allWorkspaceItems,
1491 ArrayList<ItemInfo> currentScreenItems,
1492 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001493 // Purge any null ItemInfos
1494 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1495 while (iter.hasNext()) {
1496 ItemInfo i = iter.next();
1497 if (i == null) {
1498 iter.remove();
1499 }
1500 }
1501
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001502 // If we aren't filtering on a screen, then the set of items to load is the full set of
1503 // items given.
1504 if (currentScreen < 0) {
1505 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001506 }
1507
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 // Order the set of items by their containers first, this allows use to walk through the
1509 // list sequentially, build up a list of containers that are in the specified screen,
1510 // as well as all items in those containers.
1511 Set<Long> itemsOnScreen = new HashSet<Long>();
1512 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1513 @Override
1514 public int compare(ItemInfo lhs, ItemInfo rhs) {
1515 return (int) (lhs.container - rhs.container);
1516 }
1517 });
1518 for (ItemInfo info : allWorkspaceItems) {
1519 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1520 if (info.screen == currentScreen) {
1521 currentScreenItems.add(info);
1522 itemsOnScreen.add(info.id);
1523 } else {
1524 otherScreenItems.add(info);
1525 }
1526 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1527 currentScreenItems.add(info);
1528 itemsOnScreen.add(info.id);
1529 } else {
1530 if (itemsOnScreen.contains(info.container)) {
1531 currentScreenItems.add(info);
1532 itemsOnScreen.add(info.id);
1533 } else {
1534 otherScreenItems.add(info);
1535 }
1536 }
1537 }
1538 }
1539
1540 /** Filters the set of widgets which are on the specified screen. */
1541 private void filterCurrentAppWidgets(int currentScreen,
1542 ArrayList<LauncherAppWidgetInfo> appWidgets,
1543 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1544 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1545 // If we aren't filtering on a screen, then the set of items to load is the full set of
1546 // widgets given.
1547 if (currentScreen < 0) {
1548 currentScreenWidgets.addAll(appWidgets);
1549 }
1550
1551 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001552 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001553 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1554 widget.screen == currentScreen) {
1555 currentScreenWidgets.add(widget);
1556 } else {
1557 otherScreenWidgets.add(widget);
1558 }
1559 }
1560 }
1561
1562 /** Filters the set of folders which are on the specified screen. */
1563 private void filterCurrentFolders(int currentScreen,
1564 HashMap<Long, ItemInfo> itemsIdMap,
1565 HashMap<Long, FolderInfo> folders,
1566 HashMap<Long, FolderInfo> currentScreenFolders,
1567 HashMap<Long, FolderInfo> otherScreenFolders) {
1568 // If we aren't filtering on a screen, then the set of items to load is the full set of
1569 // widgets given.
1570 if (currentScreen < 0) {
1571 currentScreenFolders.putAll(folders);
1572 }
1573
1574 for (long id : folders.keySet()) {
1575 ItemInfo info = itemsIdMap.get(id);
1576 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07001577 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1579 info.screen == currentScreen) {
1580 currentScreenFolders.put(id, folder);
1581 } else {
1582 otherScreenFolders.put(id, folder);
1583 }
1584 }
1585 }
1586
1587 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1588 * right) */
1589 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1590 // XXX: review this
1591 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001592 @Override
1593 public int compare(ItemInfo lhs, ItemInfo rhs) {
1594 int cellCountX = LauncherModel.getCellCountX();
1595 int cellCountY = LauncherModel.getCellCountY();
1596 int screenOffset = cellCountX * cellCountY;
1597 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1598 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1599 lhs.cellY * cellCountX + lhs.cellX);
1600 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1601 rhs.cellY * cellCountX + rhs.cellX);
1602 return (int) (lr - rr);
1603 }
1604 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001605 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001606
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001607 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1608 final ArrayList<ItemInfo> workspaceItems,
1609 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1610 final HashMap<Long, FolderInfo> folders,
1611 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07001612
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001613 final boolean postOnMainThread = (deferredBindRunnables != null);
1614
1615 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001616 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001617 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001618 final int start = i;
1619 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 final Runnable r = new Runnable() {
1621 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001623 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001625 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626 }
1627 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001628 };
1629 if (postOnMainThread) {
1630 deferredBindRunnables.add(r);
1631 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001632 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001633 }
Joe Onorato36115782010-06-17 13:28:48 -04001634 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001635
1636 // Bind the folders
1637 if (!folders.isEmpty()) {
1638 final Runnable r = new Runnable() {
1639 public void run() {
1640 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1641 if (callbacks != null) {
1642 callbacks.bindFolders(folders);
1643 }
1644 }
1645 };
1646 if (postOnMainThread) {
1647 deferredBindRunnables.add(r);
1648 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001649 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001650 }
1651 }
1652
1653 // Bind the widgets, one at a time
1654 N = appWidgets.size();
1655 for (int i = 0; i < N; i++) {
1656 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1657 final Runnable r = new Runnable() {
1658 public void run() {
1659 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1660 if (callbacks != null) {
1661 callbacks.bindAppWidget(widget);
1662 }
1663 }
1664 };
1665 if (postOnMainThread) {
1666 deferredBindRunnables.add(r);
1667 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001668 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669 }
1670 }
1671 }
1672
1673 /**
1674 * Binds all loaded data to actual views on the main thread.
1675 */
1676 private void bindWorkspace(int synchronizeBindPage) {
1677 final long t = SystemClock.uptimeMillis();
1678 Runnable r;
1679
1680 // Don't use these two variables in any of the callback runnables.
1681 // Otherwise we hold a reference to them.
1682 final Callbacks oldCallbacks = mCallbacks.get();
1683 if (oldCallbacks == null) {
1684 // This launcher has exited and nobody bothered to tell us. Just bail.
1685 Log.w(TAG, "LoaderTask running with no launcher");
1686 return;
1687 }
1688
Winson Chung4a2afa32012-07-19 14:53:05 -07001689 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
1690 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001691 oldCallbacks.getCurrentWorkspaceScreen();
1692
1693 // Load all the items that are on the current page first (and in the process, unbind
1694 // all the existing workspace items before we call startBinding() below.
1695 unbindWorkspaceItemsOnMainThread();
1696 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1697 ArrayList<LauncherAppWidgetInfo> appWidgets =
1698 new ArrayList<LauncherAppWidgetInfo>();
1699 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
1700 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -07001701 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 workspaceItems.addAll(sBgWorkspaceItems);
1703 appWidgets.addAll(sBgAppWidgets);
1704 folders.putAll(sBgFolders);
1705 itemsIdMap.putAll(sBgItemsIdMap);
1706 }
1707
1708 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
1709 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
1710 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
1711 new ArrayList<LauncherAppWidgetInfo>();
1712 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
1713 new ArrayList<LauncherAppWidgetInfo>();
1714 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
1715 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
1716
1717 // Separate the items that are on the current screen, and all the other remaining items
1718 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
1719 otherWorkspaceItems);
1720 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
1721 otherAppWidgets);
1722 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
1723 otherFolders);
1724 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1725 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1726
1727 // Tell the workspace that we're about to start binding items
1728 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001729 public void run() {
1730 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1731 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001733 }
1734 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001735 };
Winson Chung81b52252012-08-27 15:34:29 -07001736 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737
1738 // Load items on the current page
1739 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
1740 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07001741 if (isLoadingSynchronously) {
1742 r = new Runnable() {
1743 public void run() {
1744 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1745 if (callbacks != null) {
1746 callbacks.onPageBoundSynchronously(currentScreen);
1747 }
1748 }
1749 };
Winson Chung81b52252012-08-27 15:34:29 -07001750 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07001751 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752
Winson Chung4a2afa32012-07-19 14:53:05 -07001753 // Load all the remaining pages (if we are loading synchronously, we want to defer this
1754 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 mDeferredBindRunnables.clear();
1756 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07001757 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001758
1759 // Tell the workspace that we're done binding items
1760 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001761 public void run() {
1762 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1763 if (callbacks != null) {
1764 callbacks.finishBindingItems();
1765 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766
Winson Chung98e030b2012-05-07 16:01:11 -07001767 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001768 if (DEBUG_LOADERS) {
1769 Log.d(TAG, "bound workspace in "
1770 + (SystemClock.uptimeMillis()-t) + "ms");
1771 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001772
1773 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001774 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 };
Winson Chung4a2afa32012-07-19 14:53:05 -07001776 if (isLoadingSynchronously) {
1777 mDeferredBindRunnables.add(r);
1778 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001779 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07001780 }
Joe Onorato36115782010-06-17 13:28:48 -04001781 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001782
Joe Onorato36115782010-06-17 13:28:48 -04001783 private void loadAndBindAllApps() {
1784 if (DEBUG_LOADERS) {
1785 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1786 }
1787 if (!mAllAppsLoaded) {
1788 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001789 synchronized (LoaderTask.this) {
1790 if (mStopped) {
1791 return;
1792 }
1793 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001794 }
Joe Onorato36115782010-06-17 13:28:48 -04001795 } else {
1796 onlyBindAllApps();
1797 }
1798 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001799
Joe Onorato36115782010-06-17 13:28:48 -04001800 private void onlyBindAllApps() {
1801 final Callbacks oldCallbacks = mCallbacks.get();
1802 if (oldCallbacks == null) {
1803 // This launcher has exited and nobody bothered to tell us. Just bail.
1804 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1805 return;
1806 }
1807
1808 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001809 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001810 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07001811 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001812 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001813 public void run() {
1814 final long t = SystemClock.uptimeMillis();
1815 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1816 if (callbacks != null) {
1817 callbacks.bindAllApplications(list);
1818 }
1819 if (DEBUG_LOADERS) {
1820 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1821 + (SystemClock.uptimeMillis()-t) + "ms");
1822 }
1823 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001824 };
1825 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
1826 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) {
1827 r.run();
1828 } else {
1829 mHandler.post(r);
1830 }
Joe Onorato36115782010-06-17 13:28:48 -04001831 }
1832
1833 private void loadAllAppsByBatch() {
1834 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1835
1836 // Don't use these two variables in any of the callback runnables.
1837 // Otherwise we hold a reference to them.
1838 final Callbacks oldCallbacks = mCallbacks.get();
1839 if (oldCallbacks == null) {
1840 // This launcher has exited and nobody bothered to tell us. Just bail.
1841 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1842 return;
1843 }
1844
1845 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1846 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1847
1848 final PackageManager packageManager = mContext.getPackageManager();
1849 List<ResolveInfo> apps = null;
1850
1851 int N = Integer.MAX_VALUE;
1852
1853 int startIndex;
1854 int i=0;
1855 int batchSize = -1;
1856 while (i < N && !mStopped) {
1857 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001858 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001859 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1860 apps = packageManager.queryIntentActivities(mainIntent, 0);
1861 if (DEBUG_LOADERS) {
1862 Log.d(TAG, "queryIntentActivities took "
1863 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1864 }
1865 if (apps == null) {
1866 return;
1867 }
1868 N = apps.size();
1869 if (DEBUG_LOADERS) {
1870 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1871 }
1872 if (N == 0) {
1873 // There are no apps?!?
1874 return;
1875 }
1876 if (mBatchSize == 0) {
1877 batchSize = N;
1878 } else {
1879 batchSize = mBatchSize;
1880 }
1881
1882 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1883 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001884 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001885 if (DEBUG_LOADERS) {
1886 Log.d(TAG, "sort took "
1887 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1888 }
1889 }
1890
1891 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1892
1893 startIndex = i;
1894 for (int j=0; i<N && j<batchSize; j++) {
1895 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001896 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001897 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001898 i++;
1899 }
1900
1901 final boolean first = i <= batchSize;
1902 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001903 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
1904 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001905
Joe Onoratocc67f472010-06-08 10:54:30 -07001906 mHandler.post(new Runnable() {
1907 public void run() {
1908 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001909 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001910 if (first) {
1911 callbacks.bindAllApplications(added);
1912 } else {
1913 callbacks.bindAppsAdded(added);
1914 }
1915 if (DEBUG_LOADERS) {
1916 Log.d(TAG, "bound " + added.size() + " apps in "
1917 + (SystemClock.uptimeMillis() - t) + "ms");
1918 }
1919 } else {
1920 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001921 }
1922 }
1923 });
1924
Daniel Sandlerdca66122010-04-13 16:23:58 -04001925 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001926 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1927 + (SystemClock.uptimeMillis()-t2) + "ms");
1928 }
1929
1930 if (mAllAppsLoadDelay > 0 && i < N) {
1931 try {
1932 if (DEBUG_LOADERS) {
1933 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1934 }
1935 Thread.sleep(mAllAppsLoadDelay);
1936 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001937 }
1938 }
1939
Joe Onorato36115782010-06-17 13:28:48 -04001940 if (DEBUG_LOADERS) {
1941 Log.d(TAG, "cached all " + N + " apps in "
1942 + (SystemClock.uptimeMillis()-t) + "ms"
1943 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001944 }
1945 }
1946
1947 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07001948 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001949 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1950 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1951 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1952 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1953 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
1954 }
Joe Onorato36115782010-06-17 13:28:48 -04001955 }
1956 }
1957
1958 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001959 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001960 }
1961
1962 private class PackageUpdatedTask implements Runnable {
1963 int mOp;
1964 String[] mPackages;
1965
1966 public static final int OP_NONE = 0;
1967 public static final int OP_ADD = 1;
1968 public static final int OP_UPDATE = 2;
1969 public static final int OP_REMOVE = 3; // uninstlled
1970 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1971
1972
1973 public PackageUpdatedTask(int op, String[] packages) {
1974 mOp = op;
1975 mPackages = packages;
1976 }
1977
1978 public void run() {
1979 final Context context = mApp;
1980
1981 final String[] packages = mPackages;
1982 final int N = packages.length;
1983 switch (mOp) {
1984 case OP_ADD:
1985 for (int i=0; i<N; i++) {
1986 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001987 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001988 }
1989 break;
1990 case OP_UPDATE:
1991 for (int i=0; i<N; i++) {
1992 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001993 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01001994 LauncherApplication app =
1995 (LauncherApplication) context.getApplicationContext();
1996 WidgetPreviewLoader.removeFromDb(
1997 app.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001998 }
1999 break;
2000 case OP_REMOVE:
2001 case OP_UNAVAILABLE:
2002 for (int i=0; i<N; i++) {
2003 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002004 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002005 LauncherApplication app =
2006 (LauncherApplication) context.getApplicationContext();
2007 WidgetPreviewLoader.removeFromDb(
2008 app.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002009 }
2010 break;
2011 }
2012
2013 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002014 ArrayList<ApplicationInfo> modified = null;
Winson Chung83892cc2013-05-01 16:53:33 -07002015 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002016
Adam Cohen487f7dd2012-06-28 18:12:10 -07002017 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002018 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2019 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002020 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002021 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002022 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2023 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002024 }
Winson Chung5d55f332012-07-16 20:45:03 -07002025 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002026 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002027 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002028 }
2029
Joe Onorato36115782010-06-17 13:28:48 -04002030 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2031 if (callbacks == null) {
2032 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2033 return;
2034 }
2035
2036 if (added != null) {
2037 final ArrayList<ApplicationInfo> addedFinal = added;
2038 mHandler.post(new Runnable() {
2039 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002040 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2041 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002042 callbacks.bindAppsAdded(addedFinal);
2043 }
2044 }
2045 });
2046 }
2047 if (modified != null) {
2048 final ArrayList<ApplicationInfo> modifiedFinal = modified;
2049 mHandler.post(new Runnable() {
2050 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002051 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2052 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002053 callbacks.bindAppsUpdated(modifiedFinal);
2054 }
2055 }
2056 });
2057 }
Winson Chung83892cc2013-05-01 16:53:33 -07002058 // If a package has been removed, or an app has been removed as a result of
2059 // an update (for example), make the removed callback.
2060 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
2061 final boolean permanent = (mOp == OP_REMOVE);
2062 final ArrayList<String> removedPackageNames =
2063 new ArrayList<String>(Arrays.asList(packages));
2064
Joe Onorato36115782010-06-17 13:28:48 -04002065 mHandler.post(new Runnable() {
2066 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002067 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2068 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002069 callbacks.bindComponentsRemoved(removedPackageNames,
2070 removedApps, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04002071 }
2072 }
2073 });
Joe Onoratobe386092009-11-17 17:32:16 -08002074 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002075
2076 mHandler.post(new Runnable() {
2077 @Override
2078 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002079 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2080 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002081 callbacks.bindPackagesUpdated();
2082 }
2083 }
2084 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002085 }
2086 }
2087
2088 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002089 * This is called from the code that adds shortcuts from the intent receiver. This
2090 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002091 */
Joe Onorato56d82912010-03-07 14:32:10 -05002092 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002093 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002094 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095
Joe Onorato56d82912010-03-07 14:32:10 -05002096 /**
2097 * Make an ShortcutInfo object for a shortcut that is an application.
2098 *
2099 * If c is not null, then it will be used to fill in missing data like the title and icon.
2100 */
2101 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002102 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002103 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002104 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002105
2106 ComponentName componentName = intent.getComponent();
2107 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002108 return null;
2109 }
2110
Adam Cohen00fcb492011-11-02 21:53:47 -07002111 try {
2112 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2113 if (!pi.applicationInfo.enabled) {
2114 // If we return null here, the corresponding item will be removed from the launcher
2115 // db and will not appear in the workspace.
2116 return null;
2117 }
2118 } catch (NameNotFoundException e) {
2119 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
2120 }
2121
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002122 // TODO: See if the PackageManager knows about this case. If it doesn't
2123 // then return null & delete this.
2124
Joe Onorato56d82912010-03-07 14:32:10 -05002125 // the resource -- This may implicitly give us back the fallback icon,
2126 // but don't worry about that. All we're doing with usingFallbackIcon is
2127 // to avoid saving lots of copies of that in the database, and most apps
2128 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002129
2130 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2131 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2132 // via resolveActivity().
2133 ResolveInfo resolveInfo = null;
2134 ComponentName oldComponent = intent.getComponent();
2135 Intent newIntent = new Intent(intent.getAction(), null);
2136 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2137 newIntent.setPackage(oldComponent.getPackageName());
2138 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2139 for (ResolveInfo i : infos) {
2140 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2141 i.activityInfo.name);
2142 if (cn.equals(oldComponent)) {
2143 resolveInfo = i;
2144 }
2145 }
2146 if (resolveInfo == null) {
2147 resolveInfo = manager.resolveActivity(intent, 0);
2148 }
Joe Onorato56d82912010-03-07 14:32:10 -05002149 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002150 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002151 }
Joe Onorato56d82912010-03-07 14:32:10 -05002152 // the db
2153 if (icon == null) {
2154 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002155 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002156 }
2157 }
2158 // the fallback icon
2159 if (icon == null) {
2160 icon = getFallbackIcon();
2161 info.usingFallbackIcon = true;
2162 }
2163 info.setIcon(icon);
2164
2165 // from the resource
2166 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002167 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2168 if (labelCache != null && labelCache.containsKey(key)) {
2169 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002170 } else {
2171 info.title = resolveInfo.activityInfo.loadLabel(manager);
2172 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002173 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002174 }
2175 }
Joe Onorato56d82912010-03-07 14:32:10 -05002176 }
2177 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002178 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002179 if (c != null) {
2180 info.title = c.getString(titleIndex);
2181 }
2182 }
2183 // fall back to the class name of the activity
2184 if (info.title == null) {
2185 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002186 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002187 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2188 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 /**
Winson Chung8a435102012-08-30 17:16:53 -07002192 * Returns the set of workspace ShortcutInfos with the specified intent.
2193 */
2194 static ArrayList<ItemInfo> getWorkspaceShortcutItemInfosWithIntent(Intent intent) {
2195 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
2196 synchronized (sBgLock) {
2197 for (ItemInfo info : sBgWorkspaceItems) {
2198 if (info instanceof ShortcutInfo) {
2199 ShortcutInfo shortcut = (ShortcutInfo) info;
2200 if (shortcut.intent.toUri(0).equals(intent.toUri(0))) {
2201 items.add(shortcut);
2202 }
2203 }
2204 }
2205 }
2206 return items;
2207 }
2208
2209 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002210 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002212 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002213 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2214 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215
Joe Onorato56d82912010-03-07 14:32:10 -05002216 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002217 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002220 // TODO: If there's an explicit component and we can't install that, delete it.
2221
Joe Onorato56d82912010-03-07 14:32:10 -05002222 info.title = c.getString(titleIndex);
2223
Joe Onorato9c1289c2009-08-17 11:03:03 -04002224 int iconType = c.getInt(iconTypeIndex);
2225 switch (iconType) {
2226 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2227 String packageName = c.getString(iconPackageIndex);
2228 String resourceName = c.getString(iconResourceIndex);
2229 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002230 info.customIcon = false;
2231 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002233 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002234 if (resources != null) {
2235 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002236 icon = Utilities.createIconBitmap(
2237 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002238 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002239 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002240 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 }
Joe Onorato56d82912010-03-07 14:32:10 -05002242 // the db
2243 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002244 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002245 }
2246 // the fallback icon
2247 if (icon == null) {
2248 icon = getFallbackIcon();
2249 info.usingFallbackIcon = true;
2250 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002251 break;
2252 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002253 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002254 if (icon == null) {
2255 icon = getFallbackIcon();
2256 info.customIcon = false;
2257 info.usingFallbackIcon = true;
2258 } else {
2259 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002261 break;
2262 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002263 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002264 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002265 info.customIcon = false;
2266 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002268 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002269 return info;
2270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271
Michael Jurka931dc972011-08-05 15:08:15 -07002272 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002273 @SuppressWarnings("all") // suppress dead code warning
2274 final boolean debug = false;
2275 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002276 Log.d(TAG, "getIconFromCursor app="
2277 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2278 }
2279 byte[] data = c.getBlob(iconIndex);
2280 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002281 return Utilities.createIconBitmap(
2282 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002283 } catch (Exception e) {
2284 return null;
2285 }
2286 }
2287
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2289 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002290 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002291 if (info == null) {
2292 return null;
2293 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002294 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002295
2296 return info;
2297 }
2298
Winson Chunga9abd0e2010-10-27 17:18:37 -07002299 /**
Winson Chung55cef262010-10-28 14:14:18 -07002300 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2301 */
2302 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2303 ComponentName component) {
2304 List<AppWidgetProviderInfo> widgets =
2305 AppWidgetManager.getInstance(context).getInstalledProviders();
2306 for (AppWidgetProviderInfo info : widgets) {
2307 if (info.provider.equals(component)) {
2308 return info;
2309 }
2310 }
2311 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002312 }
2313
Winson Chung68846fd2010-10-29 11:00:27 -07002314 /**
2315 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2316 */
2317 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2318 final PackageManager packageManager = context.getPackageManager();
2319 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2320 new ArrayList<WidgetMimeTypeHandlerData>();
2321
2322 final Intent supportsIntent =
2323 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2324 supportsIntent.setType(mimeType);
2325
2326 // Create a set of widget configuration components that we can test against
2327 final List<AppWidgetProviderInfo> widgets =
2328 AppWidgetManager.getInstance(context).getInstalledProviders();
2329 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2330 new HashMap<ComponentName, AppWidgetProviderInfo>();
2331 for (AppWidgetProviderInfo info : widgets) {
2332 configurationComponentToWidget.put(info.configure, info);
2333 }
2334
2335 // Run through each of the intents that can handle this type of clip data, and cross
2336 // reference them with the components that are actual configuration components
2337 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2338 PackageManager.MATCH_DEFAULT_ONLY);
2339 for (ResolveInfo info : activities) {
2340 final ActivityInfo activityInfo = info.activityInfo;
2341 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2342 activityInfo.name);
2343 if (configurationComponentToWidget.containsKey(infoComponent)) {
2344 supportedConfigurationActivities.add(
2345 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2346 configurationComponentToWidget.get(infoComponent)));
2347 }
2348 }
2349 return supportedConfigurationActivities;
2350 }
2351
Winson Chunga9abd0e2010-10-27 17:18:37 -07002352 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002353 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2354 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2355 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2356
Adam Cohend9198822011-11-22 16:42:47 -08002357 if (intent == null) {
2358 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2359 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2360 return null;
2361 }
2362
Joe Onorato0589f0f2010-02-08 13:44:00 -08002363 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002364 boolean customIcon = false;
2365 ShortcutIconResource iconResource = null;
2366
2367 if (bitmap != null && bitmap instanceof Bitmap) {
2368 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002369 customIcon = true;
2370 } else {
2371 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2372 if (extra != null && extra instanceof ShortcutIconResource) {
2373 try {
2374 iconResource = (ShortcutIconResource) extra;
2375 final PackageManager packageManager = context.getPackageManager();
2376 Resources resources = packageManager.getResourcesForApplication(
2377 iconResource.packageName);
2378 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002379 icon = Utilities.createIconBitmap(
2380 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002381 } catch (Exception e) {
2382 Log.w(TAG, "Could not load shortcut icon: " + extra);
2383 }
2384 }
2385 }
2386
Michael Jurkac9d95c52011-08-29 14:03:34 -07002387 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002388
2389 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002390 if (fallbackIcon != null) {
2391 icon = fallbackIcon;
2392 } else {
2393 icon = getFallbackIcon();
2394 info.usingFallbackIcon = true;
2395 }
Joe Onorato56d82912010-03-07 14:32:10 -05002396 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002397 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002398
Joe Onorato0589f0f2010-02-08 13:44:00 -08002399 info.title = name;
2400 info.intent = intent;
2401 info.customIcon = customIcon;
2402 info.iconResource = iconResource;
2403
2404 return info;
2405 }
2406
Winson Chungaac01e12011-08-17 10:37:13 -07002407 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2408 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002409 // If apps can't be on SD, don't even bother.
2410 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002411 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002412 }
Joe Onorato56d82912010-03-07 14:32:10 -05002413 // If this icon doesn't have a custom icon, check to see
2414 // what's stored in the DB, and if it doesn't match what
2415 // we're going to show, store what we are going to show back
2416 // into the DB. We do this so when we're loading, if the
2417 // package manager can't find an icon (for example because
2418 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002419 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002420 cache.put(info, c.getBlob(iconIndex));
2421 return true;
2422 }
2423 return false;
2424 }
2425 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2426 boolean needSave = false;
2427 try {
2428 if (data != null) {
2429 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2430 Bitmap loaded = info.getIcon(mIconCache);
2431 needSave = !saved.sameAs(loaded);
2432 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002433 needSave = true;
2434 }
Winson Chungaac01e12011-08-17 10:37:13 -07002435 } catch (Exception e) {
2436 needSave = true;
2437 }
2438 if (needSave) {
2439 Log.d(TAG, "going to save icon bitmap for info=" + info);
2440 // This is slower than is ideal, but this only happens once
2441 // or when the app is updated with a new icon.
2442 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002443 }
2444 }
2445
Joe Onorato9c1289c2009-08-17 11:03:03 -04002446 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002447 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002448 * or make a new one.
2449 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002450 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451 // See if a placeholder was created for us already
2452 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002453 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002454 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002455 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002456 folders.put(id, folderInfo);
2457 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002458 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 }
2460
Winson Chung11904872012-09-17 16:58:46 -07002461 public static final Comparator<ApplicationInfo> getAppNameComparator() {
2462 final Collator collator = Collator.getInstance();
2463 return new Comparator<ApplicationInfo>() {
2464 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2465 int result = collator.compare(a.title.toString(), b.title.toString());
2466 if (result == 0) {
2467 result = a.componentName.compareTo(b.componentName);
2468 }
2469 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07002470 }
Winson Chung11904872012-09-17 16:58:46 -07002471 };
2472 }
Winson Chung78403fe2011-01-21 15:38:02 -08002473 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2474 = new Comparator<ApplicationInfo>() {
2475 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2476 if (a.firstInstallTime < b.firstInstallTime) return 1;
2477 if (a.firstInstallTime > b.firstInstallTime) return -1;
2478 return 0;
2479 }
2480 };
Winson Chung11904872012-09-17 16:58:46 -07002481 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
2482 final Collator collator = Collator.getInstance();
2483 return new Comparator<AppWidgetProviderInfo>() {
2484 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2485 return collator.compare(a.label.toString(), b.label.toString());
2486 }
2487 };
2488 }
Winson Chung5308f242011-08-18 12:12:41 -07002489 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2490 if (info.activityInfo != null) {
2491 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2492 } else {
2493 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2494 }
2495 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002496 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07002497 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07002498 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002499 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002500 ShortcutNameComparator(PackageManager pm) {
2501 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002502 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07002503 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07002504 }
2505 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2506 mPackageManager = pm;
2507 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07002508 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07002509 }
2510 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002511 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002512 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2513 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2514 if (mLabelCache.containsKey(keyA)) {
2515 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002516 } else {
2517 labelA = a.loadLabel(mPackageManager).toString();
2518
Winson Chung5308f242011-08-18 12:12:41 -07002519 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002520 }
Winson Chung5308f242011-08-18 12:12:41 -07002521 if (mLabelCache.containsKey(keyB)) {
2522 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002523 } else {
2524 labelB = b.loadLabel(mPackageManager).toString();
2525
Winson Chung5308f242011-08-18 12:12:41 -07002526 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002527 }
Winson Chung11904872012-09-17 16:58:46 -07002528 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07002529 }
2530 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002531 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07002532 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07002533 private PackageManager mPackageManager;
2534 private HashMap<Object, String> mLabelCache;
2535 WidgetAndShortcutNameComparator(PackageManager pm) {
2536 mPackageManager = pm;
2537 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07002538 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07002539 }
2540 public final int compare(Object a, Object b) {
2541 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002542 if (mLabelCache.containsKey(a)) {
2543 labelA = mLabelCache.get(a);
2544 } else {
2545 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002546 ((AppWidgetProviderInfo) a).label :
2547 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002548 mLabelCache.put(a, labelA);
2549 }
2550 if (mLabelCache.containsKey(b)) {
2551 labelB = mLabelCache.get(b);
2552 } else {
2553 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002554 ((AppWidgetProviderInfo) b).label :
2555 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002556 mLabelCache.put(b, labelB);
2557 }
Winson Chung11904872012-09-17 16:58:46 -07002558 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07002559 }
2560 };
Joe Onoratobe386092009-11-17 17:32:16 -08002561
2562 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002563 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002564 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2565 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2566 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2567 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002568 if (mLoaderTask != null) {
2569 mLoaderTask.dumpState();
2570 } else {
2571 Log.d(TAG, "mLoaderTask=null");
2572 }
Joe Onoratobe386092009-11-17 17:32:16 -08002573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574}