blob: 1890e74f3c0308a84ce33c5a5485e9bf025673a8 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040022import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080024import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
26import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070031import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070035import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Resources;
37import android.database.Cursor;
38import android.graphics.Bitmap;
39import android.graphics.BitmapFactory;
40import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080041import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040042import android.os.Handler;
43import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Winson Chung68846fd2010-10-29 11:00:27 -070050import com.android.launcher.R;
51import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080052
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.lang.ref.WeakReference;
54import java.net.URISyntaxException;
55import java.text.Collator;
56import java.util.ArrayList;
57import java.util.Collections;
58import java.util.Comparator;
59import java.util.HashMap;
60import java.util.List;
Michael Jurkac2f801e2011-07-12 14:19:46 -070061
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062/**
63 * Maintains in-memory state of the Launcher. It is expected that there should be only one
64 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070065 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040067public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080068 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040069 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070070
Joe Onorato36115782010-06-17 13:28:48 -040071 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080072 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040073 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040074 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040075
Joe Onoratof99f8c12009-10-31 17:27:36 -040076 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040077 private final Object mLock = new Object();
78 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040079 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070081 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
82 static {
83 sWorkerThread.start();
84 }
85 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
86
Joe Onoratocc67f472010-06-08 10:54:30 -070087 // We start off with everything not loaded. After that, we assume that
88 // our monitoring of the package manager provides all updates and we never
89 // need to do a requery. These are only ever touched from the loader thread.
90 private boolean mWorkspaceLoaded;
91 private boolean mAllAppsLoaded;
92
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Michael Jurkaa8c760d2011-04-28 14:59:33 -070095 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -070096 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080097
Adam Cohen487f7dd2012-06-28 18:12:10 -070098 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -070099 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700100 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700101
Adam Cohen487f7dd2012-06-28 18:12:10 -0700102 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
103 // created by LauncherModel that are directly on the home screen (however, no widgets or
104 // shortcuts within folders).
105 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700106
Adam Cohen487f7dd2012-06-28 18:12:10 -0700107 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
108 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700109 new ArrayList<LauncherAppWidgetInfo>();
110
Adam Cohen487f7dd2012-06-28 18:12:10 -0700111 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
112 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700113
Adam Cohen487f7dd2012-06-28 18:12:10 -0700114 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
115 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700116 // </ only access in worker thread >
117
118 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800119 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohend22015c2010-07-26 22:02:18 -0700121 private static int mCellCountX;
122 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700123
Reena Lee99a73f32011-10-24 17:27:37 -0700124 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700125
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700127 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400128 public int getCurrentWorkspaceScreen();
129 public void startBinding();
130 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500131 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400132 public void finishBindingItems();
133 public void bindAppWidget(LauncherAppWidgetInfo info);
134 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500135 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
136 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chungcd810732012-06-18 16:45:43 -0700137 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700138 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400139 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800140 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100141 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800145 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400146 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700147 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 mIconCache = iconCache;
149
150 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700151 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400152
Reena Lee93f824a2011-09-23 17:20:28 -0700153 final Resources res = app.getResources();
154 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
155 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700156 Configuration config = res.getConfiguration();
157 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800158 }
159
Joe Onorato56d82912010-03-07 14:32:10 -0500160 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800161 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chung603bcb92011-09-02 11:45:39 -0700164 public void unbindWorkspaceItems() {
165 sWorker.post(new Runnable() {
166 @Override
167 public void run() {
168 unbindWorkspaceItemsOnMainThread();
169 }
170 });
171 }
172
173 /** Unbinds all the sWorkspaceItems on the main thread, and return a copy of sWorkspaceItems
174 * that is save to reference from the main thread. */
175 private ArrayList<ItemInfo> unbindWorkspaceItemsOnMainThread() {
176 // Ensure that we don't use the same workspace items data structure on the main thread
177 // by making a copy of workspace items first.
Adam Cohen487f7dd2012-06-28 18:12:10 -0700178 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(sBgWorkspaceItems);
179 final ArrayList<ItemInfo> appWidgets = new ArrayList<ItemInfo>(sBgAppWidgets);
Winson Chung603bcb92011-09-02 11:45:39 -0700180 mHandler.post(new Runnable() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700181 @Override
Winson Chung603bcb92011-09-02 11:45:39 -0700182 public void run() {
183 for (ItemInfo item : workspaceItems) {
184 item.unbind();
185 }
Michael Jurka05bf644e2011-11-30 20:28:53 -0800186 for (ItemInfo item : appWidgets) {
187 item.unbind();
188 }
Winson Chung603bcb92011-09-02 11:45:39 -0700189 }
190 });
191
192 return workspaceItems;
Adam Cohen4eac29a2011-07-11 17:53:37 -0700193 }
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 /**
196 * Adds an item to the DB if it was not created previously, or move it to a new
197 * <container, screen, cellX, cellY>
198 */
199 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
200 int screen, int cellX, int cellY) {
201 if (item.container == ItemInfo.NO_ID) {
202 // From all apps
203 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
204 } else {
205 // From somewhere else
206 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 }
208 }
209
Michael Jurka816474f2012-06-25 14:49:02 -0700210 static void checkItemInfo(final ItemInfo item) {
211 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
212 final long itemId = item.id;
213 Runnable r = new Runnable() {
214 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700215 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Michael Jurka087f7a62012-06-27 11:48:37 -0700216 if (modelItem != null && item != modelItem) {
Michael Jurka816474f2012-06-25 14:49:02 -0700217 // the modelItem needs to match up perfectly with item if our model is to be
218 // consistent with the database-- for now, just require modelItem == item
219 String msg = "item: " + ((item != null) ? item.toString() : "null") +
220 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
221 "Error: ItemInfo passed to checkItemInfo doesn't match original";
222 RuntimeException e = new RuntimeException(msg);
223 e.setStackTrace(stackTrace);
224 throw e;
225 }
226 }
227 };
228
229 if (sWorkerThread.getThreadId() == Process.myTid()) {
230 r.run();
231 } else {
232 sWorker.post(r);
233 }
234
235 }
236
Michael Jurkac9d95c52011-08-29 14:03:34 -0700237 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
238 final ItemInfo item, final String callingFunction) {
239 final long itemId = item.id;
240 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
241 final ContentResolver cr = context.getContentResolver();
242
Adam Cohen487f7dd2012-06-28 18:12:10 -0700243 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700244 Runnable r = new Runnable() {
245 public void run() {
246 cr.update(uri, values, null, null);
247
Adam Cohen487f7dd2012-06-28 18:12:10 -0700248 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700249 if (item != modelItem) {
250 // the modelItem needs to match up perfectly with item if our model is to be
251 // consistent with the database-- for now, just require modelItem == item
252 String msg = "item: " + ((item != null) ? item.toString() : "null") +
253 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
254 "Error: ItemInfo passed to " + callingFunction + " doesn't match original";
255 throw new RuntimeException(msg);
256 }
257
Adam Cohen487f7dd2012-06-28 18:12:10 -0700258 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
259 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
260 // Item is in a folder, make sure this folder exists
261 if (!sBgFolders.containsKey(item.container)) {
262 // An items container is being set to a that of an item which is not in the
263 // list of Folders.
264 String msg = "item: " + item + " container being set to: " +
265 item.container + ", not in the list of folders";
266 RuntimeException e = new RuntimeException(msg);
267 e.setStackTrace(stackTrace);
268 Launcher.dumpDebugLogsToConsole();
269 throw e;
270 }
271 }
272
Michael Jurkac9d95c52011-08-29 14:03:34 -0700273 // Items are added/removed from the corresponding FolderInfo elsewhere, such
274 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
275 // that are on the desktop, as appropriate
276 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
277 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700278 if (!sBgWorkspaceItems.contains(modelItem)) {
279 sBgWorkspaceItems.add(modelItem);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700280 }
281 } else {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700282 sBgWorkspaceItems.remove(modelItem);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700283 }
284 }
285 };
286
287 if (sWorkerThread.getThreadId() == Process.myTid()) {
288 r.run();
289 } else {
290 sWorker.post(r);
291 }
292 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700296 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700297 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
298 final int screen, final int cellX, final int cellY) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700299 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
300 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
301 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
302 Launcher.sDumpLogs.add(transaction);
303 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 item.cellX = cellX;
306 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700307
Winson Chung3d503fb2011-07-13 17:25:49 -0700308 // We store hotseat items in canonical form which is this orientation invariant position
309 // in the hotseat
310 if (context instanceof Launcher && screen < 0 &&
311 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
312 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
313 } else {
314 item.screen = screen;
315 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316
317 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400318 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700319 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
320 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
322
Michael Jurkac9d95c52011-08-29 14:03:34 -0700323 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700324 }
325
326 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700327 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800328 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700329 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
330 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700331 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
332 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY +
333 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")";
334 Launcher.sDumpLogs.add(transaction);
335 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800336 item.cellX = cellX;
337 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700338 item.spanX = spanX;
339 item.spanY = spanY;
340
341 // We store hotseat items in canonical form which is this orientation invariant position
342 // in the hotseat
343 if (context instanceof Launcher && screen < 0 &&
344 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
345 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
346 } else {
347 item.screen = screen;
348 }
Adam Cohend4844c32011-02-18 19:25:06 -0800349
Adam Cohend4844c32011-02-18 19:25:06 -0800350 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800351 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700352 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
353 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
354 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
355 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
356 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800357
Michael Jurka816474f2012-06-25 14:49:02 -0700358 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700359 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700360
361 /**
362 * Update an item to the database in a specified container.
363 */
364 static void updateItemInDatabase(Context context, final ItemInfo item) {
365 final ContentValues values = new ContentValues();
366 item.onAddToDatabase(values);
367 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
368 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800369 }
370
371 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 * Returns true if the shortcuts already exists in the database.
373 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 static boolean shortcutExists(Context context, String title, Intent intent) {
376 final ContentResolver cr = context.getContentResolver();
377 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
378 new String[] { "title", "intent" }, "title=? and intent=?",
379 new String[] { title, intent.toUri(0) }, null);
380 boolean result = false;
381 try {
382 result = c.moveToFirst();
383 } finally {
384 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700387 }
388
Joe Onorato9c1289c2009-08-17 11:03:03 -0400389 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700390 * Returns an ItemInfo array containing all the items in the LauncherModel.
391 * The ItemInfo.id is not set through this function.
392 */
393 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
394 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
395 final ContentResolver cr = context.getContentResolver();
396 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
397 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
398 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
399 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
400
401 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
402 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
403 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
404 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
405 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
406 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
407 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
408
409 try {
410 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700411 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700412 item.cellX = c.getInt(cellXIndex);
413 item.cellY = c.getInt(cellYIndex);
414 item.spanX = c.getInt(spanXIndex);
415 item.spanY = c.getInt(spanYIndex);
416 item.container = c.getInt(containerIndex);
417 item.itemType = c.getInt(itemTypeIndex);
418 item.screen = c.getInt(screenIndex);
419
420 items.add(item);
421 }
422 } catch (Exception e) {
423 items.clear();
424 } finally {
425 c.close();
426 }
427
428 return items;
429 }
430
431 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
433 */
434 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
435 final ContentResolver cr = context.getContentResolver();
436 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
437 "_id=? and (itemType=? or itemType=?)",
438 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700439 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700440
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 try {
442 if (c.moveToFirst()) {
443 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
444 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
445 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
446 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
447 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
448 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450 FolderInfo folderInfo = null;
451 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700452 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
453 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400454 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700455 }
456
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 folderInfo.title = c.getString(titleIndex);
458 folderInfo.id = id;
459 folderInfo.container = c.getInt(containerIndex);
460 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700461 folderInfo.cellX = c.getInt(cellXIndex);
462 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400463
464 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700465 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400466 } finally {
467 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700468 }
469
470 return null;
471 }
472
Joe Onorato9c1289c2009-08-17 11:03:03 -0400473 /**
474 * Add an item to the database in a specified container. Sets the container, screen, cellX and
475 * cellY fields of the item. Also assigns an ID to the item.
476 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700477 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
478 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400479 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400480 item.cellX = cellX;
481 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700482 // We store hotseat items in canonical form which is this orientation invariant position
483 // in the hotseat
484 if (context instanceof Launcher && screen < 0 &&
485 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
486 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
487 } else {
488 item.screen = screen;
489 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400490
491 final ContentValues values = new ContentValues();
492 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400493 item.onAddToDatabase(values);
494
Michael Jurka7578ec62011-08-03 14:11:54 -0700495 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700496 item.id = app.getLauncherProvider().generateNewId();
497 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700498 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700499
Adam Cohen487f7dd2012-06-28 18:12:10 -0700500 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
501
Michael Jurkac9d95c52011-08-29 14:03:34 -0700502 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700503 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700504 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
505 + item.id + " (" + container + ", " + screen + ", " + cellX + ", "
506 + cellY + ")";
507 Launcher.sDumpLogs.add(transaction);
508 Log.d(TAG, transaction);
509
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700510 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
511 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512
Adam Cohen487f7dd2012-06-28 18:12:10 -0700513 if (sBgItemsIdMap.containsKey(item.id)) {
Winson Chungb1094bd2011-08-24 16:14:08 -0700514 // we should not be adding new items in the db with the same id
515 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
516 "addItemToDatabase already exists." + item.toString());
517 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700518 sBgItemsIdMap.put(item.id, item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700519 switch (item.itemType) {
520 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Adam Cohen487f7dd2012-06-28 18:12:10 -0700521 sBgFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700522 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700523 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
524 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700525 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
526 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700527 sBgWorkspaceItems.add(item);
528 } else {
529 if (!sBgFolders.containsKey(item.container)) {
530 // Adding an item to a folder that doesn't exist.
531 String msg = "adding item: " + item + " to a folder that " +
532 " doesn't exist";
533 RuntimeException e = new RuntimeException(msg);
534 e.setStackTrace(stackTrace);
535 Launcher.dumpDebugLogsToConsole();
536 throw e;
537 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700538 }
539 break;
540 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen487f7dd2012-06-28 18:12:10 -0700541 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700542 break;
543 }
544 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700545 };
546
547 if (sWorkerThread.getThreadId() == Process.myTid()) {
548 r.run();
549 } else {
550 sWorker.post(r);
551 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700552 }
553
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700555 * Creates a new unique child id, for a given cell span across all layouts.
556 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700557 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700558 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
559 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700560 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700561 }
562
Adam Cohend22015c2010-07-26 22:02:18 -0700563 static int getCellCountX() {
564 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700565 }
566
Adam Cohend22015c2010-07-26 22:02:18 -0700567 static int getCellCountY() {
568 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700569 }
570
571 /**
572 * Updates the model orientation helper to take into account the current layout dimensions
573 * when performing local/canonical coordinate transformations.
574 */
575 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700576 mCellCountX = shortAxisCellCount;
577 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700578 }
579
580 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700581 * Removes the specified item from the database
582 * @param context
583 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400584 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700585 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400586 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700587 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700588 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
589
Michael Jurka83df1882011-08-31 20:59:26 -0700590 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700591 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700592 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
593 + item.id + " (" + item.container + ", " + item.screen + ", " + item.cellX +
594 ", " + item.cellY + ")";
595 Launcher.sDumpLogs.add(transaction);
596 Log.d(TAG, transaction);
597
Michael Jurkac9d95c52011-08-29 14:03:34 -0700598 cr.delete(uriToDelete, null, null);
599 switch (item.itemType) {
600 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Adam Cohen487f7dd2012-06-28 18:12:10 -0700601 sBgFolders.remove(item.id);
602 for (ItemInfo info: sBgItemsIdMap.values()) {
603 if (info.container == item.id) {
604 // We are deleting a folder which still contains items that
605 // think they are contained by that folder.
606 String msg = "deleting a folder (" + item + ") which still " +
607 "contains items (" + info + ")";
608 RuntimeException e = new RuntimeException(msg);
609 e.setStackTrace(stackTrace);
610 Launcher.dumpDebugLogsToConsole();
611 throw e;
612 }
613 }
614 sBgWorkspaceItems.remove(item);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700615 break;
616 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
617 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen487f7dd2012-06-28 18:12:10 -0700618 sBgWorkspaceItems.remove(item);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700619 break;
620 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen487f7dd2012-06-28 18:12:10 -0700621 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700622 break;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700623 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700624 sBgItemsIdMap.remove(item.id);
625 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700626 }
Michael Jurka83df1882011-08-31 20:59:26 -0700627 };
628 if (sWorkerThread.getThreadId() == Process.myTid()) {
629 r.run();
630 } else {
631 sWorker.post(r);
632 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 }
634
635 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 * Remove the contents of the specified folder from the database
637 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700638 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 final ContentResolver cr = context.getContentResolver();
640
Michael Jurkac9d95c52011-08-29 14:03:34 -0700641 Runnable r = new Runnable() {
642 public void run() {
643 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700644 sBgItemsIdMap.remove(info.id);
645 sBgFolders.remove(info.id);
646 sBgDbIconCache.remove(info);
647 sBgWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700648
Michael Jurkac9d95c52011-08-29 14:03:34 -0700649 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
650 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
651 for (ItemInfo childInfo : info.contents) {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700652 sBgItemsIdMap.remove(childInfo.id);
653 sBgDbIconCache.remove(childInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700654 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700655 }
656 };
657 if (sWorkerThread.getThreadId() == Process.myTid()) {
658 r.run();
659 } else {
660 sWorker.post(r);
661 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662 }
663
664 /**
665 * Set this as the current Launcher activity object for the loader.
666 */
667 public void initialize(Callbacks callbacks) {
668 synchronized (mLock) {
669 mCallbacks = new WeakReference<Callbacks>(callbacks);
670 }
671 }
672
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700673 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
675 * ACTION_PACKAGE_CHANGED.
676 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100677 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400678 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400679 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700680
Joe Onorato36115782010-06-17 13:28:48 -0400681 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400682
Joe Onorato36115782010-06-17 13:28:48 -0400683 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
684 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
685 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
686 final String packageName = intent.getData().getSchemeSpecificPart();
687 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400688
Joe Onorato36115782010-06-17 13:28:48 -0400689 int op = PackageUpdatedTask.OP_NONE;
690
691 if (packageName == null || packageName.length() == 0) {
692 // they sent us a bad intent
693 return;
694 }
695
696 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
697 op = PackageUpdatedTask.OP_UPDATE;
698 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
699 if (!replacing) {
700 op = PackageUpdatedTask.OP_REMOVE;
701 }
702 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
703 // later, we will update the package at this time
704 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
705 if (!replacing) {
706 op = PackageUpdatedTask.OP_ADD;
707 } else {
708 op = PackageUpdatedTask.OP_UPDATE;
709 }
710 }
711
712 if (op != PackageUpdatedTask.OP_NONE) {
713 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
714 }
715
716 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400717 // First, schedule to add these apps back in.
718 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
719 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
720 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700721 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400722 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
723 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
724 enqueuePackageUpdated(new PackageUpdatedTask(
725 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700726 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700727 // If we have changed locale we need to clear out the labels in all apps/workspace.
728 forceReload();
729 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
730 // Check if configuration change was an mcc/mnc change which would affect app resources
731 // and we would need to clear out the labels in all apps/workspace. Same handling as
732 // above for ACTION_LOCALE_CHANGED
733 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700734 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700735 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700736 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700737 forceReload();
738 }
739 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700740 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700741 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
742 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700743 if (mCallbacks != null) {
744 Callbacks callbacks = mCallbacks.get();
745 if (callbacks != null) {
746 callbacks.bindSearchablesChanged();
747 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700748 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700749 }
750 }
751
Reena Lee93f824a2011-09-23 17:20:28 -0700752 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700753 resetLoadedState(true, true);
754
Reena Lee93f824a2011-09-23 17:20:28 -0700755 // Do this here because if the launcher activity is running it will be restarted.
756 // If it's not running startLoaderFromBackground will merely tell it that it needs
757 // to reload.
758 startLoaderFromBackground();
759 }
760
Winson Chungf0c6ae02012-03-21 16:10:31 -0700761 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
762 synchronized (mLock) {
763 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
764 // mWorkspaceLoaded to true later
765 stopLoaderLocked();
766 if (resetAllAppsLoaded) mAllAppsLoaded = false;
767 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
768 }
769 }
770
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700771 /**
772 * When the launcher is in the background, it's possible for it to miss paired
773 * configuration changes. So whenever we trigger the loader from the background
774 * tell the launcher that it needs to re-run the loader when it comes back instead
775 * of doing it now.
776 */
777 public void startLoaderFromBackground() {
778 boolean runLoader = false;
779 if (mCallbacks != null) {
780 Callbacks callbacks = mCallbacks.get();
781 if (callbacks != null) {
782 // Only actually run the loader if they're not paused.
783 if (!callbacks.setLoadOnResume()) {
784 runLoader = true;
785 }
786 }
787 }
788 if (runLoader) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700789 startLoader(false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700790 }
Joe Onorato36115782010-06-17 13:28:48 -0400791 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400792
Reena Lee93f824a2011-09-23 17:20:28 -0700793 // If there is already a loader task running, tell it to stop.
794 // returns true if isLaunching() was true on the old task
795 private boolean stopLoaderLocked() {
796 boolean isLaunching = false;
797 LoaderTask oldTask = mLoaderTask;
798 if (oldTask != null) {
799 if (oldTask.isLaunching()) {
800 isLaunching = true;
801 }
802 oldTask.stopLocked();
803 }
804 return isLaunching;
805 }
806
Winson Chungf0c6ae02012-03-21 16:10:31 -0700807 public void startLoader(boolean isLaunching) {
Joe Onorato36115782010-06-17 13:28:48 -0400808 synchronized (mLock) {
809 if (DEBUG_LOADERS) {
810 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
811 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812
Joe Onorato36115782010-06-17 13:28:48 -0400813 // Don't bother to start the thread if we know it's not going to do anything
814 if (mCallbacks != null && mCallbacks.get() != null) {
815 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700816 // also, don't downgrade isLaunching if we're already running
817 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700818 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chung7ed37742011-09-08 15:45:51 -0700819 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700820 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822 }
823 }
824
Joe Onorato36115782010-06-17 13:28:48 -0400825 public void stopLoader() {
826 synchronized (mLock) {
827 if (mLoaderTask != null) {
828 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 }
830 }
Joe Onorato36115782010-06-17 13:28:48 -0400831 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832
Michael Jurkac57b7a82011-08-09 22:02:20 -0700833 public boolean isAllAppsLoaded() {
834 return mAllAppsLoaded;
835 }
836
Winson Chung36a62fe2012-05-06 18:04:42 -0700837 boolean isLoadingWorkspace() {
838 synchronized (mLock) {
839 if (mLoaderTask != null) {
840 return mLoaderTask.isLoadingWorkspace();
841 }
842 }
843 return false;
844 }
845
Joe Onorato36115782010-06-17 13:28:48 -0400846 /**
847 * Runnable for the thread that loads the contents of the launcher:
848 * - workspace icons
849 * - widgets
850 * - all apps icons
851 */
852 private class LoaderTask implements Runnable {
853 private Context mContext;
854 private Thread mWaitThread;
855 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700856 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400857 private boolean mStopped;
858 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700859 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400860
861 LoaderTask(Context context, boolean isLaunching) {
862 mContext = context;
863 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700864 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865 }
866
Joe Onorato36115782010-06-17 13:28:48 -0400867 boolean isLaunching() {
868 return mIsLaunching;
869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870
Winson Chung36a62fe2012-05-06 18:04:42 -0700871 boolean isLoadingWorkspace() {
872 return mIsLoadingAndBindingWorkspace;
873 }
874
Joe Onorato36115782010-06-17 13:28:48 -0400875 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700876 mIsLoadingAndBindingWorkspace = true;
877
Joe Onorato36115782010-06-17 13:28:48 -0400878 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400879 if (DEBUG_LOADERS) {
880 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700882
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700883 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400884 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700885 synchronized (LoaderTask.this) {
886 if (mStopped) {
887 return;
888 }
889 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890 }
891 }
892
Joe Onorato36115782010-06-17 13:28:48 -0400893 // Bind the workspace
894 bindWorkspace();
895 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400896
Joe Onorato36115782010-06-17 13:28:48 -0400897 private void waitForIdle() {
898 // Wait until the either we're stopped or the other threads are done.
899 // This way we don't start loading all apps until the workspace has settled
900 // down.
901 synchronized (LoaderTask.this) {
902 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700903
Joe Onorato36115782010-06-17 13:28:48 -0400904 mHandler.postIdle(new Runnable() {
905 public void run() {
906 synchronized (LoaderTask.this) {
907 mLoadAndBindStepFinished = true;
908 if (DEBUG_LOADERS) {
909 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400910 }
Joe Onorato36115782010-06-17 13:28:48 -0400911 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400912 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400913 }
Joe Onorato36115782010-06-17 13:28:48 -0400914 });
915
916 while (!mStopped && !mLoadAndBindStepFinished) {
917 try {
918 this.wait();
919 } catch (InterruptedException ex) {
920 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400921 }
922 }
Joe Onorato36115782010-06-17 13:28:48 -0400923 if (DEBUG_LOADERS) {
924 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700925 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400926 + "ms for previous step to finish binding");
927 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400928 }
Joe Onorato36115782010-06-17 13:28:48 -0400929 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400930
Joe Onorato36115782010-06-17 13:28:48 -0400931 public void run() {
932 // Optimize for end-user experience: if the Launcher is up and // running with the
933 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
934 // workspace first (default).
935 final Callbacks cbk = mCallbacks.get();
936 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400937
Joe Onorato36115782010-06-17 13:28:48 -0400938 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400939 // Elevate priority when Home launches for the first time to avoid
940 // starving at boot time. Staring at a blank home is not cool.
941 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700942 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
943 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400944 android.os.Process.setThreadPriority(mIsLaunching
945 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
946 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400947 if (loadWorkspaceFirst) {
948 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
949 loadAndBindWorkspace();
950 } else {
951 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700952 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400953 }
954
Joe Onorato36115782010-06-17 13:28:48 -0400955 if (mStopped) {
956 break keep_running;
957 }
958
959 // Whew! Hard work done. Slow us down, and wait until the UI thread has
960 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400961 synchronized (mLock) {
962 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700963 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400964 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
965 }
966 }
Joe Onorato36115782010-06-17 13:28:48 -0400967 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400968
969 // second step
970 if (loadWorkspaceFirst) {
971 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700972 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400973 } else {
974 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
975 loadAndBindWorkspace();
976 }
Winson Chung7ed37742011-09-08 15:45:51 -0700977
978 // Restore the default thread priority after we are done loading items
979 synchronized (mLock) {
980 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
981 }
Joe Onorato36115782010-06-17 13:28:48 -0400982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983
Winson Chungaac01e12011-08-17 10:37:13 -0700984
985 // Update the saved icons if necessary
986 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Adam Cohen487f7dd2012-06-28 18:12:10 -0700987 for (Object key : sBgDbIconCache.keySet()) {
988 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700989 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700990 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700991
Joe Onorato36115782010-06-17 13:28:48 -0400992 // Clear out this reference, otherwise we end up holding it until all of the
993 // callback runnables are done.
994 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995
Joe Onorato36115782010-06-17 13:28:48 -0400996 synchronized (mLock) {
997 // If we are still the last one to be scheduled, remove ourselves.
998 if (mLoaderTask == this) {
999 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 }
Joe Onorato36115782010-06-17 13:28:48 -04001001 }
Joe Onorato36115782010-06-17 13:28:48 -04001002 }
1003
1004 public void stopLocked() {
1005 synchronized (LoaderTask.this) {
1006 mStopped = true;
1007 this.notify();
1008 }
1009 }
1010
1011 /**
1012 * Gets the callbacks object. If we've been stopped, or if the launcher object
1013 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1014 * object that was around when the deferred message was scheduled, and if there's
1015 * a new Callbacks object around then also return null. This will save us from
1016 * calling onto it with data that will be ignored.
1017 */
1018 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1019 synchronized (mLock) {
1020 if (mStopped) {
1021 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001022 }
Joe Onorato36115782010-06-17 13:28:48 -04001023
1024 if (mCallbacks == null) {
1025 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001026 }
Joe Onorato36115782010-06-17 13:28:48 -04001027
1028 final Callbacks callbacks = mCallbacks.get();
1029 if (callbacks != oldCallbacks) {
1030 return null;
1031 }
1032 if (callbacks == null) {
1033 Log.w(TAG, "no mCallbacks");
1034 return null;
1035 }
1036
1037 return callbacks;
1038 }
1039 }
1040
1041 // check & update map of what's occupied; used to discard overlapping/invalid items
1042 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001043 int containerIndex = item.screen;
1044 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001045 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08001046 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001047 return false;
1048 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001049
1050 // We use the last index to refer to the hotseat and the screen as the rank, so
1051 // test and update the occupied state accordingly
1052 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
1053 Log.e(TAG, "Error loading shortcut into hotseat " + item
1054 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
1055 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
1056 return false;
1057 } else {
1058 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
1059 return true;
1060 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001061 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1062 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001063 return true;
1064 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001065
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001066 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001067 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1068 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001069 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001070 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -07001071 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001072 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001073 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -07001074 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001075 return false;
1076 }
1077 }
1078 }
1079 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1080 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001081 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001082 }
1083 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001084
Joe Onorato36115782010-06-17 13:28:48 -04001085 return true;
1086 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087
Joe Onorato36115782010-06-17 13:28:48 -04001088 private void loadWorkspace() {
1089 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090
Joe Onorato36115782010-06-17 13:28:48 -04001091 final Context context = mContext;
1092 final ContentResolver contentResolver = context.getContentResolver();
1093 final PackageManager manager = context.getPackageManager();
1094 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1095 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001096
Michael Jurkab85f8a42012-04-25 15:48:32 -07001097 // Make sure the default workspace is loaded, if needed
1098 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary();
1099
Adam Cohen487f7dd2012-06-28 18:12:10 -07001100 sBgWorkspaceItems.clear();
1101 sBgAppWidgets.clear();
1102 sBgFolders.clear();
1103 sBgItemsIdMap.clear();
1104 sBgDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001105
Joe Onorato36115782010-06-17 13:28:48 -04001106 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107
Joe Onorato36115782010-06-17 13:28:48 -04001108 final Cursor c = contentResolver.query(
Winson Chunge61e93e2011-09-12 10:59:49 -07001109 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001110
Winson Chungf30ad5f2011-08-08 10:55:42 -07001111 // +1 for the hotseat (it can be larger than the workspace)
Winson Chung36f97362011-09-07 11:20:10 -07001112 // Load workspace in reverse order to ensure that latest items are loaded first (and
1113 // before any earlier duplicates)
Adam Cohend22015c2010-07-26 22:02:18 -07001114 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -07001115 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001116
Joe Onorato36115782010-06-17 13:28:48 -04001117 try {
1118 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1119 final int intentIndex = c.getColumnIndexOrThrow
1120 (LauncherSettings.Favorites.INTENT);
1121 final int titleIndex = c.getColumnIndexOrThrow
1122 (LauncherSettings.Favorites.TITLE);
1123 final int iconTypeIndex = c.getColumnIndexOrThrow(
1124 LauncherSettings.Favorites.ICON_TYPE);
1125 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1126 final int iconPackageIndex = c.getColumnIndexOrThrow(
1127 LauncherSettings.Favorites.ICON_PACKAGE);
1128 final int iconResourceIndex = c.getColumnIndexOrThrow(
1129 LauncherSettings.Favorites.ICON_RESOURCE);
1130 final int containerIndex = c.getColumnIndexOrThrow(
1131 LauncherSettings.Favorites.CONTAINER);
1132 final int itemTypeIndex = c.getColumnIndexOrThrow(
1133 LauncherSettings.Favorites.ITEM_TYPE);
1134 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1135 LauncherSettings.Favorites.APPWIDGET_ID);
1136 final int screenIndex = c.getColumnIndexOrThrow(
1137 LauncherSettings.Favorites.SCREEN);
1138 final int cellXIndex = c.getColumnIndexOrThrow
1139 (LauncherSettings.Favorites.CELLX);
1140 final int cellYIndex = c.getColumnIndexOrThrow
1141 (LauncherSettings.Favorites.CELLY);
1142 final int spanXIndex = c.getColumnIndexOrThrow
1143 (LauncherSettings.Favorites.SPANX);
1144 final int spanYIndex = c.getColumnIndexOrThrow(
1145 LauncherSettings.Favorites.SPANY);
Michael Jurka3a9fced2012-04-13 14:44:29 -07001146 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1147 //final int displayModeIndex = c.getColumnIndexOrThrow(
1148 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001149
Joe Onorato36115782010-06-17 13:28:48 -04001150 ShortcutInfo info;
1151 String intentDescription;
1152 LauncherAppWidgetInfo appWidgetInfo;
1153 int container;
1154 long id;
1155 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156
Joe Onorato36115782010-06-17 13:28:48 -04001157 while (!mStopped && c.moveToNext()) {
1158 try {
1159 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001160
Joe Onorato36115782010-06-17 13:28:48 -04001161 switch (itemType) {
1162 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1163 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1164 intentDescription = c.getString(intentIndex);
1165 try {
1166 intent = Intent.parseUri(intentDescription, 0);
1167 } catch (URISyntaxException e) {
1168 continue;
1169 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170
Joe Onorato36115782010-06-17 13:28:48 -04001171 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1172 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -07001173 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001174 } else {
1175 info = getShortcutInfo(c, context, iconTypeIndex,
1176 iconPackageIndex, iconResourceIndex, iconIndex,
1177 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001178
1179 // App shortcuts that used to be automatically added to Launcher
1180 // didn't always have the correct intent flags set, so do that here
Michael Jurka9ad00562012-05-14 12:24:22 -07001181 if (intent.getAction() != null &&
1182 intent.getCategories() != null &&
1183 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001184 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
1185 intent.addFlags(
1186 Intent.FLAG_ACTIVITY_NEW_TASK |
1187 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1188 }
Joe Onorato36115782010-06-17 13:28:48 -04001189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001190
Joe Onorato36115782010-06-17 13:28:48 -04001191 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001192 info.intent = intent;
1193 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001195 info.container = container;
1196 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001197 info.cellX = c.getInt(cellXIndex);
1198 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199
Daniel Sandler8802e962010-05-26 16:28:16 -04001200 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001201 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001202 break;
1203 }
1204
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001206 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001207 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen487f7dd2012-06-28 18:12:10 -07001208 sBgWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001209 break;
1210 default:
1211 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001212 FolderInfo folderInfo =
Adam Cohen487f7dd2012-06-28 18:12:10 -07001213 findOrMakeFolder(sBgFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001214 folderInfo.add(info);
1215 break;
1216 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001217 sBgItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001218
1219 // now that we've loaded everthing re-save it with the
1220 // icon in case it disappears somehow.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001221 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001222 } else {
1223 // Failed to load the shortcut, probably because the
1224 // activity manager couldn't resolve it (maybe the app
1225 // was uninstalled), or the db row was somehow screwed up.
1226 // Delete it.
1227 id = c.getLong(idIndex);
1228 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1229 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1230 id, false), null, null);
1231 }
1232 break;
1233
Adam Cohendf2cc412011-04-27 16:56:57 -07001234 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001235 id = c.getLong(idIndex);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001236 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001237
Winson Chungaafa03c2010-06-11 17:34:16 -07001238 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001239 folderInfo.id = id;
1240 container = c.getInt(containerIndex);
1241 folderInfo.container = container;
1242 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001243 folderInfo.cellX = c.getInt(cellXIndex);
1244 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001245
1246 // check & update map of what's occupied
1247 if (!checkItemPlacement(occupied, folderInfo)) {
1248 break;
1249 }
Joe Onorato36115782010-06-17 13:28:48 -04001250 switch (container) {
1251 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001252 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen487f7dd2012-06-28 18:12:10 -07001253 sBgWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001254 break;
1255 }
1256
Adam Cohen487f7dd2012-06-28 18:12:10 -07001257 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1258 sBgFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001259 break;
1260
Joe Onorato36115782010-06-17 13:28:48 -04001261 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1262 // Read all Launcher-specific widget details
1263 int appWidgetId = c.getInt(appWidgetIdIndex);
1264 id = c.getLong(idIndex);
1265
1266 final AppWidgetProviderInfo provider =
1267 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001268
Joe Onorato36115782010-06-17 13:28:48 -04001269 if (!isSafeMode && (provider == null || provider.provider == null ||
1270 provider.provider.getPackageName() == null)) {
Adam Cohen16d7ffc2011-10-05 17:49:14 -07001271 String log = "Deleting widget that isn't installed anymore: id="
1272 + id + " appWidgetId=" + appWidgetId;
1273 Log.e(TAG, log);
1274 Launcher.sDumpLogs.add(log);
Joe Onorato36115782010-06-17 13:28:48 -04001275 itemsToRemove.add(id);
1276 } else {
Winson Chung11a49372012-04-27 15:12:38 -07001277 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1278 provider.provider);
Joe Onorato36115782010-06-17 13:28:48 -04001279 appWidgetInfo.id = id;
1280 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001281 appWidgetInfo.cellX = c.getInt(cellXIndex);
1282 appWidgetInfo.cellY = c.getInt(cellYIndex);
1283 appWidgetInfo.spanX = c.getInt(spanXIndex);
1284 appWidgetInfo.spanY = c.getInt(spanYIndex);
Adam Cohen2f093b62012-04-30 18:59:53 -07001285 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1286 appWidgetInfo.minSpanX = minSpan[0];
1287 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001288
1289 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001290 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1291 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001292 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001293 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001294 continue;
1295 }
1296 appWidgetInfo.container = c.getInt(containerIndex);
1297
1298 // check & update map of what's occupied
1299 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1300 break;
1301 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001302 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1303 sBgAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001304 }
1305 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001306 }
Joe Onorato36115782010-06-17 13:28:48 -04001307 } catch (Exception e) {
1308 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001309 }
1310 }
Joe Onorato36115782010-06-17 13:28:48 -04001311 } finally {
1312 c.close();
1313 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001314
Joe Onorato36115782010-06-17 13:28:48 -04001315 if (itemsToRemove.size() > 0) {
1316 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1317 LauncherSettings.Favorites.CONTENT_URI);
1318 // Remove dead items
1319 for (long id : itemsToRemove) {
1320 if (DEBUG_LOADERS) {
1321 Log.d(TAG, "Removed id = " + id);
1322 }
1323 // Don't notify content observers
1324 try {
1325 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1326 null, null);
1327 } catch (RemoteException e) {
1328 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001329 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331 }
1332
Joe Onorato36115782010-06-17 13:28:48 -04001333 if (DEBUG_LOADERS) {
1334 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1335 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001336 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001337 String line = "";
1338 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1339 if (s > 0) {
1340 line += " | ";
1341 }
Adam Cohend22015c2010-07-26 22:02:18 -07001342 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001343 line += ((occupied[s][x][y] != null) ? "#" : ".");
1344 }
1345 }
1346 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347 }
Joe Onorato36115782010-06-17 13:28:48 -04001348 }
1349 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001350
Joe Onorato36115782010-06-17 13:28:48 -04001351 /**
1352 * Read everything out of our database.
1353 */
1354 private void bindWorkspace() {
1355 final long t = SystemClock.uptimeMillis();
1356
1357 // Don't use these two variables in any of the callback runnables.
1358 // Otherwise we hold a reference to them.
1359 final Callbacks oldCallbacks = mCallbacks.get();
1360 if (oldCallbacks == null) {
1361 // This launcher has exited and nobody bothered to tell us. Just bail.
1362 Log.w(TAG, "LoaderTask running with no launcher");
1363 return;
1364 }
1365
Winson Chungdb8a8942012-04-03 14:08:41 -07001366 // Get the list of workspace items to load and unbind the existing ShortcutInfos
1367 // before we call startBinding() below.
1368 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1369 final ArrayList<ItemInfo> tmpWorkspaceItems = unbindWorkspaceItemsOnMainThread();
1370 // Order the items for loading as follows: current workspace, hotseat, everything else
1371 Collections.sort(tmpWorkspaceItems, new Comparator<ItemInfo>() {
1372 @Override
1373 public int compare(ItemInfo lhs, ItemInfo rhs) {
1374 int cellCountX = LauncherModel.getCellCountX();
1375 int cellCountY = LauncherModel.getCellCountY();
1376 int screenOffset = cellCountX * cellCountY;
1377 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1378 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1379 lhs.cellY * cellCountX + lhs.cellX);
1380 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1381 rhs.cellY * cellCountX + rhs.cellX);
1382 return (int) (lr - rr);
1383 }
1384 });
1385 // Precondition: the items are ordered by page, screen
1386 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1387 for (ItemInfo ii : tmpWorkspaceItems) {
1388 // Prepend the current items, hotseat items, append everything else
1389 if (ii.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1390 ii.screen == currentScreen) {
1391 workspaceItems.add(0, ii);
1392 } else if (ii.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1393 workspaceItems.add(0, ii);
1394 } else {
1395 workspaceItems.add(ii);
1396 }
1397 }
1398
Joe Onorato36115782010-06-17 13:28:48 -04001399 // Tell the workspace that we're about to start firing items at it
1400 mHandler.post(new Runnable() {
1401 public void run() {
1402 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1403 if (callbacks != null) {
1404 callbacks.startBinding();
1405 }
1406 }
1407 });
Winson Chung603bcb92011-09-02 11:45:39 -07001408
Joe Onorato36115782010-06-17 13:28:48 -04001409 // Add the items to the workspace.
Winson Chungdb8a8942012-04-03 14:08:41 -07001410 int N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001411 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1412 final int start = i;
1413 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001414 mHandler.post(new Runnable() {
1415 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001416 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001418 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001419 }
1420 }
1421 });
Joe Onorato36115782010-06-17 13:28:48 -04001422 }
Winson Chung603bcb92011-09-02 11:45:39 -07001423 // Ensure that we don't use the same folders data structure on the main thread
Adam Cohen487f7dd2012-06-28 18:12:10 -07001424 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sBgFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001425 mHandler.post(new Runnable() {
1426 public void run() {
1427 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1428 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001429 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001430 }
1431 }
1432 });
1433 // Wait until the queue goes empty.
1434 mHandler.post(new Runnable() {
1435 public void run() {
1436 if (DEBUG_LOADERS) {
1437 Log.d(TAG, "Going to start binding widgets soon.");
1438 }
1439 }
1440 });
1441 // Bind the widgets, one at a time.
1442 // WARNING: this is calling into the workspace from the background thread,
1443 // but since getCurrentScreen() just returns the int, we should be okay. This
1444 // is just a hint for the order, and if it's wrong, we'll be okay.
1445 // TODO: instead, we should have that push the current screen into here.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001446 N = sBgAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001447 // once for the current screen
1448 for (int i=0; i<N; i++) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001449 final LauncherAppWidgetInfo widget = sBgAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001450 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 mHandler.post(new Runnable() {
1452 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001453 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001454 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001455 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001456 }
1457 }
1458 });
1459 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 }
Joe Onorato36115782010-06-17 13:28:48 -04001461 // once for the other screens
1462 for (int i=0; i<N; i++) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001463 final LauncherAppWidgetInfo widget = sBgAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001464 if (widget.screen != currentScreen) {
1465 mHandler.post(new Runnable() {
1466 public void run() {
1467 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1468 if (callbacks != null) {
1469 callbacks.bindAppWidget(widget);
1470 }
1471 }
1472 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001473 }
1474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475 // Tell the workspace that we're done.
1476 mHandler.post(new Runnable() {
1477 public void run() {
1478 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1479 if (callbacks != null) {
1480 callbacks.finishBindingItems();
1481 }
1482 }
1483 });
Winson Chung98e030b2012-05-07 16:01:11 -07001484 // Cleanup
Joe Onorato36115782010-06-17 13:28:48 -04001485 mHandler.post(new Runnable() {
1486 public void run() {
Winson Chung98e030b2012-05-07 16:01:11 -07001487 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001488 if (DEBUG_LOADERS) {
1489 Log.d(TAG, "bound workspace in "
1490 + (SystemClock.uptimeMillis()-t) + "ms");
1491 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001492
1493 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001494 }
1495 });
1496 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001497
Joe Onorato36115782010-06-17 13:28:48 -04001498 private void loadAndBindAllApps() {
1499 if (DEBUG_LOADERS) {
1500 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1501 }
1502 if (!mAllAppsLoaded) {
1503 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001504 synchronized (LoaderTask.this) {
1505 if (mStopped) {
1506 return;
1507 }
1508 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001509 }
Joe Onorato36115782010-06-17 13:28:48 -04001510 } else {
1511 onlyBindAllApps();
1512 }
1513 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001514
Joe Onorato36115782010-06-17 13:28:48 -04001515 private void onlyBindAllApps() {
1516 final Callbacks oldCallbacks = mCallbacks.get();
1517 if (oldCallbacks == null) {
1518 // This launcher has exited and nobody bothered to tell us. Just bail.
1519 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1520 return;
1521 }
1522
1523 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001524 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001525 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07001526 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Joe Onorato36115782010-06-17 13:28:48 -04001527 mHandler.post(new Runnable() {
1528 public void run() {
1529 final long t = SystemClock.uptimeMillis();
1530 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1531 if (callbacks != null) {
1532 callbacks.bindAllApplications(list);
1533 }
1534 if (DEBUG_LOADERS) {
1535 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1536 + (SystemClock.uptimeMillis()-t) + "ms");
1537 }
1538 }
1539 });
1540
1541 }
1542
1543 private void loadAllAppsByBatch() {
1544 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1545
1546 // Don't use these two variables in any of the callback runnables.
1547 // Otherwise we hold a reference to them.
1548 final Callbacks oldCallbacks = mCallbacks.get();
1549 if (oldCallbacks == null) {
1550 // This launcher has exited and nobody bothered to tell us. Just bail.
1551 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1552 return;
1553 }
1554
1555 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1556 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1557
1558 final PackageManager packageManager = mContext.getPackageManager();
1559 List<ResolveInfo> apps = null;
1560
1561 int N = Integer.MAX_VALUE;
1562
1563 int startIndex;
1564 int i=0;
1565 int batchSize = -1;
1566 while (i < N && !mStopped) {
1567 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07001568 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04001569 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1570 apps = packageManager.queryIntentActivities(mainIntent, 0);
1571 if (DEBUG_LOADERS) {
1572 Log.d(TAG, "queryIntentActivities took "
1573 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1574 }
1575 if (apps == null) {
1576 return;
1577 }
1578 N = apps.size();
1579 if (DEBUG_LOADERS) {
1580 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1581 }
1582 if (N == 0) {
1583 // There are no apps?!?
1584 return;
1585 }
1586 if (mBatchSize == 0) {
1587 batchSize = N;
1588 } else {
1589 batchSize = mBatchSize;
1590 }
1591
1592 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1593 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001594 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001595 if (DEBUG_LOADERS) {
1596 Log.d(TAG, "sort took "
1597 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1598 }
1599 }
1600
1601 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1602
1603 startIndex = i;
1604 for (int j=0; i<N && j<batchSize; j++) {
1605 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07001606 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001607 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001608 i++;
1609 }
1610
1611 final boolean first = i <= batchSize;
1612 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001613 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
1614 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001615
Joe Onoratocc67f472010-06-08 10:54:30 -07001616 mHandler.post(new Runnable() {
1617 public void run() {
1618 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001619 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001620 if (first) {
1621 callbacks.bindAllApplications(added);
1622 } else {
1623 callbacks.bindAppsAdded(added);
1624 }
1625 if (DEBUG_LOADERS) {
1626 Log.d(TAG, "bound " + added.size() + " apps in "
1627 + (SystemClock.uptimeMillis() - t) + "ms");
1628 }
1629 } else {
1630 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001631 }
1632 }
1633 });
1634
Daniel Sandlerdca66122010-04-13 16:23:58 -04001635 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001636 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1637 + (SystemClock.uptimeMillis()-t2) + "ms");
1638 }
1639
1640 if (mAllAppsLoadDelay > 0 && i < N) {
1641 try {
1642 if (DEBUG_LOADERS) {
1643 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1644 }
1645 Thread.sleep(mAllAppsLoadDelay);
1646 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001647 }
1648 }
1649
Joe Onorato36115782010-06-17 13:28:48 -04001650 if (DEBUG_LOADERS) {
1651 Log.d(TAG, "cached all " + N + " apps in "
1652 + (SystemClock.uptimeMillis()-t) + "ms"
1653 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001654 }
1655 }
1656
1657 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001658 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1659 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1660 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1661 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1662 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001663 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001664 }
1665 }
1666
1667 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001668 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001669 }
1670
1671 private class PackageUpdatedTask implements Runnable {
1672 int mOp;
1673 String[] mPackages;
1674
1675 public static final int OP_NONE = 0;
1676 public static final int OP_ADD = 1;
1677 public static final int OP_UPDATE = 2;
1678 public static final int OP_REMOVE = 3; // uninstlled
1679 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1680
1681
1682 public PackageUpdatedTask(int op, String[] packages) {
1683 mOp = op;
1684 mPackages = packages;
1685 }
1686
1687 public void run() {
1688 final Context context = mApp;
1689
1690 final String[] packages = mPackages;
1691 final int N = packages.length;
1692 switch (mOp) {
1693 case OP_ADD:
1694 for (int i=0; i<N; i++) {
1695 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001696 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001697 }
1698 break;
1699 case OP_UPDATE:
1700 for (int i=0; i<N; i++) {
1701 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001702 mBgAllAppsList.updatePackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001703 }
1704 break;
1705 case OP_REMOVE:
1706 case OP_UNAVAILABLE:
1707 for (int i=0; i<N; i++) {
1708 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001709 mBgAllAppsList.removePackage(packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04001710 }
1711 break;
1712 }
1713
1714 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04001715 ArrayList<ApplicationInfo> modified = null;
1716
Adam Cohen487f7dd2012-06-28 18:12:10 -07001717 if (mBgAllAppsList.added.size() > 0) {
1718 added = mBgAllAppsList.added;
1719 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001720 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001721 if (mBgAllAppsList.modified.size() > 0) {
1722 modified = mBgAllAppsList.modified;
1723 mBgAllAppsList.modified = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04001724 }
1725
Winson Chungcd810732012-06-18 16:45:43 -07001726 // We may be removing packages that have no associated launcher application, so we
1727 // pass through the removed package names directly.
1728 // NOTE: We flush the icon cache aggressively in removePackage() above.
1729 final ArrayList<String> removedPackageNames = new ArrayList<String>();
1730 for (int i = 0; i < N; ++i) {
1731 removedPackageNames.add(packages[i]);
1732 }
1733
Joe Onorato36115782010-06-17 13:28:48 -04001734 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1735 if (callbacks == null) {
1736 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1737 return;
1738 }
1739
1740 if (added != null) {
1741 final ArrayList<ApplicationInfo> addedFinal = added;
1742 mHandler.post(new Runnable() {
1743 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001744 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1745 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001746 callbacks.bindAppsAdded(addedFinal);
1747 }
1748 }
1749 });
1750 }
1751 if (modified != null) {
1752 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1753 mHandler.post(new Runnable() {
1754 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001755 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1756 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001757 callbacks.bindAppsUpdated(modifiedFinal);
1758 }
1759 }
1760 });
1761 }
Winson Chungcd810732012-06-18 16:45:43 -07001762 if (!removedPackageNames.isEmpty()) {
Joe Onorato36115782010-06-17 13:28:48 -04001763 final boolean permanent = mOp != OP_UNAVAILABLE;
Joe Onorato36115782010-06-17 13:28:48 -04001764 mHandler.post(new Runnable() {
1765 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001766 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1767 if (callbacks == cb && cb != null) {
Winson Chungcd810732012-06-18 16:45:43 -07001768 callbacks.bindAppsRemoved(removedPackageNames, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04001769 }
1770 }
1771 });
Joe Onoratobe386092009-11-17 17:32:16 -08001772 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001773
1774 mHandler.post(new Runnable() {
1775 @Override
1776 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001777 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1778 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001779 callbacks.bindPackagesUpdated();
1780 }
1781 }
1782 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 }
1784 }
1785
1786 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001787 * This is called from the code that adds shortcuts from the intent receiver. This
1788 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 */
Joe Onorato56d82912010-03-07 14:32:10 -05001790 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001791 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001792 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793
Joe Onorato56d82912010-03-07 14:32:10 -05001794 /**
1795 * Make an ShortcutInfo object for a shortcut that is an application.
1796 *
1797 * If c is not null, then it will be used to fill in missing data like the title and icon.
1798 */
1799 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001800 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001801 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001802 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001803
1804 ComponentName componentName = intent.getComponent();
1805 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001806 return null;
1807 }
1808
Adam Cohen00fcb492011-11-02 21:53:47 -07001809 try {
1810 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
1811 if (!pi.applicationInfo.enabled) {
1812 // If we return null here, the corresponding item will be removed from the launcher
1813 // db and will not appear in the workspace.
1814 return null;
1815 }
1816 } catch (NameNotFoundException e) {
1817 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
1818 }
1819
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001820 // TODO: See if the PackageManager knows about this case. If it doesn't
1821 // then return null & delete this.
1822
Joe Onorato56d82912010-03-07 14:32:10 -05001823 // the resource -- This may implicitly give us back the fallback icon,
1824 // but don't worry about that. All we're doing with usingFallbackIcon is
1825 // to avoid saving lots of copies of that in the database, and most apps
1826 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07001827
1828 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
1829 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
1830 // via resolveActivity().
1831 ResolveInfo resolveInfo = null;
1832 ComponentName oldComponent = intent.getComponent();
1833 Intent newIntent = new Intent(intent.getAction(), null);
1834 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1835 newIntent.setPackage(oldComponent.getPackageName());
1836 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
1837 for (ResolveInfo i : infos) {
1838 ComponentName cn = new ComponentName(i.activityInfo.packageName,
1839 i.activityInfo.name);
1840 if (cn.equals(oldComponent)) {
1841 resolveInfo = i;
1842 }
1843 }
1844 if (resolveInfo == null) {
1845 resolveInfo = manager.resolveActivity(intent, 0);
1846 }
Joe Onorato56d82912010-03-07 14:32:10 -05001847 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001848 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001849 }
Joe Onorato56d82912010-03-07 14:32:10 -05001850 // the db
1851 if (icon == null) {
1852 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001853 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001854 }
1855 }
1856 // the fallback icon
1857 if (icon == null) {
1858 icon = getFallbackIcon();
1859 info.usingFallbackIcon = true;
1860 }
1861 info.setIcon(icon);
1862
1863 // from the resource
1864 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001865 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1866 if (labelCache != null && labelCache.containsKey(key)) {
1867 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001868 } else {
1869 info.title = resolveInfo.activityInfo.loadLabel(manager);
1870 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001871 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001872 }
1873 }
Joe Onorato56d82912010-03-07 14:32:10 -05001874 }
1875 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001877 if (c != null) {
1878 info.title = c.getString(titleIndex);
1879 }
1880 }
1881 // fall back to the class name of the activity
1882 if (info.title == null) {
1883 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001885 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1886 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001890 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001892 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001893 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1894 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895
Joe Onorato56d82912010-03-07 14:32:10 -05001896 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001897 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001898 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001900 // TODO: If there's an explicit component and we can't install that, delete it.
1901
Joe Onorato56d82912010-03-07 14:32:10 -05001902 info.title = c.getString(titleIndex);
1903
Joe Onorato9c1289c2009-08-17 11:03:03 -04001904 int iconType = c.getInt(iconTypeIndex);
1905 switch (iconType) {
1906 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1907 String packageName = c.getString(iconPackageIndex);
1908 String resourceName = c.getString(iconResourceIndex);
1909 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001910 info.customIcon = false;
1911 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001913 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001914 if (resources != null) {
1915 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001916 icon = Utilities.createIconBitmap(
1917 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001918 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001919 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001920 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
Joe Onorato56d82912010-03-07 14:32:10 -05001922 // the db
1923 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001924 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001925 }
1926 // the fallback icon
1927 if (icon == null) {
1928 icon = getFallbackIcon();
1929 info.usingFallbackIcon = true;
1930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001931 break;
1932 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001933 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001934 if (icon == null) {
1935 icon = getFallbackIcon();
1936 info.customIcon = false;
1937 info.usingFallbackIcon = true;
1938 } else {
1939 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 break;
1942 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001943 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001944 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001945 info.customIcon = false;
1946 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001948 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001949 return info;
1950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951
Michael Jurka931dc972011-08-05 15:08:15 -07001952 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07001953 @SuppressWarnings("all") // suppress dead code warning
1954 final boolean debug = false;
1955 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05001956 Log.d(TAG, "getIconFromCursor app="
1957 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1958 }
1959 byte[] data = c.getBlob(iconIndex);
1960 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001961 return Utilities.createIconBitmap(
1962 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001963 } catch (Exception e) {
1964 return null;
1965 }
1966 }
1967
Winson Chung3d503fb2011-07-13 17:25:49 -07001968 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1969 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001970 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001971 if (info == null) {
1972 return null;
1973 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001974 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001975
1976 return info;
1977 }
1978
Winson Chunga9abd0e2010-10-27 17:18:37 -07001979 /**
Winson Chung55cef262010-10-28 14:14:18 -07001980 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1981 */
1982 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1983 ComponentName component) {
1984 List<AppWidgetProviderInfo> widgets =
1985 AppWidgetManager.getInstance(context).getInstalledProviders();
1986 for (AppWidgetProviderInfo info : widgets) {
1987 if (info.provider.equals(component)) {
1988 return info;
1989 }
1990 }
1991 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001992 }
1993
Winson Chung68846fd2010-10-29 11:00:27 -07001994 /**
1995 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1996 */
1997 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1998 final PackageManager packageManager = context.getPackageManager();
1999 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2000 new ArrayList<WidgetMimeTypeHandlerData>();
2001
2002 final Intent supportsIntent =
2003 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2004 supportsIntent.setType(mimeType);
2005
2006 // Create a set of widget configuration components that we can test against
2007 final List<AppWidgetProviderInfo> widgets =
2008 AppWidgetManager.getInstance(context).getInstalledProviders();
2009 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2010 new HashMap<ComponentName, AppWidgetProviderInfo>();
2011 for (AppWidgetProviderInfo info : widgets) {
2012 configurationComponentToWidget.put(info.configure, info);
2013 }
2014
2015 // Run through each of the intents that can handle this type of clip data, and cross
2016 // reference them with the components that are actual configuration components
2017 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2018 PackageManager.MATCH_DEFAULT_ONLY);
2019 for (ResolveInfo info : activities) {
2020 final ActivityInfo activityInfo = info.activityInfo;
2021 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2022 activityInfo.name);
2023 if (configurationComponentToWidget.containsKey(infoComponent)) {
2024 supportedConfigurationActivities.add(
2025 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2026 configurationComponentToWidget.get(infoComponent)));
2027 }
2028 }
2029 return supportedConfigurationActivities;
2030 }
2031
Winson Chunga9abd0e2010-10-27 17:18:37 -07002032 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002033 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2034 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2035 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2036
Adam Cohend9198822011-11-22 16:42:47 -08002037 if (intent == null) {
2038 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2039 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2040 return null;
2041 }
2042
Joe Onorato0589f0f2010-02-08 13:44:00 -08002043 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002044 boolean customIcon = false;
2045 ShortcutIconResource iconResource = null;
2046
2047 if (bitmap != null && bitmap instanceof Bitmap) {
2048 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002049 customIcon = true;
2050 } else {
2051 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2052 if (extra != null && extra instanceof ShortcutIconResource) {
2053 try {
2054 iconResource = (ShortcutIconResource) extra;
2055 final PackageManager packageManager = context.getPackageManager();
2056 Resources resources = packageManager.getResourcesForApplication(
2057 iconResource.packageName);
2058 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002059 icon = Utilities.createIconBitmap(
2060 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002061 } catch (Exception e) {
2062 Log.w(TAG, "Could not load shortcut icon: " + extra);
2063 }
2064 }
2065 }
2066
Michael Jurkac9d95c52011-08-29 14:03:34 -07002067 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002068
2069 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002070 if (fallbackIcon != null) {
2071 icon = fallbackIcon;
2072 } else {
2073 icon = getFallbackIcon();
2074 info.usingFallbackIcon = true;
2075 }
Joe Onorato56d82912010-03-07 14:32:10 -05002076 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002077 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002078
Joe Onorato0589f0f2010-02-08 13:44:00 -08002079 info.title = name;
2080 info.intent = intent;
2081 info.customIcon = customIcon;
2082 info.iconResource = iconResource;
2083
2084 return info;
2085 }
2086
Winson Chungaac01e12011-08-17 10:37:13 -07002087 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2088 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002089 // If apps can't be on SD, don't even bother.
2090 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002091 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002092 }
Joe Onorato56d82912010-03-07 14:32:10 -05002093 // If this icon doesn't have a custom icon, check to see
2094 // what's stored in the DB, and if it doesn't match what
2095 // we're going to show, store what we are going to show back
2096 // into the DB. We do this so when we're loading, if the
2097 // package manager can't find an icon (for example because
2098 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002099 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002100 cache.put(info, c.getBlob(iconIndex));
2101 return true;
2102 }
2103 return false;
2104 }
2105 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2106 boolean needSave = false;
2107 try {
2108 if (data != null) {
2109 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2110 Bitmap loaded = info.getIcon(mIconCache);
2111 needSave = !saved.sameAs(loaded);
2112 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002113 needSave = true;
2114 }
Winson Chungaac01e12011-08-17 10:37:13 -07002115 } catch (Exception e) {
2116 needSave = true;
2117 }
2118 if (needSave) {
2119 Log.d(TAG, "going to save icon bitmap for info=" + info);
2120 // This is slower than is ideal, but this only happens once
2121 // or when the app is updated with a new icon.
2122 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002123 }
2124 }
2125
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002127 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002128 * or make a new one.
2129 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002130 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 // See if a placeholder was created for us already
2132 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002133 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002134 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002135 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 folders.put(id, folderInfo);
2137 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002138 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 }
2140
Joe Onorato9c1289c2009-08-17 11:03:03 -04002141 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08002142 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 = new Comparator<ApplicationInfo>() {
2144 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07002145 int result = sCollator.compare(a.title.toString(), b.title.toString());
2146 if (result == 0) {
2147 result = a.componentName.compareTo(b.componentName);
2148 }
2149 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 };
Winson Chung78403fe2011-01-21 15:38:02 -08002152 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2153 = new Comparator<ApplicationInfo>() {
2154 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2155 if (a.firstInstallTime < b.firstInstallTime) return 1;
2156 if (a.firstInstallTime > b.firstInstallTime) return -1;
2157 return 0;
2158 }
2159 };
Winson Chung785d2eb2011-04-14 16:08:02 -07002160 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
2161 = new Comparator<AppWidgetProviderInfo>() {
2162 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2163 return sCollator.compare(a.label.toString(), b.label.toString());
2164 }
2165 };
Winson Chung5308f242011-08-18 12:12:41 -07002166 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2167 if (info.activityInfo != null) {
2168 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2169 } else {
2170 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2171 }
2172 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002173 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
2174 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002175 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002176 ShortcutNameComparator(PackageManager pm) {
2177 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002178 mLabelCache = new HashMap<Object, CharSequence>();
2179 }
2180 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2181 mPackageManager = pm;
2182 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002183 }
2184 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002185 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002186 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2187 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2188 if (mLabelCache.containsKey(keyA)) {
2189 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002190 } else {
2191 labelA = a.loadLabel(mPackageManager).toString();
2192
Winson Chung5308f242011-08-18 12:12:41 -07002193 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002194 }
Winson Chung5308f242011-08-18 12:12:41 -07002195 if (mLabelCache.containsKey(keyB)) {
2196 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002197 } else {
2198 labelB = b.loadLabel(mPackageManager).toString();
2199
Winson Chung5308f242011-08-18 12:12:41 -07002200 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002201 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002202 return sCollator.compare(labelA, labelB);
2203 }
2204 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002205 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
2206 private PackageManager mPackageManager;
2207 private HashMap<Object, String> mLabelCache;
2208 WidgetAndShortcutNameComparator(PackageManager pm) {
2209 mPackageManager = pm;
2210 mLabelCache = new HashMap<Object, String>();
2211 }
2212 public final int compare(Object a, Object b) {
2213 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002214 if (mLabelCache.containsKey(a)) {
2215 labelA = mLabelCache.get(a);
2216 } else {
2217 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002218 ((AppWidgetProviderInfo) a).label :
2219 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002220 mLabelCache.put(a, labelA);
2221 }
2222 if (mLabelCache.containsKey(b)) {
2223 labelB = mLabelCache.get(b);
2224 } else {
2225 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002226 ((AppWidgetProviderInfo) b).label :
2227 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002228 mLabelCache.put(b, labelB);
2229 }
Winson Chung1ed747a2011-05-03 16:18:34 -07002230 return sCollator.compare(labelA, labelB);
2231 }
2232 };
Joe Onoratobe386092009-11-17 17:32:16 -08002233
2234 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002235 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002236 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2237 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2238 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2239 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002240 if (mLoaderTask != null) {
2241 mLoaderTask.dumpState();
2242 } else {
2243 Log.d(TAG, "mLoaderTask=null");
2244 }
Joe Onoratobe386092009-11-17 17:32:16 -08002245 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246}