blob: 1657a508abf7c0015951fac9cf23ccb0af32ada7 [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 >
96 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080097
Michael Jurkaa8c760d2011-04-28 14:59:33 -070098 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
99 // LauncherModel to their ids
100 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
101
102 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
103 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
104 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700105 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700106
107 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
108 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
109 new ArrayList<LauncherAppWidgetInfo>();
110
111 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
112 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700113
114 // sDbIconCache is the set of ItemInfos that need to have their icons updated in the database
115 static final HashMap<Object, byte[]> sDbIconCache = new HashMap<Object, byte[]>();
116
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700117 // </ only access in worker thread >
118
119 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800120 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Adam Cohend22015c2010-07-26 22:02:18 -0700122 private static int mCellCountX;
123 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700124
Reena Lee99a73f32011-10-24 17:27:37 -0700125 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700126
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700128 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400129 public int getCurrentWorkspaceScreen();
130 public void startBinding();
131 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500132 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400133 public void finishBindingItems();
134 public void bindAppWidget(LauncherAppWidgetInfo info);
135 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500136 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
137 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chungcd810732012-06-18 16:45:43 -0700138 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700139 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400140 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100142 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800146 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400147 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 mAllAppsList = new AllAppsList(iconCache);
149 mIconCache = iconCache;
150
151 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700152 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400153
Reena Lee93f824a2011-09-23 17:20:28 -0700154 final Resources res = app.getResources();
155 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
156 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700157 Configuration config = res.getConfiguration();
158 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800159 }
160
Joe Onorato56d82912010-03-07 14:32:10 -0500161 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800162 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chung603bcb92011-09-02 11:45:39 -0700165 public void unbindWorkspaceItems() {
166 sWorker.post(new Runnable() {
167 @Override
168 public void run() {
169 unbindWorkspaceItemsOnMainThread();
170 }
171 });
172 }
173
174 /** Unbinds all the sWorkspaceItems on the main thread, and return a copy of sWorkspaceItems
175 * that is save to reference from the main thread. */
176 private ArrayList<ItemInfo> unbindWorkspaceItemsOnMainThread() {
177 // Ensure that we don't use the same workspace items data structure on the main thread
178 // by making a copy of workspace items first.
179 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(sWorkspaceItems);
Michael Jurka05bf644e2011-11-30 20:28:53 -0800180 final ArrayList<ItemInfo> appWidgets = new ArrayList<ItemInfo>(sAppWidgets);
Winson Chung603bcb92011-09-02 11:45:39 -0700181 mHandler.post(new Runnable() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700182 @Override
Winson Chung603bcb92011-09-02 11:45:39 -0700183 public void run() {
184 for (ItemInfo item : workspaceItems) {
185 item.unbind();
186 }
Michael Jurka05bf644e2011-11-30 20:28:53 -0800187 for (ItemInfo item : appWidgets) {
188 item.unbind();
189 }
Winson Chung603bcb92011-09-02 11:45:39 -0700190 }
191 });
192
193 return workspaceItems;
Adam Cohen4eac29a2011-07-11 17:53:37 -0700194 }
195
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 /**
197 * Adds an item to the DB if it was not created previously, or move it to a new
198 * <container, screen, cellX, cellY>
199 */
200 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
201 int screen, int cellX, int cellY) {
202 if (item.container == ItemInfo.NO_ID) {
203 // From all apps
204 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
205 } else {
206 // From somewhere else
207 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 }
209 }
210
Michael Jurka816474f2012-06-25 14:49:02 -0700211 static void checkItemInfo(final ItemInfo item) {
212 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
213 final long itemId = item.id;
214 Runnable r = new Runnable() {
215 public void run() {
216 ItemInfo modelItem = sItemsIdMap.get(itemId);
Michael Jurka087f7a62012-06-27 11:48:37 -0700217 if (modelItem != null && item != modelItem) {
Michael Jurka816474f2012-06-25 14:49:02 -0700218 // the modelItem needs to match up perfectly with item if our model is to be
219 // consistent with the database-- for now, just require modelItem == item
220 String msg = "item: " + ((item != null) ? item.toString() : "null") +
221 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
222 "Error: ItemInfo passed to checkItemInfo doesn't match original";
223 RuntimeException e = new RuntimeException(msg);
224 e.setStackTrace(stackTrace);
225 throw e;
226 }
227 }
228 };
229
230 if (sWorkerThread.getThreadId() == Process.myTid()) {
231 r.run();
232 } else {
233 sWorker.post(r);
234 }
235
236 }
237
Michael Jurkac9d95c52011-08-29 14:03:34 -0700238 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
239 final ItemInfo item, final String callingFunction) {
240 final long itemId = item.id;
241 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
242 final ContentResolver cr = context.getContentResolver();
243
244 Runnable r = new Runnable() {
245 public void run() {
246 cr.update(uri, values, null, null);
247
248 ItemInfo modelItem = sItemsIdMap.get(itemId);
249 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
258 // Items are added/removed from the corresponding FolderInfo elsewhere, such
259 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
260 // that are on the desktop, as appropriate
261 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
262 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
263 if (!sWorkspaceItems.contains(modelItem)) {
264 sWorkspaceItems.add(modelItem);
265 }
266 } else {
267 sWorkspaceItems.remove(modelItem);
268 }
269 }
270 };
271
272 if (sWorkerThread.getThreadId() == Process.myTid()) {
273 r.run();
274 } else {
275 sWorker.post(r);
276 }
277 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400280 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700281 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700282 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
283 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 item.cellX = cellX;
286 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700287
Winson Chung3d503fb2011-07-13 17:25:49 -0700288 // We store hotseat items in canonical form which is this orientation invariant position
289 // in the hotseat
290 if (context instanceof Launcher && screen < 0 &&
291 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
292 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
293 } else {
294 item.screen = screen;
295 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296
297 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700299 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
300 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
302
Michael Jurkac9d95c52011-08-29 14:03:34 -0700303 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700304 }
305
306 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700307 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800308 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700309 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
310 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
311 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800312 item.cellX = cellX;
313 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700314 item.spanX = spanX;
315 item.spanY = spanY;
316
317 // We store hotseat items in canonical form which is this orientation invariant position
318 // in the hotseat
319 if (context instanceof Launcher && screen < 0 &&
320 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
321 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
322 } else {
323 item.screen = screen;
324 }
Adam Cohend4844c32011-02-18 19:25:06 -0800325
Adam Cohend4844c32011-02-18 19:25:06 -0800326 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800327 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700328 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
329 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
330 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
331 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
332 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800333
Michael Jurka816474f2012-06-25 14:49:02 -0700334 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700335 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700336
337 /**
338 * Update an item to the database in a specified container.
339 */
340 static void updateItemInDatabase(Context context, final ItemInfo item) {
341 final ContentValues values = new ContentValues();
342 item.onAddToDatabase(values);
343 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
344 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800345 }
346
347 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400348 * Returns true if the shortcuts already exists in the database.
349 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400351 static boolean shortcutExists(Context context, String title, Intent intent) {
352 final ContentResolver cr = context.getContentResolver();
353 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
354 new String[] { "title", "intent" }, "title=? and intent=?",
355 new String[] { title, intent.toUri(0) }, null);
356 boolean result = false;
357 try {
358 result = c.moveToFirst();
359 } finally {
360 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700363 }
364
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700366 * Returns an ItemInfo array containing all the items in the LauncherModel.
367 * The ItemInfo.id is not set through this function.
368 */
369 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
370 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
371 final ContentResolver cr = context.getContentResolver();
372 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
373 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
374 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
375 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
376
377 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
378 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
379 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
380 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
381 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
382 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
383 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
384
385 try {
386 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700387 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700388 item.cellX = c.getInt(cellXIndex);
389 item.cellY = c.getInt(cellYIndex);
390 item.spanX = c.getInt(spanXIndex);
391 item.spanY = c.getInt(spanYIndex);
392 item.container = c.getInt(containerIndex);
393 item.itemType = c.getInt(itemTypeIndex);
394 item.screen = c.getInt(screenIndex);
395
396 items.add(item);
397 }
398 } catch (Exception e) {
399 items.clear();
400 } finally {
401 c.close();
402 }
403
404 return items;
405 }
406
407 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
409 */
410 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
411 final ContentResolver cr = context.getContentResolver();
412 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
413 "_id=? and (itemType=? or itemType=?)",
414 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700415 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700416
Joe Onorato9c1289c2009-08-17 11:03:03 -0400417 try {
418 if (c.moveToFirst()) {
419 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
420 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
421 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
422 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
423 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
424 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425
Joe Onorato9c1289c2009-08-17 11:03:03 -0400426 FolderInfo folderInfo = null;
427 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700428 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
429 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700431 }
432
Joe Onorato9c1289c2009-08-17 11:03:03 -0400433 folderInfo.title = c.getString(titleIndex);
434 folderInfo.id = id;
435 folderInfo.container = c.getInt(containerIndex);
436 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700437 folderInfo.cellX = c.getInt(cellXIndex);
438 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439
440 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442 } finally {
443 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700444 }
445
446 return null;
447 }
448
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 /**
450 * Add an item to the database in a specified container. Sets the container, screen, cellX and
451 * cellY fields of the item. Also assigns an ID to the item.
452 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700453 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
454 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400456 item.cellX = cellX;
457 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700458 // We store hotseat items in canonical form which is this orientation invariant position
459 // in the hotseat
460 if (context instanceof Launcher && screen < 0 &&
461 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
462 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
463 } else {
464 item.screen = screen;
465 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400466
467 final ContentValues values = new ContentValues();
468 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400469 item.onAddToDatabase(values);
470
Michael Jurka7578ec62011-08-03 14:11:54 -0700471 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700472 item.id = app.getLauncherProvider().generateNewId();
473 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700474 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700475
Michael Jurkac9d95c52011-08-29 14:03:34 -0700476 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700477 public void run() {
478 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
479 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400480
Winson Chungb1094bd2011-08-24 16:14:08 -0700481 if (sItemsIdMap.containsKey(item.id)) {
482 // we should not be adding new items in the db with the same id
483 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
484 "addItemToDatabase already exists." + item.toString());
485 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700486 sItemsIdMap.put(item.id, item);
487 switch (item.itemType) {
488 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
489 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700490 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700491 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
492 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700493 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
494 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700495 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700496 }
497 break;
498 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
499 sAppWidgets.add((LauncherAppWidgetInfo) item);
500 break;
501 }
502 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700503 };
504
505 if (sWorkerThread.getThreadId() == Process.myTid()) {
506 r.run();
507 } else {
508 sWorker.post(r);
509 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700510 }
511
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700513 * Creates a new unique child id, for a given cell span across all layouts.
514 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700515 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700516 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
517 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700518 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700519 }
520
Adam Cohend22015c2010-07-26 22:02:18 -0700521 static int getCellCountX() {
522 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700523 }
524
Adam Cohend22015c2010-07-26 22:02:18 -0700525 static int getCellCountY() {
526 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700527 }
528
529 /**
530 * Updates the model orientation helper to take into account the current layout dimensions
531 * when performing local/canonical coordinate transformations.
532 */
533 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700534 mCellCountX = shortAxisCellCount;
535 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700536 }
537
538 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700539 * Removes the specified item from the database
540 * @param context
541 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700543 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700545 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Michael Jurka83df1882011-08-31 20:59:26 -0700546 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700547 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700548 cr.delete(uriToDelete, null, null);
549 switch (item.itemType) {
550 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
551 sFolders.remove(item.id);
552 sWorkspaceItems.remove(item);
553 break;
554 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
555 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
556 sWorkspaceItems.remove(item);
557 break;
558 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
559 sAppWidgets.remove((LauncherAppWidgetInfo) item);
560 break;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700561 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700562 sItemsIdMap.remove(item.id);
563 sDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700564 }
Michael Jurka83df1882011-08-31 20:59:26 -0700565 };
566 if (sWorkerThread.getThreadId() == Process.myTid()) {
567 r.run();
568 } else {
569 sWorker.post(r);
570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 }
572
573 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 * Remove the contents of the specified folder from the database
575 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700576 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577 final ContentResolver cr = context.getContentResolver();
578
Michael Jurkac9d95c52011-08-29 14:03:34 -0700579 Runnable r = new Runnable() {
580 public void run() {
581 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
582 sItemsIdMap.remove(info.id);
583 sFolders.remove(info.id);
584 sDbIconCache.remove(info);
585 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700586
Michael Jurkac9d95c52011-08-29 14:03:34 -0700587 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
588 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
589 for (ItemInfo childInfo : info.contents) {
590 sItemsIdMap.remove(childInfo.id);
591 sDbIconCache.remove(childInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700592 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700593 }
594 };
595 if (sWorkerThread.getThreadId() == Process.myTid()) {
596 r.run();
597 } else {
598 sWorker.post(r);
599 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600 }
601
602 /**
603 * Set this as the current Launcher activity object for the loader.
604 */
605 public void initialize(Callbacks callbacks) {
606 synchronized (mLock) {
607 mCallbacks = new WeakReference<Callbacks>(callbacks);
608 }
609 }
610
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700611 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
613 * ACTION_PACKAGE_CHANGED.
614 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100615 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400616 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400617 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700618
Joe Onorato36115782010-06-17 13:28:48 -0400619 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400620
Joe Onorato36115782010-06-17 13:28:48 -0400621 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
622 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
623 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
624 final String packageName = intent.getData().getSchemeSpecificPart();
625 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626
Joe Onorato36115782010-06-17 13:28:48 -0400627 int op = PackageUpdatedTask.OP_NONE;
628
629 if (packageName == null || packageName.length() == 0) {
630 // they sent us a bad intent
631 return;
632 }
633
634 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
635 op = PackageUpdatedTask.OP_UPDATE;
636 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
637 if (!replacing) {
638 op = PackageUpdatedTask.OP_REMOVE;
639 }
640 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
641 // later, we will update the package at this time
642 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
643 if (!replacing) {
644 op = PackageUpdatedTask.OP_ADD;
645 } else {
646 op = PackageUpdatedTask.OP_UPDATE;
647 }
648 }
649
650 if (op != PackageUpdatedTask.OP_NONE) {
651 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
652 }
653
654 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400655 // First, schedule to add these apps back in.
656 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
657 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
658 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700659 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400660 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
661 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
662 enqueuePackageUpdated(new PackageUpdatedTask(
663 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700664 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700665 // If we have changed locale we need to clear out the labels in all apps/workspace.
666 forceReload();
667 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
668 // Check if configuration change was an mcc/mnc change which would affect app resources
669 // and we would need to clear out the labels in all apps/workspace. Same handling as
670 // above for ACTION_LOCALE_CHANGED
671 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700672 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700673 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700674 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700675 forceReload();
676 }
677 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700678 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700679 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
680 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700681 if (mCallbacks != null) {
682 Callbacks callbacks = mCallbacks.get();
683 if (callbacks != null) {
684 callbacks.bindSearchablesChanged();
685 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700686 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700687 }
688 }
689
Reena Lee93f824a2011-09-23 17:20:28 -0700690 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700691 resetLoadedState(true, true);
692
Reena Lee93f824a2011-09-23 17:20:28 -0700693 // Do this here because if the launcher activity is running it will be restarted.
694 // If it's not running startLoaderFromBackground will merely tell it that it needs
695 // to reload.
696 startLoaderFromBackground();
697 }
698
Winson Chungf0c6ae02012-03-21 16:10:31 -0700699 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
700 synchronized (mLock) {
701 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
702 // mWorkspaceLoaded to true later
703 stopLoaderLocked();
704 if (resetAllAppsLoaded) mAllAppsLoaded = false;
705 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
706 }
707 }
708
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700709 /**
710 * When the launcher is in the background, it's possible for it to miss paired
711 * configuration changes. So whenever we trigger the loader from the background
712 * tell the launcher that it needs to re-run the loader when it comes back instead
713 * of doing it now.
714 */
715 public void startLoaderFromBackground() {
716 boolean runLoader = false;
717 if (mCallbacks != null) {
718 Callbacks callbacks = mCallbacks.get();
719 if (callbacks != null) {
720 // Only actually run the loader if they're not paused.
721 if (!callbacks.setLoadOnResume()) {
722 runLoader = true;
723 }
724 }
725 }
726 if (runLoader) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700727 startLoader(false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700728 }
Joe Onorato36115782010-06-17 13:28:48 -0400729 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400730
Reena Lee93f824a2011-09-23 17:20:28 -0700731 // If there is already a loader task running, tell it to stop.
732 // returns true if isLaunching() was true on the old task
733 private boolean stopLoaderLocked() {
734 boolean isLaunching = false;
735 LoaderTask oldTask = mLoaderTask;
736 if (oldTask != null) {
737 if (oldTask.isLaunching()) {
738 isLaunching = true;
739 }
740 oldTask.stopLocked();
741 }
742 return isLaunching;
743 }
744
Winson Chungf0c6ae02012-03-21 16:10:31 -0700745 public void startLoader(boolean isLaunching) {
Joe Onorato36115782010-06-17 13:28:48 -0400746 synchronized (mLock) {
747 if (DEBUG_LOADERS) {
748 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750
Joe Onorato36115782010-06-17 13:28:48 -0400751 // Don't bother to start the thread if we know it's not going to do anything
752 if (mCallbacks != null && mCallbacks.get() != null) {
753 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700754 // also, don't downgrade isLaunching if we're already running
755 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700756 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chung7ed37742011-09-08 15:45:51 -0700757 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700758 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 }
761 }
762
Joe Onorato36115782010-06-17 13:28:48 -0400763 public void stopLoader() {
764 synchronized (mLock) {
765 if (mLoaderTask != null) {
766 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400767 }
768 }
Joe Onorato36115782010-06-17 13:28:48 -0400769 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770
Michael Jurkac57b7a82011-08-09 22:02:20 -0700771 public boolean isAllAppsLoaded() {
772 return mAllAppsLoaded;
773 }
774
Winson Chung36a62fe2012-05-06 18:04:42 -0700775 boolean isLoadingWorkspace() {
776 synchronized (mLock) {
777 if (mLoaderTask != null) {
778 return mLoaderTask.isLoadingWorkspace();
779 }
780 }
781 return false;
782 }
783
Joe Onorato36115782010-06-17 13:28:48 -0400784 /**
785 * Runnable for the thread that loads the contents of the launcher:
786 * - workspace icons
787 * - widgets
788 * - all apps icons
789 */
790 private class LoaderTask implements Runnable {
791 private Context mContext;
792 private Thread mWaitThread;
793 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700794 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400795 private boolean mStopped;
796 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700797 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400798
799 LoaderTask(Context context, boolean isLaunching) {
800 mContext = context;
801 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700802 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803 }
804
Joe Onorato36115782010-06-17 13:28:48 -0400805 boolean isLaunching() {
806 return mIsLaunching;
807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808
Winson Chung36a62fe2012-05-06 18:04:42 -0700809 boolean isLoadingWorkspace() {
810 return mIsLoadingAndBindingWorkspace;
811 }
812
Joe Onorato36115782010-06-17 13:28:48 -0400813 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700814 mIsLoadingAndBindingWorkspace = true;
815
Joe Onorato36115782010-06-17 13:28:48 -0400816 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400817 if (DEBUG_LOADERS) {
818 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700820
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700821 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400822 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700823 synchronized (LoaderTask.this) {
824 if (mStopped) {
825 return;
826 }
827 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828 }
829 }
830
Joe Onorato36115782010-06-17 13:28:48 -0400831 // Bind the workspace
832 bindWorkspace();
833 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400834
Joe Onorato36115782010-06-17 13:28:48 -0400835 private void waitForIdle() {
836 // Wait until the either we're stopped or the other threads are done.
837 // This way we don't start loading all apps until the workspace has settled
838 // down.
839 synchronized (LoaderTask.this) {
840 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700841
Joe Onorato36115782010-06-17 13:28:48 -0400842 mHandler.postIdle(new Runnable() {
843 public void run() {
844 synchronized (LoaderTask.this) {
845 mLoadAndBindStepFinished = true;
846 if (DEBUG_LOADERS) {
847 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400848 }
Joe Onorato36115782010-06-17 13:28:48 -0400849 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400850 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400851 }
Joe Onorato36115782010-06-17 13:28:48 -0400852 });
853
854 while (!mStopped && !mLoadAndBindStepFinished) {
855 try {
856 this.wait();
857 } catch (InterruptedException ex) {
858 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400859 }
860 }
Joe Onorato36115782010-06-17 13:28:48 -0400861 if (DEBUG_LOADERS) {
862 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700863 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400864 + "ms for previous step to finish binding");
865 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400866 }
Joe Onorato36115782010-06-17 13:28:48 -0400867 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400868
Joe Onorato36115782010-06-17 13:28:48 -0400869 public void run() {
870 // Optimize for end-user experience: if the Launcher is up and // running with the
871 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
872 // workspace first (default).
873 final Callbacks cbk = mCallbacks.get();
874 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400875
Joe Onorato36115782010-06-17 13:28:48 -0400876 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400877 // Elevate priority when Home launches for the first time to avoid
878 // starving at boot time. Staring at a blank home is not cool.
879 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700880 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
881 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400882 android.os.Process.setThreadPriority(mIsLaunching
883 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
884 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400885 if (loadWorkspaceFirst) {
886 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
887 loadAndBindWorkspace();
888 } else {
889 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700890 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400891 }
892
Joe Onorato36115782010-06-17 13:28:48 -0400893 if (mStopped) {
894 break keep_running;
895 }
896
897 // Whew! Hard work done. Slow us down, and wait until the UI thread has
898 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400899 synchronized (mLock) {
900 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700901 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400902 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
903 }
904 }
Joe Onorato36115782010-06-17 13:28:48 -0400905 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400906
907 // second step
908 if (loadWorkspaceFirst) {
909 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700910 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400911 } else {
912 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
913 loadAndBindWorkspace();
914 }
Winson Chung7ed37742011-09-08 15:45:51 -0700915
916 // Restore the default thread priority after we are done loading items
917 synchronized (mLock) {
918 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
919 }
Joe Onorato36115782010-06-17 13:28:48 -0400920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921
Winson Chungaac01e12011-08-17 10:37:13 -0700922
923 // Update the saved icons if necessary
924 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700925 for (Object key : sDbIconCache.keySet()) {
926 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700927 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700928 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700929
Joe Onorato36115782010-06-17 13:28:48 -0400930 // Clear out this reference, otherwise we end up holding it until all of the
931 // callback runnables are done.
932 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933
Joe Onorato36115782010-06-17 13:28:48 -0400934 synchronized (mLock) {
935 // If we are still the last one to be scheduled, remove ourselves.
936 if (mLoaderTask == this) {
937 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 }
Joe Onorato36115782010-06-17 13:28:48 -0400939 }
Joe Onorato36115782010-06-17 13:28:48 -0400940 }
941
942 public void stopLocked() {
943 synchronized (LoaderTask.this) {
944 mStopped = true;
945 this.notify();
946 }
947 }
948
949 /**
950 * Gets the callbacks object. If we've been stopped, or if the launcher object
951 * has somehow been garbage collected, return null instead. Pass in the Callbacks
952 * object that was around when the deferred message was scheduled, and if there's
953 * a new Callbacks object around then also return null. This will save us from
954 * calling onto it with data that will be ignored.
955 */
956 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
957 synchronized (mLock) {
958 if (mStopped) {
959 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400960 }
Joe Onorato36115782010-06-17 13:28:48 -0400961
962 if (mCallbacks == null) {
963 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400964 }
Joe Onorato36115782010-06-17 13:28:48 -0400965
966 final Callbacks callbacks = mCallbacks.get();
967 if (callbacks != oldCallbacks) {
968 return null;
969 }
970 if (callbacks == null) {
971 Log.w(TAG, "no mCallbacks");
972 return null;
973 }
974
975 return callbacks;
976 }
977 }
978
979 // check & update map of what's occupied; used to discard overlapping/invalid items
980 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700981 int containerIndex = item.screen;
982 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700983 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800984 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700985 return false;
986 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700987
988 // We use the last index to refer to the hotseat and the screen as the rank, so
989 // test and update the occupied state accordingly
990 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
991 Log.e(TAG, "Error loading shortcut into hotseat " + item
992 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
993 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
994 return false;
995 } else {
996 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
997 return true;
998 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700999 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1000 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001001 return true;
1002 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001003
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001004 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001005 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1006 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001007 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001008 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -07001009 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001010 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001011 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -07001012 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001013 return false;
1014 }
1015 }
1016 }
1017 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1018 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001019 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001020 }
1021 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001022
Joe Onorato36115782010-06-17 13:28:48 -04001023 return true;
1024 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025
Joe Onorato36115782010-06-17 13:28:48 -04001026 private void loadWorkspace() {
1027 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028
Joe Onorato36115782010-06-17 13:28:48 -04001029 final Context context = mContext;
1030 final ContentResolver contentResolver = context.getContentResolver();
1031 final PackageManager manager = context.getPackageManager();
1032 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1033 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001034
Michael Jurkab85f8a42012-04-25 15:48:32 -07001035 // Make sure the default workspace is loaded, if needed
1036 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary();
1037
Adam Cohen4eac29a2011-07-11 17:53:37 -07001038 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001039 sAppWidgets.clear();
1040 sFolders.clear();
1041 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -07001042 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001043
Joe Onorato36115782010-06-17 13:28:48 -04001044 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045
Joe Onorato36115782010-06-17 13:28:48 -04001046 final Cursor c = contentResolver.query(
Winson Chunge61e93e2011-09-12 10:59:49 -07001047 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001048
Winson Chungf30ad5f2011-08-08 10:55:42 -07001049 // +1 for the hotseat (it can be larger than the workspace)
Winson Chung36f97362011-09-07 11:20:10 -07001050 // Load workspace in reverse order to ensure that latest items are loaded first (and
1051 // before any earlier duplicates)
Adam Cohend22015c2010-07-26 22:02:18 -07001052 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -07001053 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 try {
1056 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1057 final int intentIndex = c.getColumnIndexOrThrow
1058 (LauncherSettings.Favorites.INTENT);
1059 final int titleIndex = c.getColumnIndexOrThrow
1060 (LauncherSettings.Favorites.TITLE);
1061 final int iconTypeIndex = c.getColumnIndexOrThrow(
1062 LauncherSettings.Favorites.ICON_TYPE);
1063 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1064 final int iconPackageIndex = c.getColumnIndexOrThrow(
1065 LauncherSettings.Favorites.ICON_PACKAGE);
1066 final int iconResourceIndex = c.getColumnIndexOrThrow(
1067 LauncherSettings.Favorites.ICON_RESOURCE);
1068 final int containerIndex = c.getColumnIndexOrThrow(
1069 LauncherSettings.Favorites.CONTAINER);
1070 final int itemTypeIndex = c.getColumnIndexOrThrow(
1071 LauncherSettings.Favorites.ITEM_TYPE);
1072 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1073 LauncherSettings.Favorites.APPWIDGET_ID);
1074 final int screenIndex = c.getColumnIndexOrThrow(
1075 LauncherSettings.Favorites.SCREEN);
1076 final int cellXIndex = c.getColumnIndexOrThrow
1077 (LauncherSettings.Favorites.CELLX);
1078 final int cellYIndex = c.getColumnIndexOrThrow
1079 (LauncherSettings.Favorites.CELLY);
1080 final int spanXIndex = c.getColumnIndexOrThrow
1081 (LauncherSettings.Favorites.SPANX);
1082 final int spanYIndex = c.getColumnIndexOrThrow(
1083 LauncherSettings.Favorites.SPANY);
Michael Jurka3a9fced2012-04-13 14:44:29 -07001084 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1085 //final int displayModeIndex = c.getColumnIndexOrThrow(
1086 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087
Joe Onorato36115782010-06-17 13:28:48 -04001088 ShortcutInfo info;
1089 String intentDescription;
1090 LauncherAppWidgetInfo appWidgetInfo;
1091 int container;
1092 long id;
1093 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094
Joe Onorato36115782010-06-17 13:28:48 -04001095 while (!mStopped && c.moveToNext()) {
1096 try {
1097 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001098
Joe Onorato36115782010-06-17 13:28:48 -04001099 switch (itemType) {
1100 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1101 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1102 intentDescription = c.getString(intentIndex);
1103 try {
1104 intent = Intent.parseUri(intentDescription, 0);
1105 } catch (URISyntaxException e) {
1106 continue;
1107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108
Joe Onorato36115782010-06-17 13:28:48 -04001109 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1110 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -07001111 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001112 } else {
1113 info = getShortcutInfo(c, context, iconTypeIndex,
1114 iconPackageIndex, iconResourceIndex, iconIndex,
1115 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001116
1117 // App shortcuts that used to be automatically added to Launcher
1118 // didn't always have the correct intent flags set, so do that here
Michael Jurka9ad00562012-05-14 12:24:22 -07001119 if (intent.getAction() != null &&
1120 intent.getCategories() != null &&
1121 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001122 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
1123 intent.addFlags(
1124 Intent.FLAG_ACTIVITY_NEW_TASK |
1125 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1126 }
Joe Onorato36115782010-06-17 13:28:48 -04001127 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128
Joe Onorato36115782010-06-17 13:28:48 -04001129 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001130 info.intent = intent;
1131 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001133 info.container = container;
1134 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001135 info.cellX = c.getInt(cellXIndex);
1136 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137
Daniel Sandler8802e962010-05-26 16:28:16 -04001138 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001139 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001140 break;
1141 }
1142
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001144 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001145 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001146 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001147 break;
1148 default:
1149 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001150 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001151 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001152 folderInfo.add(info);
1153 break;
1154 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001155 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001156
1157 // now that we've loaded everthing re-save it with the
1158 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001159 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001160 } else {
1161 // Failed to load the shortcut, probably because the
1162 // activity manager couldn't resolve it (maybe the app
1163 // was uninstalled), or the db row was somehow screwed up.
1164 // Delete it.
1165 id = c.getLong(idIndex);
1166 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1167 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1168 id, false), null, null);
1169 }
1170 break;
1171
Adam Cohendf2cc412011-04-27 16:56:57 -07001172 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001173 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001174 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001175
Winson Chungaafa03c2010-06-11 17:34:16 -07001176 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001177 folderInfo.id = id;
1178 container = c.getInt(containerIndex);
1179 folderInfo.container = container;
1180 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001181 folderInfo.cellX = c.getInt(cellXIndex);
1182 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001183
1184 // check & update map of what's occupied
1185 if (!checkItemPlacement(occupied, folderInfo)) {
1186 break;
1187 }
Joe Onorato36115782010-06-17 13:28:48 -04001188 switch (container) {
1189 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001191 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001192 break;
1193 }
1194
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001195 sItemsIdMap.put(folderInfo.id, folderInfo);
1196 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001197 break;
1198
Joe Onorato36115782010-06-17 13:28:48 -04001199 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1200 // Read all Launcher-specific widget details
1201 int appWidgetId = c.getInt(appWidgetIdIndex);
1202 id = c.getLong(idIndex);
1203
1204 final AppWidgetProviderInfo provider =
1205 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001206
Joe Onorato36115782010-06-17 13:28:48 -04001207 if (!isSafeMode && (provider == null || provider.provider == null ||
1208 provider.provider.getPackageName() == null)) {
Adam Cohen16d7ffc2011-10-05 17:49:14 -07001209 String log = "Deleting widget that isn't installed anymore: id="
1210 + id + " appWidgetId=" + appWidgetId;
1211 Log.e(TAG, log);
1212 Launcher.sDumpLogs.add(log);
Joe Onorato36115782010-06-17 13:28:48 -04001213 itemsToRemove.add(id);
1214 } else {
Winson Chung11a49372012-04-27 15:12:38 -07001215 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1216 provider.provider);
Joe Onorato36115782010-06-17 13:28:48 -04001217 appWidgetInfo.id = id;
1218 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001219 appWidgetInfo.cellX = c.getInt(cellXIndex);
1220 appWidgetInfo.cellY = c.getInt(cellYIndex);
1221 appWidgetInfo.spanX = c.getInt(spanXIndex);
1222 appWidgetInfo.spanY = c.getInt(spanYIndex);
Adam Cohen2f093b62012-04-30 18:59:53 -07001223 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1224 appWidgetInfo.minSpanX = minSpan[0];
1225 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001226
1227 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001228 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1229 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001230 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001231 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001232 continue;
1233 }
1234 appWidgetInfo.container = c.getInt(containerIndex);
1235
1236 // check & update map of what's occupied
1237 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1238 break;
1239 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001240 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1241 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001242 }
1243 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001244 }
Joe Onorato36115782010-06-17 13:28:48 -04001245 } catch (Exception e) {
1246 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001247 }
1248 }
Joe Onorato36115782010-06-17 13:28:48 -04001249 } finally {
1250 c.close();
1251 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001252
Joe Onorato36115782010-06-17 13:28:48 -04001253 if (itemsToRemove.size() > 0) {
1254 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1255 LauncherSettings.Favorites.CONTENT_URI);
1256 // Remove dead items
1257 for (long id : itemsToRemove) {
1258 if (DEBUG_LOADERS) {
1259 Log.d(TAG, "Removed id = " + id);
1260 }
1261 // Don't notify content observers
1262 try {
1263 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1264 null, null);
1265 } catch (RemoteException e) {
1266 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001267 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001268 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001269 }
1270
Joe Onorato36115782010-06-17 13:28:48 -04001271 if (DEBUG_LOADERS) {
1272 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1273 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001274 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001275 String line = "";
1276 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1277 if (s > 0) {
1278 line += " | ";
1279 }
Adam Cohend22015c2010-07-26 22:02:18 -07001280 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001281 line += ((occupied[s][x][y] != null) ? "#" : ".");
1282 }
1283 }
1284 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 }
Joe Onorato36115782010-06-17 13:28:48 -04001286 }
1287 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001288
Joe Onorato36115782010-06-17 13:28:48 -04001289 /**
1290 * Read everything out of our database.
1291 */
1292 private void bindWorkspace() {
1293 final long t = SystemClock.uptimeMillis();
1294
1295 // Don't use these two variables in any of the callback runnables.
1296 // Otherwise we hold a reference to them.
1297 final Callbacks oldCallbacks = mCallbacks.get();
1298 if (oldCallbacks == null) {
1299 // This launcher has exited and nobody bothered to tell us. Just bail.
1300 Log.w(TAG, "LoaderTask running with no launcher");
1301 return;
1302 }
1303
Winson Chungdb8a8942012-04-03 14:08:41 -07001304 // Get the list of workspace items to load and unbind the existing ShortcutInfos
1305 // before we call startBinding() below.
1306 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1307 final ArrayList<ItemInfo> tmpWorkspaceItems = unbindWorkspaceItemsOnMainThread();
1308 // Order the items for loading as follows: current workspace, hotseat, everything else
1309 Collections.sort(tmpWorkspaceItems, new Comparator<ItemInfo>() {
1310 @Override
1311 public int compare(ItemInfo lhs, ItemInfo rhs) {
1312 int cellCountX = LauncherModel.getCellCountX();
1313 int cellCountY = LauncherModel.getCellCountY();
1314 int screenOffset = cellCountX * cellCountY;
1315 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1316 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1317 lhs.cellY * cellCountX + lhs.cellX);
1318 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1319 rhs.cellY * cellCountX + rhs.cellX);
1320 return (int) (lr - rr);
1321 }
1322 });
1323 // Precondition: the items are ordered by page, screen
1324 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1325 for (ItemInfo ii : tmpWorkspaceItems) {
1326 // Prepend the current items, hotseat items, append everything else
1327 if (ii.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1328 ii.screen == currentScreen) {
1329 workspaceItems.add(0, ii);
1330 } else if (ii.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1331 workspaceItems.add(0, ii);
1332 } else {
1333 workspaceItems.add(ii);
1334 }
1335 }
1336
Joe Onorato36115782010-06-17 13:28:48 -04001337 // Tell the workspace that we're about to start firing items at it
1338 mHandler.post(new Runnable() {
1339 public void run() {
1340 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1341 if (callbacks != null) {
1342 callbacks.startBinding();
1343 }
1344 }
1345 });
Winson Chung603bcb92011-09-02 11:45:39 -07001346
Joe Onorato36115782010-06-17 13:28:48 -04001347 // Add the items to the workspace.
Winson Chungdb8a8942012-04-03 14:08:41 -07001348 int N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001349 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1350 final int start = i;
1351 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001352 mHandler.post(new Runnable() {
1353 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001354 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001356 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 }
1358 }
1359 });
Joe Onorato36115782010-06-17 13:28:48 -04001360 }
Winson Chung603bcb92011-09-02 11:45:39 -07001361 // Ensure that we don't use the same folders data structure on the main thread
1362 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001363 mHandler.post(new Runnable() {
1364 public void run() {
1365 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1366 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001367 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001368 }
1369 }
1370 });
1371 // Wait until the queue goes empty.
1372 mHandler.post(new Runnable() {
1373 public void run() {
1374 if (DEBUG_LOADERS) {
1375 Log.d(TAG, "Going to start binding widgets soon.");
1376 }
1377 }
1378 });
1379 // Bind the widgets, one at a time.
1380 // WARNING: this is calling into the workspace from the background thread,
1381 // but since getCurrentScreen() just returns the int, we should be okay. This
1382 // is just a hint for the order, and if it's wrong, we'll be okay.
1383 // TODO: instead, we should have that push the current screen into here.
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001384 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001385 // once for the current screen
1386 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001387 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001388 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001389 mHandler.post(new Runnable() {
1390 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001391 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001393 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 }
1395 }
1396 });
1397 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001398 }
Joe Onorato36115782010-06-17 13:28:48 -04001399 // once for the other screens
1400 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001401 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001402 if (widget.screen != currentScreen) {
1403 mHandler.post(new Runnable() {
1404 public void run() {
1405 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1406 if (callbacks != null) {
1407 callbacks.bindAppWidget(widget);
1408 }
1409 }
1410 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001411 }
1412 }
Joe Onorato36115782010-06-17 13:28:48 -04001413 // Tell the workspace that we're done.
1414 mHandler.post(new Runnable() {
1415 public void run() {
1416 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1417 if (callbacks != null) {
1418 callbacks.finishBindingItems();
1419 }
1420 }
1421 });
Winson Chung98e030b2012-05-07 16:01:11 -07001422 // Cleanup
Joe Onorato36115782010-06-17 13:28:48 -04001423 mHandler.post(new Runnable() {
1424 public void run() {
Winson Chung98e030b2012-05-07 16:01:11 -07001425 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001426 if (DEBUG_LOADERS) {
1427 Log.d(TAG, "bound workspace in "
1428 + (SystemClock.uptimeMillis()-t) + "ms");
1429 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001430
1431 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001432 }
1433 });
1434 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001435
Joe Onorato36115782010-06-17 13:28:48 -04001436 private void loadAndBindAllApps() {
1437 if (DEBUG_LOADERS) {
1438 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1439 }
1440 if (!mAllAppsLoaded) {
1441 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001442 synchronized (LoaderTask.this) {
1443 if (mStopped) {
1444 return;
1445 }
1446 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001447 }
Joe Onorato36115782010-06-17 13:28:48 -04001448 } else {
1449 onlyBindAllApps();
1450 }
1451 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001452
Joe Onorato36115782010-06-17 13:28:48 -04001453 private void onlyBindAllApps() {
1454 final Callbacks oldCallbacks = mCallbacks.get();
1455 if (oldCallbacks == null) {
1456 // This launcher has exited and nobody bothered to tell us. Just bail.
1457 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1458 return;
1459 }
1460
1461 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001462 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001463 final ArrayList<ApplicationInfo> list
Michael Jurka92f3d462011-11-22 21:02:29 -08001464 = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone();
Joe Onorato36115782010-06-17 13:28:48 -04001465 mHandler.post(new Runnable() {
1466 public void run() {
1467 final long t = SystemClock.uptimeMillis();
1468 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1469 if (callbacks != null) {
1470 callbacks.bindAllApplications(list);
1471 }
1472 if (DEBUG_LOADERS) {
1473 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1474 + (SystemClock.uptimeMillis()-t) + "ms");
1475 }
1476 }
1477 });
1478
1479 }
1480
1481 private void loadAllAppsByBatch() {
1482 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1483
1484 // Don't use these two variables in any of the callback runnables.
1485 // Otherwise we hold a reference to them.
1486 final Callbacks oldCallbacks = mCallbacks.get();
1487 if (oldCallbacks == null) {
1488 // This launcher has exited and nobody bothered to tell us. Just bail.
1489 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1490 return;
1491 }
1492
1493 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1494 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1495
1496 final PackageManager packageManager = mContext.getPackageManager();
1497 List<ResolveInfo> apps = null;
1498
1499 int N = Integer.MAX_VALUE;
1500
1501 int startIndex;
1502 int i=0;
1503 int batchSize = -1;
1504 while (i < N && !mStopped) {
1505 if (i == 0) {
1506 mAllAppsList.clear();
1507 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1508 apps = packageManager.queryIntentActivities(mainIntent, 0);
1509 if (DEBUG_LOADERS) {
1510 Log.d(TAG, "queryIntentActivities took "
1511 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1512 }
1513 if (apps == null) {
1514 return;
1515 }
1516 N = apps.size();
1517 if (DEBUG_LOADERS) {
1518 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1519 }
1520 if (N == 0) {
1521 // There are no apps?!?
1522 return;
1523 }
1524 if (mBatchSize == 0) {
1525 batchSize = N;
1526 } else {
1527 batchSize = mBatchSize;
1528 }
1529
1530 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1531 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001532 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001533 if (DEBUG_LOADERS) {
1534 Log.d(TAG, "sort took "
1535 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1536 }
1537 }
1538
1539 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1540
1541 startIndex = i;
1542 for (int j=0; i<N && j<batchSize; j++) {
1543 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001544 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001545 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001546 i++;
1547 }
1548
1549 final boolean first = i <= batchSize;
1550 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1551 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1552 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1553
Joe Onoratocc67f472010-06-08 10:54:30 -07001554 mHandler.post(new Runnable() {
1555 public void run() {
1556 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001557 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001558 if (first) {
1559 callbacks.bindAllApplications(added);
1560 } else {
1561 callbacks.bindAppsAdded(added);
1562 }
1563 if (DEBUG_LOADERS) {
1564 Log.d(TAG, "bound " + added.size() + " apps in "
1565 + (SystemClock.uptimeMillis() - t) + "ms");
1566 }
1567 } else {
1568 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001569 }
1570 }
1571 });
1572
Daniel Sandlerdca66122010-04-13 16:23:58 -04001573 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001574 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1575 + (SystemClock.uptimeMillis()-t2) + "ms");
1576 }
1577
1578 if (mAllAppsLoadDelay > 0 && i < N) {
1579 try {
1580 if (DEBUG_LOADERS) {
1581 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1582 }
1583 Thread.sleep(mAllAppsLoadDelay);
1584 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 }
1586 }
1587
Joe Onorato36115782010-06-17 13:28:48 -04001588 if (DEBUG_LOADERS) {
1589 Log.d(TAG, "cached all " + N + " apps in "
1590 + (SystemClock.uptimeMillis()-t) + "ms"
1591 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001592 }
1593 }
1594
1595 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001596 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1597 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1598 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1599 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1600 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001601 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001602 }
1603 }
1604
1605 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001606 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001607 }
1608
1609 private class PackageUpdatedTask implements Runnable {
1610 int mOp;
1611 String[] mPackages;
1612
1613 public static final int OP_NONE = 0;
1614 public static final int OP_ADD = 1;
1615 public static final int OP_UPDATE = 2;
1616 public static final int OP_REMOVE = 3; // uninstlled
1617 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1618
1619
1620 public PackageUpdatedTask(int op, String[] packages) {
1621 mOp = op;
1622 mPackages = packages;
1623 }
1624
1625 public void run() {
1626 final Context context = mApp;
1627
1628 final String[] packages = mPackages;
1629 final int N = packages.length;
1630 switch (mOp) {
1631 case OP_ADD:
1632 for (int i=0; i<N; i++) {
1633 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1634 mAllAppsList.addPackage(context, packages[i]);
1635 }
1636 break;
1637 case OP_UPDATE:
1638 for (int i=0; i<N; i++) {
1639 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1640 mAllAppsList.updatePackage(context, packages[i]);
1641 }
1642 break;
1643 case OP_REMOVE:
1644 case OP_UNAVAILABLE:
1645 for (int i=0; i<N; i++) {
1646 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1647 mAllAppsList.removePackage(packages[i]);
1648 }
1649 break;
1650 }
1651
1652 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04001653 ArrayList<ApplicationInfo> modified = null;
1654
1655 if (mAllAppsList.added.size() > 0) {
1656 added = mAllAppsList.added;
1657 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1658 }
Joe Onorato36115782010-06-17 13:28:48 -04001659 if (mAllAppsList.modified.size() > 0) {
1660 modified = mAllAppsList.modified;
1661 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1662 }
1663
Winson Chungcd810732012-06-18 16:45:43 -07001664 // We may be removing packages that have no associated launcher application, so we
1665 // pass through the removed package names directly.
1666 // NOTE: We flush the icon cache aggressively in removePackage() above.
1667 final ArrayList<String> removedPackageNames = new ArrayList<String>();
1668 for (int i = 0; i < N; ++i) {
1669 removedPackageNames.add(packages[i]);
1670 }
1671
Joe Onorato36115782010-06-17 13:28:48 -04001672 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1673 if (callbacks == null) {
1674 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1675 return;
1676 }
1677
1678 if (added != null) {
1679 final ArrayList<ApplicationInfo> addedFinal = added;
1680 mHandler.post(new Runnable() {
1681 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001682 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1683 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001684 callbacks.bindAppsAdded(addedFinal);
1685 }
1686 }
1687 });
1688 }
1689 if (modified != null) {
1690 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1691 mHandler.post(new Runnable() {
1692 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001693 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1694 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001695 callbacks.bindAppsUpdated(modifiedFinal);
1696 }
1697 }
1698 });
1699 }
Winson Chungcd810732012-06-18 16:45:43 -07001700 if (!removedPackageNames.isEmpty()) {
Joe Onorato36115782010-06-17 13:28:48 -04001701 final boolean permanent = mOp != OP_UNAVAILABLE;
Joe Onorato36115782010-06-17 13:28:48 -04001702 mHandler.post(new Runnable() {
1703 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001704 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1705 if (callbacks == cb && cb != null) {
Winson Chungcd810732012-06-18 16:45:43 -07001706 callbacks.bindAppsRemoved(removedPackageNames, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04001707 }
1708 }
1709 });
Joe Onoratobe386092009-11-17 17:32:16 -08001710 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001711
1712 mHandler.post(new Runnable() {
1713 @Override
1714 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001715 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1716 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001717 callbacks.bindPackagesUpdated();
1718 }
1719 }
1720 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001721 }
1722 }
1723
1724 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001725 * This is called from the code that adds shortcuts from the intent receiver. This
1726 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727 */
Joe Onorato56d82912010-03-07 14:32:10 -05001728 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001729 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001731
Joe Onorato56d82912010-03-07 14:32:10 -05001732 /**
1733 * Make an ShortcutInfo object for a shortcut that is an application.
1734 *
1735 * If c is not null, then it will be used to fill in missing data like the title and icon.
1736 */
1737 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001738 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001739 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001740 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001741
1742 ComponentName componentName = intent.getComponent();
1743 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001744 return null;
1745 }
1746
Adam Cohen00fcb492011-11-02 21:53:47 -07001747 try {
1748 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
1749 if (!pi.applicationInfo.enabled) {
1750 // If we return null here, the corresponding item will be removed from the launcher
1751 // db and will not appear in the workspace.
1752 return null;
1753 }
1754 } catch (NameNotFoundException e) {
1755 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
1756 }
1757
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001758 // TODO: See if the PackageManager knows about this case. If it doesn't
1759 // then return null & delete this.
1760
Joe Onorato56d82912010-03-07 14:32:10 -05001761 // the resource -- This may implicitly give us back the fallback icon,
1762 // but don't worry about that. All we're doing with usingFallbackIcon is
1763 // to avoid saving lots of copies of that in the database, and most apps
1764 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07001765
1766 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
1767 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
1768 // via resolveActivity().
1769 ResolveInfo resolveInfo = null;
1770 ComponentName oldComponent = intent.getComponent();
1771 Intent newIntent = new Intent(intent.getAction(), null);
1772 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1773 newIntent.setPackage(oldComponent.getPackageName());
1774 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
1775 for (ResolveInfo i : infos) {
1776 ComponentName cn = new ComponentName(i.activityInfo.packageName,
1777 i.activityInfo.name);
1778 if (cn.equals(oldComponent)) {
1779 resolveInfo = i;
1780 }
1781 }
1782 if (resolveInfo == null) {
1783 resolveInfo = manager.resolveActivity(intent, 0);
1784 }
Joe Onorato56d82912010-03-07 14:32:10 -05001785 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001786 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001787 }
Joe Onorato56d82912010-03-07 14:32:10 -05001788 // the db
1789 if (icon == null) {
1790 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001791 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001792 }
1793 }
1794 // the fallback icon
1795 if (icon == null) {
1796 icon = getFallbackIcon();
1797 info.usingFallbackIcon = true;
1798 }
1799 info.setIcon(icon);
1800
1801 // from the resource
1802 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001803 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1804 if (labelCache != null && labelCache.containsKey(key)) {
1805 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001806 } else {
1807 info.title = resolveInfo.activityInfo.loadLabel(manager);
1808 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001809 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001810 }
1811 }
Joe Onorato56d82912010-03-07 14:32:10 -05001812 }
1813 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001815 if (c != null) {
1816 info.title = c.getString(titleIndex);
1817 }
1818 }
1819 // fall back to the class name of the activity
1820 if (info.title == null) {
1821 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001822 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1824 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001826
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001828 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001830 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001831 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1832 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833
Joe Onorato56d82912010-03-07 14:32:10 -05001834 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001835 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001838 // TODO: If there's an explicit component and we can't install that, delete it.
1839
Joe Onorato56d82912010-03-07 14:32:10 -05001840 info.title = c.getString(titleIndex);
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 int iconType = c.getInt(iconTypeIndex);
1843 switch (iconType) {
1844 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1845 String packageName = c.getString(iconPackageIndex);
1846 String resourceName = c.getString(iconResourceIndex);
1847 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001848 info.customIcon = false;
1849 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001851 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001852 if (resources != null) {
1853 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001854 icon = Utilities.createIconBitmap(
1855 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001856 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001857 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001858 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
Joe Onorato56d82912010-03-07 14:32:10 -05001860 // the db
1861 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001862 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001863 }
1864 // the fallback icon
1865 if (icon == null) {
1866 icon = getFallbackIcon();
1867 info.usingFallbackIcon = true;
1868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001869 break;
1870 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001871 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001872 if (icon == null) {
1873 icon = getFallbackIcon();
1874 info.customIcon = false;
1875 info.usingFallbackIcon = true;
1876 } else {
1877 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001879 break;
1880 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001881 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001882 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001883 info.customIcon = false;
1884 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001886 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 return info;
1888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889
Michael Jurka931dc972011-08-05 15:08:15 -07001890 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07001891 @SuppressWarnings("all") // suppress dead code warning
1892 final boolean debug = false;
1893 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05001894 Log.d(TAG, "getIconFromCursor app="
1895 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1896 }
1897 byte[] data = c.getBlob(iconIndex);
1898 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001899 return Utilities.createIconBitmap(
1900 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001901 } catch (Exception e) {
1902 return null;
1903 }
1904 }
1905
Winson Chung3d503fb2011-07-13 17:25:49 -07001906 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1907 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001908 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001909 if (info == null) {
1910 return null;
1911 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001912 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001913
1914 return info;
1915 }
1916
Winson Chunga9abd0e2010-10-27 17:18:37 -07001917 /**
Winson Chung55cef262010-10-28 14:14:18 -07001918 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1919 */
1920 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1921 ComponentName component) {
1922 List<AppWidgetProviderInfo> widgets =
1923 AppWidgetManager.getInstance(context).getInstalledProviders();
1924 for (AppWidgetProviderInfo info : widgets) {
1925 if (info.provider.equals(component)) {
1926 return info;
1927 }
1928 }
1929 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001930 }
1931
Winson Chung68846fd2010-10-29 11:00:27 -07001932 /**
1933 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1934 */
1935 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1936 final PackageManager packageManager = context.getPackageManager();
1937 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1938 new ArrayList<WidgetMimeTypeHandlerData>();
1939
1940 final Intent supportsIntent =
1941 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1942 supportsIntent.setType(mimeType);
1943
1944 // Create a set of widget configuration components that we can test against
1945 final List<AppWidgetProviderInfo> widgets =
1946 AppWidgetManager.getInstance(context).getInstalledProviders();
1947 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1948 new HashMap<ComponentName, AppWidgetProviderInfo>();
1949 for (AppWidgetProviderInfo info : widgets) {
1950 configurationComponentToWidget.put(info.configure, info);
1951 }
1952
1953 // Run through each of the intents that can handle this type of clip data, and cross
1954 // reference them with the components that are actual configuration components
1955 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1956 PackageManager.MATCH_DEFAULT_ONLY);
1957 for (ResolveInfo info : activities) {
1958 final ActivityInfo activityInfo = info.activityInfo;
1959 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1960 activityInfo.name);
1961 if (configurationComponentToWidget.containsKey(infoComponent)) {
1962 supportedConfigurationActivities.add(
1963 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1964 configurationComponentToWidget.get(infoComponent)));
1965 }
1966 }
1967 return supportedConfigurationActivities;
1968 }
1969
Winson Chunga9abd0e2010-10-27 17:18:37 -07001970 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001971 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1972 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1973 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1974
Adam Cohend9198822011-11-22 16:42:47 -08001975 if (intent == null) {
1976 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
1977 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
1978 return null;
1979 }
1980
Joe Onorato0589f0f2010-02-08 13:44:00 -08001981 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001982 boolean customIcon = false;
1983 ShortcutIconResource iconResource = null;
1984
1985 if (bitmap != null && bitmap instanceof Bitmap) {
1986 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001987 customIcon = true;
1988 } else {
1989 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1990 if (extra != null && extra instanceof ShortcutIconResource) {
1991 try {
1992 iconResource = (ShortcutIconResource) extra;
1993 final PackageManager packageManager = context.getPackageManager();
1994 Resources resources = packageManager.getResourcesForApplication(
1995 iconResource.packageName);
1996 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001997 icon = Utilities.createIconBitmap(
1998 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001999 } catch (Exception e) {
2000 Log.w(TAG, "Could not load shortcut icon: " + extra);
2001 }
2002 }
2003 }
2004
Michael Jurkac9d95c52011-08-29 14:03:34 -07002005 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002006
2007 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002008 if (fallbackIcon != null) {
2009 icon = fallbackIcon;
2010 } else {
2011 icon = getFallbackIcon();
2012 info.usingFallbackIcon = true;
2013 }
Joe Onorato56d82912010-03-07 14:32:10 -05002014 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002015 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002016
Joe Onorato0589f0f2010-02-08 13:44:00 -08002017 info.title = name;
2018 info.intent = intent;
2019 info.customIcon = customIcon;
2020 info.iconResource = iconResource;
2021
2022 return info;
2023 }
2024
Winson Chungaac01e12011-08-17 10:37:13 -07002025 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2026 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002027 // If apps can't be on SD, don't even bother.
2028 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002029 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002030 }
Joe Onorato56d82912010-03-07 14:32:10 -05002031 // If this icon doesn't have a custom icon, check to see
2032 // what's stored in the DB, and if it doesn't match what
2033 // we're going to show, store what we are going to show back
2034 // into the DB. We do this so when we're loading, if the
2035 // package manager can't find an icon (for example because
2036 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002037 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002038 cache.put(info, c.getBlob(iconIndex));
2039 return true;
2040 }
2041 return false;
2042 }
2043 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2044 boolean needSave = false;
2045 try {
2046 if (data != null) {
2047 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2048 Bitmap loaded = info.getIcon(mIconCache);
2049 needSave = !saved.sameAs(loaded);
2050 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002051 needSave = true;
2052 }
Winson Chungaac01e12011-08-17 10:37:13 -07002053 } catch (Exception e) {
2054 needSave = true;
2055 }
2056 if (needSave) {
2057 Log.d(TAG, "going to save icon bitmap for info=" + info);
2058 // This is slower than is ideal, but this only happens once
2059 // or when the app is updated with a new icon.
2060 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002061 }
2062 }
2063
Joe Onorato9c1289c2009-08-17 11:03:03 -04002064 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002065 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002066 * or make a new one.
2067 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002068 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002069 // See if a placeholder was created for us already
2070 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002071 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002073 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074 folders.put(id, folderInfo);
2075 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002076 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
2078
Joe Onorato9c1289c2009-08-17 11:03:03 -04002079 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08002080 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 = new Comparator<ApplicationInfo>() {
2082 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07002083 int result = sCollator.compare(a.title.toString(), b.title.toString());
2084 if (result == 0) {
2085 result = a.componentName.compareTo(b.componentName);
2086 }
2087 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002089 };
Winson Chung78403fe2011-01-21 15:38:02 -08002090 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2091 = new Comparator<ApplicationInfo>() {
2092 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2093 if (a.firstInstallTime < b.firstInstallTime) return 1;
2094 if (a.firstInstallTime > b.firstInstallTime) return -1;
2095 return 0;
2096 }
2097 };
Winson Chung785d2eb2011-04-14 16:08:02 -07002098 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
2099 = new Comparator<AppWidgetProviderInfo>() {
2100 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2101 return sCollator.compare(a.label.toString(), b.label.toString());
2102 }
2103 };
Winson Chung5308f242011-08-18 12:12:41 -07002104 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2105 if (info.activityInfo != null) {
2106 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2107 } else {
2108 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2109 }
2110 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002111 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
2112 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002113 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002114 ShortcutNameComparator(PackageManager pm) {
2115 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002116 mLabelCache = new HashMap<Object, CharSequence>();
2117 }
2118 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2119 mPackageManager = pm;
2120 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002121 }
2122 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002123 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002124 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2125 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2126 if (mLabelCache.containsKey(keyA)) {
2127 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002128 } else {
2129 labelA = a.loadLabel(mPackageManager).toString();
2130
Winson Chung5308f242011-08-18 12:12:41 -07002131 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002132 }
Winson Chung5308f242011-08-18 12:12:41 -07002133 if (mLabelCache.containsKey(keyB)) {
2134 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002135 } else {
2136 labelB = b.loadLabel(mPackageManager).toString();
2137
Winson Chung5308f242011-08-18 12:12:41 -07002138 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002139 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002140 return sCollator.compare(labelA, labelB);
2141 }
2142 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002143 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
2144 private PackageManager mPackageManager;
2145 private HashMap<Object, String> mLabelCache;
2146 WidgetAndShortcutNameComparator(PackageManager pm) {
2147 mPackageManager = pm;
2148 mLabelCache = new HashMap<Object, String>();
2149 }
2150 public final int compare(Object a, Object b) {
2151 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002152 if (mLabelCache.containsKey(a)) {
2153 labelA = mLabelCache.get(a);
2154 } else {
2155 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002156 ((AppWidgetProviderInfo) a).label :
2157 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002158 mLabelCache.put(a, labelA);
2159 }
2160 if (mLabelCache.containsKey(b)) {
2161 labelB = mLabelCache.get(b);
2162 } else {
2163 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002164 ((AppWidgetProviderInfo) b).label :
2165 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002166 mLabelCache.put(b, labelB);
2167 }
Winson Chung1ed747a2011-05-03 16:18:34 -07002168 return sCollator.compare(labelA, labelB);
2169 }
2170 };
Joe Onoratobe386092009-11-17 17:32:16 -08002171
2172 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002173 Log.d(TAG, "mCallbacks=" + mCallbacks);
2174 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
2175 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
2176 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
2177 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002178 if (mLoaderTask != null) {
2179 mLoaderTask.dumpState();
2180 } else {
2181 Log.d(TAG, "mLoaderTask=null");
2182 }
Joe Onoratobe386092009-11-17 17:32:16 -08002183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184}