blob: 99ebd962e7a34ebc9b4406003c93b37b4a3ac988 [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;
Michael Jurkac7700af2013-05-14 20:17:58 +020085 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Winson Chung81b52252012-08-27 15:34:29 -070087 // Specific runnable types that are run on the main thread deferred handler, this allows us to
88 // clear all queued binding runnables when the Launcher activity is destroyed.
89 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
90 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
91
92
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070093 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
94 static {
95 sWorkerThread.start();
96 }
97 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
98
Joe Onoratocc67f472010-06-08 10:54:30 -070099 // We start off with everything not loaded. After that, we assume that
100 // our monitoring of the package manager provides all updates and we never
101 // need to do a requery. These are only ever touched from the loader thread.
102 private boolean mWorkspaceLoaded;
103 private boolean mAllAppsLoaded;
104
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700105 // When we are loading pages synchronously, we can't just post the binding of items on the side
106 // pages as this delays the rotation process. Instead, we wait for a callback from the first
107 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
108 // a normal load, we also clear this set of Runnables.
109 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700113 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700114 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800115
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700116 // The lock that must be acquired before referencing any static bg data structures. Unlike
117 // other locks, this one can generally be held long-term because we never expect any of these
118 // static data structures to be referenced outside of the worker thread except on the first
119 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700120 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700121
Adam Cohen487f7dd2012-06-28 18:12:10 -0700122 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700125
Adam Cohen487f7dd2012-06-28 18:12:10 -0700126 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
127 // created by LauncherModel that are directly on the home screen (however, no widgets or
128 // shortcuts within folders).
129 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130
Adam Cohen487f7dd2012-06-28 18:12:10 -0700131 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
132 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133 new ArrayList<LauncherAppWidgetInfo>();
134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
136 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700137
Adam Cohen487f7dd2012-06-28 18:12:10 -0700138 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
139 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // </ only access in worker thread >
141
142 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Adam Cohend22015c2010-07-26 22:02:18 -0700145 private static int mCellCountX;
146 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700147
Reena Lee99a73f32011-10-24 17:27:37 -0700148 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700149
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700151 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 public int getCurrentWorkspaceScreen();
153 public void startBinding();
154 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500155 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 public void finishBindingItems();
157 public void bindAppWidget(LauncherAppWidgetInfo info);
158 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500159 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
160 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700161 public void bindComponentsRemoved(ArrayList<String> packageNames,
162 ArrayList<ApplicationInfo> appInfos,
163 boolean matchPackageNamesOnly);
Winson Chung80baf5a2010-08-09 16:03:15 -0700164 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400165 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800166 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100167 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700168 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Joe Onorato0589f0f2010-02-08 13:44:00 -0800171 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800172 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400173 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700174 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800175 mIconCache = iconCache;
176
177 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700178 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400179
Reena Lee93f824a2011-09-23 17:20:28 -0700180 final Resources res = app.getResources();
181 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
182 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700183 Configuration config = res.getConfiguration();
184 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800185 }
186
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700187 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
188 * posted on the main thread handler. */
189 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700190 runOnMainThread(r, 0);
191 }
192 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700193 if (sWorkerThread.getThreadId() == Process.myTid()) {
194 // If we are on the worker thread, post onto the main handler
195 mHandler.post(r);
196 } else {
197 r.run();
198 }
199 }
200
201 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
202 * posted on the worker thread handler. */
203 private static void runOnWorkerThread(Runnable r) {
204 if (sWorkerThread.getThreadId() == Process.myTid()) {
205 r.run();
206 } else {
207 // If we are not on the worker thread, then post to the worker handler
208 sWorker.post(r);
209 }
210 }
211
Joe Onorato56d82912010-03-07 14:32:10 -0500212 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chung81b52252012-08-27 15:34:29 -0700216 public void unbindItemInfosAndClearQueuedBindRunnables() {
217 if (sWorkerThread.getThreadId() == Process.myTid()) {
218 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
219 "main thread");
220 }
221
222 // Clear any deferred bind runnables
223 mDeferredBindRunnables.clear();
224 // Remove any queued bind runnables
225 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
226 // Unbind all the workspace items
227 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700228 }
229
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700230 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700231 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700232 // Ensure that we don't use the same workspace items data structure on the main thread
233 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700234 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
235 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700236 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700237 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
238 tmpAppWidgets.addAll(sBgAppWidgets);
239 }
240 Runnable r = new Runnable() {
241 @Override
242 public void run() {
243 for (ItemInfo item : tmpWorkspaceItems) {
244 item.unbind();
245 }
246 for (ItemInfo item : tmpAppWidgets) {
247 item.unbind();
248 }
249 }
250 };
251 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700252 }
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 /**
255 * Adds an item to the DB if it was not created previously, or move it to a new
256 * <container, screen, cellX, cellY>
257 */
258 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
259 int screen, int cellX, int cellY) {
260 if (item.container == ItemInfo.NO_ID) {
261 // From all apps
262 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
263 } else {
264 // From somewhere else
265 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
267 }
268
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700269 static void checkItemInfoLocked(
270 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
271 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
272 if (modelItem != null && item != modelItem) {
273 // check all the data is consistent
274 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
275 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
276 ShortcutInfo shortcut = (ShortcutInfo) item;
277 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
278 modelShortcut.intent.filterEquals(shortcut.intent) &&
279 modelShortcut.id == shortcut.id &&
280 modelShortcut.itemType == shortcut.itemType &&
281 modelShortcut.container == shortcut.container &&
282 modelShortcut.screen == shortcut.screen &&
283 modelShortcut.cellX == shortcut.cellX &&
284 modelShortcut.cellY == shortcut.cellY &&
285 modelShortcut.spanX == shortcut.spanX &&
286 modelShortcut.spanY == shortcut.spanY &&
287 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
288 (modelShortcut.dropPos != null &&
289 shortcut.dropPos != null &&
290 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
291 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
292 // For all intents and purposes, this is the same object
293 return;
294 }
295 }
296
297 // the modelItem needs to match up perfectly with item if our model is
298 // to be consistent with the database-- for now, just require
299 // modelItem == item or the equality check above
300 String msg = "item: " + ((item != null) ? item.toString() : "null") +
301 "modelItem: " +
302 ((modelItem != null) ? modelItem.toString() : "null") +
303 "Error: ItemInfo passed to checkItemInfo doesn't match original";
304 RuntimeException e = new RuntimeException(msg);
305 if (stackTrace != null) {
306 e.setStackTrace(stackTrace);
307 }
308 throw e;
309 }
310 }
311
Michael Jurka816474f2012-06-25 14:49:02 -0700312 static void checkItemInfo(final ItemInfo item) {
313 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
314 final long itemId = item.id;
315 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700316 public void run() {
317 synchronized (sBgLock) {
318 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700319 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700320 }
321 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700322 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700323 }
324
Michael Jurkac9d95c52011-08-29 14:03:34 -0700325 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
326 final ItemInfo item, final String callingFunction) {
327 final long itemId = item.id;
328 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
329 final ContentResolver cr = context.getContentResolver();
330
Adam Cohen487f7dd2012-06-28 18:12:10 -0700331 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700332 Runnable r = new Runnable() {
333 public void run() {
334 cr.update(uri, values, null, null);
335
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700336 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700337 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700338 checkItemInfoLocked(itemId, item, stackTrace);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700339
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700340 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
341 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
342 // Item is in a folder, make sure this folder exists
343 if (!sBgFolders.containsKey(item.container)) {
344 // An items container is being set to a that of an item which is not in
345 // the list of Folders.
346 String msg = "item: " + item + " container being set to: " +
347 item.container + ", not in the list of folders";
Adam Cohen28b3e102012-10-04 17:21:33 -0700348 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700349 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700350 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700351 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700352
353 // Items are added/removed from the corresponding FolderInfo elsewhere, such
354 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
355 // that are on the desktop, as appropriate
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700356 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700357 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
358 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
359 switch (modelItem.itemType) {
360 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
361 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
362 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
363 if (!sBgWorkspaceItems.contains(modelItem)) {
364 sBgWorkspaceItems.add(modelItem);
365 }
366 break;
367 default:
368 break;
369 }
370 } else {
371 sBgWorkspaceItems.remove(modelItem);
372 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700373 }
374 }
375 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700376 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700377 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700378
Michael Jurkac7700af2013-05-14 20:17:58 +0200379 public void flushWorkerThread() {
380 mFlushingWorkerThread = true;
381 Runnable waiter = new Runnable() {
382 public void run() {
383 synchronized (this) {
384 notifyAll();
385 mFlushingWorkerThread = false;
386 }
387 }
388 };
389
390 synchronized(waiter) {
391 runOnWorkerThread(waiter);
392 if (mLoaderTask != null) {
393 synchronized(mLoaderTask) {
394 mLoaderTask.notify();
395 }
396 }
397 boolean success = false;
398 while (!success) {
399 try {
400 waiter.wait();
401 success = true;
402 } catch (InterruptedException e) {
403 }
404 }
405 }
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700410 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700411 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
412 final int screen, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700413 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
414 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700415 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
416 Launcher.sDumpLogs.add(transaction);
417 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400418 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 item.cellX = cellX;
420 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700421
Winson Chung3d503fb2011-07-13 17:25:49 -0700422 // We store hotseat items in canonical form which is this orientation invariant position
423 // in the hotseat
424 if (context instanceof Launcher && screen < 0 &&
425 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
426 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
427 } else {
428 item.screen = screen;
429 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430
431 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700433 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
434 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400435 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
436
Michael Jurkac9d95c52011-08-29 14:03:34 -0700437 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700438 }
439
440 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700441 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800442 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700443 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
444 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700445 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
446 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700447 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
448 Launcher.sDumpLogs.add(transaction);
449 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800450 item.cellX = cellX;
451 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700452 item.spanX = spanX;
453 item.spanY = spanY;
454
455 // We store hotseat items in canonical form which is this orientation invariant position
456 // in the hotseat
457 if (context instanceof Launcher && screen < 0 &&
458 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
459 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
460 } else {
461 item.screen = screen;
462 }
Adam Cohend4844c32011-02-18 19:25:06 -0800463
Adam Cohend4844c32011-02-18 19:25:06 -0800464 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800465 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700466 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
467 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
468 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
469 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
470 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800471
Michael Jurka816474f2012-06-25 14:49:02 -0700472 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700473 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700474
475 /**
476 * Update an item to the database in a specified container.
477 */
478 static void updateItemInDatabase(Context context, final ItemInfo item) {
479 final ContentValues values = new ContentValues();
480 item.onAddToDatabase(values);
481 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
482 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800483 }
484
485 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400486 * Returns true if the shortcuts already exists in the database.
487 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400489 static boolean shortcutExists(Context context, String title, Intent intent) {
490 final ContentResolver cr = context.getContentResolver();
491 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
492 new String[] { "title", "intent" }, "title=? and intent=?",
493 new String[] { title, intent.toUri(0) }, null);
494 boolean result = false;
495 try {
496 result = c.moveToFirst();
497 } finally {
498 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400500 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700501 }
502
Joe Onorato9c1289c2009-08-17 11:03:03 -0400503 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700504 * Returns an ItemInfo array containing all the items in the LauncherModel.
505 * The ItemInfo.id is not set through this function.
506 */
507 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
508 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
509 final ContentResolver cr = context.getContentResolver();
510 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
511 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
512 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
513 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
514
515 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
516 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
517 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
518 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
519 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
520 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
521 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
522
523 try {
524 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700525 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700526 item.cellX = c.getInt(cellXIndex);
527 item.cellY = c.getInt(cellYIndex);
528 item.spanX = c.getInt(spanXIndex);
529 item.spanY = c.getInt(spanYIndex);
530 item.container = c.getInt(containerIndex);
531 item.itemType = c.getInt(itemTypeIndex);
532 item.screen = c.getInt(screenIndex);
533
534 items.add(item);
535 }
536 } catch (Exception e) {
537 items.clear();
538 } finally {
539 c.close();
540 }
541
542 return items;
543 }
544
545 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
547 */
548 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
549 final ContentResolver cr = context.getContentResolver();
550 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
551 "_id=? and (itemType=? or itemType=?)",
552 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700553 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700554
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555 try {
556 if (c.moveToFirst()) {
557 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
558 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
559 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
560 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
561 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
562 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 FolderInfo folderInfo = null;
565 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700566 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
567 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700569 }
570
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 folderInfo.title = c.getString(titleIndex);
572 folderInfo.id = id;
573 folderInfo.container = c.getInt(containerIndex);
574 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700575 folderInfo.cellX = c.getInt(cellXIndex);
576 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577
578 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400580 } finally {
581 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700582 }
583
584 return null;
585 }
586
Joe Onorato9c1289c2009-08-17 11:03:03 -0400587 /**
588 * Add an item to the database in a specified container. Sets the container, screen, cellX and
589 * cellY fields of the item. Also assigns an ID to the item.
590 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700591 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
592 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400593 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400594 item.cellX = cellX;
595 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700596 // We store hotseat items in canonical form which is this orientation invariant position
597 // in the hotseat
598 if (context instanceof Launcher && screen < 0 &&
599 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
600 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
601 } else {
602 item.screen = screen;
603 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400604
605 final ContentValues values = new ContentValues();
606 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607 item.onAddToDatabase(values);
608
Michael Jurka7578ec62011-08-03 14:11:54 -0700609 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700610 item.id = app.getLauncherProvider().generateNewId();
611 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700612 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700613
Michael Jurkac9d95c52011-08-29 14:03:34 -0700614 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700615 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700616 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
617 + item.id + " (" + container + ", " + screen + ", " + cellX + ", "
618 + cellY + ")";
619 Launcher.sDumpLogs.add(transaction);
620 Log.d(TAG, transaction);
621
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700622 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
623 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700625 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700626 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700627 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700628 sBgItemsIdMap.put(item.id, item);
629 switch (item.itemType) {
630 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
631 sBgFolders.put(item.id, (FolderInfo) item);
632 // Fall through
633 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
634 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
635 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
636 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
637 sBgWorkspaceItems.add(item);
638 } else {
639 if (!sBgFolders.containsKey(item.container)) {
640 // Adding an item to a folder that doesn't exist.
641 String msg = "adding item: " + item + " to a folder that " +
642 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700643 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700644 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700645 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700646 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700647 break;
648 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
649 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
650 break;
651 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700652 }
653 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700654 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700655 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700656 }
657
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700659 * Creates a new unique child id, for a given cell span across all layouts.
660 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700661 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700662 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
663 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700664 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700665 }
666
Adam Cohend22015c2010-07-26 22:02:18 -0700667 static int getCellCountX() {
668 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700669 }
670
Adam Cohend22015c2010-07-26 22:02:18 -0700671 static int getCellCountY() {
672 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700673 }
674
675 /**
676 * Updates the model orientation helper to take into account the current layout dimensions
677 * when performing local/canonical coordinate transformations.
678 */
679 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700680 mCellCountX = shortAxisCellCount;
681 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700682 }
683
684 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700685 * Removes the specified item from the database
686 * @param context
687 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400688 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700689 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700691 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700692
Michael Jurka83df1882011-08-31 20:59:26 -0700693 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700694 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700695 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
696 + item.id + " (" + item.container + ", " + item.screen + ", " + item.cellX +
697 ", " + item.cellY + ")";
698 Launcher.sDumpLogs.add(transaction);
699 Log.d(TAG, transaction);
700
Michael Jurkac9d95c52011-08-29 14:03:34 -0700701 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700702
703 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700704 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700705 switch (item.itemType) {
706 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
707 sBgFolders.remove(item.id);
708 for (ItemInfo info: sBgItemsIdMap.values()) {
709 if (info.container == item.id) {
710 // We are deleting a folder which still contains items that
711 // think they are contained by that folder.
712 String msg = "deleting a folder (" + item + ") which still " +
713 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700714 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700715 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700716 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700717 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700718 sBgWorkspaceItems.remove(item);
719 break;
720 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
721 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
722 sBgWorkspaceItems.remove(item);
723 break;
724 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
725 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
726 break;
727 }
728 sBgItemsIdMap.remove(item.id);
729 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700730 }
731 }
Michael Jurka83df1882011-08-31 20:59:26 -0700732 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700733 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 }
735
736 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 * Remove the contents of the specified folder from the database
738 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700739 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 final ContentResolver cr = context.getContentResolver();
741
Michael Jurkac9d95c52011-08-29 14:03:34 -0700742 Runnable r = new Runnable() {
743 public void run() {
744 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700745 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700746 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700747 sBgItemsIdMap.remove(info.id);
748 sBgFolders.remove(info.id);
749 sBgDbIconCache.remove(info);
750 sBgWorkspaceItems.remove(info);
751 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700752
Michael Jurkac9d95c52011-08-29 14:03:34 -0700753 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
754 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700755 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700756 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700757 for (ItemInfo childInfo : info.contents) {
758 sBgItemsIdMap.remove(childInfo.id);
759 sBgDbIconCache.remove(childInfo);
760 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700761 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700762 }
763 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700764 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400765 }
766
767 /**
768 * Set this as the current Launcher activity object for the loader.
769 */
770 public void initialize(Callbacks callbacks) {
771 synchronized (mLock) {
772 mCallbacks = new WeakReference<Callbacks>(callbacks);
773 }
774 }
775
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700776 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400777 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
778 * ACTION_PACKAGE_CHANGED.
779 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100780 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400781 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400782 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700783
Joe Onorato36115782010-06-17 13:28:48 -0400784 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400785
Joe Onorato36115782010-06-17 13:28:48 -0400786 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
787 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
788 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
789 final String packageName = intent.getData().getSchemeSpecificPart();
790 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791
Joe Onorato36115782010-06-17 13:28:48 -0400792 int op = PackageUpdatedTask.OP_NONE;
793
794 if (packageName == null || packageName.length() == 0) {
795 // they sent us a bad intent
796 return;
797 }
798
799 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
800 op = PackageUpdatedTask.OP_UPDATE;
801 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
802 if (!replacing) {
803 op = PackageUpdatedTask.OP_REMOVE;
804 }
805 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
806 // later, we will update the package at this time
807 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
808 if (!replacing) {
809 op = PackageUpdatedTask.OP_ADD;
810 } else {
811 op = PackageUpdatedTask.OP_UPDATE;
812 }
813 }
814
815 if (op != PackageUpdatedTask.OP_NONE) {
816 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
817 }
818
819 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400820 // First, schedule to add these apps back in.
821 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
822 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
823 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700824 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400825 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
826 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
827 enqueuePackageUpdated(new PackageUpdatedTask(
828 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700829 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700830 // If we have changed locale we need to clear out the labels in all apps/workspace.
831 forceReload();
832 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
833 // Check if configuration change was an mcc/mnc change which would affect app resources
834 // and we would need to clear out the labels in all apps/workspace. Same handling as
835 // above for ACTION_LOCALE_CHANGED
836 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700837 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700838 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700839 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700840 forceReload();
841 }
842 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700843 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700844 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
845 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700846 if (mCallbacks != null) {
847 Callbacks callbacks = mCallbacks.get();
848 if (callbacks != null) {
849 callbacks.bindSearchablesChanged();
850 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700851 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700852 }
853 }
854
Reena Lee93f824a2011-09-23 17:20:28 -0700855 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700856 resetLoadedState(true, true);
857
Reena Lee93f824a2011-09-23 17:20:28 -0700858 // Do this here because if the launcher activity is running it will be restarted.
859 // If it's not running startLoaderFromBackground will merely tell it that it needs
860 // to reload.
861 startLoaderFromBackground();
862 }
863
Winson Chungf0c6ae02012-03-21 16:10:31 -0700864 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
865 synchronized (mLock) {
866 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
867 // mWorkspaceLoaded to true later
868 stopLoaderLocked();
869 if (resetAllAppsLoaded) mAllAppsLoaded = false;
870 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
871 }
872 }
873
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700874 /**
875 * When the launcher is in the background, it's possible for it to miss paired
876 * configuration changes. So whenever we trigger the loader from the background
877 * tell the launcher that it needs to re-run the loader when it comes back instead
878 * of doing it now.
879 */
880 public void startLoaderFromBackground() {
881 boolean runLoader = false;
882 if (mCallbacks != null) {
883 Callbacks callbacks = mCallbacks.get();
884 if (callbacks != null) {
885 // Only actually run the loader if they're not paused.
886 if (!callbacks.setLoadOnResume()) {
887 runLoader = true;
888 }
889 }
890 }
891 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700892 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -0700893 }
Joe Onorato36115782010-06-17 13:28:48 -0400894 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400895
Reena Lee93f824a2011-09-23 17:20:28 -0700896 // If there is already a loader task running, tell it to stop.
897 // returns true if isLaunching() was true on the old task
898 private boolean stopLoaderLocked() {
899 boolean isLaunching = false;
900 LoaderTask oldTask = mLoaderTask;
901 if (oldTask != null) {
902 if (oldTask.isLaunching()) {
903 isLaunching = true;
904 }
905 oldTask.stopLocked();
906 }
907 return isLaunching;
908 }
909
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700910 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -0400911 synchronized (mLock) {
912 if (DEBUG_LOADERS) {
913 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
914 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700916 // Clear any deferred bind-runnables from the synchronized load process
917 // We must do this before any loading/binding is scheduled below.
918 mDeferredBindRunnables.clear();
919
Joe Onorato36115782010-06-17 13:28:48 -0400920 // Don't bother to start the thread if we know it's not going to do anything
921 if (mCallbacks != null && mCallbacks.get() != null) {
922 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700923 // also, don't downgrade isLaunching if we're already running
924 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700925 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700926 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
927 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
928 } else {
929 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
930 sWorker.post(mLoaderTask);
931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 }
934 }
935
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700936 void bindRemainingSynchronousPages() {
937 // Post the remaining side pages to be loaded
938 if (!mDeferredBindRunnables.isEmpty()) {
939 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -0700940 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700941 }
942 mDeferredBindRunnables.clear();
943 }
944 }
945
Joe Onorato36115782010-06-17 13:28:48 -0400946 public void stopLoader() {
947 synchronized (mLock) {
948 if (mLoaderTask != null) {
949 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 }
951 }
Joe Onorato36115782010-06-17 13:28:48 -0400952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953
Michael Jurkac57b7a82011-08-09 22:02:20 -0700954 public boolean isAllAppsLoaded() {
955 return mAllAppsLoaded;
956 }
957
Winson Chung36a62fe2012-05-06 18:04:42 -0700958 boolean isLoadingWorkspace() {
959 synchronized (mLock) {
960 if (mLoaderTask != null) {
961 return mLoaderTask.isLoadingWorkspace();
962 }
963 }
964 return false;
965 }
966
Joe Onorato36115782010-06-17 13:28:48 -0400967 /**
968 * Runnable for the thread that loads the contents of the launcher:
969 * - workspace icons
970 * - widgets
971 * - all apps icons
972 */
973 private class LoaderTask implements Runnable {
974 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -0400975 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700976 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400977 private boolean mStopped;
978 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700979
Winson Chungc3eecff2011-07-11 17:44:15 -0700980 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400981
982 LoaderTask(Context context, boolean isLaunching) {
983 mContext = context;
984 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700985 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 }
987
Joe Onorato36115782010-06-17 13:28:48 -0400988 boolean isLaunching() {
989 return mIsLaunching;
990 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991
Winson Chung36a62fe2012-05-06 18:04:42 -0700992 boolean isLoadingWorkspace() {
993 return mIsLoadingAndBindingWorkspace;
994 }
995
Joe Onorato36115782010-06-17 13:28:48 -0400996 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700997 mIsLoadingAndBindingWorkspace = true;
998
Joe Onorato36115782010-06-17 13:28:48 -0400999 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001000 if (DEBUG_LOADERS) {
1001 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001003
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001004 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -04001005 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001006 synchronized (LoaderTask.this) {
1007 if (mStopped) {
1008 return;
1009 }
1010 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 }
1012 }
1013
Joe Onorato36115782010-06-17 13:28:48 -04001014 // Bind the workspace
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001016 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001017
Joe Onorato36115782010-06-17 13:28:48 -04001018 private void waitForIdle() {
1019 // Wait until the either we're stopped or the other threads are done.
1020 // This way we don't start loading all apps until the workspace has settled
1021 // down.
1022 synchronized (LoaderTask.this) {
1023 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001024
Joe Onorato36115782010-06-17 13:28:48 -04001025 mHandler.postIdle(new Runnable() {
1026 public void run() {
1027 synchronized (LoaderTask.this) {
1028 mLoadAndBindStepFinished = true;
1029 if (DEBUG_LOADERS) {
1030 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001031 }
Joe Onorato36115782010-06-17 13:28:48 -04001032 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001033 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001034 }
Joe Onorato36115782010-06-17 13:28:48 -04001035 });
1036
Michael Jurkac7700af2013-05-14 20:17:58 +02001037 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001038 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001039 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1040 // wait no longer than 1sec at a time
1041 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001042 } catch (InterruptedException ex) {
1043 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001044 }
1045 }
Joe Onorato36115782010-06-17 13:28:48 -04001046 if (DEBUG_LOADERS) {
1047 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001048 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001049 + "ms for previous step to finish binding");
1050 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001051 }
Joe Onorato36115782010-06-17 13:28:48 -04001052 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001053
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054 void runBindSynchronousPage(int synchronousBindPage) {
1055 if (synchronousBindPage < 0) {
1056 // Ensure that we have a valid page index to load synchronously
1057 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1058 "valid page index");
1059 }
1060 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1061 // Ensure that we don't try and bind a specified page when the pages have not been
1062 // loaded already (we should load everything asynchronously in that case)
1063 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1064 }
1065 synchronized (mLock) {
1066 if (mIsLoaderTaskRunning) {
1067 // Ensure that we are never running the background loading at this point since
1068 // we also touch the background collections
1069 throw new RuntimeException("Error! Background loading is already running");
1070 }
1071 }
1072
1073 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1074 // data structures, we can't allow any other thread to touch that data, but because
1075 // this call is synchronous, we can get away with not locking).
1076
Adam Cohena13a2f22012-07-23 14:29:15 -07001077 // The LauncherModel is static in the LauncherApplication and mHandler may have queued
1078 // operations from the previous activity. We need to ensure that all queued operations
1079 // are executed before any synchronous binding work is done.
1080 mHandler.flush();
1081
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001082 // Divide the set of loaded items into those that we are binding synchronously, and
1083 // everything else that is to be bound normally (asynchronously).
1084 bindWorkspace(synchronousBindPage);
1085 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1086 // arise from that.
1087 onlyBindAllApps();
1088 }
1089
Joe Onorato36115782010-06-17 13:28:48 -04001090 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001091 synchronized (mLock) {
1092 mIsLoaderTaskRunning = true;
1093 }
Joe Onorato36115782010-06-17 13:28:48 -04001094 // Optimize for end-user experience: if the Launcher is up and // running with the
1095 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1096 // workspace first (default).
1097 final Callbacks cbk = mCallbacks.get();
1098 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -04001099
Joe Onorato36115782010-06-17 13:28:48 -04001100 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001101 // Elevate priority when Home launches for the first time to avoid
1102 // starving at boot time. Staring at a blank home is not cool.
1103 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001104 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1105 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001106 android.os.Process.setThreadPriority(mIsLaunching
1107 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1108 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001109 if (loadWorkspaceFirst) {
1110 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1111 loadAndBindWorkspace();
1112 } else {
1113 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001114 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001115 }
1116
Joe Onorato36115782010-06-17 13:28:48 -04001117 if (mStopped) {
1118 break keep_running;
1119 }
1120
1121 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1122 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001123 synchronized (mLock) {
1124 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001125 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001126 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1127 }
1128 }
Joe Onorato36115782010-06-17 13:28:48 -04001129 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001130
1131 // second step
1132 if (loadWorkspaceFirst) {
1133 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001134 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001135 } else {
1136 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
1137 loadAndBindWorkspace();
1138 }
Winson Chung7ed37742011-09-08 15:45:51 -07001139
1140 // Restore the default thread priority after we are done loading items
1141 synchronized (mLock) {
1142 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1143 }
Joe Onorato36115782010-06-17 13:28:48 -04001144 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001145
Winson Chungaac01e12011-08-17 10:37:13 -07001146
1147 // Update the saved icons if necessary
1148 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001149 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001150 for (Object key : sBgDbIconCache.keySet()) {
1151 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1152 }
1153 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001154 }
Winson Chungaac01e12011-08-17 10:37:13 -07001155
Joe Onorato36115782010-06-17 13:28:48 -04001156 // Clear out this reference, otherwise we end up holding it until all of the
1157 // callback runnables are done.
1158 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159
Joe Onorato36115782010-06-17 13:28:48 -04001160 synchronized (mLock) {
1161 // If we are still the last one to be scheduled, remove ourselves.
1162 if (mLoaderTask == this) {
1163 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001164 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001165 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001166 }
Joe Onorato36115782010-06-17 13:28:48 -04001167 }
1168
1169 public void stopLocked() {
1170 synchronized (LoaderTask.this) {
1171 mStopped = true;
1172 this.notify();
1173 }
1174 }
1175
1176 /**
1177 * Gets the callbacks object. If we've been stopped, or if the launcher object
1178 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1179 * object that was around when the deferred message was scheduled, and if there's
1180 * a new Callbacks object around then also return null. This will save us from
1181 * calling onto it with data that will be ignored.
1182 */
1183 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1184 synchronized (mLock) {
1185 if (mStopped) {
1186 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001187 }
Joe Onorato36115782010-06-17 13:28:48 -04001188
1189 if (mCallbacks == null) {
1190 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001191 }
Joe Onorato36115782010-06-17 13:28:48 -04001192
1193 final Callbacks callbacks = mCallbacks.get();
1194 if (callbacks != oldCallbacks) {
1195 return null;
1196 }
1197 if (callbacks == null) {
1198 Log.w(TAG, "no mCallbacks");
1199 return null;
1200 }
1201
1202 return callbacks;
1203 }
1204 }
1205
1206 // check & update map of what's occupied; used to discard overlapping/invalid items
1207 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001208 int containerIndex = item.screen;
1209 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001210 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08001211 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001212 return false;
1213 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001214
1215 // We use the last index to refer to the hotseat and the screen as the rank, so
1216 // test and update the occupied state accordingly
1217 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
1218 Log.e(TAG, "Error loading shortcut into hotseat " + item
1219 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
1220 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
1221 return false;
1222 } else {
1223 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
1224 return true;
1225 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001226 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1227 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001228 return true;
1229 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001230
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001231 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001232 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1233 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001234 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001235 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -07001236 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001237 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001238 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -07001239 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001240 return false;
1241 }
1242 }
1243 }
1244 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1245 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001246 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001247 }
1248 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001249
Joe Onorato36115782010-06-17 13:28:48 -04001250 return true;
1251 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252
Joe Onorato36115782010-06-17 13:28:48 -04001253 private void loadWorkspace() {
1254 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255
Joe Onorato36115782010-06-17 13:28:48 -04001256 final Context context = mContext;
1257 final ContentResolver contentResolver = context.getContentResolver();
1258 final PackageManager manager = context.getPackageManager();
1259 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1260 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001261
Michael Jurkab85f8a42012-04-25 15:48:32 -07001262 // Make sure the default workspace is loaded, if needed
Brian Muramatsu5524b492012-10-02 16:55:54 -07001263 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001264
Winson Chung2abf94d2012-07-18 18:16:38 -07001265 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001266 sBgWorkspaceItems.clear();
1267 sBgAppWidgets.clear();
1268 sBgFolders.clear();
1269 sBgItemsIdMap.clear();
1270 sBgDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001271
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001272 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001274 final Cursor c = contentResolver.query(
1275 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001276
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001277 // +1 for the hotseat (it can be larger than the workspace)
1278 // Load workspace in reverse order to ensure that latest items are loaded first (and
1279 // before any earlier duplicates)
1280 final ItemInfo occupied[][][] =
1281 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001282
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001283 try {
1284 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1285 final int intentIndex = c.getColumnIndexOrThrow
1286 (LauncherSettings.Favorites.INTENT);
1287 final int titleIndex = c.getColumnIndexOrThrow
1288 (LauncherSettings.Favorites.TITLE);
1289 final int iconTypeIndex = c.getColumnIndexOrThrow(
1290 LauncherSettings.Favorites.ICON_TYPE);
1291 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1292 final int iconPackageIndex = c.getColumnIndexOrThrow(
1293 LauncherSettings.Favorites.ICON_PACKAGE);
1294 final int iconResourceIndex = c.getColumnIndexOrThrow(
1295 LauncherSettings.Favorites.ICON_RESOURCE);
1296 final int containerIndex = c.getColumnIndexOrThrow(
1297 LauncherSettings.Favorites.CONTAINER);
1298 final int itemTypeIndex = c.getColumnIndexOrThrow(
1299 LauncherSettings.Favorites.ITEM_TYPE);
1300 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1301 LauncherSettings.Favorites.APPWIDGET_ID);
1302 final int screenIndex = c.getColumnIndexOrThrow(
1303 LauncherSettings.Favorites.SCREEN);
1304 final int cellXIndex = c.getColumnIndexOrThrow
1305 (LauncherSettings.Favorites.CELLX);
1306 final int cellYIndex = c.getColumnIndexOrThrow
1307 (LauncherSettings.Favorites.CELLY);
1308 final int spanXIndex = c.getColumnIndexOrThrow
1309 (LauncherSettings.Favorites.SPANX);
1310 final int spanYIndex = c.getColumnIndexOrThrow(
1311 LauncherSettings.Favorites.SPANY);
1312 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1313 //final int displayModeIndex = c.getColumnIndexOrThrow(
1314 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001316 ShortcutInfo info;
1317 String intentDescription;
1318 LauncherAppWidgetInfo appWidgetInfo;
1319 int container;
1320 long id;
1321 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 while (!mStopped && c.moveToNext()) {
1324 try {
1325 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001327 switch (itemType) {
1328 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1329 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1330 intentDescription = c.getString(intentIndex);
1331 try {
1332 intent = Intent.parseUri(intentDescription, 0);
1333 } catch (URISyntaxException e) {
1334 continue;
1335 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001336
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001337 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1338 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1339 titleIndex, mLabelCache);
1340 } else {
1341 info = getShortcutInfo(c, context, iconTypeIndex,
1342 iconPackageIndex, iconResourceIndex, iconIndex,
1343 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001344
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001345 // App shortcuts that used to be automatically added to Launcher
1346 // didn't always have the correct intent flags set, so do that
1347 // here
1348 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001349 intent.getCategories() != null &&
1350 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001351 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001352 intent.addFlags(
1353 Intent.FLAG_ACTIVITY_NEW_TASK |
1354 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1355 }
Michael Jurka96879562012-03-22 05:54:33 -07001356 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 if (info != null) {
1359 info.intent = intent;
1360 info.id = c.getLong(idIndex);
1361 container = c.getInt(containerIndex);
1362 info.container = container;
1363 info.screen = c.getInt(screenIndex);
1364 info.cellX = c.getInt(cellXIndex);
1365 info.cellY = c.getInt(cellYIndex);
1366
1367 // check & update map of what's occupied
1368 if (!checkItemPlacement(occupied, info)) {
1369 break;
1370 }
1371
1372 switch (container) {
1373 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1374 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1375 sBgWorkspaceItems.add(info);
1376 break;
1377 default:
1378 // Item is in a user folder
1379 FolderInfo folderInfo =
1380 findOrMakeFolder(sBgFolders, container);
1381 folderInfo.add(info);
1382 break;
1383 }
1384 sBgItemsIdMap.put(info.id, info);
1385
1386 // now that we've loaded everthing re-save it with the
1387 // icon in case it disappears somehow.
1388 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
1389 } else {
1390 // Failed to load the shortcut, probably because the
1391 // activity manager couldn't resolve it (maybe the app
1392 // was uninstalled), or the db row was somehow screwed up.
1393 // Delete it.
1394 id = c.getLong(idIndex);
1395 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1396 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1397 id, false), null, null);
1398 }
1399 break;
1400
1401 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1402 id = c.getLong(idIndex);
1403 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1404
1405 folderInfo.title = c.getString(titleIndex);
1406 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001408 folderInfo.container = container;
1409 folderInfo.screen = c.getInt(screenIndex);
1410 folderInfo.cellX = c.getInt(cellXIndex);
1411 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412
Daniel Sandler8802e962010-05-26 16:28:16 -04001413 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001414 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001415 break;
1416 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1419 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1420 sBgWorkspaceItems.add(folderInfo);
1421 break;
Joe Onorato36115782010-06-17 13:28:48 -04001422 }
Joe Onorato17a89222011-02-08 17:26:11 -08001423
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001424 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1425 sBgFolders.put(folderInfo.id, folderInfo);
1426 break;
1427
1428 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1429 // Read all Launcher-specific widget details
1430 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001431 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001432
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001433 final AppWidgetProviderInfo provider =
1434 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001435
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001436 if (!isSafeMode && (provider == null || provider.provider == null ||
1437 provider.provider.getPackageName() == null)) {
1438 String log = "Deleting widget that isn't installed anymore: id="
1439 + id + " appWidgetId=" + appWidgetId;
1440 Log.e(TAG, log);
1441 Launcher.sDumpLogs.add(log);
1442 itemsToRemove.add(id);
1443 } else {
1444 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1445 provider.provider);
1446 appWidgetInfo.id = id;
1447 appWidgetInfo.screen = c.getInt(screenIndex);
1448 appWidgetInfo.cellX = c.getInt(cellXIndex);
1449 appWidgetInfo.cellY = c.getInt(cellYIndex);
1450 appWidgetInfo.spanX = c.getInt(spanXIndex);
1451 appWidgetInfo.spanY = c.getInt(spanYIndex);
1452 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1453 appWidgetInfo.minSpanX = minSpan[0];
1454 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001455
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 container = c.getInt(containerIndex);
1457 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1458 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1459 Log.e(TAG, "Widget found where container != " +
1460 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1461 continue;
1462 }
1463 appWidgetInfo.container = c.getInt(containerIndex);
1464
1465 // check & update map of what's occupied
1466 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1467 break;
1468 }
1469 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1470 sBgAppWidgets.add(appWidgetInfo);
1471 }
Joe Onorato36115782010-06-17 13:28:48 -04001472 break;
1473 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001474 } catch (Exception e) {
1475 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001476 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001477 }
1478 } finally {
1479 c.close();
1480 }
1481
1482 if (itemsToRemove.size() > 0) {
1483 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1484 LauncherSettings.Favorites.CONTENT_URI);
1485 // Remove dead items
1486 for (long id : itemsToRemove) {
1487 if (DEBUG_LOADERS) {
1488 Log.d(TAG, "Removed id = " + id);
1489 }
1490 // Don't notify content observers
1491 try {
1492 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1493 null, null);
1494 } catch (RemoteException e) {
1495 Log.w(TAG, "Could not remove id = " + id);
1496 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001497 }
1498 }
1499
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001500 if (DEBUG_LOADERS) {
1501 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1502 Log.d(TAG, "workspace layout: ");
1503 for (int y = 0; y < mCellCountY; y++) {
1504 String line = "";
1505 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1506 if (s > 0) {
1507 line += " | ";
1508 }
1509 for (int x = 0; x < mCellCountX; x++) {
1510 line += ((occupied[s][x][y] != null) ? "#" : ".");
1511 }
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001513 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001514 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001515 }
Joe Onorato36115782010-06-17 13:28:48 -04001516 }
1517 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001518
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001519 /** Filters the set of items who are directly or indirectly (via another container) on the
1520 * specified screen. */
1521 private void filterCurrentWorkspaceItems(int currentScreen,
1522 ArrayList<ItemInfo> allWorkspaceItems,
1523 ArrayList<ItemInfo> currentScreenItems,
1524 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001525 // Purge any null ItemInfos
1526 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1527 while (iter.hasNext()) {
1528 ItemInfo i = iter.next();
1529 if (i == null) {
1530 iter.remove();
1531 }
1532 }
1533
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001534 // If we aren't filtering on a screen, then the set of items to load is the full set of
1535 // items given.
1536 if (currentScreen < 0) {
1537 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001538 }
1539
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001540 // Order the set of items by their containers first, this allows use to walk through the
1541 // list sequentially, build up a list of containers that are in the specified screen,
1542 // as well as all items in those containers.
1543 Set<Long> itemsOnScreen = new HashSet<Long>();
1544 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1545 @Override
1546 public int compare(ItemInfo lhs, ItemInfo rhs) {
1547 return (int) (lhs.container - rhs.container);
1548 }
1549 });
1550 for (ItemInfo info : allWorkspaceItems) {
1551 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1552 if (info.screen == currentScreen) {
1553 currentScreenItems.add(info);
1554 itemsOnScreen.add(info.id);
1555 } else {
1556 otherScreenItems.add(info);
1557 }
1558 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1559 currentScreenItems.add(info);
1560 itemsOnScreen.add(info.id);
1561 } else {
1562 if (itemsOnScreen.contains(info.container)) {
1563 currentScreenItems.add(info);
1564 itemsOnScreen.add(info.id);
1565 } else {
1566 otherScreenItems.add(info);
1567 }
1568 }
1569 }
1570 }
1571
1572 /** Filters the set of widgets which are on the specified screen. */
1573 private void filterCurrentAppWidgets(int currentScreen,
1574 ArrayList<LauncherAppWidgetInfo> appWidgets,
1575 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1576 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1577 // If we aren't filtering on a screen, then the set of items to load is the full set of
1578 // widgets given.
1579 if (currentScreen < 0) {
1580 currentScreenWidgets.addAll(appWidgets);
1581 }
1582
1583 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001584 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001585 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1586 widget.screen == currentScreen) {
1587 currentScreenWidgets.add(widget);
1588 } else {
1589 otherScreenWidgets.add(widget);
1590 }
1591 }
1592 }
1593
1594 /** Filters the set of folders which are on the specified screen. */
1595 private void filterCurrentFolders(int currentScreen,
1596 HashMap<Long, ItemInfo> itemsIdMap,
1597 HashMap<Long, FolderInfo> folders,
1598 HashMap<Long, FolderInfo> currentScreenFolders,
1599 HashMap<Long, FolderInfo> otherScreenFolders) {
1600 // If we aren't filtering on a screen, then the set of items to load is the full set of
1601 // widgets given.
1602 if (currentScreen < 0) {
1603 currentScreenFolders.putAll(folders);
1604 }
1605
1606 for (long id : folders.keySet()) {
1607 ItemInfo info = itemsIdMap.get(id);
1608 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07001609 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001610 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1611 info.screen == currentScreen) {
1612 currentScreenFolders.put(id, folder);
1613 } else {
1614 otherScreenFolders.put(id, folder);
1615 }
1616 }
1617 }
1618
1619 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1620 * right) */
1621 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1622 // XXX: review this
1623 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001624 @Override
1625 public int compare(ItemInfo lhs, ItemInfo rhs) {
1626 int cellCountX = LauncherModel.getCellCountX();
1627 int cellCountY = LauncherModel.getCellCountY();
1628 int screenOffset = cellCountX * cellCountY;
1629 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1630 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1631 lhs.cellY * cellCountX + lhs.cellX);
1632 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1633 rhs.cellY * cellCountX + rhs.cellX);
1634 return (int) (lr - rr);
1635 }
1636 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001638
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001639 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1640 final ArrayList<ItemInfo> workspaceItems,
1641 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1642 final HashMap<Long, FolderInfo> folders,
1643 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07001644
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001645 final boolean postOnMainThread = (deferredBindRunnables != null);
1646
1647 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001648 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001649 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001650 final int start = i;
1651 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001652 final Runnable r = new Runnable() {
1653 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001654 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001655 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001656 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001657 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 }
1659 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001660 };
1661 if (postOnMainThread) {
1662 deferredBindRunnables.add(r);
1663 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001664 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001665 }
Joe Onorato36115782010-06-17 13:28:48 -04001666 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001667
1668 // Bind the folders
1669 if (!folders.isEmpty()) {
1670 final Runnable r = new Runnable() {
1671 public void run() {
1672 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1673 if (callbacks != null) {
1674 callbacks.bindFolders(folders);
1675 }
1676 }
1677 };
1678 if (postOnMainThread) {
1679 deferredBindRunnables.add(r);
1680 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001681 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001682 }
1683 }
1684
1685 // Bind the widgets, one at a time
1686 N = appWidgets.size();
1687 for (int i = 0; i < N; i++) {
1688 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1689 final Runnable r = new Runnable() {
1690 public void run() {
1691 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1692 if (callbacks != null) {
1693 callbacks.bindAppWidget(widget);
1694 }
1695 }
1696 };
1697 if (postOnMainThread) {
1698 deferredBindRunnables.add(r);
1699 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001700 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 }
1702 }
1703 }
1704
1705 /**
1706 * Binds all loaded data to actual views on the main thread.
1707 */
1708 private void bindWorkspace(int synchronizeBindPage) {
1709 final long t = SystemClock.uptimeMillis();
1710 Runnable r;
1711
1712 // Don't use these two variables in any of the callback runnables.
1713 // Otherwise we hold a reference to them.
1714 final Callbacks oldCallbacks = mCallbacks.get();
1715 if (oldCallbacks == null) {
1716 // This launcher has exited and nobody bothered to tell us. Just bail.
1717 Log.w(TAG, "LoaderTask running with no launcher");
1718 return;
1719 }
1720
Winson Chung4a2afa32012-07-19 14:53:05 -07001721 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
1722 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001723 oldCallbacks.getCurrentWorkspaceScreen();
1724
1725 // Load all the items that are on the current page first (and in the process, unbind
1726 // all the existing workspace items before we call startBinding() below.
1727 unbindWorkspaceItemsOnMainThread();
1728 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1729 ArrayList<LauncherAppWidgetInfo> appWidgets =
1730 new ArrayList<LauncherAppWidgetInfo>();
1731 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
1732 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -07001733 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001734 workspaceItems.addAll(sBgWorkspaceItems);
1735 appWidgets.addAll(sBgAppWidgets);
1736 folders.putAll(sBgFolders);
1737 itemsIdMap.putAll(sBgItemsIdMap);
1738 }
1739
1740 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
1741 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
1742 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
1743 new ArrayList<LauncherAppWidgetInfo>();
1744 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
1745 new ArrayList<LauncherAppWidgetInfo>();
1746 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
1747 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
1748
1749 // Separate the items that are on the current screen, and all the other remaining items
1750 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
1751 otherWorkspaceItems);
1752 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
1753 otherAppWidgets);
1754 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
1755 otherFolders);
1756 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1757 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1758
1759 // Tell the workspace that we're about to start 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) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001765 }
1766 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 };
Winson Chung81b52252012-08-27 15:34:29 -07001768 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001769
1770 // Load items on the current page
1771 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
1772 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07001773 if (isLoadingSynchronously) {
1774 r = new Runnable() {
1775 public void run() {
1776 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1777 if (callbacks != null) {
1778 callbacks.onPageBoundSynchronously(currentScreen);
1779 }
1780 }
1781 };
Winson Chung81b52252012-08-27 15:34:29 -07001782 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07001783 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784
Winson Chung4a2afa32012-07-19 14:53:05 -07001785 // Load all the remaining pages (if we are loading synchronously, we want to defer this
1786 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 mDeferredBindRunnables.clear();
1788 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07001789 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001790
1791 // Tell the workspace that we're done binding items
1792 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001793 public void run() {
1794 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1795 if (callbacks != null) {
1796 callbacks.finishBindingItems();
1797 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798
Winson Chung98e030b2012-05-07 16:01:11 -07001799 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001800 if (DEBUG_LOADERS) {
1801 Log.d(TAG, "bound workspace in "
1802 + (SystemClock.uptimeMillis()-t) + "ms");
1803 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001804
1805 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001806 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 };
Winson Chung4a2afa32012-07-19 14:53:05 -07001808 if (isLoadingSynchronously) {
1809 mDeferredBindRunnables.add(r);
1810 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001811 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07001812 }
Joe Onorato36115782010-06-17 13:28:48 -04001813 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001814
Joe Onorato36115782010-06-17 13:28:48 -04001815 private void loadAndBindAllApps() {
1816 if (DEBUG_LOADERS) {
1817 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1818 }
1819 if (!mAllAppsLoaded) {
1820 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001821 synchronized (LoaderTask.this) {
1822 if (mStopped) {
1823 return;
1824 }
1825 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001826 }
Joe Onorato36115782010-06-17 13:28:48 -04001827 } else {
1828 onlyBindAllApps();
1829 }
1830 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001831
Joe Onorato36115782010-06-17 13:28:48 -04001832 private void onlyBindAllApps() {
1833 final Callbacks oldCallbacks = mCallbacks.get();
1834 if (oldCallbacks == null) {
1835 // This launcher has exited and nobody bothered to tell us. Just bail.
1836 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1837 return;
1838 }
1839
1840 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001841 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001842 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07001843 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001844 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001845 public void run() {
1846 final long t = SystemClock.uptimeMillis();
1847 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1848 if (callbacks != null) {
1849 callbacks.bindAllApplications(list);
1850 }
1851 if (DEBUG_LOADERS) {
1852 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1853 + (SystemClock.uptimeMillis()-t) + "ms");
1854 }
1855 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001856 };
1857 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
1858 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) {
1859 r.run();
1860 } else {
1861 mHandler.post(r);
1862 }
Joe Onorato36115782010-06-17 13:28:48 -04001863 }
1864
1865 private void loadAllAppsByBatch() {
1866 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1867
1868 // Don't use these two variables in any of the callback runnables.
1869 // Otherwise we hold a reference to them.
1870 final Callbacks oldCallbacks = mCallbacks.get();
1871 if (oldCallbacks == null) {
1872 // This launcher has exited and nobody bothered to tell us. Just bail.
1873 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1874 return;
1875 }
1876
1877 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1878 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1879
1880 final PackageManager packageManager = mContext.getPackageManager();
1881 List<ResolveInfo> apps = null;
1882
1883 int N = Integer.MAX_VALUE;
1884
1885 int startIndex;
1886 int i=0;
1887 int batchSize = -1;
1888 while (i < N && !mStopped) {
1889 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001890 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001891 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1892 apps = packageManager.queryIntentActivities(mainIntent, 0);
1893 if (DEBUG_LOADERS) {
1894 Log.d(TAG, "queryIntentActivities took "
1895 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1896 }
1897 if (apps == null) {
1898 return;
1899 }
1900 N = apps.size();
1901 if (DEBUG_LOADERS) {
1902 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1903 }
1904 if (N == 0) {
1905 // There are no apps?!?
1906 return;
1907 }
1908 if (mBatchSize == 0) {
1909 batchSize = N;
1910 } else {
1911 batchSize = mBatchSize;
1912 }
1913
1914 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1915 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001916 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001917 if (DEBUG_LOADERS) {
1918 Log.d(TAG, "sort took "
1919 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1920 }
1921 }
1922
1923 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1924
1925 startIndex = i;
1926 for (int j=0; i<N && j<batchSize; j++) {
1927 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001928 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001929 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001930 i++;
1931 }
1932
1933 final boolean first = i <= batchSize;
1934 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001935 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
1936 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001937
Joe Onoratocc67f472010-06-08 10:54:30 -07001938 mHandler.post(new Runnable() {
1939 public void run() {
1940 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001941 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001942 if (first) {
1943 callbacks.bindAllApplications(added);
1944 } else {
1945 callbacks.bindAppsAdded(added);
1946 }
1947 if (DEBUG_LOADERS) {
1948 Log.d(TAG, "bound " + added.size() + " apps in "
1949 + (SystemClock.uptimeMillis() - t) + "ms");
1950 }
1951 } else {
1952 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001953 }
1954 }
1955 });
1956
Daniel Sandlerdca66122010-04-13 16:23:58 -04001957 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001958 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1959 + (SystemClock.uptimeMillis()-t2) + "ms");
1960 }
1961
1962 if (mAllAppsLoadDelay > 0 && i < N) {
1963 try {
1964 if (DEBUG_LOADERS) {
1965 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1966 }
1967 Thread.sleep(mAllAppsLoadDelay);
1968 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001969 }
1970 }
1971
Joe Onorato36115782010-06-17 13:28:48 -04001972 if (DEBUG_LOADERS) {
1973 Log.d(TAG, "cached all " + N + " apps in "
1974 + (SystemClock.uptimeMillis()-t) + "ms"
1975 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001976 }
1977 }
1978
1979 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07001980 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1982 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1983 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1984 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1985 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
1986 }
Joe Onorato36115782010-06-17 13:28:48 -04001987 }
1988 }
1989
1990 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001991 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001992 }
1993
1994 private class PackageUpdatedTask implements Runnable {
1995 int mOp;
1996 String[] mPackages;
1997
1998 public static final int OP_NONE = 0;
1999 public static final int OP_ADD = 1;
2000 public static final int OP_UPDATE = 2;
2001 public static final int OP_REMOVE = 3; // uninstlled
2002 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2003
2004
2005 public PackageUpdatedTask(int op, String[] packages) {
2006 mOp = op;
2007 mPackages = packages;
2008 }
2009
2010 public void run() {
2011 final Context context = mApp;
2012
2013 final String[] packages = mPackages;
2014 final int N = packages.length;
2015 switch (mOp) {
2016 case OP_ADD:
2017 for (int i=0; i<N; i++) {
2018 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002019 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002020 }
2021 break;
2022 case OP_UPDATE:
2023 for (int i=0; i<N; i++) {
2024 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002025 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002026 LauncherApplication app =
2027 (LauncherApplication) context.getApplicationContext();
2028 WidgetPreviewLoader.removeFromDb(
2029 app.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002030 }
2031 break;
2032 case OP_REMOVE:
2033 case OP_UNAVAILABLE:
2034 for (int i=0; i<N; i++) {
2035 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002036 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002037 LauncherApplication app =
2038 (LauncherApplication) context.getApplicationContext();
2039 WidgetPreviewLoader.removeFromDb(
2040 app.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002041 }
2042 break;
2043 }
2044
2045 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002046 ArrayList<ApplicationInfo> modified = null;
Winson Chung83892cc2013-05-01 16:53:33 -07002047 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002048
Adam Cohen487f7dd2012-06-28 18:12:10 -07002049 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002050 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2051 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002052 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002053 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002054 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2055 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002056 }
Winson Chung5d55f332012-07-16 20:45:03 -07002057 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002058 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002059 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002060 }
2061
Joe Onorato36115782010-06-17 13:28:48 -04002062 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2063 if (callbacks == null) {
2064 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2065 return;
2066 }
2067
2068 if (added != null) {
2069 final ArrayList<ApplicationInfo> addedFinal = added;
2070 mHandler.post(new Runnable() {
2071 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002072 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2073 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002074 callbacks.bindAppsAdded(addedFinal);
2075 }
2076 }
2077 });
2078 }
2079 if (modified != null) {
2080 final ArrayList<ApplicationInfo> modifiedFinal = modified;
2081 mHandler.post(new Runnable() {
2082 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002083 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2084 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002085 callbacks.bindAppsUpdated(modifiedFinal);
2086 }
2087 }
2088 });
2089 }
Winson Chung83892cc2013-05-01 16:53:33 -07002090 // If a package has been removed, or an app has been removed as a result of
2091 // an update (for example), make the removed callback.
2092 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
2093 final boolean permanent = (mOp == OP_REMOVE);
2094 final ArrayList<String> removedPackageNames =
2095 new ArrayList<String>(Arrays.asList(packages));
2096
Joe Onorato36115782010-06-17 13:28:48 -04002097 mHandler.post(new Runnable() {
2098 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002099 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2100 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002101 callbacks.bindComponentsRemoved(removedPackageNames,
2102 removedApps, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04002103 }
2104 }
2105 });
Joe Onoratobe386092009-11-17 17:32:16 -08002106 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002107
2108 mHandler.post(new Runnable() {
2109 @Override
2110 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002111 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2112 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002113 callbacks.bindPackagesUpdated();
2114 }
2115 }
2116 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002117 }
2118 }
2119
2120 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002121 * This is called from the code that adds shortcuts from the intent receiver. This
2122 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002123 */
Joe Onorato56d82912010-03-07 14:32:10 -05002124 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002125 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002126 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002127
Joe Onorato56d82912010-03-07 14:32:10 -05002128 /**
2129 * Make an ShortcutInfo object for a shortcut that is an application.
2130 *
2131 * If c is not null, then it will be used to fill in missing data like the title and icon.
2132 */
2133 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002134 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002135 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002136 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002137
2138 ComponentName componentName = intent.getComponent();
2139 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002140 return null;
2141 }
2142
Adam Cohen00fcb492011-11-02 21:53:47 -07002143 try {
2144 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2145 if (!pi.applicationInfo.enabled) {
2146 // If we return null here, the corresponding item will be removed from the launcher
2147 // db and will not appear in the workspace.
2148 return null;
2149 }
2150 } catch (NameNotFoundException e) {
2151 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
2152 }
2153
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002154 // TODO: See if the PackageManager knows about this case. If it doesn't
2155 // then return null & delete this.
2156
Joe Onorato56d82912010-03-07 14:32:10 -05002157 // the resource -- This may implicitly give us back the fallback icon,
2158 // but don't worry about that. All we're doing with usingFallbackIcon is
2159 // to avoid saving lots of copies of that in the database, and most apps
2160 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002161
2162 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2163 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2164 // via resolveActivity().
2165 ResolveInfo resolveInfo = null;
2166 ComponentName oldComponent = intent.getComponent();
2167 Intent newIntent = new Intent(intent.getAction(), null);
2168 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2169 newIntent.setPackage(oldComponent.getPackageName());
2170 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2171 for (ResolveInfo i : infos) {
2172 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2173 i.activityInfo.name);
2174 if (cn.equals(oldComponent)) {
2175 resolveInfo = i;
2176 }
2177 }
2178 if (resolveInfo == null) {
2179 resolveInfo = manager.resolveActivity(intent, 0);
2180 }
Joe Onorato56d82912010-03-07 14:32:10 -05002181 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002182 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002183 }
Joe Onorato56d82912010-03-07 14:32:10 -05002184 // the db
2185 if (icon == null) {
2186 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002187 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002188 }
2189 }
2190 // the fallback icon
2191 if (icon == null) {
2192 icon = getFallbackIcon();
2193 info.usingFallbackIcon = true;
2194 }
2195 info.setIcon(icon);
2196
2197 // from the resource
2198 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002199 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2200 if (labelCache != null && labelCache.containsKey(key)) {
2201 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002202 } else {
2203 info.title = resolveInfo.activityInfo.loadLabel(manager);
2204 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002205 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002206 }
2207 }
Joe Onorato56d82912010-03-07 14:32:10 -05002208 }
2209 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002211 if (c != null) {
2212 info.title = c.getString(titleIndex);
2213 }
2214 }
2215 // fall back to the class name of the activity
2216 if (info.title == null) {
2217 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002218 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002219 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2220 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 /**
Winson Chung8a435102012-08-30 17:16:53 -07002224 * Returns the set of workspace ShortcutInfos with the specified intent.
2225 */
2226 static ArrayList<ItemInfo> getWorkspaceShortcutItemInfosWithIntent(Intent intent) {
2227 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
2228 synchronized (sBgLock) {
2229 for (ItemInfo info : sBgWorkspaceItems) {
2230 if (info instanceof ShortcutInfo) {
2231 ShortcutInfo shortcut = (ShortcutInfo) info;
2232 if (shortcut.intent.toUri(0).equals(intent.toUri(0))) {
2233 items.add(shortcut);
2234 }
2235 }
2236 }
2237 }
2238 return items;
2239 }
2240
2241 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002242 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002244 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002245 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2246 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247
Joe Onorato56d82912010-03-07 14:32:10 -05002248 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002249 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002250 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002252 // TODO: If there's an explicit component and we can't install that, delete it.
2253
Joe Onorato56d82912010-03-07 14:32:10 -05002254 info.title = c.getString(titleIndex);
2255
Joe Onorato9c1289c2009-08-17 11:03:03 -04002256 int iconType = c.getInt(iconTypeIndex);
2257 switch (iconType) {
2258 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2259 String packageName = c.getString(iconPackageIndex);
2260 String resourceName = c.getString(iconResourceIndex);
2261 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002262 info.customIcon = false;
2263 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002265 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002266 if (resources != null) {
2267 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002268 icon = Utilities.createIconBitmap(
2269 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002270 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002271 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002272 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 }
Joe Onorato56d82912010-03-07 14:32:10 -05002274 // the db
2275 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002276 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002277 }
2278 // the fallback icon
2279 if (icon == null) {
2280 icon = getFallbackIcon();
2281 info.usingFallbackIcon = true;
2282 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002283 break;
2284 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002285 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002286 if (icon == null) {
2287 icon = getFallbackIcon();
2288 info.customIcon = false;
2289 info.usingFallbackIcon = true;
2290 } else {
2291 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002292 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002293 break;
2294 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002295 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002296 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002297 info.customIcon = false;
2298 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002300 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002301 return info;
2302 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303
Michael Jurka931dc972011-08-05 15:08:15 -07002304 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002305 @SuppressWarnings("all") // suppress dead code warning
2306 final boolean debug = false;
2307 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002308 Log.d(TAG, "getIconFromCursor app="
2309 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2310 }
2311 byte[] data = c.getBlob(iconIndex);
2312 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002313 return Utilities.createIconBitmap(
2314 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002315 } catch (Exception e) {
2316 return null;
2317 }
2318 }
2319
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2321 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002322 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002323 if (info == null) {
2324 return null;
2325 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002326 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002327
2328 return info;
2329 }
2330
Winson Chunga9abd0e2010-10-27 17:18:37 -07002331 /**
Winson Chung55cef262010-10-28 14:14:18 -07002332 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2333 */
2334 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2335 ComponentName component) {
2336 List<AppWidgetProviderInfo> widgets =
2337 AppWidgetManager.getInstance(context).getInstalledProviders();
2338 for (AppWidgetProviderInfo info : widgets) {
2339 if (info.provider.equals(component)) {
2340 return info;
2341 }
2342 }
2343 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002344 }
2345
Winson Chung68846fd2010-10-29 11:00:27 -07002346 /**
2347 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2348 */
2349 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2350 final PackageManager packageManager = context.getPackageManager();
2351 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2352 new ArrayList<WidgetMimeTypeHandlerData>();
2353
2354 final Intent supportsIntent =
2355 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2356 supportsIntent.setType(mimeType);
2357
2358 // Create a set of widget configuration components that we can test against
2359 final List<AppWidgetProviderInfo> widgets =
2360 AppWidgetManager.getInstance(context).getInstalledProviders();
2361 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2362 new HashMap<ComponentName, AppWidgetProviderInfo>();
2363 for (AppWidgetProviderInfo info : widgets) {
2364 configurationComponentToWidget.put(info.configure, info);
2365 }
2366
2367 // Run through each of the intents that can handle this type of clip data, and cross
2368 // reference them with the components that are actual configuration components
2369 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2370 PackageManager.MATCH_DEFAULT_ONLY);
2371 for (ResolveInfo info : activities) {
2372 final ActivityInfo activityInfo = info.activityInfo;
2373 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2374 activityInfo.name);
2375 if (configurationComponentToWidget.containsKey(infoComponent)) {
2376 supportedConfigurationActivities.add(
2377 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2378 configurationComponentToWidget.get(infoComponent)));
2379 }
2380 }
2381 return supportedConfigurationActivities;
2382 }
2383
Winson Chunga9abd0e2010-10-27 17:18:37 -07002384 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002385 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2386 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2387 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2388
Adam Cohend9198822011-11-22 16:42:47 -08002389 if (intent == null) {
2390 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2391 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2392 return null;
2393 }
2394
Joe Onorato0589f0f2010-02-08 13:44:00 -08002395 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002396 boolean customIcon = false;
2397 ShortcutIconResource iconResource = null;
2398
2399 if (bitmap != null && bitmap instanceof Bitmap) {
2400 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002401 customIcon = true;
2402 } else {
2403 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2404 if (extra != null && extra instanceof ShortcutIconResource) {
2405 try {
2406 iconResource = (ShortcutIconResource) extra;
2407 final PackageManager packageManager = context.getPackageManager();
2408 Resources resources = packageManager.getResourcesForApplication(
2409 iconResource.packageName);
2410 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002411 icon = Utilities.createIconBitmap(
2412 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002413 } catch (Exception e) {
2414 Log.w(TAG, "Could not load shortcut icon: " + extra);
2415 }
2416 }
2417 }
2418
Michael Jurkac9d95c52011-08-29 14:03:34 -07002419 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002420
2421 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002422 if (fallbackIcon != null) {
2423 icon = fallbackIcon;
2424 } else {
2425 icon = getFallbackIcon();
2426 info.usingFallbackIcon = true;
2427 }
Joe Onorato56d82912010-03-07 14:32:10 -05002428 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002429 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002430
Joe Onorato0589f0f2010-02-08 13:44:00 -08002431 info.title = name;
2432 info.intent = intent;
2433 info.customIcon = customIcon;
2434 info.iconResource = iconResource;
2435
2436 return info;
2437 }
2438
Winson Chungaac01e12011-08-17 10:37:13 -07002439 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2440 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002441 // If apps can't be on SD, don't even bother.
2442 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002443 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002444 }
Joe Onorato56d82912010-03-07 14:32:10 -05002445 // If this icon doesn't have a custom icon, check to see
2446 // what's stored in the DB, and if it doesn't match what
2447 // we're going to show, store what we are going to show back
2448 // into the DB. We do this so when we're loading, if the
2449 // package manager can't find an icon (for example because
2450 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002451 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002452 cache.put(info, c.getBlob(iconIndex));
2453 return true;
2454 }
2455 return false;
2456 }
2457 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2458 boolean needSave = false;
2459 try {
2460 if (data != null) {
2461 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2462 Bitmap loaded = info.getIcon(mIconCache);
2463 needSave = !saved.sameAs(loaded);
2464 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002465 needSave = true;
2466 }
Winson Chungaac01e12011-08-17 10:37:13 -07002467 } catch (Exception e) {
2468 needSave = true;
2469 }
2470 if (needSave) {
2471 Log.d(TAG, "going to save icon bitmap for info=" + info);
2472 // This is slower than is ideal, but this only happens once
2473 // or when the app is updated with a new icon.
2474 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002475 }
2476 }
2477
Joe Onorato9c1289c2009-08-17 11:03:03 -04002478 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002479 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002480 * or make a new one.
2481 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002482 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002483 // See if a placeholder was created for us already
2484 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002485 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002486 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002487 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002488 folders.put(id, folderInfo);
2489 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002490 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 }
2492
Winson Chung11904872012-09-17 16:58:46 -07002493 public static final Comparator<ApplicationInfo> getAppNameComparator() {
2494 final Collator collator = Collator.getInstance();
2495 return new Comparator<ApplicationInfo>() {
2496 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2497 int result = collator.compare(a.title.toString(), b.title.toString());
2498 if (result == 0) {
2499 result = a.componentName.compareTo(b.componentName);
2500 }
2501 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07002502 }
Winson Chung11904872012-09-17 16:58:46 -07002503 };
2504 }
Winson Chung78403fe2011-01-21 15:38:02 -08002505 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2506 = new Comparator<ApplicationInfo>() {
2507 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2508 if (a.firstInstallTime < b.firstInstallTime) return 1;
2509 if (a.firstInstallTime > b.firstInstallTime) return -1;
2510 return 0;
2511 }
2512 };
Winson Chung11904872012-09-17 16:58:46 -07002513 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
2514 final Collator collator = Collator.getInstance();
2515 return new Comparator<AppWidgetProviderInfo>() {
2516 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2517 return collator.compare(a.label.toString(), b.label.toString());
2518 }
2519 };
2520 }
Winson Chung5308f242011-08-18 12:12:41 -07002521 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2522 if (info.activityInfo != null) {
2523 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2524 } else {
2525 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2526 }
2527 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002528 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07002529 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07002530 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002531 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002532 ShortcutNameComparator(PackageManager pm) {
2533 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002534 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07002535 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07002536 }
2537 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2538 mPackageManager = pm;
2539 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07002540 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07002541 }
2542 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002543 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002544 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2545 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2546 if (mLabelCache.containsKey(keyA)) {
2547 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002548 } else {
2549 labelA = a.loadLabel(mPackageManager).toString();
2550
Winson Chung5308f242011-08-18 12:12:41 -07002551 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002552 }
Winson Chung5308f242011-08-18 12:12:41 -07002553 if (mLabelCache.containsKey(keyB)) {
2554 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002555 } else {
2556 labelB = b.loadLabel(mPackageManager).toString();
2557
Winson Chung5308f242011-08-18 12:12:41 -07002558 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002559 }
Winson Chung11904872012-09-17 16:58:46 -07002560 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07002561 }
2562 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002563 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07002564 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07002565 private PackageManager mPackageManager;
2566 private HashMap<Object, String> mLabelCache;
2567 WidgetAndShortcutNameComparator(PackageManager pm) {
2568 mPackageManager = pm;
2569 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07002570 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07002571 }
2572 public final int compare(Object a, Object b) {
2573 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002574 if (mLabelCache.containsKey(a)) {
2575 labelA = mLabelCache.get(a);
2576 } else {
2577 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002578 ((AppWidgetProviderInfo) a).label :
2579 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002580 mLabelCache.put(a, labelA);
2581 }
2582 if (mLabelCache.containsKey(b)) {
2583 labelB = mLabelCache.get(b);
2584 } else {
2585 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002586 ((AppWidgetProviderInfo) b).label :
2587 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002588 mLabelCache.put(b, labelB);
2589 }
Winson Chung11904872012-09-17 16:58:46 -07002590 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07002591 }
2592 };
Joe Onoratobe386092009-11-17 17:32:16 -08002593
2594 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002595 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002596 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2597 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2598 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2599 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002600 if (mLoaderTask != null) {
2601 mLoaderTask.dumpState();
2602 } else {
2603 Log.d(TAG, "mLoaderTask=null");
2604 }
Joe Onoratobe386092009-11-17 17:32:16 -08002605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606}