blob: 1273ea7b15131c7b9fce5c8b661b2efd2434698b [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
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetManager;
20import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040021import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080023import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.ContentResolver;
25import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.ActivityInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Resources;
33import android.database.Cursor;
34import android.graphics.Bitmap;
35import android.graphics.BitmapFactory;
36import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080037import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045
Winson Chung68846fd2010-10-29 11:00:27 -070046import com.android.launcher.R;
47import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080048
Michael Jurkac2f801e2011-07-12 14:19:46 -070049import java.lang.ref.WeakReference;
50import java.net.URISyntaxException;
51import java.text.Collator;
52import java.util.ArrayList;
53import java.util.Collections;
54import java.util.Comparator;
55import java.util.HashMap;
56import java.util.List;
57import java.util.Locale;
58
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059/**
60 * Maintains in-memory state of the Launcher. It is expected that there should be only one
61 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070062 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040064public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080065 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040066 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070067
Joe Onorato36115782010-06-17 13:28:48 -040068 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080069 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040070 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040071 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040072
Joe Onoratof99f8c12009-10-31 17:27:36 -040073 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 private final Object mLock = new Object();
75 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040076 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070078 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
79 static {
80 sWorkerThread.start();
81 }
82 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
83
Joe Onoratocc67f472010-06-08 10:54:30 -070084 // We start off with everything not loaded. After that, we assume that
85 // our monitoring of the package manager provides all updates and we never
86 // need to do a requery. These are only ever touched from the loader thread.
87 private boolean mWorkspaceLoaded;
88 private boolean mAllAppsLoaded;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089 private Locale mLocale;
Joe Onoratocc67f472010-06-08 10:54:30 -070090
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Michael Jurkaa8c760d2011-04-28 14:59:33 -070093 // < only access in worker thread >
94 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080095
Michael Jurkaa8c760d2011-04-28 14:59:33 -070096 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
97 // LauncherModel to their ids
98 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
99
100 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
101 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
102 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700103 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700104
105 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
106 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
107 new ArrayList<LauncherAppWidgetInfo>();
108
109 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
110 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
111 // </ only access in worker thread >
112
113 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Adam Cohend22015c2010-07-26 22:02:18 -0700116 private static int mCellCountX;
117 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700120 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 public int getCurrentWorkspaceScreen();
122 public void startBinding();
123 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500124 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 public void finishBindingItems();
126 public void bindAppWidget(LauncherAppWidgetInfo info);
127 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500128 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
129 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400130 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700131 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400132 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Joe Onorato0589f0f2010-02-08 13:44:00 -0800135 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800136 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400137 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800138 mAllAppsList = new AllAppsList(iconCache);
139 mIconCache = iconCache;
140
141 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700142 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400143
144 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400145
146 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800147 }
148
Joe Onorato56d82912010-03-07 14:32:10 -0500149 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800150 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Adam Cohen4eac29a2011-07-11 17:53:37 -0700153 public static void unbindWorkspaceItems() {
154 for (ItemInfo item: sWorkspaceItems) {
155 item.unbind();
156 }
157 }
158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 /**
160 * Adds an item to the DB if it was not created previously, or move it to a new
161 * <container, screen, cellX, cellY>
162 */
163 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
164 int screen, int cellX, int cellY) {
165 if (item.container == ItemInfo.NO_ID) {
166 // From all apps
167 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
168 } else {
169 // From somewhere else
170 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 }
172 }
173
174 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700176 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
178 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 item.container = container;
180 item.screen = screen;
181 item.cellX = cellX;
182 item.cellY = cellY;
183
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700184 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 final ContentValues values = new ContentValues();
186 final ContentResolver cr = context.getContentResolver();
187
188 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700189 values.put(LauncherSettings.Favorites.CELLX, cellX);
190 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
192
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700193 sWorker.post(new Runnable() {
194 public void run() {
195 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700196 ItemInfo modelItem = sItemsIdMap.get(item.id);
197 if (item != modelItem) {
198 // the modelItem needs to match up perfectly with item if our model is to be
199 // consistent with the database-- for now, just require modelItem == item
200 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
201 "doesn't match original");
202 }
203
204 // Items are added/removed from the corresponding FolderInfo elsewhere, such
205 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
206 // that are on the desktop, as appropriate
207 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700208 if (!sWorkspaceItems.contains(modelItem)) {
209 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700210 }
211 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700212 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700213 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700214 }
215 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700216 }
217
218 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800219 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800220 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700221 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
222 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800223 item.spanX = spanX;
224 item.spanY = spanY;
225 item.cellX = cellX;
226 item.cellY = cellY;
227
228 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
229 final ContentValues values = new ContentValues();
230 final ContentResolver cr = context.getContentResolver();
231
232 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
233 values.put(LauncherSettings.Favorites.SPANX, spanX);
234 values.put(LauncherSettings.Favorites.SPANY, spanY);
235 values.put(LauncherSettings.Favorites.CELLX, cellX);
236 values.put(LauncherSettings.Favorites.CELLY, cellY);
237
238 sWorker.post(new Runnable() {
239 public void run() {
240 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700241 ItemInfo modelItem = sItemsIdMap.get(item.id);
242 if (item != modelItem) {
243 // the modelItem needs to match up perfectly with item if our model is to be
244 // consistent with the database-- for now, just require modelItem == item
245 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
246 "doesn't match original");
247 }
Adam Cohend4844c32011-02-18 19:25:06 -0800248 }
249 });
250 }
251
252 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 * Returns true if the shortcuts already exists in the database.
254 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 static boolean shortcutExists(Context context, String title, Intent intent) {
257 final ContentResolver cr = context.getContentResolver();
258 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
259 new String[] { "title", "intent" }, "title=? and intent=?",
260 new String[] { title, intent.toUri(0) }, null);
261 boolean result = false;
262 try {
263 result = c.moveToFirst();
264 } finally {
265 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700268 }
269
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700271 * Returns an ItemInfo array containing all the items in the LauncherModel.
272 * The ItemInfo.id is not set through this function.
273 */
274 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
275 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
276 final ContentResolver cr = context.getContentResolver();
277 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
278 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
279 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
280 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
281
282 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
283 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
284 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
285 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
286 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
287 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
288 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
289
290 try {
291 while (c.moveToNext()) {
292 ItemInfo item = new ItemInfo();
293 item.cellX = c.getInt(cellXIndex);
294 item.cellY = c.getInt(cellYIndex);
295 item.spanX = c.getInt(spanXIndex);
296 item.spanY = c.getInt(spanYIndex);
297 item.container = c.getInt(containerIndex);
298 item.itemType = c.getInt(itemTypeIndex);
299 item.screen = c.getInt(screenIndex);
300
301 items.add(item);
302 }
303 } catch (Exception e) {
304 items.clear();
305 } finally {
306 c.close();
307 }
308
309 return items;
310 }
311
312 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400313 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
314 */
315 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
316 final ContentResolver cr = context.getContentResolver();
317 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
318 "_id=? and (itemType=? or itemType=?)",
319 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700320 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700321
Joe Onorato9c1289c2009-08-17 11:03:03 -0400322 try {
323 if (c.moveToFirst()) {
324 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
325 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
326 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
327 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
328 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
329 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330
Joe Onorato9c1289c2009-08-17 11:03:03 -0400331 FolderInfo folderInfo = null;
332 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700333 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
334 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400335 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700336 }
337
Joe Onorato9c1289c2009-08-17 11:03:03 -0400338 folderInfo.title = c.getString(titleIndex);
339 folderInfo.id = id;
340 folderInfo.container = c.getInt(containerIndex);
341 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700342 folderInfo.cellX = c.getInt(cellXIndex);
343 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400344
345 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700346 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400347 } finally {
348 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700349 }
350
351 return null;
352 }
353
Joe Onorato9c1289c2009-08-17 11:03:03 -0400354 /**
355 * Add an item to the database in a specified container. Sets the container, screen, cellX and
356 * cellY fields of the item. Also assigns an ID to the item.
357 */
Adam Cohend0445262011-07-04 23:53:22 -0700358 static void addItemToDatabase(Context context, final ItemInfo item, long container,
359 int screen, int cellX, int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360 item.container = container;
361 item.screen = screen;
362 item.cellX = cellX;
363 item.cellY = cellY;
364
365 final ContentValues values = new ContentValues();
366 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 item.onAddToDatabase(values);
368
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700369 Launcher l = (Launcher) context;
370 LauncherApplication app = (LauncherApplication) l.getApplication();
371 item.id = app.getLauncherProvider().generateNewId();
372 values.put(LauncherSettings.Favorites._ID, item.id);
Adam Cohend22015c2010-07-26 22:02:18 -0700373 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700374
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700375 sWorker.post(new Runnable() {
376 public void run() {
377 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
378 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700380 sItemsIdMap.put(item.id, item);
381 switch (item.itemType) {
382 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
383 sFolders.put(item.id, (FolderInfo) item);
384 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700385 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700386 }
387 break;
388 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
389 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
390 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700391 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700392 }
393 break;
394 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
395 sAppWidgets.add((LauncherAppWidgetInfo) item);
396 break;
397 }
398 }
399 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700400 }
401
Joe Onorato9c1289c2009-08-17 11:03:03 -0400402 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700403 * Creates a new unique child id, for a given cell span across all layouts.
404 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700405 static int getCellLayoutChildId(
406 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
407 return ((cellId & 0xFF) << 24)
408 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700409 }
410
Adam Cohend22015c2010-07-26 22:02:18 -0700411 static int getCellCountX() {
412 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700413 }
414
Adam Cohend22015c2010-07-26 22:02:18 -0700415 static int getCellCountY() {
416 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700417 }
418
419 /**
420 * Updates the model orientation helper to take into account the current layout dimensions
421 * when performing local/canonical coordinate transformations.
422 */
423 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700424 mCellCountX = shortAxisCellCount;
425 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700426 }
427
428 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 * Update an item to the database in a specified container.
430 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700431 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 final ContentValues values = new ContentValues();
433 final ContentResolver cr = context.getContentResolver();
434
435 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700436 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700437
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700438 sWorker.post(new Runnable() {
439 public void run() {
440 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
441 values, null, null);
442 final ItemInfo modelItem = sItemsIdMap.get(item.id);
443 if (item != modelItem) {
444 // the modelItem needs to match up perfectly with item if our model is to be
445 // consistent with the database-- for now, just require modelItem == item
446 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
447 "doesn't match original");
448 }
449 }
450 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 }
452
453 /**
454 * Removes the specified item from the database
455 * @param context
456 * @param item
457 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700458 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400459 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700460 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700461 sWorker.post(new Runnable() {
462 public void run() {
463 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700464 switch (item.itemType) {
465 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
466 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700467 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700468 break;
469 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
470 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700471 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700472 break;
473 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
474 sAppWidgets.remove((LauncherAppWidgetInfo) item);
475 break;
476 }
477 sItemsIdMap.remove(item.id);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700478 }
479 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400480 }
481
482 /**
483 * Remove the contents of the specified folder from the database
484 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700485 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400486 final ContentResolver cr = context.getContentResolver();
487
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700488 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700489 public void run() {
490 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700491 sItemsIdMap.remove(info.id);
492 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700493 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700494
Adam Cohenafb01ee2011-06-23 15:38:03 -0700495 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
496 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700497 for (ItemInfo childInfo : info.contents) {
498 sItemsIdMap.remove(childInfo.id);
499 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700500 }
501 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400502 }
503
504 /**
505 * Set this as the current Launcher activity object for the loader.
506 */
507 public void initialize(Callbacks callbacks) {
508 synchronized (mLock) {
509 mCallbacks = new WeakReference<Callbacks>(callbacks);
510 }
511 }
512
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700513 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
515 * ACTION_PACKAGE_CHANGED.
516 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400517 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400518 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700519
Joe Onorato36115782010-06-17 13:28:48 -0400520 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400521
Joe Onorato36115782010-06-17 13:28:48 -0400522 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
523 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
524 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
525 final String packageName = intent.getData().getSchemeSpecificPart();
526 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400527
Joe Onorato36115782010-06-17 13:28:48 -0400528 int op = PackageUpdatedTask.OP_NONE;
529
530 if (packageName == null || packageName.length() == 0) {
531 // they sent us a bad intent
532 return;
533 }
534
535 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
536 op = PackageUpdatedTask.OP_UPDATE;
537 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
538 if (!replacing) {
539 op = PackageUpdatedTask.OP_REMOVE;
540 }
541 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
542 // later, we will update the package at this time
543 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
544 if (!replacing) {
545 op = PackageUpdatedTask.OP_ADD;
546 } else {
547 op = PackageUpdatedTask.OP_UPDATE;
548 }
549 }
550
551 if (op != PackageUpdatedTask.OP_NONE) {
552 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
553 }
554
555 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400556 // First, schedule to add these apps back in.
557 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
558 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
559 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700560 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400561 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
562 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
563 enqueuePackageUpdated(new PackageUpdatedTask(
564 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700565 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
566 // If we have changed locale we need to clear out the labels in all apps.
567 // Do this here because if the launcher activity is running it will be restarted.
568 // If it's not running startLoaderFromBackground will merely tell it that it needs
569 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
570 // next time.
571 mAllAppsLoaded = false;
572 startLoaderFromBackground();
573 }
574 }
575
576 /**
577 * When the launcher is in the background, it's possible for it to miss paired
578 * configuration changes. So whenever we trigger the loader from the background
579 * tell the launcher that it needs to re-run the loader when it comes back instead
580 * of doing it now.
581 */
582 public void startLoaderFromBackground() {
583 boolean runLoader = false;
584 if (mCallbacks != null) {
585 Callbacks callbacks = mCallbacks.get();
586 if (callbacks != null) {
587 // Only actually run the loader if they're not paused.
588 if (!callbacks.setLoadOnResume()) {
589 runLoader = true;
590 }
591 }
592 }
593 if (runLoader) {
594 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700595 }
Joe Onorato36115782010-06-17 13:28:48 -0400596 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400597
Joe Onorato36115782010-06-17 13:28:48 -0400598 public void startLoader(Context context, boolean isLaunching) {
599 synchronized (mLock) {
600 if (DEBUG_LOADERS) {
601 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400603
Joe Onorato36115782010-06-17 13:28:48 -0400604 // Don't bother to start the thread if we know it's not going to do anything
605 if (mCallbacks != null && mCallbacks.get() != null) {
606 // If there is already one running, tell it to stop.
607 LoaderTask oldTask = mLoaderTask;
608 if (oldTask != null) {
609 if (oldTask.isLaunching()) {
610 // don't downgrade isLaunching if we're already running
611 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500612 }
Joe Onorato36115782010-06-17 13:28:48 -0400613 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500614 }
Joe Onorato36115782010-06-17 13:28:48 -0400615 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700616 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400618 }
619 }
620
Joe Onorato36115782010-06-17 13:28:48 -0400621 public void stopLoader() {
622 synchronized (mLock) {
623 if (mLoaderTask != null) {
624 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 }
626 }
Joe Onorato36115782010-06-17 13:28:48 -0400627 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628
Joe Onorato36115782010-06-17 13:28:48 -0400629 /**
630 * Runnable for the thread that loads the contents of the launcher:
631 * - workspace icons
632 * - widgets
633 * - all apps icons
634 */
635 private class LoaderTask implements Runnable {
636 private Context mContext;
637 private Thread mWaitThread;
638 private boolean mIsLaunching;
639 private boolean mStopped;
640 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700641 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400642
643 LoaderTask(Context context, boolean isLaunching) {
644 mContext = context;
645 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700646 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647 }
648
Joe Onorato36115782010-06-17 13:28:48 -0400649 boolean isLaunching() {
650 return mIsLaunching;
651 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652
Joe Onorato36115782010-06-17 13:28:48 -0400653 private void loadAndBindWorkspace() {
654 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400655 if (DEBUG_LOADERS) {
656 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 }
Michael Jurkac2f801e2011-07-12 14:19:46 -0700658 Locale l = mContext.getResources().getConfiguration().locale;
659 if (mLocale != null && !l.equals(mLocale)) {
660 mWorkspaceLoaded = false;
661 }
662 mLocale = l;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700663 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400664 loadWorkspace();
665 if (mStopped) {
666 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400667 }
Joe Onorato36115782010-06-17 13:28:48 -0400668 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 }
670
Joe Onorato36115782010-06-17 13:28:48 -0400671 // Bind the workspace
672 bindWorkspace();
673 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400674
Joe Onorato36115782010-06-17 13:28:48 -0400675 private void waitForIdle() {
676 // Wait until the either we're stopped or the other threads are done.
677 // This way we don't start loading all apps until the workspace has settled
678 // down.
679 synchronized (LoaderTask.this) {
680 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700681
Joe Onorato36115782010-06-17 13:28:48 -0400682 mHandler.postIdle(new Runnable() {
683 public void run() {
684 synchronized (LoaderTask.this) {
685 mLoadAndBindStepFinished = true;
686 if (DEBUG_LOADERS) {
687 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400688 }
Joe Onorato36115782010-06-17 13:28:48 -0400689 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400690 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400691 }
Joe Onorato36115782010-06-17 13:28:48 -0400692 });
693
694 while (!mStopped && !mLoadAndBindStepFinished) {
695 try {
696 this.wait();
697 } catch (InterruptedException ex) {
698 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400699 }
700 }
Joe Onorato36115782010-06-17 13:28:48 -0400701 if (DEBUG_LOADERS) {
702 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700703 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400704 + "ms for previous step to finish binding");
705 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400706 }
Joe Onorato36115782010-06-17 13:28:48 -0400707 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400708
Joe Onorato36115782010-06-17 13:28:48 -0400709 public void run() {
710 // Optimize for end-user experience: if the Launcher is up and // running with the
711 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
712 // workspace first (default).
713 final Callbacks cbk = mCallbacks.get();
714 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400715
Joe Onorato36115782010-06-17 13:28:48 -0400716 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400717 // Elevate priority when Home launches for the first time to avoid
718 // starving at boot time. Staring at a blank home is not cool.
719 synchronized (mLock) {
720 android.os.Process.setThreadPriority(mIsLaunching
721 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
722 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400723 if (loadWorkspaceFirst) {
724 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
725 loadAndBindWorkspace();
726 } else {
727 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700728 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400729 }
730
Joe Onorato36115782010-06-17 13:28:48 -0400731 if (mStopped) {
732 break keep_running;
733 }
734
735 // Whew! Hard work done. Slow us down, and wait until the UI thread has
736 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400737 synchronized (mLock) {
738 if (mIsLaunching) {
739 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
740 }
741 }
Joe Onorato36115782010-06-17 13:28:48 -0400742 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400743
744 // second step
745 if (loadWorkspaceFirst) {
746 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700747 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400748 } else {
749 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
750 loadAndBindWorkspace();
751 }
Joe Onorato36115782010-06-17 13:28:48 -0400752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753
Joe Onorato36115782010-06-17 13:28:48 -0400754 // Clear out this reference, otherwise we end up holding it until all of the
755 // callback runnables are done.
756 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757
Joe Onorato36115782010-06-17 13:28:48 -0400758 synchronized (mLock) {
759 // If we are still the last one to be scheduled, remove ourselves.
760 if (mLoaderTask == this) {
761 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762 }
Joe Onorato36115782010-06-17 13:28:48 -0400763 }
Joe Onorato36115782010-06-17 13:28:48 -0400764 }
765
766 public void stopLocked() {
767 synchronized (LoaderTask.this) {
768 mStopped = true;
769 this.notify();
770 }
771 }
772
773 /**
774 * Gets the callbacks object. If we've been stopped, or if the launcher object
775 * has somehow been garbage collected, return null instead. Pass in the Callbacks
776 * object that was around when the deferred message was scheduled, and if there's
777 * a new Callbacks object around then also return null. This will save us from
778 * calling onto it with data that will be ignored.
779 */
780 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
781 synchronized (mLock) {
782 if (mStopped) {
783 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400784 }
Joe Onorato36115782010-06-17 13:28:48 -0400785
786 if (mCallbacks == null) {
787 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400788 }
Joe Onorato36115782010-06-17 13:28:48 -0400789
790 final Callbacks callbacks = mCallbacks.get();
791 if (callbacks != oldCallbacks) {
792 return null;
793 }
794 if (callbacks == null) {
795 Log.w(TAG, "no mCallbacks");
796 return null;
797 }
798
799 return callbacks;
800 }
801 }
802
803 // check & update map of what's occupied; used to discard overlapping/invalid items
804 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
805 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400806 return true;
807 }
Joe Onorato36115782010-06-17 13:28:48 -0400808 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
809 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
810 if (occupied[item.screen][x][y] != null) {
811 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700812 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400813 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700814 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400815 + occupied[item.screen][x][y]);
816 return false;
817 }
818 }
819 }
820 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
821 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
822 occupied[item.screen][x][y] = item;
823 }
824 }
825 return true;
826 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827
Joe Onorato36115782010-06-17 13:28:48 -0400828 private void loadWorkspace() {
829 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830
Joe Onorato36115782010-06-17 13:28:48 -0400831 final Context context = mContext;
832 final ContentResolver contentResolver = context.getContentResolver();
833 final PackageManager manager = context.getPackageManager();
834 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
835 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400836
Adam Cohen4eac29a2011-07-11 17:53:37 -0700837 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700838 sAppWidgets.clear();
839 sFolders.clear();
840 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800841
Joe Onorato36115782010-06-17 13:28:48 -0400842 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843
Joe Onorato36115782010-06-17 13:28:48 -0400844 final Cursor c = contentResolver.query(
845 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400846
Adam Cohend22015c2010-07-26 22:02:18 -0700847 final ItemInfo occupied[][][] =
848 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849
Joe Onorato36115782010-06-17 13:28:48 -0400850 try {
851 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
852 final int intentIndex = c.getColumnIndexOrThrow
853 (LauncherSettings.Favorites.INTENT);
854 final int titleIndex = c.getColumnIndexOrThrow
855 (LauncherSettings.Favorites.TITLE);
856 final int iconTypeIndex = c.getColumnIndexOrThrow(
857 LauncherSettings.Favorites.ICON_TYPE);
858 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
859 final int iconPackageIndex = c.getColumnIndexOrThrow(
860 LauncherSettings.Favorites.ICON_PACKAGE);
861 final int iconResourceIndex = c.getColumnIndexOrThrow(
862 LauncherSettings.Favorites.ICON_RESOURCE);
863 final int containerIndex = c.getColumnIndexOrThrow(
864 LauncherSettings.Favorites.CONTAINER);
865 final int itemTypeIndex = c.getColumnIndexOrThrow(
866 LauncherSettings.Favorites.ITEM_TYPE);
867 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
868 LauncherSettings.Favorites.APPWIDGET_ID);
869 final int screenIndex = c.getColumnIndexOrThrow(
870 LauncherSettings.Favorites.SCREEN);
871 final int cellXIndex = c.getColumnIndexOrThrow
872 (LauncherSettings.Favorites.CELLX);
873 final int cellYIndex = c.getColumnIndexOrThrow
874 (LauncherSettings.Favorites.CELLY);
875 final int spanXIndex = c.getColumnIndexOrThrow
876 (LauncherSettings.Favorites.SPANX);
877 final int spanYIndex = c.getColumnIndexOrThrow(
878 LauncherSettings.Favorites.SPANY);
879 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
880 final int displayModeIndex = c.getColumnIndexOrThrow(
881 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Joe Onorato36115782010-06-17 13:28:48 -0400883 ShortcutInfo info;
884 String intentDescription;
885 LauncherAppWidgetInfo appWidgetInfo;
886 int container;
887 long id;
888 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889
Joe Onorato36115782010-06-17 13:28:48 -0400890 while (!mStopped && c.moveToNext()) {
891 try {
892 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893
Joe Onorato36115782010-06-17 13:28:48 -0400894 switch (itemType) {
895 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
896 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
897 intentDescription = c.getString(intentIndex);
898 try {
899 intent = Intent.parseUri(intentDescription, 0);
900 } catch (URISyntaxException e) {
901 continue;
902 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903
Joe Onorato36115782010-06-17 13:28:48 -0400904 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
905 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700906 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400907 } else {
908 info = getShortcutInfo(c, context, iconTypeIndex,
909 iconPackageIndex, iconResourceIndex, iconIndex,
910 titleIndex);
911 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912
Joe Onorato36115782010-06-17 13:28:48 -0400913 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400914 info.intent = intent;
915 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400917 info.container = container;
918 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700919 info.cellX = c.getInt(cellXIndex);
920 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921
Daniel Sandler8802e962010-05-26 16:28:16 -0400922 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400923 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400924 break;
925 }
926
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400928 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700929 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400930 break;
931 default:
932 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700933 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700934 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400935 folderInfo.add(info);
936 break;
937 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700938 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800939
940 // now that we've loaded everthing re-save it with the
941 // icon in case it disappears somehow.
942 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400943 } else {
944 // Failed to load the shortcut, probably because the
945 // activity manager couldn't resolve it (maybe the app
946 // was uninstalled), or the db row was somehow screwed up.
947 // Delete it.
948 id = c.getLong(idIndex);
949 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
950 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
951 id, false), null, null);
952 }
953 break;
954
Adam Cohendf2cc412011-04-27 16:56:57 -0700955 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -0400956 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700957 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400958
Winson Chungaafa03c2010-06-11 17:34:16 -0700959 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400960 folderInfo.id = id;
961 container = c.getInt(containerIndex);
962 folderInfo.container = container;
963 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700964 folderInfo.cellX = c.getInt(cellXIndex);
965 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400966
967 // check & update map of what's occupied
968 if (!checkItemPlacement(occupied, folderInfo)) {
969 break;
970 }
Joe Onorato36115782010-06-17 13:28:48 -0400971 switch (container) {
972 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700973 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400974 break;
975 }
976
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700977 sItemsIdMap.put(folderInfo.id, folderInfo);
978 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400979 break;
980
Joe Onorato36115782010-06-17 13:28:48 -0400981 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
982 // Read all Launcher-specific widget details
983 int appWidgetId = c.getInt(appWidgetIdIndex);
984 id = c.getLong(idIndex);
985
986 final AppWidgetProviderInfo provider =
987 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700988
Joe Onorato36115782010-06-17 13:28:48 -0400989 if (!isSafeMode && (provider == null || provider.provider == null ||
990 provider.provider.getPackageName() == null)) {
991 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
992 + id + " appWidgetId=" + appWidgetId);
993 itemsToRemove.add(id);
994 } else {
995 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
996 appWidgetInfo.id = id;
997 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700998 appWidgetInfo.cellX = c.getInt(cellXIndex);
999 appWidgetInfo.cellY = c.getInt(cellYIndex);
1000 appWidgetInfo.spanX = c.getInt(spanXIndex);
1001 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001002
1003 container = c.getInt(containerIndex);
1004 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1005 Log.e(TAG, "Widget found where container "
1006 + "!= CONTAINER_DESKTOP -- ignoring!");
1007 continue;
1008 }
1009 appWidgetInfo.container = c.getInt(containerIndex);
1010
1011 // check & update map of what's occupied
1012 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1013 break;
1014 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001015 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1016 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001017 }
1018 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001019 }
Joe Onorato36115782010-06-17 13:28:48 -04001020 } catch (Exception e) {
1021 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001022 }
1023 }
Joe Onorato36115782010-06-17 13:28:48 -04001024 } finally {
1025 c.close();
1026 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001027
Joe Onorato36115782010-06-17 13:28:48 -04001028 if (itemsToRemove.size() > 0) {
1029 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1030 LauncherSettings.Favorites.CONTENT_URI);
1031 // Remove dead items
1032 for (long id : itemsToRemove) {
1033 if (DEBUG_LOADERS) {
1034 Log.d(TAG, "Removed id = " + id);
1035 }
1036 // Don't notify content observers
1037 try {
1038 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1039 null, null);
1040 } catch (RemoteException e) {
1041 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001042 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044 }
1045
Joe Onorato36115782010-06-17 13:28:48 -04001046 if (DEBUG_LOADERS) {
1047 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1048 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001049 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001050 String line = "";
1051 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1052 if (s > 0) {
1053 line += " | ";
1054 }
Adam Cohend22015c2010-07-26 22:02:18 -07001055 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001056 line += ((occupied[s][x][y] != null) ? "#" : ".");
1057 }
1058 }
1059 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060 }
Joe Onorato36115782010-06-17 13:28:48 -04001061 }
1062 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063
Joe Onorato36115782010-06-17 13:28:48 -04001064 /**
1065 * Read everything out of our database.
1066 */
1067 private void bindWorkspace() {
1068 final long t = SystemClock.uptimeMillis();
1069
1070 // Don't use these two variables in any of the callback runnables.
1071 // Otherwise we hold a reference to them.
1072 final Callbacks oldCallbacks = mCallbacks.get();
1073 if (oldCallbacks == null) {
1074 // This launcher has exited and nobody bothered to tell us. Just bail.
1075 Log.w(TAG, "LoaderTask running with no launcher");
1076 return;
1077 }
1078
1079 int N;
1080 // Tell the workspace that we're about to start firing items at it
1081 mHandler.post(new Runnable() {
1082 public void run() {
1083 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1084 if (callbacks != null) {
1085 callbacks.startBinding();
1086 }
1087 }
1088 });
1089 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001090 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001091 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1092 final int start = i;
1093 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094 mHandler.post(new Runnable() {
1095 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001096 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001097 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001098 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099 }
1100 }
1101 });
Joe Onorato36115782010-06-17 13:28:48 -04001102 }
1103 mHandler.post(new Runnable() {
1104 public void run() {
1105 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1106 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001107 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001108 }
1109 }
1110 });
1111 // Wait until the queue goes empty.
1112 mHandler.post(new Runnable() {
1113 public void run() {
1114 if (DEBUG_LOADERS) {
1115 Log.d(TAG, "Going to start binding widgets soon.");
1116 }
1117 }
1118 });
1119 // Bind the widgets, one at a time.
1120 // WARNING: this is calling into the workspace from the background thread,
1121 // but since getCurrentScreen() just returns the int, we should be okay. This
1122 // is just a hint for the order, and if it's wrong, we'll be okay.
1123 // TODO: instead, we should have that push the current screen into here.
1124 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001125 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001126 // once for the current screen
1127 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001128 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001129 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 mHandler.post(new Runnable() {
1131 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001132 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001134 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 }
1136 }
1137 });
1138 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 }
Joe Onorato36115782010-06-17 13:28:48 -04001140 // once for the other screens
1141 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001142 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001143 if (widget.screen != currentScreen) {
1144 mHandler.post(new Runnable() {
1145 public void run() {
1146 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1147 if (callbacks != null) {
1148 callbacks.bindAppWidget(widget);
1149 }
1150 }
1151 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001152 }
1153 }
Joe Onorato36115782010-06-17 13:28:48 -04001154 // Tell the workspace that we're done.
1155 mHandler.post(new Runnable() {
1156 public void run() {
1157 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1158 if (callbacks != null) {
1159 callbacks.finishBindingItems();
1160 }
1161 }
1162 });
1163 // If we're profiling, this is the last thing in the queue.
1164 mHandler.post(new Runnable() {
1165 public void run() {
1166 if (DEBUG_LOADERS) {
1167 Log.d(TAG, "bound workspace in "
1168 + (SystemClock.uptimeMillis()-t) + "ms");
1169 }
1170 }
1171 });
1172 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001173
Joe Onorato36115782010-06-17 13:28:48 -04001174 private void loadAndBindAllApps() {
1175 if (DEBUG_LOADERS) {
1176 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1177 }
1178 if (!mAllAppsLoaded) {
1179 loadAllAppsByBatch();
1180 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001181 return;
1182 }
Joe Onorato36115782010-06-17 13:28:48 -04001183 mAllAppsLoaded = true;
1184 } else {
1185 onlyBindAllApps();
1186 }
1187 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001188
Joe Onorato36115782010-06-17 13:28:48 -04001189 private void onlyBindAllApps() {
1190 final Callbacks oldCallbacks = mCallbacks.get();
1191 if (oldCallbacks == null) {
1192 // This launcher has exited and nobody bothered to tell us. Just bail.
1193 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1194 return;
1195 }
1196
1197 // shallow copy
1198 final ArrayList<ApplicationInfo> list
1199 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1200 mHandler.post(new Runnable() {
1201 public void run() {
1202 final long t = SystemClock.uptimeMillis();
1203 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1204 if (callbacks != null) {
1205 callbacks.bindAllApplications(list);
1206 }
1207 if (DEBUG_LOADERS) {
1208 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1209 + (SystemClock.uptimeMillis()-t) + "ms");
1210 }
1211 }
1212 });
1213
1214 }
1215
1216 private void loadAllAppsByBatch() {
1217 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1218
1219 // Don't use these two variables in any of the callback runnables.
1220 // Otherwise we hold a reference to them.
1221 final Callbacks oldCallbacks = mCallbacks.get();
1222 if (oldCallbacks == null) {
1223 // This launcher has exited and nobody bothered to tell us. Just bail.
1224 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1225 return;
1226 }
1227
1228 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1229 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1230
1231 final PackageManager packageManager = mContext.getPackageManager();
1232 List<ResolveInfo> apps = null;
1233
1234 int N = Integer.MAX_VALUE;
1235
1236 int startIndex;
1237 int i=0;
1238 int batchSize = -1;
1239 while (i < N && !mStopped) {
1240 if (i == 0) {
1241 mAllAppsList.clear();
1242 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1243 apps = packageManager.queryIntentActivities(mainIntent, 0);
1244 if (DEBUG_LOADERS) {
1245 Log.d(TAG, "queryIntentActivities took "
1246 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1247 }
1248 if (apps == null) {
1249 return;
1250 }
1251 N = apps.size();
1252 if (DEBUG_LOADERS) {
1253 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1254 }
1255 if (N == 0) {
1256 // There are no apps?!?
1257 return;
1258 }
1259 if (mBatchSize == 0) {
1260 batchSize = N;
1261 } else {
1262 batchSize = mBatchSize;
1263 }
1264
1265 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1266 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001267 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001268 if (DEBUG_LOADERS) {
1269 Log.d(TAG, "sort took "
1270 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1271 }
1272 }
1273
1274 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1275
1276 startIndex = i;
1277 for (int j=0; i<N && j<batchSize; j++) {
1278 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001279 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1280 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001281 i++;
1282 }
1283
1284 final boolean first = i <= batchSize;
1285 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1286 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1287 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1288
Joe Onoratocc67f472010-06-08 10:54:30 -07001289 mHandler.post(new Runnable() {
1290 public void run() {
1291 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001292 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001293 if (first) {
1294 callbacks.bindAllApplications(added);
1295 } else {
1296 callbacks.bindAppsAdded(added);
1297 }
1298 if (DEBUG_LOADERS) {
1299 Log.d(TAG, "bound " + added.size() + " apps in "
1300 + (SystemClock.uptimeMillis() - t) + "ms");
1301 }
1302 } else {
1303 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001304 }
1305 }
1306 });
1307
Daniel Sandlerdca66122010-04-13 16:23:58 -04001308 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001309 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1310 + (SystemClock.uptimeMillis()-t2) + "ms");
1311 }
1312
1313 if (mAllAppsLoadDelay > 0 && i < N) {
1314 try {
1315 if (DEBUG_LOADERS) {
1316 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1317 }
1318 Thread.sleep(mAllAppsLoadDelay);
1319 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320 }
1321 }
1322
Joe Onorato36115782010-06-17 13:28:48 -04001323 if (DEBUG_LOADERS) {
1324 Log.d(TAG, "cached all " + N + " apps in "
1325 + (SystemClock.uptimeMillis()-t) + "ms"
1326 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001327 }
1328 }
1329
1330 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001331 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1332 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1333 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1334 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1335 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001336 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001337 }
1338 }
1339
1340 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001341 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001342 }
1343
1344 private class PackageUpdatedTask implements Runnable {
1345 int mOp;
1346 String[] mPackages;
1347
1348 public static final int OP_NONE = 0;
1349 public static final int OP_ADD = 1;
1350 public static final int OP_UPDATE = 2;
1351 public static final int OP_REMOVE = 3; // uninstlled
1352 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1353
1354
1355 public PackageUpdatedTask(int op, String[] packages) {
1356 mOp = op;
1357 mPackages = packages;
1358 }
1359
1360 public void run() {
1361 final Context context = mApp;
1362
1363 final String[] packages = mPackages;
1364 final int N = packages.length;
1365 switch (mOp) {
1366 case OP_ADD:
1367 for (int i=0; i<N; i++) {
1368 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1369 mAllAppsList.addPackage(context, packages[i]);
1370 }
1371 break;
1372 case OP_UPDATE:
1373 for (int i=0; i<N; i++) {
1374 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1375 mAllAppsList.updatePackage(context, packages[i]);
1376 }
1377 break;
1378 case OP_REMOVE:
1379 case OP_UNAVAILABLE:
1380 for (int i=0; i<N; i++) {
1381 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1382 mAllAppsList.removePackage(packages[i]);
1383 }
1384 break;
1385 }
1386
1387 ArrayList<ApplicationInfo> added = null;
1388 ArrayList<ApplicationInfo> removed = null;
1389 ArrayList<ApplicationInfo> modified = null;
1390
1391 if (mAllAppsList.added.size() > 0) {
1392 added = mAllAppsList.added;
1393 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1394 }
1395 if (mAllAppsList.removed.size() > 0) {
1396 removed = mAllAppsList.removed;
1397 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1398 for (ApplicationInfo info: removed) {
1399 mIconCache.remove(info.intent.getComponent());
1400 }
1401 }
1402 if (mAllAppsList.modified.size() > 0) {
1403 modified = mAllAppsList.modified;
1404 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1405 }
1406
1407 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1408 if (callbacks == null) {
1409 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1410 return;
1411 }
1412
1413 if (added != null) {
1414 final ArrayList<ApplicationInfo> addedFinal = added;
1415 mHandler.post(new Runnable() {
1416 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001417 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1418 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001419 callbacks.bindAppsAdded(addedFinal);
1420 }
1421 }
1422 });
1423 }
1424 if (modified != null) {
1425 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1426 mHandler.post(new Runnable() {
1427 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001428 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1429 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001430 callbacks.bindAppsUpdated(modifiedFinal);
1431 }
1432 }
1433 });
1434 }
1435 if (removed != null) {
1436 final boolean permanent = mOp != OP_UNAVAILABLE;
1437 final ArrayList<ApplicationInfo> removedFinal = removed;
1438 mHandler.post(new Runnable() {
1439 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001440 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1441 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001442 callbacks.bindAppsRemoved(removedFinal, permanent);
1443 }
1444 }
1445 });
Joe Onoratobe386092009-11-17 17:32:16 -08001446 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001447
1448 mHandler.post(new Runnable() {
1449 @Override
1450 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001451 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1452 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001453 callbacks.bindPackagesUpdated();
1454 }
1455 }
1456 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001457 }
1458 }
1459
1460 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001461 * This is called from the code that adds shortcuts from the intent receiver. This
1462 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463 */
Joe Onorato56d82912010-03-07 14:32:10 -05001464 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001465 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467
Joe Onorato56d82912010-03-07 14:32:10 -05001468 /**
1469 * Make an ShortcutInfo object for a shortcut that is an application.
1470 *
1471 * If c is not null, then it will be used to fill in missing data like the title and icon.
1472 */
1473 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001474 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001475 Bitmap icon = null;
1476 final ShortcutInfo info = new ShortcutInfo();
1477
1478 ComponentName componentName = intent.getComponent();
1479 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001480 return null;
1481 }
1482
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001483 // TODO: See if the PackageManager knows about this case. If it doesn't
1484 // then return null & delete this.
1485
Joe Onorato56d82912010-03-07 14:32:10 -05001486 // the resource -- This may implicitly give us back the fallback icon,
1487 // but don't worry about that. All we're doing with usingFallbackIcon is
1488 // to avoid saving lots of copies of that in the database, and most apps
1489 // have icons anyway.
1490 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1491 if (resolveInfo != null) {
1492 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001493 }
Joe Onorato56d82912010-03-07 14:32:10 -05001494 // the db
1495 if (icon == null) {
1496 if (c != null) {
1497 icon = getIconFromCursor(c, iconIndex);
1498 }
1499 }
1500 // the fallback icon
1501 if (icon == null) {
1502 icon = getFallbackIcon();
1503 info.usingFallbackIcon = true;
1504 }
1505 info.setIcon(icon);
1506
1507 // from the resource
1508 if (resolveInfo != null) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001509 if (labelCache != null && labelCache.containsKey(resolveInfo)) {
1510 info.title = labelCache.get(resolveInfo);
1511 } else {
1512 info.title = resolveInfo.activityInfo.loadLabel(manager);
1513 if (labelCache != null) {
1514 labelCache.put(resolveInfo, info.title);
1515 }
1516 }
Joe Onorato56d82912010-03-07 14:32:10 -05001517 }
1518 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001519 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001520 if (c != null) {
1521 info.title = c.getString(titleIndex);
1522 }
1523 }
1524 // fall back to the class name of the activity
1525 if (info.title == null) {
1526 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001527 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001528 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1529 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001533 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001535 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001536 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1537 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
Joe Onorato56d82912010-03-07 14:32:10 -05001539 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001540 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001541 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001543 // TODO: If there's an explicit component and we can't install that, delete it.
1544
Joe Onorato56d82912010-03-07 14:32:10 -05001545 info.title = c.getString(titleIndex);
1546
Joe Onorato9c1289c2009-08-17 11:03:03 -04001547 int iconType = c.getInt(iconTypeIndex);
1548 switch (iconType) {
1549 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1550 String packageName = c.getString(iconPackageIndex);
1551 String resourceName = c.getString(iconResourceIndex);
1552 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001553 info.customIcon = false;
1554 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001557 if (resources != null) {
1558 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001559 icon = Utilities.createIconBitmap(
1560 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001561 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001563 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
Joe Onorato56d82912010-03-07 14:32:10 -05001565 // the db
1566 if (icon == null) {
1567 icon = getIconFromCursor(c, iconIndex);
1568 }
1569 // the fallback icon
1570 if (icon == null) {
1571 icon = getFallbackIcon();
1572 info.usingFallbackIcon = true;
1573 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 break;
1575 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001576 icon = getIconFromCursor(c, iconIndex);
1577 if (icon == null) {
1578 icon = getFallbackIcon();
1579 info.customIcon = false;
1580 info.usingFallbackIcon = true;
1581 } else {
1582 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001584 break;
1585 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001586 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001587 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 info.customIcon = false;
1589 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001591 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001592 return info;
1593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594
Joe Onorato56d82912010-03-07 14:32:10 -05001595 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1596 if (false) {
1597 Log.d(TAG, "getIconFromCursor app="
1598 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1599 }
1600 byte[] data = c.getBlob(iconIndex);
1601 try {
1602 return BitmapFactory.decodeByteArray(data, 0, data.length);
1603 } catch (Exception e) {
1604 return null;
1605 }
1606 }
1607
Joe Onorato0589f0f2010-02-08 13:44:00 -08001608 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001609 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001610
Winson Chunga9abd0e2010-10-27 17:18:37 -07001611 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001612 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001613 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001614
1615 return info;
1616 }
1617
Winson Chunga9abd0e2010-10-27 17:18:37 -07001618 /**
Winson Chung55cef262010-10-28 14:14:18 -07001619 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1620 */
1621 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1622 ComponentName component) {
1623 List<AppWidgetProviderInfo> widgets =
1624 AppWidgetManager.getInstance(context).getInstalledProviders();
1625 for (AppWidgetProviderInfo info : widgets) {
1626 if (info.provider.equals(component)) {
1627 return info;
1628 }
1629 }
1630 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001631 }
1632
Winson Chung68846fd2010-10-29 11:00:27 -07001633 /**
1634 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1635 */
1636 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1637 final PackageManager packageManager = context.getPackageManager();
1638 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1639 new ArrayList<WidgetMimeTypeHandlerData>();
1640
1641 final Intent supportsIntent =
1642 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1643 supportsIntent.setType(mimeType);
1644
1645 // Create a set of widget configuration components that we can test against
1646 final List<AppWidgetProviderInfo> widgets =
1647 AppWidgetManager.getInstance(context).getInstalledProviders();
1648 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1649 new HashMap<ComponentName, AppWidgetProviderInfo>();
1650 for (AppWidgetProviderInfo info : widgets) {
1651 configurationComponentToWidget.put(info.configure, info);
1652 }
1653
1654 // Run through each of the intents that can handle this type of clip data, and cross
1655 // reference them with the components that are actual configuration components
1656 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1657 PackageManager.MATCH_DEFAULT_ONLY);
1658 for (ResolveInfo info : activities) {
1659 final ActivityInfo activityInfo = info.activityInfo;
1660 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1661 activityInfo.name);
1662 if (configurationComponentToWidget.containsKey(infoComponent)) {
1663 supportedConfigurationActivities.add(
1664 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1665 configurationComponentToWidget.get(infoComponent)));
1666 }
1667 }
1668 return supportedConfigurationActivities;
1669 }
1670
Winson Chunga9abd0e2010-10-27 17:18:37 -07001671 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001672 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1673 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1674 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1675
1676 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001677 boolean customIcon = false;
1678 ShortcutIconResource iconResource = null;
1679
1680 if (bitmap != null && bitmap instanceof Bitmap) {
1681 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001682 customIcon = true;
1683 } else {
1684 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1685 if (extra != null && extra instanceof ShortcutIconResource) {
1686 try {
1687 iconResource = (ShortcutIconResource) extra;
1688 final PackageManager packageManager = context.getPackageManager();
1689 Resources resources = packageManager.getResourcesForApplication(
1690 iconResource.packageName);
1691 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001692 icon = Utilities.createIconBitmap(
1693 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001694 } catch (Exception e) {
1695 Log.w(TAG, "Could not load shortcut icon: " + extra);
1696 }
1697 }
1698 }
1699
Joe Onorato0589f0f2010-02-08 13:44:00 -08001700 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001701
1702 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001703 if (fallbackIcon != null) {
1704 icon = fallbackIcon;
1705 } else {
1706 icon = getFallbackIcon();
1707 info.usingFallbackIcon = true;
1708 }
Joe Onorato56d82912010-03-07 14:32:10 -05001709 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001710 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001711
Joe Onorato0589f0f2010-02-08 13:44:00 -08001712 info.title = name;
1713 info.intent = intent;
1714 info.customIcon = customIcon;
1715 info.iconResource = iconResource;
1716
1717 return info;
1718 }
1719
Joe Onorato56d82912010-03-07 14:32:10 -05001720 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001721 // If apps can't be on SD, don't even bother.
1722 if (!mAppsCanBeOnExternalStorage) {
1723 return;
1724 }
Joe Onorato56d82912010-03-07 14:32:10 -05001725 // If this icon doesn't have a custom icon, check to see
1726 // what's stored in the DB, and if it doesn't match what
1727 // we're going to show, store what we are going to show back
1728 // into the DB. We do this so when we're loading, if the
1729 // package manager can't find an icon (for example because
1730 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001731 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001732 boolean needSave;
1733 byte[] data = c.getBlob(iconIndex);
1734 try {
1735 if (data != null) {
1736 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1737 Bitmap loaded = info.getIcon(mIconCache);
1738 needSave = !saved.sameAs(loaded);
1739 } else {
1740 needSave = true;
1741 }
1742 } catch (Exception e) {
1743 needSave = true;
1744 }
1745 if (needSave) {
1746 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001747 // This is slower than is ideal, but this only happens once
1748 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001749 updateItemInDatabase(context, info);
1750 }
1751 }
1752 }
1753
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001755 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756 * or make a new one.
1757 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001758 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759 // See if a placeholder was created for us already
1760 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001761 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001763 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001764 folders.put(id, folderInfo);
1765 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001766 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 }
1768
Joe Onorato9c1289c2009-08-17 11:03:03 -04001769 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001770 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 = new Comparator<ApplicationInfo>() {
1772 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001773 int result = sCollator.compare(a.title.toString(), b.title.toString());
1774 if (result == 0) {
1775 result = a.componentName.compareTo(b.componentName);
1776 }
1777 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001779 };
Winson Chung78403fe2011-01-21 15:38:02 -08001780 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1781 = new Comparator<ApplicationInfo>() {
1782 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1783 if (a.firstInstallTime < b.firstInstallTime) return 1;
1784 if (a.firstInstallTime > b.firstInstallTime) return -1;
1785 return 0;
1786 }
1787 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001788 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1789 = new Comparator<AppWidgetProviderInfo>() {
1790 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1791 return sCollator.compare(a.label.toString(), b.label.toString());
1792 }
1793 };
1794 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1795 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001796 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001797 ShortcutNameComparator(PackageManager pm) {
1798 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001799 mLabelCache = new HashMap<Object, CharSequence>();
1800 }
1801 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1802 mPackageManager = pm;
1803 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001804 }
1805 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001806 CharSequence labelA, labelB;
1807 if (mLabelCache.containsKey(a)) {
1808 labelA = mLabelCache.get(a);
1809 } else {
1810 labelA = a.loadLabel(mPackageManager).toString();
1811
1812 mLabelCache.put(a, labelA);
1813 }
1814 if (mLabelCache.containsKey(b)) {
1815 labelB = mLabelCache.get(b);
1816 } else {
1817 labelB = b.loadLabel(mPackageManager).toString();
1818
1819 mLabelCache.put(b, labelB);
1820 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001821 return sCollator.compare(labelA, labelB);
1822 }
1823 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001824 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1825 private PackageManager mPackageManager;
1826 private HashMap<Object, String> mLabelCache;
1827 WidgetAndShortcutNameComparator(PackageManager pm) {
1828 mPackageManager = pm;
1829 mLabelCache = new HashMap<Object, String>();
1830 }
1831 public final int compare(Object a, Object b) {
1832 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001833 if (mLabelCache.containsKey(a)) {
1834 labelA = mLabelCache.get(a);
1835 } else {
1836 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001837 ((AppWidgetProviderInfo) a).label :
1838 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001839 mLabelCache.put(a, labelA);
1840 }
1841 if (mLabelCache.containsKey(b)) {
1842 labelB = mLabelCache.get(b);
1843 } else {
1844 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001845 ((AppWidgetProviderInfo) b).label :
1846 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001847 mLabelCache.put(b, labelB);
1848 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001849 return sCollator.compare(labelA, labelB);
1850 }
1851 };
Joe Onoratobe386092009-11-17 17:32:16 -08001852
1853 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001854 Log.d(TAG, "mCallbacks=" + mCallbacks);
1855 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1856 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1857 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1858 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001859 if (mLoaderTask != null) {
1860 mLoaderTask.dumpState();
1861 } else {
1862 Log.d(TAG, "mLoaderTask=null");
1863 }
Joe Onoratobe386092009-11-17 17:32:16 -08001864 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865}