blob: 33e350fadce203216cecf11bc96e70030ebe9497 [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
Winson Chung68846fd2010-10-29 11:00:27 -070019import java.lang.ref.WeakReference;
20import java.net.URISyntaxException;
21import java.text.Collator;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.List;
Winson Chungaafa03c2010-06-11 17:34:16 -070027
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040030import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080032import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ContentResolver;
34import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.ActivityInfo;
39import android.content.pm.PackageManager;
40import android.content.pm.ResolveInfo;
41import android.content.res.Resources;
42import android.database.Cursor;
43import android.graphics.Bitmap;
44import android.graphics.BitmapFactory;
45import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080046import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040047import android.os.Handler;
48import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040052import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
Winson Chung68846fd2010-10-29 11:00:27 -070055import com.android.launcher.R;
56import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080057
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * Maintains in-memory state of the Launcher. It is expected that there should be only one
60 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070061 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040063public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080064 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070066
Joe Onorato36115782010-06-17 13:28:48 -040067 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080068 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040069 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040070 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040071
Joe Onoratof99f8c12009-10-31 17:27:36 -040072 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040073 private final Object mLock = new Object();
74 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040075 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070077 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
78 static {
79 sWorkerThread.start();
80 }
81 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
82
Joe Onoratocc67f472010-06-08 10:54:30 -070083 // We start off with everything not loaded. After that, we assume that
84 // our monitoring of the package manager provides all updates and we never
85 // need to do a requery. These are only ever touched from the loader thread.
86 private boolean mWorkspaceLoaded;
87 private boolean mAllAppsLoaded;
88
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Michael Jurkaa8c760d2011-04-28 14:59:33 -070091 // < only access in worker thread >
92 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080093
Michael Jurkaa8c760d2011-04-28 14:59:33 -070094 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
95 // LauncherModel to their ids
96 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
97
98 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
99 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
100 // within folders).
101 static final ArrayList<ItemInfo> sItems = new ArrayList<ItemInfo>();
102
103 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
104 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
105 new ArrayList<LauncherAppWidgetInfo>();
106
107 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
108 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
109 // </ only access in worker thread >
110
111 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800112 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Adam Cohend22015c2010-07-26 22:02:18 -0700114 private static int mCellCountX;
115 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700118 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 public int getCurrentWorkspaceScreen();
120 public void startBinding();
121 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500122 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 public void finishBindingItems();
124 public void bindAppWidget(LauncherAppWidgetInfo info);
125 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500126 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
127 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400128 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700129 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400130 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato0589f0f2010-02-08 13:44:00 -0800133 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800134 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400135 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136 mAllAppsList = new AllAppsList(iconCache);
137 mIconCache = iconCache;
138
139 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700140 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400141
142 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400143
144 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 }
146
Joe Onorato56d82912010-03-07 14:32:10 -0500147 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400149 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 /**
152 * Adds an item to the DB if it was not created previously, or move it to a new
153 * <container, screen, cellX, cellY>
154 */
155 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
156 int screen, int cellX, int cellY) {
157 if (item.container == ItemInfo.NO_ID) {
158 // From all apps
159 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
160 } else {
161 // From somewhere else
162 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 }
164 }
165
166 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400167 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700168 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700169 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
170 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 item.container = container;
172 item.screen = screen;
173 item.cellX = cellX;
174 item.cellY = cellY;
175
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700176 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 final ContentValues values = new ContentValues();
178 final ContentResolver cr = context.getContentResolver();
179
180 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700181 values.put(LauncherSettings.Favorites.CELLX, cellX);
182 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
184
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700185 sWorker.post(new Runnable() {
186 public void run() {
187 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700188 ItemInfo modelItem = sItemsIdMap.get(item.id);
189 if (item != modelItem) {
190 // the modelItem needs to match up perfectly with item if our model is to be
191 // consistent with the database-- for now, just require modelItem == item
192 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
193 "doesn't match original");
194 }
195
196 // Items are added/removed from the corresponding FolderInfo elsewhere, such
197 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
198 // that are on the desktop, as appropriate
199 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
200 if (!sItems.contains(modelItem)) {
201 sItems.add(modelItem);
202 }
203 } else {
204 sItems.remove(modelItem);
205 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700206 }
207 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700208 }
209
210 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800211 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800212 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700213 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
214 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800215 item.spanX = spanX;
216 item.spanY = spanY;
217 item.cellX = cellX;
218 item.cellY = cellY;
219
220 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
221 final ContentValues values = new ContentValues();
222 final ContentResolver cr = context.getContentResolver();
223
224 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
225 values.put(LauncherSettings.Favorites.SPANX, spanX);
226 values.put(LauncherSettings.Favorites.SPANY, spanY);
227 values.put(LauncherSettings.Favorites.CELLX, cellX);
228 values.put(LauncherSettings.Favorites.CELLY, cellY);
229
230 sWorker.post(new Runnable() {
231 public void run() {
232 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700233 ItemInfo modelItem = sItemsIdMap.get(item.id);
234 if (item != modelItem) {
235 // the modelItem needs to match up perfectly with item if our model is to be
236 // consistent with the database-- for now, just require modelItem == item
237 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
238 "doesn't match original");
239 }
Adam Cohend4844c32011-02-18 19:25:06 -0800240 }
241 });
242 }
243
244 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 * Returns true if the shortcuts already exists in the database.
246 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248 static boolean shortcutExists(Context context, String title, Intent intent) {
249 final ContentResolver cr = context.getContentResolver();
250 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
251 new String[] { "title", "intent" }, "title=? and intent=?",
252 new String[] { title, intent.toUri(0) }, null);
253 boolean result = false;
254 try {
255 result = c.moveToFirst();
256 } finally {
257 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700260 }
261
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700263 * Returns an ItemInfo array containing all the items in the LauncherModel.
264 * The ItemInfo.id is not set through this function.
265 */
266 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
267 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
268 final ContentResolver cr = context.getContentResolver();
269 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
270 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
271 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
272 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
273
274 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
275 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
276 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
277 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
278 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
279 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
280 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
281
282 try {
283 while (c.moveToNext()) {
284 ItemInfo item = new ItemInfo();
285 item.cellX = c.getInt(cellXIndex);
286 item.cellY = c.getInt(cellYIndex);
287 item.spanX = c.getInt(spanXIndex);
288 item.spanY = c.getInt(spanYIndex);
289 item.container = c.getInt(containerIndex);
290 item.itemType = c.getInt(itemTypeIndex);
291 item.screen = c.getInt(screenIndex);
292
293 items.add(item);
294 }
295 } catch (Exception e) {
296 items.clear();
297 } finally {
298 c.close();
299 }
300
301 return items;
302 }
303
304 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
306 */
307 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
308 final ContentResolver cr = context.getContentResolver();
309 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
310 "_id=? and (itemType=? or itemType=?)",
311 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700312 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700313
Joe Onorato9c1289c2009-08-17 11:03:03 -0400314 try {
315 if (c.moveToFirst()) {
316 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
317 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
318 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
319 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
320 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
321 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322
Joe Onorato9c1289c2009-08-17 11:03:03 -0400323 FolderInfo folderInfo = null;
324 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700325 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
326 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400327 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700328 }
329
Joe Onorato9c1289c2009-08-17 11:03:03 -0400330 folderInfo.title = c.getString(titleIndex);
331 folderInfo.id = id;
332 folderInfo.container = c.getInt(containerIndex);
333 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700334 folderInfo.cellX = c.getInt(cellXIndex);
335 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400336
337 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400339 } finally {
340 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700341 }
342
343 return null;
344 }
345
Joe Onorato9c1289c2009-08-17 11:03:03 -0400346 /**
347 * Add an item to the database in a specified container. Sets the container, screen, cellX and
348 * cellY fields of the item. Also assigns an ID to the item.
349 */
Adam Cohend0445262011-07-04 23:53:22 -0700350 static void addItemToDatabase(Context context, final ItemInfo item, long container,
351 int screen, int cellX, int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 item.container = container;
353 item.screen = screen;
354 item.cellX = cellX;
355 item.cellY = cellY;
356
357 final ContentValues values = new ContentValues();
358 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359 item.onAddToDatabase(values);
360
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700361 Launcher l = (Launcher) context;
362 LauncherApplication app = (LauncherApplication) l.getApplication();
363 item.id = app.getLauncherProvider().generateNewId();
364 values.put(LauncherSettings.Favorites._ID, item.id);
Adam Cohend22015c2010-07-26 22:02:18 -0700365 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700366
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700367 sWorker.post(new Runnable() {
368 public void run() {
369 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
370 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700372 sItemsIdMap.put(item.id, item);
373 switch (item.itemType) {
374 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
375 sFolders.put(item.id, (FolderInfo) item);
376 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
377 sItems.add(item);
378 }
379 break;
380 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
381 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
382 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
383 sItems.add(item);
384 }
385 break;
386 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
387 sAppWidgets.add((LauncherAppWidgetInfo) item);
388 break;
389 }
390 }
391 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700392 }
393
Joe Onorato9c1289c2009-08-17 11:03:03 -0400394 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700395 * Creates a new unique child id, for a given cell span across all layouts.
396 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700397 static int getCellLayoutChildId(
398 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
399 return ((cellId & 0xFF) << 24)
400 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700401 }
402
Adam Cohend22015c2010-07-26 22:02:18 -0700403 static int getCellCountX() {
404 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700405 }
406
Adam Cohend22015c2010-07-26 22:02:18 -0700407 static int getCellCountY() {
408 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700409 }
410
411 /**
412 * Updates the model orientation helper to take into account the current layout dimensions
413 * when performing local/canonical coordinate transformations.
414 */
415 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700416 mCellCountX = shortAxisCellCount;
417 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700418 }
419
420 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400421 * Update an item to the database in a specified container.
422 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700423 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400424 final ContentValues values = new ContentValues();
425 final ContentResolver cr = context.getContentResolver();
426
427 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700428 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700429
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700430 sWorker.post(new Runnable() {
431 public void run() {
432 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
433 values, null, null);
434 final ItemInfo modelItem = sItemsIdMap.get(item.id);
435 if (item != modelItem) {
436 // the modelItem needs to match up perfectly with item if our model is to be
437 // consistent with the database-- for now, just require modelItem == item
438 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
439 "doesn't match original");
440 }
441 }
442 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 }
444
445 /**
446 * Removes the specified item from the database
447 * @param context
448 * @param item
449 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700450 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700452 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700453 sWorker.post(new Runnable() {
454 public void run() {
455 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700456 switch (item.itemType) {
457 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
458 sFolders.remove(item.id);
459 sItems.remove(item);
460 break;
461 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
462 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
463 sItems.remove(item);
464 break;
465 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
466 sAppWidgets.remove((LauncherAppWidgetInfo) item);
467 break;
468 }
469 sItemsIdMap.remove(item.id);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700470 }
471 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400472 }
473
474 /**
475 * Remove the contents of the specified folder from the database
476 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700477 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400478 final ContentResolver cr = context.getContentResolver();
479
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700480 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700481 public void run() {
482 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700483 sItemsIdMap.remove(info.id);
484 sFolders.remove(info.id);
485 sItems.remove(info);
486
Adam Cohenafb01ee2011-06-23 15:38:03 -0700487 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
488 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700489 for (ItemInfo childInfo : info.contents) {
490 sItemsIdMap.remove(childInfo.id);
491 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700492 }
493 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400494 }
495
496 /**
497 * Set this as the current Launcher activity object for the loader.
498 */
499 public void initialize(Callbacks callbacks) {
500 synchronized (mLock) {
501 mCallbacks = new WeakReference<Callbacks>(callbacks);
502 }
503 }
504
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700505 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400506 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
507 * ACTION_PACKAGE_CHANGED.
508 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400509 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400510 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700511
Joe Onorato36115782010-06-17 13:28:48 -0400512 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400513
Joe Onorato36115782010-06-17 13:28:48 -0400514 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
515 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
516 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
517 final String packageName = intent.getData().getSchemeSpecificPart();
518 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519
Joe Onorato36115782010-06-17 13:28:48 -0400520 int op = PackageUpdatedTask.OP_NONE;
521
522 if (packageName == null || packageName.length() == 0) {
523 // they sent us a bad intent
524 return;
525 }
526
527 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
528 op = PackageUpdatedTask.OP_UPDATE;
529 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
530 if (!replacing) {
531 op = PackageUpdatedTask.OP_REMOVE;
532 }
533 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
534 // later, we will update the package at this time
535 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
536 if (!replacing) {
537 op = PackageUpdatedTask.OP_ADD;
538 } else {
539 op = PackageUpdatedTask.OP_UPDATE;
540 }
541 }
542
543 if (op != PackageUpdatedTask.OP_NONE) {
544 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
545 }
546
547 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400548 // First, schedule to add these apps back in.
549 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
550 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
551 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700552 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400553 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
554 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
555 enqueuePackageUpdated(new PackageUpdatedTask(
556 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700557 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
558 // If we have changed locale we need to clear out the labels in all apps.
559 // Do this here because if the launcher activity is running it will be restarted.
560 // If it's not running startLoaderFromBackground will merely tell it that it needs
561 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
562 // next time.
563 mAllAppsLoaded = false;
564 startLoaderFromBackground();
565 }
566 }
567
568 /**
569 * When the launcher is in the background, it's possible for it to miss paired
570 * configuration changes. So whenever we trigger the loader from the background
571 * tell the launcher that it needs to re-run the loader when it comes back instead
572 * of doing it now.
573 */
574 public void startLoaderFromBackground() {
575 boolean runLoader = false;
576 if (mCallbacks != null) {
577 Callbacks callbacks = mCallbacks.get();
578 if (callbacks != null) {
579 // Only actually run the loader if they're not paused.
580 if (!callbacks.setLoadOnResume()) {
581 runLoader = true;
582 }
583 }
584 }
585 if (runLoader) {
586 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700587 }
Joe Onorato36115782010-06-17 13:28:48 -0400588 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400589
Joe Onorato36115782010-06-17 13:28:48 -0400590 public void startLoader(Context context, boolean isLaunching) {
591 synchronized (mLock) {
592 if (DEBUG_LOADERS) {
593 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
594 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400595
Joe Onorato36115782010-06-17 13:28:48 -0400596 // Don't bother to start the thread if we know it's not going to do anything
597 if (mCallbacks != null && mCallbacks.get() != null) {
598 // If there is already one running, tell it to stop.
599 LoaderTask oldTask = mLoaderTask;
600 if (oldTask != null) {
601 if (oldTask.isLaunching()) {
602 // don't downgrade isLaunching if we're already running
603 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500604 }
Joe Onorato36115782010-06-17 13:28:48 -0400605 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500606 }
Joe Onorato36115782010-06-17 13:28:48 -0400607 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700608 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400609 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400610 }
611 }
612
Joe Onorato36115782010-06-17 13:28:48 -0400613 public void stopLoader() {
614 synchronized (mLock) {
615 if (mLoaderTask != null) {
616 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400617 }
618 }
Joe Onorato36115782010-06-17 13:28:48 -0400619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400620
Joe Onorato36115782010-06-17 13:28:48 -0400621 /**
622 * Runnable for the thread that loads the contents of the launcher:
623 * - workspace icons
624 * - widgets
625 * - all apps icons
626 */
627 private class LoaderTask implements Runnable {
628 private Context mContext;
629 private Thread mWaitThread;
630 private boolean mIsLaunching;
631 private boolean mStopped;
632 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700633 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400634
635 LoaderTask(Context context, boolean isLaunching) {
636 mContext = context;
637 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700638 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 }
640
Joe Onorato36115782010-06-17 13:28:48 -0400641 boolean isLaunching() {
642 return mIsLaunching;
643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644
Joe Onorato36115782010-06-17 13:28:48 -0400645 private void loadAndBindWorkspace() {
646 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400647 if (DEBUG_LOADERS) {
648 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700650 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400651 loadWorkspace();
652 if (mStopped) {
653 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 }
Joe Onorato36115782010-06-17 13:28:48 -0400655 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 }
657
Joe Onorato36115782010-06-17 13:28:48 -0400658 // Bind the workspace
659 bindWorkspace();
660 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400661
Joe Onorato36115782010-06-17 13:28:48 -0400662 private void waitForIdle() {
663 // Wait until the either we're stopped or the other threads are done.
664 // This way we don't start loading all apps until the workspace has settled
665 // down.
666 synchronized (LoaderTask.this) {
667 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700668
Joe Onorato36115782010-06-17 13:28:48 -0400669 mHandler.postIdle(new Runnable() {
670 public void run() {
671 synchronized (LoaderTask.this) {
672 mLoadAndBindStepFinished = true;
673 if (DEBUG_LOADERS) {
674 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400675 }
Joe Onorato36115782010-06-17 13:28:48 -0400676 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400677 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400678 }
Joe Onorato36115782010-06-17 13:28:48 -0400679 });
680
681 while (!mStopped && !mLoadAndBindStepFinished) {
682 try {
683 this.wait();
684 } catch (InterruptedException ex) {
685 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400686 }
687 }
Joe Onorato36115782010-06-17 13:28:48 -0400688 if (DEBUG_LOADERS) {
689 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700690 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400691 + "ms for previous step to finish binding");
692 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400693 }
Joe Onorato36115782010-06-17 13:28:48 -0400694 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400695
Joe Onorato36115782010-06-17 13:28:48 -0400696 public void run() {
697 // Optimize for end-user experience: if the Launcher is up and // running with the
698 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
699 // workspace first (default).
700 final Callbacks cbk = mCallbacks.get();
701 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400702
Joe Onorato36115782010-06-17 13:28:48 -0400703 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400704 // Elevate priority when Home launches for the first time to avoid
705 // starving at boot time. Staring at a blank home is not cool.
706 synchronized (mLock) {
707 android.os.Process.setThreadPriority(mIsLaunching
708 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
709 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400710 if (loadWorkspaceFirst) {
711 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
712 loadAndBindWorkspace();
713 } else {
714 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700715 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400716 }
717
Joe Onorato36115782010-06-17 13:28:48 -0400718 if (mStopped) {
719 break keep_running;
720 }
721
722 // Whew! Hard work done. Slow us down, and wait until the UI thread has
723 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400724 synchronized (mLock) {
725 if (mIsLaunching) {
726 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
727 }
728 }
Joe Onorato36115782010-06-17 13:28:48 -0400729 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400730
731 // second step
732 if (loadWorkspaceFirst) {
733 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700734 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400735 } else {
736 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
737 loadAndBindWorkspace();
738 }
Joe Onorato36115782010-06-17 13:28:48 -0400739 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740
Joe Onorato36115782010-06-17 13:28:48 -0400741 // Clear out this reference, otherwise we end up holding it until all of the
742 // callback runnables are done.
743 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744
Joe Onorato36115782010-06-17 13:28:48 -0400745 synchronized (mLock) {
746 // If we are still the last one to be scheduled, remove ourselves.
747 if (mLoaderTask == this) {
748 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 }
Joe Onorato36115782010-06-17 13:28:48 -0400750 }
Joe Onorato36115782010-06-17 13:28:48 -0400751 }
752
753 public void stopLocked() {
754 synchronized (LoaderTask.this) {
755 mStopped = true;
756 this.notify();
757 }
758 }
759
760 /**
761 * Gets the callbacks object. If we've been stopped, or if the launcher object
762 * has somehow been garbage collected, return null instead. Pass in the Callbacks
763 * object that was around when the deferred message was scheduled, and if there's
764 * a new Callbacks object around then also return null. This will save us from
765 * calling onto it with data that will be ignored.
766 */
767 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
768 synchronized (mLock) {
769 if (mStopped) {
770 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400771 }
Joe Onorato36115782010-06-17 13:28:48 -0400772
773 if (mCallbacks == null) {
774 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400775 }
Joe Onorato36115782010-06-17 13:28:48 -0400776
777 final Callbacks callbacks = mCallbacks.get();
778 if (callbacks != oldCallbacks) {
779 return null;
780 }
781 if (callbacks == null) {
782 Log.w(TAG, "no mCallbacks");
783 return null;
784 }
785
786 return callbacks;
787 }
788 }
789
790 // check & update map of what's occupied; used to discard overlapping/invalid items
791 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
792 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400793 return true;
794 }
Joe Onorato36115782010-06-17 13:28:48 -0400795 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
796 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
797 if (occupied[item.screen][x][y] != null) {
798 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700799 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400800 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700801 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400802 + occupied[item.screen][x][y]);
803 return false;
804 }
805 }
806 }
807 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
808 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
809 occupied[item.screen][x][y] = item;
810 }
811 }
812 return true;
813 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814
Joe Onorato36115782010-06-17 13:28:48 -0400815 private void loadWorkspace() {
816 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817
Joe Onorato36115782010-06-17 13:28:48 -0400818 final Context context = mContext;
819 final ContentResolver contentResolver = context.getContentResolver();
820 final PackageManager manager = context.getPackageManager();
821 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
822 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400823
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700824 sItems.clear();
825 sAppWidgets.clear();
826 sFolders.clear();
827 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800828
Joe Onorato36115782010-06-17 13:28:48 -0400829 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830
Joe Onorato36115782010-06-17 13:28:48 -0400831 final Cursor c = contentResolver.query(
832 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400833
Adam Cohend22015c2010-07-26 22:02:18 -0700834 final ItemInfo occupied[][][] =
835 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836
Joe Onorato36115782010-06-17 13:28:48 -0400837 try {
838 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
839 final int intentIndex = c.getColumnIndexOrThrow
840 (LauncherSettings.Favorites.INTENT);
841 final int titleIndex = c.getColumnIndexOrThrow
842 (LauncherSettings.Favorites.TITLE);
843 final int iconTypeIndex = c.getColumnIndexOrThrow(
844 LauncherSettings.Favorites.ICON_TYPE);
845 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
846 final int iconPackageIndex = c.getColumnIndexOrThrow(
847 LauncherSettings.Favorites.ICON_PACKAGE);
848 final int iconResourceIndex = c.getColumnIndexOrThrow(
849 LauncherSettings.Favorites.ICON_RESOURCE);
850 final int containerIndex = c.getColumnIndexOrThrow(
851 LauncherSettings.Favorites.CONTAINER);
852 final int itemTypeIndex = c.getColumnIndexOrThrow(
853 LauncherSettings.Favorites.ITEM_TYPE);
854 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
855 LauncherSettings.Favorites.APPWIDGET_ID);
856 final int screenIndex = c.getColumnIndexOrThrow(
857 LauncherSettings.Favorites.SCREEN);
858 final int cellXIndex = c.getColumnIndexOrThrow
859 (LauncherSettings.Favorites.CELLX);
860 final int cellYIndex = c.getColumnIndexOrThrow
861 (LauncherSettings.Favorites.CELLY);
862 final int spanXIndex = c.getColumnIndexOrThrow
863 (LauncherSettings.Favorites.SPANX);
864 final int spanYIndex = c.getColumnIndexOrThrow(
865 LauncherSettings.Favorites.SPANY);
866 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
867 final int displayModeIndex = c.getColumnIndexOrThrow(
868 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869
Joe Onorato36115782010-06-17 13:28:48 -0400870 ShortcutInfo info;
871 String intentDescription;
872 LauncherAppWidgetInfo appWidgetInfo;
873 int container;
874 long id;
875 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876
Joe Onorato36115782010-06-17 13:28:48 -0400877 while (!mStopped && c.moveToNext()) {
878 try {
879 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880
Joe Onorato36115782010-06-17 13:28:48 -0400881 switch (itemType) {
882 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
883 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
884 intentDescription = c.getString(intentIndex);
885 try {
886 intent = Intent.parseUri(intentDescription, 0);
887 } catch (URISyntaxException e) {
888 continue;
889 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890
Joe Onorato36115782010-06-17 13:28:48 -0400891 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
892 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700893 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400894 } else {
895 info = getShortcutInfo(c, context, iconTypeIndex,
896 iconPackageIndex, iconResourceIndex, iconIndex,
897 titleIndex);
898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899
Joe Onorato36115782010-06-17 13:28:48 -0400900 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400901 info.intent = intent;
902 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400904 info.container = container;
905 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700906 info.cellX = c.getInt(cellXIndex);
907 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908
Daniel Sandler8802e962010-05-26 16:28:16 -0400909 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400910 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400911 break;
912 }
913
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400915 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700916 sItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400917 break;
918 default:
919 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700920 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700921 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400922 folderInfo.add(info);
923 break;
924 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700925 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800926
927 // now that we've loaded everthing re-save it with the
928 // icon in case it disappears somehow.
929 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400930 } else {
931 // Failed to load the shortcut, probably because the
932 // activity manager couldn't resolve it (maybe the app
933 // was uninstalled), or the db row was somehow screwed up.
934 // Delete it.
935 id = c.getLong(idIndex);
936 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
937 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
938 id, false), null, null);
939 }
940 break;
941
Adam Cohendf2cc412011-04-27 16:56:57 -0700942 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -0400943 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700944 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400945
Winson Chungaafa03c2010-06-11 17:34:16 -0700946 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400947 folderInfo.id = id;
948 container = c.getInt(containerIndex);
949 folderInfo.container = container;
950 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700951 folderInfo.cellX = c.getInt(cellXIndex);
952 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400953
954 // check & update map of what's occupied
955 if (!checkItemPlacement(occupied, folderInfo)) {
956 break;
957 }
Joe Onorato36115782010-06-17 13:28:48 -0400958 switch (container) {
959 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700960 sItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400961 break;
962 }
963
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700964 sItemsIdMap.put(folderInfo.id, folderInfo);
965 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400966 break;
967
Joe Onorato36115782010-06-17 13:28:48 -0400968 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
969 // Read all Launcher-specific widget details
970 int appWidgetId = c.getInt(appWidgetIdIndex);
971 id = c.getLong(idIndex);
972
973 final AppWidgetProviderInfo provider =
974 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700975
Joe Onorato36115782010-06-17 13:28:48 -0400976 if (!isSafeMode && (provider == null || provider.provider == null ||
977 provider.provider.getPackageName() == null)) {
978 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
979 + id + " appWidgetId=" + appWidgetId);
980 itemsToRemove.add(id);
981 } else {
982 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
983 appWidgetInfo.id = id;
984 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700985 appWidgetInfo.cellX = c.getInt(cellXIndex);
986 appWidgetInfo.cellY = c.getInt(cellYIndex);
987 appWidgetInfo.spanX = c.getInt(spanXIndex);
988 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400989
990 container = c.getInt(containerIndex);
991 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
992 Log.e(TAG, "Widget found where container "
993 + "!= CONTAINER_DESKTOP -- ignoring!");
994 continue;
995 }
996 appWidgetInfo.container = c.getInt(containerIndex);
997
998 // check & update map of what's occupied
999 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1000 break;
1001 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001002 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1003 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001004 }
1005 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001006 }
Joe Onorato36115782010-06-17 13:28:48 -04001007 } catch (Exception e) {
1008 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001009 }
1010 }
Joe Onorato36115782010-06-17 13:28:48 -04001011 } finally {
1012 c.close();
1013 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001014
Joe Onorato36115782010-06-17 13:28:48 -04001015 if (itemsToRemove.size() > 0) {
1016 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1017 LauncherSettings.Favorites.CONTENT_URI);
1018 // Remove dead items
1019 for (long id : itemsToRemove) {
1020 if (DEBUG_LOADERS) {
1021 Log.d(TAG, "Removed id = " + id);
1022 }
1023 // Don't notify content observers
1024 try {
1025 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1026 null, null);
1027 } catch (RemoteException e) {
1028 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001029 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001030 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001031 }
1032
Joe Onorato36115782010-06-17 13:28:48 -04001033 if (DEBUG_LOADERS) {
1034 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1035 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001036 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001037 String line = "";
1038 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1039 if (s > 0) {
1040 line += " | ";
1041 }
Adam Cohend22015c2010-07-26 22:02:18 -07001042 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001043 line += ((occupied[s][x][y] != null) ? "#" : ".");
1044 }
1045 }
1046 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 }
Joe Onorato36115782010-06-17 13:28:48 -04001048 }
1049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050
Joe Onorato36115782010-06-17 13:28:48 -04001051 /**
1052 * Read everything out of our database.
1053 */
1054 private void bindWorkspace() {
1055 final long t = SystemClock.uptimeMillis();
1056
1057 // Don't use these two variables in any of the callback runnables.
1058 // Otherwise we hold a reference to them.
1059 final Callbacks oldCallbacks = mCallbacks.get();
1060 if (oldCallbacks == null) {
1061 // This launcher has exited and nobody bothered to tell us. Just bail.
1062 Log.w(TAG, "LoaderTask running with no launcher");
1063 return;
1064 }
1065
1066 int N;
1067 // Tell the workspace that we're about to start firing items at it
1068 mHandler.post(new Runnable() {
1069 public void run() {
1070 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1071 if (callbacks != null) {
1072 callbacks.startBinding();
1073 }
1074 }
1075 });
1076 // Add the items to the workspace.
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001077 N = sItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001078 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1079 final int start = i;
1080 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081 mHandler.post(new Runnable() {
1082 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001083 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001085 callbacks.bindItems(sItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086 }
1087 }
1088 });
Joe Onorato36115782010-06-17 13:28:48 -04001089 }
1090 mHandler.post(new Runnable() {
1091 public void run() {
1092 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1093 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001094 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001095 }
1096 }
1097 });
1098 // Wait until the queue goes empty.
1099 mHandler.post(new Runnable() {
1100 public void run() {
1101 if (DEBUG_LOADERS) {
1102 Log.d(TAG, "Going to start binding widgets soon.");
1103 }
1104 }
1105 });
1106 // Bind the widgets, one at a time.
1107 // WARNING: this is calling into the workspace from the background thread,
1108 // but since getCurrentScreen() just returns the int, we should be okay. This
1109 // is just a hint for the order, and if it's wrong, we'll be okay.
1110 // TODO: instead, we should have that push the current screen into here.
1111 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001112 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001113 // once for the current screen
1114 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001115 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001116 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 mHandler.post(new Runnable() {
1118 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001119 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001120 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001121 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 }
1123 }
1124 });
1125 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 }
Joe Onorato36115782010-06-17 13:28:48 -04001127 // once for the other screens
1128 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001129 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001130 if (widget.screen != currentScreen) {
1131 mHandler.post(new Runnable() {
1132 public void run() {
1133 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1134 if (callbacks != null) {
1135 callbacks.bindAppWidget(widget);
1136 }
1137 }
1138 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001139 }
1140 }
Joe Onorato36115782010-06-17 13:28:48 -04001141 // Tell the workspace that we're done.
1142 mHandler.post(new Runnable() {
1143 public void run() {
1144 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1145 if (callbacks != null) {
1146 callbacks.finishBindingItems();
1147 }
1148 }
1149 });
1150 // If we're profiling, this is the last thing in the queue.
1151 mHandler.post(new Runnable() {
1152 public void run() {
1153 if (DEBUG_LOADERS) {
1154 Log.d(TAG, "bound workspace in "
1155 + (SystemClock.uptimeMillis()-t) + "ms");
1156 }
1157 }
1158 });
1159 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001160
Joe Onorato36115782010-06-17 13:28:48 -04001161 private void loadAndBindAllApps() {
1162 if (DEBUG_LOADERS) {
1163 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1164 }
1165 if (!mAllAppsLoaded) {
1166 loadAllAppsByBatch();
1167 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001168 return;
1169 }
Joe Onorato36115782010-06-17 13:28:48 -04001170 mAllAppsLoaded = true;
1171 } else {
1172 onlyBindAllApps();
1173 }
1174 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001175
Joe Onorato36115782010-06-17 13:28:48 -04001176 private void onlyBindAllApps() {
1177 final Callbacks oldCallbacks = mCallbacks.get();
1178 if (oldCallbacks == null) {
1179 // This launcher has exited and nobody bothered to tell us. Just bail.
1180 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1181 return;
1182 }
1183
1184 // shallow copy
1185 final ArrayList<ApplicationInfo> list
1186 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1187 mHandler.post(new Runnable() {
1188 public void run() {
1189 final long t = SystemClock.uptimeMillis();
1190 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1191 if (callbacks != null) {
1192 callbacks.bindAllApplications(list);
1193 }
1194 if (DEBUG_LOADERS) {
1195 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1196 + (SystemClock.uptimeMillis()-t) + "ms");
1197 }
1198 }
1199 });
1200
1201 }
1202
1203 private void loadAllAppsByBatch() {
1204 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1205
1206 // Don't use these two variables in any of the callback runnables.
1207 // Otherwise we hold a reference to them.
1208 final Callbacks oldCallbacks = mCallbacks.get();
1209 if (oldCallbacks == null) {
1210 // This launcher has exited and nobody bothered to tell us. Just bail.
1211 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1212 return;
1213 }
1214
1215 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1216 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1217
1218 final PackageManager packageManager = mContext.getPackageManager();
1219 List<ResolveInfo> apps = null;
1220
1221 int N = Integer.MAX_VALUE;
1222
1223 int startIndex;
1224 int i=0;
1225 int batchSize = -1;
1226 while (i < N && !mStopped) {
1227 if (i == 0) {
1228 mAllAppsList.clear();
1229 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1230 apps = packageManager.queryIntentActivities(mainIntent, 0);
1231 if (DEBUG_LOADERS) {
1232 Log.d(TAG, "queryIntentActivities took "
1233 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1234 }
1235 if (apps == null) {
1236 return;
1237 }
1238 N = apps.size();
1239 if (DEBUG_LOADERS) {
1240 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1241 }
1242 if (N == 0) {
1243 // There are no apps?!?
1244 return;
1245 }
1246 if (mBatchSize == 0) {
1247 batchSize = N;
1248 } else {
1249 batchSize = mBatchSize;
1250 }
1251
1252 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1253 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001254 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001255 if (DEBUG_LOADERS) {
1256 Log.d(TAG, "sort took "
1257 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1258 }
1259 }
1260
1261 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1262
1263 startIndex = i;
1264 for (int j=0; i<N && j<batchSize; j++) {
1265 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001266 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1267 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001268 i++;
1269 }
1270
1271 final boolean first = i <= batchSize;
1272 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1273 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1274 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1275
Joe Onoratocc67f472010-06-08 10:54:30 -07001276 mHandler.post(new Runnable() {
1277 public void run() {
1278 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001279 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001280 if (first) {
1281 callbacks.bindAllApplications(added);
1282 } else {
1283 callbacks.bindAppsAdded(added);
1284 }
1285 if (DEBUG_LOADERS) {
1286 Log.d(TAG, "bound " + added.size() + " apps in "
1287 + (SystemClock.uptimeMillis() - t) + "ms");
1288 }
1289 } else {
1290 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001291 }
1292 }
1293 });
1294
Daniel Sandlerdca66122010-04-13 16:23:58 -04001295 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001296 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1297 + (SystemClock.uptimeMillis()-t2) + "ms");
1298 }
1299
1300 if (mAllAppsLoadDelay > 0 && i < N) {
1301 try {
1302 if (DEBUG_LOADERS) {
1303 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1304 }
1305 Thread.sleep(mAllAppsLoadDelay);
1306 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001307 }
1308 }
1309
Joe Onorato36115782010-06-17 13:28:48 -04001310 if (DEBUG_LOADERS) {
1311 Log.d(TAG, "cached all " + N + " apps in "
1312 + (SystemClock.uptimeMillis()-t) + "ms"
1313 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001314 }
1315 }
1316
1317 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001318 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1319 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1320 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1321 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1322 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001323 Log.d(TAG, "mItems size=" + sItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001324 }
1325 }
1326
1327 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001328 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001329 }
1330
1331 private class PackageUpdatedTask implements Runnable {
1332 int mOp;
1333 String[] mPackages;
1334
1335 public static final int OP_NONE = 0;
1336 public static final int OP_ADD = 1;
1337 public static final int OP_UPDATE = 2;
1338 public static final int OP_REMOVE = 3; // uninstlled
1339 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1340
1341
1342 public PackageUpdatedTask(int op, String[] packages) {
1343 mOp = op;
1344 mPackages = packages;
1345 }
1346
1347 public void run() {
1348 final Context context = mApp;
1349
1350 final String[] packages = mPackages;
1351 final int N = packages.length;
1352 switch (mOp) {
1353 case OP_ADD:
1354 for (int i=0; i<N; i++) {
1355 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1356 mAllAppsList.addPackage(context, packages[i]);
1357 }
1358 break;
1359 case OP_UPDATE:
1360 for (int i=0; i<N; i++) {
1361 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1362 mAllAppsList.updatePackage(context, packages[i]);
1363 }
1364 break;
1365 case OP_REMOVE:
1366 case OP_UNAVAILABLE:
1367 for (int i=0; i<N; i++) {
1368 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1369 mAllAppsList.removePackage(packages[i]);
1370 }
1371 break;
1372 }
1373
1374 ArrayList<ApplicationInfo> added = null;
1375 ArrayList<ApplicationInfo> removed = null;
1376 ArrayList<ApplicationInfo> modified = null;
1377
1378 if (mAllAppsList.added.size() > 0) {
1379 added = mAllAppsList.added;
1380 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1381 }
1382 if (mAllAppsList.removed.size() > 0) {
1383 removed = mAllAppsList.removed;
1384 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1385 for (ApplicationInfo info: removed) {
1386 mIconCache.remove(info.intent.getComponent());
1387 }
1388 }
1389 if (mAllAppsList.modified.size() > 0) {
1390 modified = mAllAppsList.modified;
1391 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1392 }
1393
1394 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1395 if (callbacks == null) {
1396 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1397 return;
1398 }
1399
1400 if (added != null) {
1401 final ArrayList<ApplicationInfo> addedFinal = added;
1402 mHandler.post(new Runnable() {
1403 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001404 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1405 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001406 callbacks.bindAppsAdded(addedFinal);
1407 }
1408 }
1409 });
1410 }
1411 if (modified != null) {
1412 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1413 mHandler.post(new Runnable() {
1414 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001415 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1416 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001417 callbacks.bindAppsUpdated(modifiedFinal);
1418 }
1419 }
1420 });
1421 }
1422 if (removed != null) {
1423 final boolean permanent = mOp != OP_UNAVAILABLE;
1424 final ArrayList<ApplicationInfo> removedFinal = removed;
1425 mHandler.post(new Runnable() {
1426 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001427 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1428 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001429 callbacks.bindAppsRemoved(removedFinal, permanent);
1430 }
1431 }
1432 });
Joe Onoratobe386092009-11-17 17:32:16 -08001433 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001434
1435 mHandler.post(new Runnable() {
1436 @Override
1437 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001438 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1439 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001440 callbacks.bindPackagesUpdated();
1441 }
1442 }
1443 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001444 }
1445 }
1446
1447 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001448 * This is called from the code that adds shortcuts from the intent receiver. This
1449 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 */
Joe Onorato56d82912010-03-07 14:32:10 -05001451 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001452 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001453 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001454
Joe Onorato56d82912010-03-07 14:32:10 -05001455 /**
1456 * Make an ShortcutInfo object for a shortcut that is an application.
1457 *
1458 * If c is not null, then it will be used to fill in missing data like the title and icon.
1459 */
1460 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001461 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001462 Bitmap icon = null;
1463 final ShortcutInfo info = new ShortcutInfo();
1464
1465 ComponentName componentName = intent.getComponent();
1466 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001467 return null;
1468 }
1469
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001470 // TODO: See if the PackageManager knows about this case. If it doesn't
1471 // then return null & delete this.
1472
Joe Onorato56d82912010-03-07 14:32:10 -05001473 // the resource -- This may implicitly give us back the fallback icon,
1474 // but don't worry about that. All we're doing with usingFallbackIcon is
1475 // to avoid saving lots of copies of that in the database, and most apps
1476 // have icons anyway.
1477 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1478 if (resolveInfo != null) {
1479 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001480 }
Joe Onorato56d82912010-03-07 14:32:10 -05001481 // the db
1482 if (icon == null) {
1483 if (c != null) {
1484 icon = getIconFromCursor(c, iconIndex);
1485 }
1486 }
1487 // the fallback icon
1488 if (icon == null) {
1489 icon = getFallbackIcon();
1490 info.usingFallbackIcon = true;
1491 }
1492 info.setIcon(icon);
1493
1494 // from the resource
1495 if (resolveInfo != null) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001496 if (labelCache != null && labelCache.containsKey(resolveInfo)) {
1497 info.title = labelCache.get(resolveInfo);
1498 } else {
1499 info.title = resolveInfo.activityInfo.loadLabel(manager);
1500 if (labelCache != null) {
1501 labelCache.put(resolveInfo, info.title);
1502 }
1503 }
Joe Onorato56d82912010-03-07 14:32:10 -05001504 }
1505 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001506 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001507 if (c != null) {
1508 info.title = c.getString(titleIndex);
1509 }
1510 }
1511 // fall back to the class name of the activity
1512 if (info.title == null) {
1513 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001514 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001515 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1516 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001520 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001522 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001523 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1524 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525
Joe Onorato56d82912010-03-07 14:32:10 -05001526 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001527 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001528 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001530 // TODO: If there's an explicit component and we can't install that, delete it.
1531
Joe Onorato56d82912010-03-07 14:32:10 -05001532 info.title = c.getString(titleIndex);
1533
Joe Onorato9c1289c2009-08-17 11:03:03 -04001534 int iconType = c.getInt(iconTypeIndex);
1535 switch (iconType) {
1536 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1537 String packageName = c.getString(iconPackageIndex);
1538 String resourceName = c.getString(iconResourceIndex);
1539 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001540 info.customIcon = false;
1541 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001543 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001544 if (resources != null) {
1545 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001546 icon = Utilities.createIconBitmap(
1547 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001548 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001549 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001550 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
Joe Onorato56d82912010-03-07 14:32:10 -05001552 // the db
1553 if (icon == null) {
1554 icon = getIconFromCursor(c, iconIndex);
1555 }
1556 // the fallback icon
1557 if (icon == null) {
1558 icon = getFallbackIcon();
1559 info.usingFallbackIcon = true;
1560 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561 break;
1562 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001563 icon = getIconFromCursor(c, iconIndex);
1564 if (icon == null) {
1565 icon = getFallbackIcon();
1566 info.customIcon = false;
1567 info.usingFallbackIcon = true;
1568 } else {
1569 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 break;
1572 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001573 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001574 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001575 info.customIcon = false;
1576 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001578 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001579 return info;
1580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581
Joe Onorato56d82912010-03-07 14:32:10 -05001582 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1583 if (false) {
1584 Log.d(TAG, "getIconFromCursor app="
1585 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1586 }
1587 byte[] data = c.getBlob(iconIndex);
1588 try {
1589 return BitmapFactory.decodeByteArray(data, 0, data.length);
1590 } catch (Exception e) {
1591 return null;
1592 }
1593 }
1594
Joe Onorato0589f0f2010-02-08 13:44:00 -08001595 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001596 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001597
Winson Chunga9abd0e2010-10-27 17:18:37 -07001598 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001599 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001600 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001601
1602 return info;
1603 }
1604
Winson Chunga9abd0e2010-10-27 17:18:37 -07001605 /**
Winson Chung55cef262010-10-28 14:14:18 -07001606 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1607 */
1608 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1609 ComponentName component) {
1610 List<AppWidgetProviderInfo> widgets =
1611 AppWidgetManager.getInstance(context).getInstalledProviders();
1612 for (AppWidgetProviderInfo info : widgets) {
1613 if (info.provider.equals(component)) {
1614 return info;
1615 }
1616 }
1617 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001618 }
1619
Winson Chung68846fd2010-10-29 11:00:27 -07001620 /**
1621 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1622 */
1623 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1624 final PackageManager packageManager = context.getPackageManager();
1625 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1626 new ArrayList<WidgetMimeTypeHandlerData>();
1627
1628 final Intent supportsIntent =
1629 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1630 supportsIntent.setType(mimeType);
1631
1632 // Create a set of widget configuration components that we can test against
1633 final List<AppWidgetProviderInfo> widgets =
1634 AppWidgetManager.getInstance(context).getInstalledProviders();
1635 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1636 new HashMap<ComponentName, AppWidgetProviderInfo>();
1637 for (AppWidgetProviderInfo info : widgets) {
1638 configurationComponentToWidget.put(info.configure, info);
1639 }
1640
1641 // Run through each of the intents that can handle this type of clip data, and cross
1642 // reference them with the components that are actual configuration components
1643 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1644 PackageManager.MATCH_DEFAULT_ONLY);
1645 for (ResolveInfo info : activities) {
1646 final ActivityInfo activityInfo = info.activityInfo;
1647 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1648 activityInfo.name);
1649 if (configurationComponentToWidget.containsKey(infoComponent)) {
1650 supportedConfigurationActivities.add(
1651 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1652 configurationComponentToWidget.get(infoComponent)));
1653 }
1654 }
1655 return supportedConfigurationActivities;
1656 }
1657
Winson Chunga9abd0e2010-10-27 17:18:37 -07001658 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001659 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1660 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1661 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1662
1663 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001664 boolean customIcon = false;
1665 ShortcutIconResource iconResource = null;
1666
1667 if (bitmap != null && bitmap instanceof Bitmap) {
1668 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001669 customIcon = true;
1670 } else {
1671 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1672 if (extra != null && extra instanceof ShortcutIconResource) {
1673 try {
1674 iconResource = (ShortcutIconResource) extra;
1675 final PackageManager packageManager = context.getPackageManager();
1676 Resources resources = packageManager.getResourcesForApplication(
1677 iconResource.packageName);
1678 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001679 icon = Utilities.createIconBitmap(
1680 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001681 } catch (Exception e) {
1682 Log.w(TAG, "Could not load shortcut icon: " + extra);
1683 }
1684 }
1685 }
1686
Joe Onorato0589f0f2010-02-08 13:44:00 -08001687 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001688
1689 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001690 if (fallbackIcon != null) {
1691 icon = fallbackIcon;
1692 } else {
1693 icon = getFallbackIcon();
1694 info.usingFallbackIcon = true;
1695 }
Joe Onorato56d82912010-03-07 14:32:10 -05001696 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001697 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001698
Joe Onorato0589f0f2010-02-08 13:44:00 -08001699 info.title = name;
1700 info.intent = intent;
1701 info.customIcon = customIcon;
1702 info.iconResource = iconResource;
1703
1704 return info;
1705 }
1706
Joe Onorato56d82912010-03-07 14:32:10 -05001707 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001708 // If apps can't be on SD, don't even bother.
1709 if (!mAppsCanBeOnExternalStorage) {
1710 return;
1711 }
Joe Onorato56d82912010-03-07 14:32:10 -05001712 // If this icon doesn't have a custom icon, check to see
1713 // what's stored in the DB, and if it doesn't match what
1714 // we're going to show, store what we are going to show back
1715 // into the DB. We do this so when we're loading, if the
1716 // package manager can't find an icon (for example because
1717 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001718 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001719 boolean needSave;
1720 byte[] data = c.getBlob(iconIndex);
1721 try {
1722 if (data != null) {
1723 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1724 Bitmap loaded = info.getIcon(mIconCache);
1725 needSave = !saved.sameAs(loaded);
1726 } else {
1727 needSave = true;
1728 }
1729 } catch (Exception e) {
1730 needSave = true;
1731 }
1732 if (needSave) {
1733 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001734 // This is slower than is ideal, but this only happens once
1735 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001736 updateItemInDatabase(context, info);
1737 }
1738 }
1739 }
1740
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001742 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 * or make a new one.
1744 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001745 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001746 // See if a placeholder was created for us already
1747 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001748 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001750 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751 folders.put(id, folderInfo);
1752 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001753 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
1755
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001757 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 = new Comparator<ApplicationInfo>() {
1759 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1760 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 };
Winson Chung78403fe2011-01-21 15:38:02 -08001763 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1764 = new Comparator<ApplicationInfo>() {
1765 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1766 if (a.firstInstallTime < b.firstInstallTime) return 1;
1767 if (a.firstInstallTime > b.firstInstallTime) return -1;
1768 return 0;
1769 }
1770 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001771 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1772 = new Comparator<AppWidgetProviderInfo>() {
1773 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1774 return sCollator.compare(a.label.toString(), b.label.toString());
1775 }
1776 };
1777 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1778 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001779 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001780 ShortcutNameComparator(PackageManager pm) {
1781 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001782 mLabelCache = new HashMap<Object, CharSequence>();
1783 }
1784 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1785 mPackageManager = pm;
1786 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001787 }
1788 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001789 CharSequence labelA, labelB;
1790 if (mLabelCache.containsKey(a)) {
1791 labelA = mLabelCache.get(a);
1792 } else {
1793 labelA = a.loadLabel(mPackageManager).toString();
1794
1795 mLabelCache.put(a, labelA);
1796 }
1797 if (mLabelCache.containsKey(b)) {
1798 labelB = mLabelCache.get(b);
1799 } else {
1800 labelB = b.loadLabel(mPackageManager).toString();
1801
1802 mLabelCache.put(b, labelB);
1803 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001804 return sCollator.compare(labelA, labelB);
1805 }
1806 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001807 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1808 private PackageManager mPackageManager;
1809 private HashMap<Object, String> mLabelCache;
1810 WidgetAndShortcutNameComparator(PackageManager pm) {
1811 mPackageManager = pm;
1812 mLabelCache = new HashMap<Object, String>();
1813 }
1814 public final int compare(Object a, Object b) {
1815 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001816 if (mLabelCache.containsKey(a)) {
1817 labelA = mLabelCache.get(a);
1818 } else {
1819 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001820 ((AppWidgetProviderInfo) a).label :
1821 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001822 mLabelCache.put(a, labelA);
1823 }
1824 if (mLabelCache.containsKey(b)) {
1825 labelB = mLabelCache.get(b);
1826 } else {
1827 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001828 ((AppWidgetProviderInfo) b).label :
1829 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001830 mLabelCache.put(b, labelB);
1831 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001832 return sCollator.compare(labelA, labelB);
1833 }
1834 };
Joe Onoratobe386092009-11-17 17:32:16 -08001835
1836 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001837 Log.d(TAG, "mCallbacks=" + mCallbacks);
1838 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1839 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1840 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1841 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001842 if (mLoaderTask != null) {
1843 mLoaderTask.dumpState();
1844 } else {
1845 Log.d(TAG, "mLoaderTask=null");
1846 }
Joe Onoratobe386092009-11-17 17:32:16 -08001847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848}