blob: ec629d03f2bddf2fecdde3b1a70b25062d293992 [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).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700101 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700102
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
Adam Cohen4eac29a2011-07-11 17:53:37 -0700151 public static void unbindWorkspaceItems() {
152 for (ItemInfo item: sWorkspaceItems) {
153 item.unbind();
154 }
155 }
156
Joe Onorato9c1289c2009-08-17 11:03:03 -0400157 /**
158 * Adds an item to the DB if it was not created previously, or move it to a new
159 * <container, screen, cellX, cellY>
160 */
161 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
162 int screen, int cellX, int cellY) {
163 if (item.container == ItemInfo.NO_ID) {
164 // From all apps
165 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
166 } else {
167 // From somewhere else
168 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 }
170 }
171
172 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700174 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
176 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 item.container = container;
178 item.screen = screen;
179 item.cellX = cellX;
180 item.cellY = cellY;
181
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700182 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 final ContentValues values = new ContentValues();
184 final ContentResolver cr = context.getContentResolver();
185
186 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700187 values.put(LauncherSettings.Favorites.CELLX, cellX);
188 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
190
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700191 sWorker.post(new Runnable() {
192 public void run() {
193 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700194 ItemInfo modelItem = sItemsIdMap.get(item.id);
195 if (item != modelItem) {
196 // the modelItem needs to match up perfectly with item if our model is to be
197 // consistent with the database-- for now, just require modelItem == item
198 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
199 "doesn't match original");
200 }
201
202 // Items are added/removed from the corresponding FolderInfo elsewhere, such
203 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
204 // that are on the desktop, as appropriate
205 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700206 if (!sWorkspaceItems.contains(modelItem)) {
207 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700208 }
209 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700210 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700211 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700212 }
213 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700214 }
215
216 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800217 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800218 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700219 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
220 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800221 item.spanX = spanX;
222 item.spanY = spanY;
223 item.cellX = cellX;
224 item.cellY = cellY;
225
226 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
227 final ContentValues values = new ContentValues();
228 final ContentResolver cr = context.getContentResolver();
229
230 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
231 values.put(LauncherSettings.Favorites.SPANX, spanX);
232 values.put(LauncherSettings.Favorites.SPANY, spanY);
233 values.put(LauncherSettings.Favorites.CELLX, cellX);
234 values.put(LauncherSettings.Favorites.CELLY, cellY);
235
236 sWorker.post(new Runnable() {
237 public void run() {
238 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700239 ItemInfo modelItem = sItemsIdMap.get(item.id);
240 if (item != modelItem) {
241 // the modelItem needs to match up perfectly with item if our model is to be
242 // consistent with the database-- for now, just require modelItem == item
243 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
244 "doesn't match original");
245 }
Adam Cohend4844c32011-02-18 19:25:06 -0800246 }
247 });
248 }
249
250 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 * Returns true if the shortcuts already exists in the database.
252 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 static boolean shortcutExists(Context context, String title, Intent intent) {
255 final ContentResolver cr = context.getContentResolver();
256 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
257 new String[] { "title", "intent" }, "title=? and intent=?",
258 new String[] { title, intent.toUri(0) }, null);
259 boolean result = false;
260 try {
261 result = c.moveToFirst();
262 } finally {
263 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700266 }
267
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700269 * Returns an ItemInfo array containing all the items in the LauncherModel.
270 * The ItemInfo.id is not set through this function.
271 */
272 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
273 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
274 final ContentResolver cr = context.getContentResolver();
275 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
276 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
277 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
278 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
279
280 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
281 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
282 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
283 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
284 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
285 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
286 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
287
288 try {
289 while (c.moveToNext()) {
290 ItemInfo item = new ItemInfo();
291 item.cellX = c.getInt(cellXIndex);
292 item.cellY = c.getInt(cellYIndex);
293 item.spanX = c.getInt(spanXIndex);
294 item.spanY = c.getInt(spanYIndex);
295 item.container = c.getInt(containerIndex);
296 item.itemType = c.getInt(itemTypeIndex);
297 item.screen = c.getInt(screenIndex);
298
299 items.add(item);
300 }
301 } catch (Exception e) {
302 items.clear();
303 } finally {
304 c.close();
305 }
306
307 return items;
308 }
309
310 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
312 */
313 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
314 final ContentResolver cr = context.getContentResolver();
315 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
316 "_id=? and (itemType=? or itemType=?)",
317 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700318 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700319
Joe Onorato9c1289c2009-08-17 11:03:03 -0400320 try {
321 if (c.moveToFirst()) {
322 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
323 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
324 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
325 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
326 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
327 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328
Joe Onorato9c1289c2009-08-17 11:03:03 -0400329 FolderInfo folderInfo = null;
330 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700331 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
332 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400333 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700334 }
335
Joe Onorato9c1289c2009-08-17 11:03:03 -0400336 folderInfo.title = c.getString(titleIndex);
337 folderInfo.id = id;
338 folderInfo.container = c.getInt(containerIndex);
339 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700340 folderInfo.cellX = c.getInt(cellXIndex);
341 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342
343 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700344 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400345 } finally {
346 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700347 }
348
349 return null;
350 }
351
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 /**
353 * Add an item to the database in a specified container. Sets the container, screen, cellX and
354 * cellY fields of the item. Also assigns an ID to the item.
355 */
Adam Cohend0445262011-07-04 23:53:22 -0700356 static void addItemToDatabase(Context context, final ItemInfo item, long container,
357 int screen, int cellX, int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 item.container = container;
359 item.screen = screen;
360 item.cellX = cellX;
361 item.cellY = cellY;
362
363 final ContentValues values = new ContentValues();
364 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 item.onAddToDatabase(values);
366
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700367 Launcher l = (Launcher) context;
368 LauncherApplication app = (LauncherApplication) l.getApplication();
369 item.id = app.getLauncherProvider().generateNewId();
370 values.put(LauncherSettings.Favorites._ID, item.id);
Adam Cohend22015c2010-07-26 22:02:18 -0700371 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700372
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700373 sWorker.post(new Runnable() {
374 public void run() {
375 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
376 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700378 sItemsIdMap.put(item.id, item);
379 switch (item.itemType) {
380 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
381 sFolders.put(item.id, (FolderInfo) item);
382 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700383 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700384 }
385 break;
386 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
387 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
388 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700389 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700390 }
391 break;
392 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
393 sAppWidgets.add((LauncherAppWidgetInfo) item);
394 break;
395 }
396 }
397 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700398 }
399
Joe Onorato9c1289c2009-08-17 11:03:03 -0400400 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700401 * Creates a new unique child id, for a given cell span across all layouts.
402 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700403 static int getCellLayoutChildId(
404 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
405 return ((cellId & 0xFF) << 24)
406 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700407 }
408
Adam Cohend22015c2010-07-26 22:02:18 -0700409 static int getCellCountX() {
410 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700411 }
412
Adam Cohend22015c2010-07-26 22:02:18 -0700413 static int getCellCountY() {
414 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700415 }
416
417 /**
418 * Updates the model orientation helper to take into account the current layout dimensions
419 * when performing local/canonical coordinate transformations.
420 */
421 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700422 mCellCountX = shortAxisCellCount;
423 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700424 }
425
426 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427 * Update an item to the database in a specified container.
428 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700429 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 final ContentValues values = new ContentValues();
431 final ContentResolver cr = context.getContentResolver();
432
433 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700434 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700435
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700436 sWorker.post(new Runnable() {
437 public void run() {
438 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
439 values, null, null);
440 final ItemInfo modelItem = sItemsIdMap.get(item.id);
441 if (item != modelItem) {
442 // the modelItem needs to match up perfectly with item if our model is to be
443 // consistent with the database-- for now, just require modelItem == item
444 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
445 "doesn't match original");
446 }
447 }
448 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 }
450
451 /**
452 * Removes the specified item from the database
453 * @param context
454 * @param item
455 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700456 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700458 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700459 sWorker.post(new Runnable() {
460 public void run() {
461 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700462 switch (item.itemType) {
463 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
464 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700465 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700466 break;
467 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
468 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700469 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700470 break;
471 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
472 sAppWidgets.remove((LauncherAppWidgetInfo) item);
473 break;
474 }
475 sItemsIdMap.remove(item.id);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700476 }
477 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400478 }
479
480 /**
481 * Remove the contents of the specified folder from the database
482 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700483 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400484 final ContentResolver cr = context.getContentResolver();
485
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700486 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700487 public void run() {
488 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700489 sItemsIdMap.remove(info.id);
490 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700491 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700492
Adam Cohenafb01ee2011-06-23 15:38:03 -0700493 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
494 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700495 for (ItemInfo childInfo : info.contents) {
496 sItemsIdMap.remove(childInfo.id);
497 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700498 }
499 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400500 }
501
502 /**
503 * Set this as the current Launcher activity object for the loader.
504 */
505 public void initialize(Callbacks callbacks) {
506 synchronized (mLock) {
507 mCallbacks = new WeakReference<Callbacks>(callbacks);
508 }
509 }
510
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700511 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
513 * ACTION_PACKAGE_CHANGED.
514 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400515 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400516 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700517
Joe Onorato36115782010-06-17 13:28:48 -0400518 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400519
Joe Onorato36115782010-06-17 13:28:48 -0400520 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
521 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
522 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
523 final String packageName = intent.getData().getSchemeSpecificPart();
524 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525
Joe Onorato36115782010-06-17 13:28:48 -0400526 int op = PackageUpdatedTask.OP_NONE;
527
528 if (packageName == null || packageName.length() == 0) {
529 // they sent us a bad intent
530 return;
531 }
532
533 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
534 op = PackageUpdatedTask.OP_UPDATE;
535 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
536 if (!replacing) {
537 op = PackageUpdatedTask.OP_REMOVE;
538 }
539 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
540 // later, we will update the package at this time
541 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
542 if (!replacing) {
543 op = PackageUpdatedTask.OP_ADD;
544 } else {
545 op = PackageUpdatedTask.OP_UPDATE;
546 }
547 }
548
549 if (op != PackageUpdatedTask.OP_NONE) {
550 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
551 }
552
553 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400554 // First, schedule to add these apps back in.
555 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
556 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
557 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700558 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400559 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
560 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
561 enqueuePackageUpdated(new PackageUpdatedTask(
562 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700563 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
564 // If we have changed locale we need to clear out the labels in all apps.
565 // Do this here because if the launcher activity is running it will be restarted.
566 // If it's not running startLoaderFromBackground will merely tell it that it needs
567 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
568 // next time.
569 mAllAppsLoaded = false;
570 startLoaderFromBackground();
571 }
572 }
573
574 /**
575 * When the launcher is in the background, it's possible for it to miss paired
576 * configuration changes. So whenever we trigger the loader from the background
577 * tell the launcher that it needs to re-run the loader when it comes back instead
578 * of doing it now.
579 */
580 public void startLoaderFromBackground() {
581 boolean runLoader = false;
582 if (mCallbacks != null) {
583 Callbacks callbacks = mCallbacks.get();
584 if (callbacks != null) {
585 // Only actually run the loader if they're not paused.
586 if (!callbacks.setLoadOnResume()) {
587 runLoader = true;
588 }
589 }
590 }
591 if (runLoader) {
592 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700593 }
Joe Onorato36115782010-06-17 13:28:48 -0400594 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400595
Joe Onorato36115782010-06-17 13:28:48 -0400596 public void startLoader(Context context, boolean isLaunching) {
597 synchronized (mLock) {
598 if (DEBUG_LOADERS) {
599 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601
Joe Onorato36115782010-06-17 13:28:48 -0400602 // Don't bother to start the thread if we know it's not going to do anything
603 if (mCallbacks != null && mCallbacks.get() != null) {
604 // If there is already one running, tell it to stop.
605 LoaderTask oldTask = mLoaderTask;
606 if (oldTask != null) {
607 if (oldTask.isLaunching()) {
608 // don't downgrade isLaunching if we're already running
609 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500610 }
Joe Onorato36115782010-06-17 13:28:48 -0400611 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500612 }
Joe Onorato36115782010-06-17 13:28:48 -0400613 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700614 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 }
617 }
618
Joe Onorato36115782010-06-17 13:28:48 -0400619 public void stopLoader() {
620 synchronized (mLock) {
621 if (mLoaderTask != null) {
622 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 }
624 }
Joe Onorato36115782010-06-17 13:28:48 -0400625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626
Joe Onorato36115782010-06-17 13:28:48 -0400627 /**
628 * Runnable for the thread that loads the contents of the launcher:
629 * - workspace icons
630 * - widgets
631 * - all apps icons
632 */
633 private class LoaderTask implements Runnable {
634 private Context mContext;
635 private Thread mWaitThread;
636 private boolean mIsLaunching;
637 private boolean mStopped;
638 private boolean mLoadAndBindStepFinished;
639
640 LoaderTask(Context context, boolean isLaunching) {
641 mContext = context;
642 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 }
644
Joe Onorato36115782010-06-17 13:28:48 -0400645 boolean isLaunching() {
646 return mIsLaunching;
647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648
Joe Onorato36115782010-06-17 13:28:48 -0400649 private void loadAndBindWorkspace() {
650 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400651 if (DEBUG_LOADERS) {
652 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700654 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400655 loadWorkspace();
656 if (mStopped) {
657 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 }
Joe Onorato36115782010-06-17 13:28:48 -0400659 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 }
661
Joe Onorato36115782010-06-17 13:28:48 -0400662 // Bind the workspace
663 bindWorkspace();
664 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400665
Joe Onorato36115782010-06-17 13:28:48 -0400666 private void waitForIdle() {
667 // Wait until the either we're stopped or the other threads are done.
668 // This way we don't start loading all apps until the workspace has settled
669 // down.
670 synchronized (LoaderTask.this) {
671 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700672
Joe Onorato36115782010-06-17 13:28:48 -0400673 mHandler.postIdle(new Runnable() {
674 public void run() {
675 synchronized (LoaderTask.this) {
676 mLoadAndBindStepFinished = true;
677 if (DEBUG_LOADERS) {
678 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400679 }
Joe Onorato36115782010-06-17 13:28:48 -0400680 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400681 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400682 }
Joe Onorato36115782010-06-17 13:28:48 -0400683 });
684
685 while (!mStopped && !mLoadAndBindStepFinished) {
686 try {
687 this.wait();
688 } catch (InterruptedException ex) {
689 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400690 }
691 }
Joe Onorato36115782010-06-17 13:28:48 -0400692 if (DEBUG_LOADERS) {
693 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700694 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400695 + "ms for previous step to finish binding");
696 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400697 }
Joe Onorato36115782010-06-17 13:28:48 -0400698 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400699
Joe Onorato36115782010-06-17 13:28:48 -0400700 public void run() {
701 // Optimize for end-user experience: if the Launcher is up and // running with the
702 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
703 // workspace first (default).
704 final Callbacks cbk = mCallbacks.get();
705 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400706
Joe Onorato36115782010-06-17 13:28:48 -0400707 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400708 // Elevate priority when Home launches for the first time to avoid
709 // starving at boot time. Staring at a blank home is not cool.
710 synchronized (mLock) {
711 android.os.Process.setThreadPriority(mIsLaunching
712 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
713 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400714 if (loadWorkspaceFirst) {
715 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
716 loadAndBindWorkspace();
717 } else {
718 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700719 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400720 }
721
Joe Onorato36115782010-06-17 13:28:48 -0400722 if (mStopped) {
723 break keep_running;
724 }
725
726 // Whew! Hard work done. Slow us down, and wait until the UI thread has
727 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400728 synchronized (mLock) {
729 if (mIsLaunching) {
730 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
731 }
732 }
Joe Onorato36115782010-06-17 13:28:48 -0400733 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400734
735 // second step
736 if (loadWorkspaceFirst) {
737 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700738 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400739 } else {
740 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
741 loadAndBindWorkspace();
742 }
Joe Onorato36115782010-06-17 13:28:48 -0400743 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744
Joe Onorato36115782010-06-17 13:28:48 -0400745 // Clear out this reference, otherwise we end up holding it until all of the
746 // callback runnables are done.
747 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748
Joe Onorato36115782010-06-17 13:28:48 -0400749 synchronized (mLock) {
750 // If we are still the last one to be scheduled, remove ourselves.
751 if (mLoaderTask == this) {
752 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 }
Joe Onorato36115782010-06-17 13:28:48 -0400754 }
Joe Onorato36115782010-06-17 13:28:48 -0400755 }
756
757 public void stopLocked() {
758 synchronized (LoaderTask.this) {
759 mStopped = true;
760 this.notify();
761 }
762 }
763
764 /**
765 * Gets the callbacks object. If we've been stopped, or if the launcher object
766 * has somehow been garbage collected, return null instead. Pass in the Callbacks
767 * object that was around when the deferred message was scheduled, and if there's
768 * a new Callbacks object around then also return null. This will save us from
769 * calling onto it with data that will be ignored.
770 */
771 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
772 synchronized (mLock) {
773 if (mStopped) {
774 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400775 }
Joe Onorato36115782010-06-17 13:28:48 -0400776
777 if (mCallbacks == null) {
778 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400779 }
Joe Onorato36115782010-06-17 13:28:48 -0400780
781 final Callbacks callbacks = mCallbacks.get();
782 if (callbacks != oldCallbacks) {
783 return null;
784 }
785 if (callbacks == null) {
786 Log.w(TAG, "no mCallbacks");
787 return null;
788 }
789
790 return callbacks;
791 }
792 }
793
794 // check & update map of what's occupied; used to discard overlapping/invalid items
795 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
796 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400797 return true;
798 }
Joe Onorato36115782010-06-17 13:28:48 -0400799 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
800 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
801 if (occupied[item.screen][x][y] != null) {
802 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700803 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400804 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700805 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400806 + occupied[item.screen][x][y]);
807 return false;
808 }
809 }
810 }
811 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
812 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
813 occupied[item.screen][x][y] = item;
814 }
815 }
816 return true;
817 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818
Joe Onorato36115782010-06-17 13:28:48 -0400819 private void loadWorkspace() {
820 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400821
Joe Onorato36115782010-06-17 13:28:48 -0400822 final Context context = mContext;
823 final ContentResolver contentResolver = context.getContentResolver();
824 final PackageManager manager = context.getPackageManager();
825 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
826 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400827
Adam Cohen4eac29a2011-07-11 17:53:37 -0700828 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700829 sAppWidgets.clear();
830 sFolders.clear();
831 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800832
Joe Onorato36115782010-06-17 13:28:48 -0400833 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834
Joe Onorato36115782010-06-17 13:28:48 -0400835 final Cursor c = contentResolver.query(
836 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400837
Adam Cohend22015c2010-07-26 22:02:18 -0700838 final ItemInfo occupied[][][] =
839 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840
Joe Onorato36115782010-06-17 13:28:48 -0400841 try {
842 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
843 final int intentIndex = c.getColumnIndexOrThrow
844 (LauncherSettings.Favorites.INTENT);
845 final int titleIndex = c.getColumnIndexOrThrow
846 (LauncherSettings.Favorites.TITLE);
847 final int iconTypeIndex = c.getColumnIndexOrThrow(
848 LauncherSettings.Favorites.ICON_TYPE);
849 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
850 final int iconPackageIndex = c.getColumnIndexOrThrow(
851 LauncherSettings.Favorites.ICON_PACKAGE);
852 final int iconResourceIndex = c.getColumnIndexOrThrow(
853 LauncherSettings.Favorites.ICON_RESOURCE);
854 final int containerIndex = c.getColumnIndexOrThrow(
855 LauncherSettings.Favorites.CONTAINER);
856 final int itemTypeIndex = c.getColumnIndexOrThrow(
857 LauncherSettings.Favorites.ITEM_TYPE);
858 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
859 LauncherSettings.Favorites.APPWIDGET_ID);
860 final int screenIndex = c.getColumnIndexOrThrow(
861 LauncherSettings.Favorites.SCREEN);
862 final int cellXIndex = c.getColumnIndexOrThrow
863 (LauncherSettings.Favorites.CELLX);
864 final int cellYIndex = c.getColumnIndexOrThrow
865 (LauncherSettings.Favorites.CELLY);
866 final int spanXIndex = c.getColumnIndexOrThrow
867 (LauncherSettings.Favorites.SPANX);
868 final int spanYIndex = c.getColumnIndexOrThrow(
869 LauncherSettings.Favorites.SPANY);
870 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
871 final int displayModeIndex = c.getColumnIndexOrThrow(
872 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873
Joe Onorato36115782010-06-17 13:28:48 -0400874 ShortcutInfo info;
875 String intentDescription;
876 LauncherAppWidgetInfo appWidgetInfo;
877 int container;
878 long id;
879 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880
Joe Onorato36115782010-06-17 13:28:48 -0400881 while (!mStopped && c.moveToNext()) {
882 try {
883 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884
Joe Onorato36115782010-06-17 13:28:48 -0400885 switch (itemType) {
886 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
887 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
888 intentDescription = c.getString(intentIndex);
889 try {
890 intent = Intent.parseUri(intentDescription, 0);
891 } catch (URISyntaxException e) {
892 continue;
893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894
Joe Onorato36115782010-06-17 13:28:48 -0400895 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
896 info = getShortcutInfo(manager, intent, context, c, iconIndex,
897 titleIndex);
898 } else {
899 info = getShortcutInfo(c, context, iconTypeIndex,
900 iconPackageIndex, iconResourceIndex, iconIndex,
901 titleIndex);
902 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903
Joe Onorato36115782010-06-17 13:28:48 -0400904 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400905 info.intent = intent;
906 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400908 info.container = container;
909 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700910 info.cellX = c.getInt(cellXIndex);
911 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912
Daniel Sandler8802e962010-05-26 16:28:16 -0400913 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400914 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400915 break;
916 }
917
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400919 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700920 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400921 break;
922 default:
923 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700924 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700925 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400926 folderInfo.add(info);
927 break;
928 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700929 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800930
931 // now that we've loaded everthing re-save it with the
932 // icon in case it disappears somehow.
933 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400934 } else {
935 // Failed to load the shortcut, probably because the
936 // activity manager couldn't resolve it (maybe the app
937 // was uninstalled), or the db row was somehow screwed up.
938 // Delete it.
939 id = c.getLong(idIndex);
940 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
941 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
942 id, false), null, null);
943 }
944 break;
945
Adam Cohendf2cc412011-04-27 16:56:57 -0700946 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -0400947 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700948 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400949
Winson Chungaafa03c2010-06-11 17:34:16 -0700950 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400951 folderInfo.id = id;
952 container = c.getInt(containerIndex);
953 folderInfo.container = container;
954 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700955 folderInfo.cellX = c.getInt(cellXIndex);
956 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400957
958 // check & update map of what's occupied
959 if (!checkItemPlacement(occupied, folderInfo)) {
960 break;
961 }
Joe Onorato36115782010-06-17 13:28:48 -0400962 switch (container) {
963 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700964 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400965 break;
966 }
967
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700968 sItemsIdMap.put(folderInfo.id, folderInfo);
969 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400970 break;
971
Joe Onorato36115782010-06-17 13:28:48 -0400972 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
973 // Read all Launcher-specific widget details
974 int appWidgetId = c.getInt(appWidgetIdIndex);
975 id = c.getLong(idIndex);
976
977 final AppWidgetProviderInfo provider =
978 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700979
Joe Onorato36115782010-06-17 13:28:48 -0400980 if (!isSafeMode && (provider == null || provider.provider == null ||
981 provider.provider.getPackageName() == null)) {
982 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
983 + id + " appWidgetId=" + appWidgetId);
984 itemsToRemove.add(id);
985 } else {
986 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
987 appWidgetInfo.id = id;
988 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700989 appWidgetInfo.cellX = c.getInt(cellXIndex);
990 appWidgetInfo.cellY = c.getInt(cellYIndex);
991 appWidgetInfo.spanX = c.getInt(spanXIndex);
992 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400993
994 container = c.getInt(containerIndex);
995 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
996 Log.e(TAG, "Widget found where container "
997 + "!= CONTAINER_DESKTOP -- ignoring!");
998 continue;
999 }
1000 appWidgetInfo.container = c.getInt(containerIndex);
1001
1002 // check & update map of what's occupied
1003 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1004 break;
1005 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1007 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001008 }
1009 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001010 }
Joe Onorato36115782010-06-17 13:28:48 -04001011 } catch (Exception e) {
1012 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001013 }
1014 }
Joe Onorato36115782010-06-17 13:28:48 -04001015 } finally {
1016 c.close();
1017 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001018
Joe Onorato36115782010-06-17 13:28:48 -04001019 if (itemsToRemove.size() > 0) {
1020 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1021 LauncherSettings.Favorites.CONTENT_URI);
1022 // Remove dead items
1023 for (long id : itemsToRemove) {
1024 if (DEBUG_LOADERS) {
1025 Log.d(TAG, "Removed id = " + id);
1026 }
1027 // Don't notify content observers
1028 try {
1029 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1030 null, null);
1031 } catch (RemoteException e) {
1032 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001033 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001034 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 }
1036
Joe Onorato36115782010-06-17 13:28:48 -04001037 if (DEBUG_LOADERS) {
1038 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1039 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001040 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001041 String line = "";
1042 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1043 if (s > 0) {
1044 line += " | ";
1045 }
Adam Cohend22015c2010-07-26 22:02:18 -07001046 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001047 line += ((occupied[s][x][y] != null) ? "#" : ".");
1048 }
1049 }
1050 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 }
Joe Onorato36115782010-06-17 13:28:48 -04001052 }
1053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 /**
1056 * Read everything out of our database.
1057 */
1058 private void bindWorkspace() {
1059 final long t = SystemClock.uptimeMillis();
1060
1061 // Don't use these two variables in any of the callback runnables.
1062 // Otherwise we hold a reference to them.
1063 final Callbacks oldCallbacks = mCallbacks.get();
1064 if (oldCallbacks == null) {
1065 // This launcher has exited and nobody bothered to tell us. Just bail.
1066 Log.w(TAG, "LoaderTask running with no launcher");
1067 return;
1068 }
1069
1070 int N;
1071 // Tell the workspace that we're about to start firing items at it
1072 mHandler.post(new Runnable() {
1073 public void run() {
1074 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1075 if (callbacks != null) {
1076 callbacks.startBinding();
1077 }
1078 }
1079 });
1080 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001081 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001082 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1083 final int start = i;
1084 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 mHandler.post(new Runnable() {
1086 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001087 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001089 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 }
1091 }
1092 });
Joe Onorato36115782010-06-17 13:28:48 -04001093 }
1094 mHandler.post(new Runnable() {
1095 public void run() {
1096 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1097 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001098 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001099 }
1100 }
1101 });
1102 // Wait until the queue goes empty.
1103 mHandler.post(new Runnable() {
1104 public void run() {
1105 if (DEBUG_LOADERS) {
1106 Log.d(TAG, "Going to start binding widgets soon.");
1107 }
1108 }
1109 });
1110 // Bind the widgets, one at a time.
1111 // WARNING: this is calling into the workspace from the background thread,
1112 // but since getCurrentScreen() just returns the int, we should be okay. This
1113 // is just a hint for the order, and if it's wrong, we'll be okay.
1114 // TODO: instead, we should have that push the current screen into here.
1115 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001116 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001117 // once for the current screen
1118 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001119 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001120 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001121 mHandler.post(new Runnable() {
1122 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001123 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001125 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 }
1127 }
1128 });
1129 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 }
Joe Onorato36115782010-06-17 13:28:48 -04001131 // once for the other screens
1132 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001133 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001134 if (widget.screen != currentScreen) {
1135 mHandler.post(new Runnable() {
1136 public void run() {
1137 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1138 if (callbacks != null) {
1139 callbacks.bindAppWidget(widget);
1140 }
1141 }
1142 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001143 }
1144 }
Joe Onorato36115782010-06-17 13:28:48 -04001145 // Tell the workspace that we're done.
1146 mHandler.post(new Runnable() {
1147 public void run() {
1148 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1149 if (callbacks != null) {
1150 callbacks.finishBindingItems();
1151 }
1152 }
1153 });
1154 // If we're profiling, this is the last thing in the queue.
1155 mHandler.post(new Runnable() {
1156 public void run() {
1157 if (DEBUG_LOADERS) {
1158 Log.d(TAG, "bound workspace in "
1159 + (SystemClock.uptimeMillis()-t) + "ms");
1160 }
1161 }
1162 });
1163 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001164
Joe Onorato36115782010-06-17 13:28:48 -04001165 private void loadAndBindAllApps() {
1166 if (DEBUG_LOADERS) {
1167 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1168 }
1169 if (!mAllAppsLoaded) {
1170 loadAllAppsByBatch();
1171 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001172 return;
1173 }
Joe Onorato36115782010-06-17 13:28:48 -04001174 mAllAppsLoaded = true;
1175 } else {
1176 onlyBindAllApps();
1177 }
1178 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001179
Joe Onorato36115782010-06-17 13:28:48 -04001180 private void onlyBindAllApps() {
1181 final Callbacks oldCallbacks = mCallbacks.get();
1182 if (oldCallbacks == null) {
1183 // This launcher has exited and nobody bothered to tell us. Just bail.
1184 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1185 return;
1186 }
1187
1188 // shallow copy
1189 final ArrayList<ApplicationInfo> list
1190 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1191 mHandler.post(new Runnable() {
1192 public void run() {
1193 final long t = SystemClock.uptimeMillis();
1194 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1195 if (callbacks != null) {
1196 callbacks.bindAllApplications(list);
1197 }
1198 if (DEBUG_LOADERS) {
1199 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1200 + (SystemClock.uptimeMillis()-t) + "ms");
1201 }
1202 }
1203 });
1204
1205 }
1206
1207 private void loadAllAppsByBatch() {
1208 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1209
1210 // Don't use these two variables in any of the callback runnables.
1211 // Otherwise we hold a reference to them.
1212 final Callbacks oldCallbacks = mCallbacks.get();
1213 if (oldCallbacks == null) {
1214 // This launcher has exited and nobody bothered to tell us. Just bail.
1215 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1216 return;
1217 }
1218
1219 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1220 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1221
1222 final PackageManager packageManager = mContext.getPackageManager();
1223 List<ResolveInfo> apps = null;
1224
1225 int N = Integer.MAX_VALUE;
1226
1227 int startIndex;
1228 int i=0;
1229 int batchSize = -1;
1230 while (i < N && !mStopped) {
1231 if (i == 0) {
1232 mAllAppsList.clear();
1233 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1234 apps = packageManager.queryIntentActivities(mainIntent, 0);
1235 if (DEBUG_LOADERS) {
1236 Log.d(TAG, "queryIntentActivities took "
1237 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1238 }
1239 if (apps == null) {
1240 return;
1241 }
1242 N = apps.size();
1243 if (DEBUG_LOADERS) {
1244 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1245 }
1246 if (N == 0) {
1247 // There are no apps?!?
1248 return;
1249 }
1250 if (mBatchSize == 0) {
1251 batchSize = N;
1252 } else {
1253 batchSize = mBatchSize;
1254 }
1255
1256 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1257 Collections.sort(apps,
1258 new ResolveInfo.DisplayNameComparator(packageManager));
1259 if (DEBUG_LOADERS) {
1260 Log.d(TAG, "sort took "
1261 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1262 }
1263 }
1264
1265 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1266
1267 startIndex = i;
1268 for (int j=0; i<N && j<batchSize; j++) {
1269 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001270 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001271 i++;
1272 }
1273
1274 final boolean first = i <= batchSize;
1275 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1276 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1277 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1278
Joe Onoratocc67f472010-06-08 10:54:30 -07001279 mHandler.post(new Runnable() {
1280 public void run() {
1281 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001282 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001283 if (first) {
1284 callbacks.bindAllApplications(added);
1285 } else {
1286 callbacks.bindAppsAdded(added);
1287 }
1288 if (DEBUG_LOADERS) {
1289 Log.d(TAG, "bound " + added.size() + " apps in "
1290 + (SystemClock.uptimeMillis() - t) + "ms");
1291 }
1292 } else {
1293 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001294 }
1295 }
1296 });
1297
Daniel Sandlerdca66122010-04-13 16:23:58 -04001298 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001299 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1300 + (SystemClock.uptimeMillis()-t2) + "ms");
1301 }
1302
1303 if (mAllAppsLoadDelay > 0 && i < N) {
1304 try {
1305 if (DEBUG_LOADERS) {
1306 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1307 }
1308 Thread.sleep(mAllAppsLoadDelay);
1309 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 }
1311 }
1312
Joe Onorato36115782010-06-17 13:28:48 -04001313 if (DEBUG_LOADERS) {
1314 Log.d(TAG, "cached all " + N + " apps in "
1315 + (SystemClock.uptimeMillis()-t) + "ms"
1316 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001317 }
1318 }
1319
1320 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001321 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1322 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1323 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1324 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1325 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001326 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001327 }
1328 }
1329
1330 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001331 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001332 }
1333
1334 private class PackageUpdatedTask implements Runnable {
1335 int mOp;
1336 String[] mPackages;
1337
1338 public static final int OP_NONE = 0;
1339 public static final int OP_ADD = 1;
1340 public static final int OP_UPDATE = 2;
1341 public static final int OP_REMOVE = 3; // uninstlled
1342 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1343
1344
1345 public PackageUpdatedTask(int op, String[] packages) {
1346 mOp = op;
1347 mPackages = packages;
1348 }
1349
1350 public void run() {
1351 final Context context = mApp;
1352
1353 final String[] packages = mPackages;
1354 final int N = packages.length;
1355 switch (mOp) {
1356 case OP_ADD:
1357 for (int i=0; i<N; i++) {
1358 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1359 mAllAppsList.addPackage(context, packages[i]);
1360 }
1361 break;
1362 case OP_UPDATE:
1363 for (int i=0; i<N; i++) {
1364 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1365 mAllAppsList.updatePackage(context, packages[i]);
1366 }
1367 break;
1368 case OP_REMOVE:
1369 case OP_UNAVAILABLE:
1370 for (int i=0; i<N; i++) {
1371 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1372 mAllAppsList.removePackage(packages[i]);
1373 }
1374 break;
1375 }
1376
1377 ArrayList<ApplicationInfo> added = null;
1378 ArrayList<ApplicationInfo> removed = null;
1379 ArrayList<ApplicationInfo> modified = null;
1380
1381 if (mAllAppsList.added.size() > 0) {
1382 added = mAllAppsList.added;
1383 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1384 }
1385 if (mAllAppsList.removed.size() > 0) {
1386 removed = mAllAppsList.removed;
1387 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1388 for (ApplicationInfo info: removed) {
1389 mIconCache.remove(info.intent.getComponent());
1390 }
1391 }
1392 if (mAllAppsList.modified.size() > 0) {
1393 modified = mAllAppsList.modified;
1394 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1395 }
1396
1397 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1398 if (callbacks == null) {
1399 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1400 return;
1401 }
1402
1403 if (added != null) {
1404 final ArrayList<ApplicationInfo> addedFinal = added;
1405 mHandler.post(new Runnable() {
1406 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001407 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1408 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001409 callbacks.bindAppsAdded(addedFinal);
1410 }
1411 }
1412 });
1413 }
1414 if (modified != null) {
1415 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1416 mHandler.post(new Runnable() {
1417 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001418 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1419 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001420 callbacks.bindAppsUpdated(modifiedFinal);
1421 }
1422 }
1423 });
1424 }
1425 if (removed != null) {
1426 final boolean permanent = mOp != OP_UNAVAILABLE;
1427 final ArrayList<ApplicationInfo> removedFinal = removed;
1428 mHandler.post(new Runnable() {
1429 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001430 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1431 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001432 callbacks.bindAppsRemoved(removedFinal, permanent);
1433 }
1434 }
1435 });
Joe Onoratobe386092009-11-17 17:32:16 -08001436 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001437
1438 mHandler.post(new Runnable() {
1439 @Override
1440 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001441 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1442 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001443 callbacks.bindPackagesUpdated();
1444 }
1445 }
1446 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001447 }
1448 }
1449
1450 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001451 * This is called from the code that adds shortcuts from the intent receiver. This
1452 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 */
Joe Onorato56d82912010-03-07 14:32:10 -05001454 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001455 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001456 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001457
Joe Onorato56d82912010-03-07 14:32:10 -05001458 /**
1459 * Make an ShortcutInfo object for a shortcut that is an application.
1460 *
1461 * If c is not null, then it will be used to fill in missing data like the title and icon.
1462 */
1463 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1464 Cursor c, int iconIndex, int titleIndex) {
1465 Bitmap icon = null;
1466 final ShortcutInfo info = new ShortcutInfo();
1467
1468 ComponentName componentName = intent.getComponent();
1469 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001470 return null;
1471 }
1472
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001473 // TODO: See if the PackageManager knows about this case. If it doesn't
1474 // then return null & delete this.
1475
Joe Onorato56d82912010-03-07 14:32:10 -05001476 // the resource -- This may implicitly give us back the fallback icon,
1477 // but don't worry about that. All we're doing with usingFallbackIcon is
1478 // to avoid saving lots of copies of that in the database, and most apps
1479 // have icons anyway.
1480 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1481 if (resolveInfo != null) {
1482 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001483 }
Joe Onorato56d82912010-03-07 14:32:10 -05001484 // the db
1485 if (icon == null) {
1486 if (c != null) {
1487 icon = getIconFromCursor(c, iconIndex);
1488 }
1489 }
1490 // the fallback icon
1491 if (icon == null) {
1492 icon = getFallbackIcon();
1493 info.usingFallbackIcon = true;
1494 }
1495 info.setIcon(icon);
1496
1497 // from the resource
1498 if (resolveInfo != null) {
1499 info.title = resolveInfo.activityInfo.loadLabel(manager);
1500 }
1501 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001502 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001503 if (c != null) {
1504 info.title = c.getString(titleIndex);
1505 }
1506 }
1507 // fall back to the class name of the activity
1508 if (info.title == null) {
1509 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001510 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001511 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1512 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001516 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001518 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001519 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1520 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521
Joe Onorato56d82912010-03-07 14:32:10 -05001522 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001523 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001524 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001526 // TODO: If there's an explicit component and we can't install that, delete it.
1527
Joe Onorato56d82912010-03-07 14:32:10 -05001528 info.title = c.getString(titleIndex);
1529
Joe Onorato9c1289c2009-08-17 11:03:03 -04001530 int iconType = c.getInt(iconTypeIndex);
1531 switch (iconType) {
1532 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1533 String packageName = c.getString(iconPackageIndex);
1534 String resourceName = c.getString(iconResourceIndex);
1535 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001536 info.customIcon = false;
1537 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001539 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001540 if (resources != null) {
1541 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001542 icon = Utilities.createIconBitmap(
1543 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001544 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001545 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001546 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
Joe Onorato56d82912010-03-07 14:32:10 -05001548 // the db
1549 if (icon == null) {
1550 icon = getIconFromCursor(c, iconIndex);
1551 }
1552 // the fallback icon
1553 if (icon == null) {
1554 icon = getFallbackIcon();
1555 info.usingFallbackIcon = true;
1556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001557 break;
1558 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001559 icon = getIconFromCursor(c, iconIndex);
1560 if (icon == null) {
1561 icon = getFallbackIcon();
1562 info.customIcon = false;
1563 info.usingFallbackIcon = true;
1564 } else {
1565 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001566 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567 break;
1568 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001569 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001570 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 info.customIcon = false;
1572 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001574 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001575 return info;
1576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577
Joe Onorato56d82912010-03-07 14:32:10 -05001578 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1579 if (false) {
1580 Log.d(TAG, "getIconFromCursor app="
1581 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1582 }
1583 byte[] data = c.getBlob(iconIndex);
1584 try {
1585 return BitmapFactory.decodeByteArray(data, 0, data.length);
1586 } catch (Exception e) {
1587 return null;
1588 }
1589 }
1590
Joe Onorato0589f0f2010-02-08 13:44:00 -08001591 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001592 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001593
Winson Chunga9abd0e2010-10-27 17:18:37 -07001594 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001595 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001596 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001597
1598 return info;
1599 }
1600
Winson Chunga9abd0e2010-10-27 17:18:37 -07001601 /**
Winson Chung55cef262010-10-28 14:14:18 -07001602 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1603 */
1604 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1605 ComponentName component) {
1606 List<AppWidgetProviderInfo> widgets =
1607 AppWidgetManager.getInstance(context).getInstalledProviders();
1608 for (AppWidgetProviderInfo info : widgets) {
1609 if (info.provider.equals(component)) {
1610 return info;
1611 }
1612 }
1613 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001614 }
1615
Winson Chung68846fd2010-10-29 11:00:27 -07001616 /**
1617 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1618 */
1619 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1620 final PackageManager packageManager = context.getPackageManager();
1621 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1622 new ArrayList<WidgetMimeTypeHandlerData>();
1623
1624 final Intent supportsIntent =
1625 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1626 supportsIntent.setType(mimeType);
1627
1628 // Create a set of widget configuration components that we can test against
1629 final List<AppWidgetProviderInfo> widgets =
1630 AppWidgetManager.getInstance(context).getInstalledProviders();
1631 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1632 new HashMap<ComponentName, AppWidgetProviderInfo>();
1633 for (AppWidgetProviderInfo info : widgets) {
1634 configurationComponentToWidget.put(info.configure, info);
1635 }
1636
1637 // Run through each of the intents that can handle this type of clip data, and cross
1638 // reference them with the components that are actual configuration components
1639 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1640 PackageManager.MATCH_DEFAULT_ONLY);
1641 for (ResolveInfo info : activities) {
1642 final ActivityInfo activityInfo = info.activityInfo;
1643 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1644 activityInfo.name);
1645 if (configurationComponentToWidget.containsKey(infoComponent)) {
1646 supportedConfigurationActivities.add(
1647 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1648 configurationComponentToWidget.get(infoComponent)));
1649 }
1650 }
1651 return supportedConfigurationActivities;
1652 }
1653
Winson Chunga9abd0e2010-10-27 17:18:37 -07001654 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001655 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1656 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1657 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1658
1659 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001660 boolean customIcon = false;
1661 ShortcutIconResource iconResource = null;
1662
1663 if (bitmap != null && bitmap instanceof Bitmap) {
1664 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001665 customIcon = true;
1666 } else {
1667 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1668 if (extra != null && extra instanceof ShortcutIconResource) {
1669 try {
1670 iconResource = (ShortcutIconResource) extra;
1671 final PackageManager packageManager = context.getPackageManager();
1672 Resources resources = packageManager.getResourcesForApplication(
1673 iconResource.packageName);
1674 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001675 icon = Utilities.createIconBitmap(
1676 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001677 } catch (Exception e) {
1678 Log.w(TAG, "Could not load shortcut icon: " + extra);
1679 }
1680 }
1681 }
1682
Joe Onorato0589f0f2010-02-08 13:44:00 -08001683 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001684
1685 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001686 if (fallbackIcon != null) {
1687 icon = fallbackIcon;
1688 } else {
1689 icon = getFallbackIcon();
1690 info.usingFallbackIcon = true;
1691 }
Joe Onorato56d82912010-03-07 14:32:10 -05001692 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001693 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001694
Joe Onorato0589f0f2010-02-08 13:44:00 -08001695 info.title = name;
1696 info.intent = intent;
1697 info.customIcon = customIcon;
1698 info.iconResource = iconResource;
1699
1700 return info;
1701 }
1702
Joe Onorato56d82912010-03-07 14:32:10 -05001703 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001704 // If apps can't be on SD, don't even bother.
1705 if (!mAppsCanBeOnExternalStorage) {
1706 return;
1707 }
Joe Onorato56d82912010-03-07 14:32:10 -05001708 // If this icon doesn't have a custom icon, check to see
1709 // what's stored in the DB, and if it doesn't match what
1710 // we're going to show, store what we are going to show back
1711 // into the DB. We do this so when we're loading, if the
1712 // package manager can't find an icon (for example because
1713 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001714 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001715 boolean needSave;
1716 byte[] data = c.getBlob(iconIndex);
1717 try {
1718 if (data != null) {
1719 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1720 Bitmap loaded = info.getIcon(mIconCache);
1721 needSave = !saved.sameAs(loaded);
1722 } else {
1723 needSave = true;
1724 }
1725 } catch (Exception e) {
1726 needSave = true;
1727 }
1728 if (needSave) {
1729 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001730 // This is slower than is ideal, but this only happens once
1731 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001732 updateItemInDatabase(context, info);
1733 }
1734 }
1735 }
1736
Joe Onorato9c1289c2009-08-17 11:03:03 -04001737 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001738 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001739 * or make a new one.
1740 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001741 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742 // See if a placeholder was created for us already
1743 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001744 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001746 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747 folders.put(id, folderInfo);
1748 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001749 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
1751
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001753 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754 = new Comparator<ApplicationInfo>() {
1755 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1756 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 };
Winson Chung78403fe2011-01-21 15:38:02 -08001759 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1760 = new Comparator<ApplicationInfo>() {
1761 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1762 if (a.firstInstallTime < b.firstInstallTime) return 1;
1763 if (a.firstInstallTime > b.firstInstallTime) return -1;
1764 return 0;
1765 }
1766 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001767 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1768 = new Comparator<AppWidgetProviderInfo>() {
1769 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1770 return sCollator.compare(a.label.toString(), b.label.toString());
1771 }
1772 };
1773 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1774 private PackageManager mPackageManager;
1775 private HashMap<Object, String> mLabelCache;
1776 ShortcutNameComparator(PackageManager pm) {
1777 mPackageManager = pm;
1778 mLabelCache = new HashMap<Object, String>();
1779 }
1780 public final int compare(ResolveInfo a, ResolveInfo b) {
1781 String labelA, labelB;
1782 if (mLabelCache.containsKey(a)) labelA = mLabelCache.get(a);
1783 else labelA = a.loadLabel(mPackageManager).toString();
1784 if (mLabelCache.containsKey(b)) labelB = mLabelCache.get(b);
1785 else labelB = b.loadLabel(mPackageManager).toString();
1786 return sCollator.compare(labelA, labelB);
1787 }
1788 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001789 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1790 private PackageManager mPackageManager;
1791 private HashMap<Object, String> mLabelCache;
1792 WidgetAndShortcutNameComparator(PackageManager pm) {
1793 mPackageManager = pm;
1794 mLabelCache = new HashMap<Object, String>();
1795 }
1796 public final int compare(Object a, Object b) {
1797 String labelA, labelB;
1798 if (mLabelCache.containsKey(a)) labelA = mLabelCache.get(a);
1799 else labelA = (a instanceof AppWidgetProviderInfo) ?
1800 ((AppWidgetProviderInfo) a).label :
1801 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
1802 if (mLabelCache.containsKey(b)) labelB = mLabelCache.get(b);
1803 else labelB = (b instanceof AppWidgetProviderInfo) ?
1804 ((AppWidgetProviderInfo) b).label :
1805 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
1806 return sCollator.compare(labelA, labelB);
1807 }
1808 };
Joe Onoratobe386092009-11-17 17:32:16 -08001809
1810 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001811 Log.d(TAG, "mCallbacks=" + mCallbacks);
1812 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1813 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1814 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1815 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001816 if (mLoaderTask != null) {
1817 mLoaderTask.dumpState();
1818 } else {
1819 Log.d(TAG, "mLoaderTask=null");
1820 }
Joe Onoratobe386092009-11-17 17:32:16 -08001821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822}