blob: b4e632a41412d744420bec639fc34419ef61079e [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040022import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080024import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
26import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
37import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Winson Chung68846fd2010-10-29 11:00:27 -070047import com.android.launcher.R;
48import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080049
Michael Jurkac2f801e2011-07-12 14:19:46 -070050import java.lang.ref.WeakReference;
51import java.net.URISyntaxException;
52import java.text.Collator;
53import java.util.ArrayList;
54import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
57import java.util.List;
58import java.util.Locale;
59
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060/**
61 * Maintains in-memory state of the Launcher. It is expected that there should be only one
62 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070063 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040065public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080066 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040067 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070068
Joe Onorato36115782010-06-17 13:28:48 -040069 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080070 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040071 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040072 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040073
Joe Onoratof99f8c12009-10-31 17:27:36 -040074 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075 private final Object mLock = new Object();
76 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040077 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070079 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
80 static {
81 sWorkerThread.start();
82 }
83 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
84
Joe Onoratocc67f472010-06-08 10:54:30 -070085 // We start off with everything not loaded. After that, we assume that
86 // our monitoring of the package manager provides all updates and we never
87 // need to do a requery. These are only ever touched from the loader thread.
88 private boolean mWorkspaceLoaded;
89 private boolean mAllAppsLoaded;
90
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();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100133 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800137 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400138 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800139 mAllAppsList = new AllAppsList(iconCache);
140 mIconCache = iconCache;
141
142 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700143 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400144
145 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400146
147 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 }
149
Joe Onorato56d82912010-03-07 14:32:10 -0500150 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Adam Cohen4eac29a2011-07-11 17:53:37 -0700154 public static void unbindWorkspaceItems() {
155 for (ItemInfo item: sWorkspaceItems) {
156 item.unbind();
157 }
158 }
159
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 /**
161 * Adds an item to the DB if it was not created previously, or move it to a new
162 * <container, screen, cellX, cellY>
163 */
164 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
165 int screen, int cellX, int cellY) {
166 if (item.container == ItemInfo.NO_ID) {
167 // From all apps
168 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
169 } else {
170 // From somewhere else
171 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 }
173 }
174
175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700177 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700178 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
179 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 item.container = container;
181 item.screen = screen;
182 item.cellX = cellX;
183 item.cellY = cellY;
184
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700185 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 final ContentValues values = new ContentValues();
187 final ContentResolver cr = context.getContentResolver();
188
189 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700190 values.put(LauncherSettings.Favorites.CELLX, cellX);
191 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
193
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700194 sWorker.post(new Runnable() {
195 public void run() {
196 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700197 ItemInfo modelItem = sItemsIdMap.get(item.id);
198 if (item != modelItem) {
199 // the modelItem needs to match up perfectly with item if our model is to be
200 // consistent with the database-- for now, just require modelItem == item
201 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
202 "doesn't match original");
203 }
204
205 // Items are added/removed from the corresponding FolderInfo elsewhere, such
206 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
207 // that are on the desktop, as appropriate
208 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700209 if (!sWorkspaceItems.contains(modelItem)) {
210 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700211 }
212 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700213 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700214 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700215 }
216 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700217 }
218
219 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800220 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800221 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700222 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
223 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800224 item.spanX = spanX;
225 item.spanY = spanY;
226 item.cellX = cellX;
227 item.cellY = cellY;
228
229 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
230 final ContentValues values = new ContentValues();
231 final ContentResolver cr = context.getContentResolver();
232
233 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
234 values.put(LauncherSettings.Favorites.SPANX, spanX);
235 values.put(LauncherSettings.Favorites.SPANY, spanY);
236 values.put(LauncherSettings.Favorites.CELLX, cellX);
237 values.put(LauncherSettings.Favorites.CELLY, cellY);
238
239 sWorker.post(new Runnable() {
240 public void run() {
241 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700242 ItemInfo modelItem = sItemsIdMap.get(item.id);
243 if (item != modelItem) {
244 // the modelItem needs to match up perfectly with item if our model is to be
245 // consistent with the database-- for now, just require modelItem == item
246 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
247 "doesn't match original");
248 }
Adam Cohend4844c32011-02-18 19:25:06 -0800249 }
250 });
251 }
252
253 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 * Returns true if the shortcuts already exists in the database.
255 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257 static boolean shortcutExists(Context context, String title, Intent intent) {
258 final ContentResolver cr = context.getContentResolver();
259 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
260 new String[] { "title", "intent" }, "title=? and intent=?",
261 new String[] { title, intent.toUri(0) }, null);
262 boolean result = false;
263 try {
264 result = c.moveToFirst();
265 } finally {
266 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700269 }
270
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700272 * Returns an ItemInfo array containing all the items in the LauncherModel.
273 * The ItemInfo.id is not set through this function.
274 */
275 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
276 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
277 final ContentResolver cr = context.getContentResolver();
278 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
279 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
280 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
281 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
282
283 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
284 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
285 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
286 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
287 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
288 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
289 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
290
291 try {
292 while (c.moveToNext()) {
293 ItemInfo item = new ItemInfo();
294 item.cellX = c.getInt(cellXIndex);
295 item.cellY = c.getInt(cellYIndex);
296 item.spanX = c.getInt(spanXIndex);
297 item.spanY = c.getInt(spanYIndex);
298 item.container = c.getInt(containerIndex);
299 item.itemType = c.getInt(itemTypeIndex);
300 item.screen = c.getInt(screenIndex);
301
302 items.add(item);
303 }
304 } catch (Exception e) {
305 items.clear();
306 } finally {
307 c.close();
308 }
309
310 return items;
311 }
312
313 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400314 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
315 */
316 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
317 final ContentResolver cr = context.getContentResolver();
318 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
319 "_id=? and (itemType=? or itemType=?)",
320 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700321 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700322
Joe Onorato9c1289c2009-08-17 11:03:03 -0400323 try {
324 if (c.moveToFirst()) {
325 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
326 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
327 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
328 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
329 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
330 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331
Joe Onorato9c1289c2009-08-17 11:03:03 -0400332 FolderInfo folderInfo = null;
333 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700334 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
335 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400336 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700337 }
338
Joe Onorato9c1289c2009-08-17 11:03:03 -0400339 folderInfo.title = c.getString(titleIndex);
340 folderInfo.id = id;
341 folderInfo.container = c.getInt(containerIndex);
342 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700343 folderInfo.cellX = c.getInt(cellXIndex);
344 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400345
346 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700347 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400348 } finally {
349 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700350 }
351
352 return null;
353 }
354
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 /**
356 * Add an item to the database in a specified container. Sets the container, screen, cellX and
357 * cellY fields of the item. Also assigns an ID to the item.
358 */
Adam Cohend0445262011-07-04 23:53:22 -0700359 static void addItemToDatabase(Context context, final ItemInfo item, long container,
360 int screen, int cellX, int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 item.container = container;
362 item.screen = screen;
363 item.cellX = cellX;
364 item.cellY = cellY;
365
366 final ContentValues values = new ContentValues();
367 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 item.onAddToDatabase(values);
369
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700370 Launcher l = (Launcher) context;
371 LauncherApplication app = (LauncherApplication) l.getApplication();
372 item.id = app.getLauncherProvider().generateNewId();
373 values.put(LauncherSettings.Favorites._ID, item.id);
Adam Cohend22015c2010-07-26 22:02:18 -0700374 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700375
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700376 sWorker.post(new Runnable() {
377 public void run() {
378 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
379 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700381 sItemsIdMap.put(item.id, item);
382 switch (item.itemType) {
383 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
384 sFolders.put(item.id, (FolderInfo) item);
385 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700386 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700387 }
388 break;
389 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
390 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
391 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700392 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700393 }
394 break;
395 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
396 sAppWidgets.add((LauncherAppWidgetInfo) item);
397 break;
398 }
399 }
400 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700401 }
402
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700404 * Creates a new unique child id, for a given cell span across all layouts.
405 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700406 static int getCellLayoutChildId(
407 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
408 return ((cellId & 0xFF) << 24)
409 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700410 }
411
Adam Cohend22015c2010-07-26 22:02:18 -0700412 static int getCellCountX() {
413 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700414 }
415
Adam Cohend22015c2010-07-26 22:02:18 -0700416 static int getCellCountY() {
417 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700418 }
419
420 /**
421 * Updates the model orientation helper to take into account the current layout dimensions
422 * when performing local/canonical coordinate transformations.
423 */
424 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700425 mCellCountX = shortAxisCellCount;
426 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700427 }
428
429 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 * Update an item to the database in a specified container.
431 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700432 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400433 final ContentValues values = new ContentValues();
434 final ContentResolver cr = context.getContentResolver();
435
436 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700437 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700438
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700439 sWorker.post(new Runnable() {
440 public void run() {
441 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
442 values, null, null);
443 final ItemInfo modelItem = sItemsIdMap.get(item.id);
444 if (item != modelItem) {
445 // the modelItem needs to match up perfectly with item if our model is to be
446 // consistent with the database-- for now, just require modelItem == item
447 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
448 "doesn't match original");
449 }
450 }
451 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400452 }
453
454 /**
455 * Removes the specified item from the database
456 * @param context
457 * @param item
458 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700459 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400460 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700461 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700462 sWorker.post(new Runnable() {
463 public void run() {
464 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700465 switch (item.itemType) {
466 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
467 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700468 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700469 break;
470 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
471 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700472 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700473 break;
474 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
475 sAppWidgets.remove((LauncherAppWidgetInfo) item);
476 break;
477 }
478 sItemsIdMap.remove(item.id);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700479 }
480 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400481 }
482
483 /**
484 * Remove the contents of the specified folder from the database
485 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700486 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400487 final ContentResolver cr = context.getContentResolver();
488
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700489 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700490 public void run() {
491 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700492 sItemsIdMap.remove(info.id);
493 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700494 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700495
Adam Cohenafb01ee2011-06-23 15:38:03 -0700496 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
497 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700498 for (ItemInfo childInfo : info.contents) {
499 sItemsIdMap.remove(childInfo.id);
500 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700501 }
502 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400503 }
504
505 /**
506 * Set this as the current Launcher activity object for the loader.
507 */
508 public void initialize(Callbacks callbacks) {
509 synchronized (mLock) {
510 mCallbacks = new WeakReference<Callbacks>(callbacks);
511 }
512 }
513
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700514 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
516 * ACTION_PACKAGE_CHANGED.
517 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100518 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400519 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400520 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700521
Joe Onorato36115782010-06-17 13:28:48 -0400522 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400523
Joe Onorato36115782010-06-17 13:28:48 -0400524 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
525 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
526 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
527 final String packageName = intent.getData().getSchemeSpecificPart();
528 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400529
Joe Onorato36115782010-06-17 13:28:48 -0400530 int op = PackageUpdatedTask.OP_NONE;
531
532 if (packageName == null || packageName.length() == 0) {
533 // they sent us a bad intent
534 return;
535 }
536
537 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
538 op = PackageUpdatedTask.OP_UPDATE;
539 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
540 if (!replacing) {
541 op = PackageUpdatedTask.OP_REMOVE;
542 }
543 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
544 // later, we will update the package at this time
545 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
546 if (!replacing) {
547 op = PackageUpdatedTask.OP_ADD;
548 } else {
549 op = PackageUpdatedTask.OP_UPDATE;
550 }
551 }
552
553 if (op != PackageUpdatedTask.OP_NONE) {
554 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
555 }
556
557 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400558 // First, schedule to add these apps back in.
559 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
560 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
561 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700562 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400563 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
564 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
565 enqueuePackageUpdated(new PackageUpdatedTask(
566 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700567 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
568 // If we have changed locale we need to clear out the labels in all apps.
569 // Do this here because if the launcher activity is running it will be restarted.
570 // If it's not running startLoaderFromBackground will merely tell it that it needs
571 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
572 // next time.
573 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700574 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700575 startLoaderFromBackground();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100576 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
577 Callbacks callbacks = mCallbacks.get();
578 callbacks.bindSearchablesChanged();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700579 }
580 }
581
582 /**
583 * When the launcher is in the background, it's possible for it to miss paired
584 * configuration changes. So whenever we trigger the loader from the background
585 * tell the launcher that it needs to re-run the loader when it comes back instead
586 * of doing it now.
587 */
588 public void startLoaderFromBackground() {
589 boolean runLoader = false;
590 if (mCallbacks != null) {
591 Callbacks callbacks = mCallbacks.get();
592 if (callbacks != null) {
593 // Only actually run the loader if they're not paused.
594 if (!callbacks.setLoadOnResume()) {
595 runLoader = true;
596 }
597 }
598 }
599 if (runLoader) {
600 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700601 }
Joe Onorato36115782010-06-17 13:28:48 -0400602 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400603
Joe Onorato36115782010-06-17 13:28:48 -0400604 public void startLoader(Context context, boolean isLaunching) {
605 synchronized (mLock) {
606 if (DEBUG_LOADERS) {
607 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
608 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400609
Joe Onorato36115782010-06-17 13:28:48 -0400610 // Don't bother to start the thread if we know it's not going to do anything
611 if (mCallbacks != null && mCallbacks.get() != null) {
612 // If there is already one running, tell it to stop.
613 LoaderTask oldTask = mLoaderTask;
614 if (oldTask != null) {
615 if (oldTask.isLaunching()) {
616 // don't downgrade isLaunching if we're already running
617 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500618 }
Joe Onorato36115782010-06-17 13:28:48 -0400619 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500620 }
Joe Onorato36115782010-06-17 13:28:48 -0400621 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700622 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624 }
625 }
626
Joe Onorato36115782010-06-17 13:28:48 -0400627 public void stopLoader() {
628 synchronized (mLock) {
629 if (mLoaderTask != null) {
630 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 }
632 }
Joe Onorato36115782010-06-17 13:28:48 -0400633 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400634
Joe Onorato36115782010-06-17 13:28:48 -0400635 /**
636 * Runnable for the thread that loads the contents of the launcher:
637 * - workspace icons
638 * - widgets
639 * - all apps icons
640 */
641 private class LoaderTask implements Runnable {
642 private Context mContext;
643 private Thread mWaitThread;
644 private boolean mIsLaunching;
645 private boolean mStopped;
646 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700647 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400648
649 LoaderTask(Context context, boolean isLaunching) {
650 mContext = context;
651 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700652 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 }
654
Joe Onorato36115782010-06-17 13:28:48 -0400655 boolean isLaunching() {
656 return mIsLaunching;
657 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658
Joe Onorato36115782010-06-17 13:28:48 -0400659 private void loadAndBindWorkspace() {
660 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400661 if (DEBUG_LOADERS) {
662 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700664
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700665 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400666 loadWorkspace();
667 if (mStopped) {
668 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 }
Joe Onorato36115782010-06-17 13:28:48 -0400670 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671 }
672
Joe Onorato36115782010-06-17 13:28:48 -0400673 // Bind the workspace
674 bindWorkspace();
675 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400676
Joe Onorato36115782010-06-17 13:28:48 -0400677 private void waitForIdle() {
678 // Wait until the either we're stopped or the other threads are done.
679 // This way we don't start loading all apps until the workspace has settled
680 // down.
681 synchronized (LoaderTask.this) {
682 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700683
Joe Onorato36115782010-06-17 13:28:48 -0400684 mHandler.postIdle(new Runnable() {
685 public void run() {
686 synchronized (LoaderTask.this) {
687 mLoadAndBindStepFinished = true;
688 if (DEBUG_LOADERS) {
689 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400690 }
Joe Onorato36115782010-06-17 13:28:48 -0400691 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400692 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400693 }
Joe Onorato36115782010-06-17 13:28:48 -0400694 });
695
696 while (!mStopped && !mLoadAndBindStepFinished) {
697 try {
698 this.wait();
699 } catch (InterruptedException ex) {
700 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400701 }
702 }
Joe Onorato36115782010-06-17 13:28:48 -0400703 if (DEBUG_LOADERS) {
704 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700705 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400706 + "ms for previous step to finish binding");
707 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400708 }
Joe Onorato36115782010-06-17 13:28:48 -0400709 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400710
Joe Onorato36115782010-06-17 13:28:48 -0400711 public void run() {
712 // Optimize for end-user experience: if the Launcher is up and // running with the
713 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
714 // workspace first (default).
715 final Callbacks cbk = mCallbacks.get();
716 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400717
Joe Onorato36115782010-06-17 13:28:48 -0400718 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400719 // Elevate priority when Home launches for the first time to avoid
720 // starving at boot time. Staring at a blank home is not cool.
721 synchronized (mLock) {
722 android.os.Process.setThreadPriority(mIsLaunching
723 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
724 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400725 if (loadWorkspaceFirst) {
726 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
727 loadAndBindWorkspace();
728 } else {
729 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700730 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400731 }
732
Joe Onorato36115782010-06-17 13:28:48 -0400733 if (mStopped) {
734 break keep_running;
735 }
736
737 // Whew! Hard work done. Slow us down, and wait until the UI thread has
738 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400739 synchronized (mLock) {
740 if (mIsLaunching) {
741 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
742 }
743 }
Joe Onorato36115782010-06-17 13:28:48 -0400744 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400745
746 // second step
747 if (loadWorkspaceFirst) {
748 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700749 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400750 } else {
751 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
752 loadAndBindWorkspace();
753 }
Joe Onorato36115782010-06-17 13:28:48 -0400754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755
Joe Onorato36115782010-06-17 13:28:48 -0400756 // Clear out this reference, otherwise we end up holding it until all of the
757 // callback runnables are done.
758 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759
Joe Onorato36115782010-06-17 13:28:48 -0400760 synchronized (mLock) {
761 // If we are still the last one to be scheduled, remove ourselves.
762 if (mLoaderTask == this) {
763 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764 }
Joe Onorato36115782010-06-17 13:28:48 -0400765 }
Joe Onorato36115782010-06-17 13:28:48 -0400766 }
767
768 public void stopLocked() {
769 synchronized (LoaderTask.this) {
770 mStopped = true;
771 this.notify();
772 }
773 }
774
775 /**
776 * Gets the callbacks object. If we've been stopped, or if the launcher object
777 * has somehow been garbage collected, return null instead. Pass in the Callbacks
778 * object that was around when the deferred message was scheduled, and if there's
779 * a new Callbacks object around then also return null. This will save us from
780 * calling onto it with data that will be ignored.
781 */
782 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
783 synchronized (mLock) {
784 if (mStopped) {
785 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400786 }
Joe Onorato36115782010-06-17 13:28:48 -0400787
788 if (mCallbacks == null) {
789 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400790 }
Joe Onorato36115782010-06-17 13:28:48 -0400791
792 final Callbacks callbacks = mCallbacks.get();
793 if (callbacks != oldCallbacks) {
794 return null;
795 }
796 if (callbacks == null) {
797 Log.w(TAG, "no mCallbacks");
798 return null;
799 }
800
801 return callbacks;
802 }
803 }
804
805 // check & update map of what's occupied; used to discard overlapping/invalid items
806 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
807 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400808 return true;
809 }
Joe Onorato36115782010-06-17 13:28:48 -0400810 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
811 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
812 if (occupied[item.screen][x][y] != null) {
813 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700814 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400815 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700816 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400817 + occupied[item.screen][x][y]);
818 return false;
819 }
820 }
821 }
822 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
823 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
824 occupied[item.screen][x][y] = item;
825 }
826 }
827 return true;
828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829
Joe Onorato36115782010-06-17 13:28:48 -0400830 private void loadWorkspace() {
831 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832
Joe Onorato36115782010-06-17 13:28:48 -0400833 final Context context = mContext;
834 final ContentResolver contentResolver = context.getContentResolver();
835 final PackageManager manager = context.getPackageManager();
836 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
837 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400838
Adam Cohen4eac29a2011-07-11 17:53:37 -0700839 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700840 sAppWidgets.clear();
841 sFolders.clear();
842 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800843
Joe Onorato36115782010-06-17 13:28:48 -0400844 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845
Joe Onorato36115782010-06-17 13:28:48 -0400846 final Cursor c = contentResolver.query(
847 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400848
Adam Cohend22015c2010-07-26 22:02:18 -0700849 final ItemInfo occupied[][][] =
850 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851
Joe Onorato36115782010-06-17 13:28:48 -0400852 try {
853 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
854 final int intentIndex = c.getColumnIndexOrThrow
855 (LauncherSettings.Favorites.INTENT);
856 final int titleIndex = c.getColumnIndexOrThrow
857 (LauncherSettings.Favorites.TITLE);
858 final int iconTypeIndex = c.getColumnIndexOrThrow(
859 LauncherSettings.Favorites.ICON_TYPE);
860 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
861 final int iconPackageIndex = c.getColumnIndexOrThrow(
862 LauncherSettings.Favorites.ICON_PACKAGE);
863 final int iconResourceIndex = c.getColumnIndexOrThrow(
864 LauncherSettings.Favorites.ICON_RESOURCE);
865 final int containerIndex = c.getColumnIndexOrThrow(
866 LauncherSettings.Favorites.CONTAINER);
867 final int itemTypeIndex = c.getColumnIndexOrThrow(
868 LauncherSettings.Favorites.ITEM_TYPE);
869 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
870 LauncherSettings.Favorites.APPWIDGET_ID);
871 final int screenIndex = c.getColumnIndexOrThrow(
872 LauncherSettings.Favorites.SCREEN);
873 final int cellXIndex = c.getColumnIndexOrThrow
874 (LauncherSettings.Favorites.CELLX);
875 final int cellYIndex = c.getColumnIndexOrThrow
876 (LauncherSettings.Favorites.CELLY);
877 final int spanXIndex = c.getColumnIndexOrThrow
878 (LauncherSettings.Favorites.SPANX);
879 final int spanYIndex = c.getColumnIndexOrThrow(
880 LauncherSettings.Favorites.SPANY);
881 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
882 final int displayModeIndex = c.getColumnIndexOrThrow(
883 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884
Joe Onorato36115782010-06-17 13:28:48 -0400885 ShortcutInfo info;
886 String intentDescription;
887 LauncherAppWidgetInfo appWidgetInfo;
888 int container;
889 long id;
890 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891
Joe Onorato36115782010-06-17 13:28:48 -0400892 while (!mStopped && c.moveToNext()) {
893 try {
894 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895
Joe Onorato36115782010-06-17 13:28:48 -0400896 switch (itemType) {
897 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
898 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
899 intentDescription = c.getString(intentIndex);
900 try {
901 intent = Intent.parseUri(intentDescription, 0);
902 } catch (URISyntaxException e) {
903 continue;
904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905
Joe Onorato36115782010-06-17 13:28:48 -0400906 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
907 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700908 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400909 } else {
910 info = getShortcutInfo(c, context, iconTypeIndex,
911 iconPackageIndex, iconResourceIndex, iconIndex,
912 titleIndex);
913 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914
Joe Onorato36115782010-06-17 13:28:48 -0400915 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400916 info.intent = intent;
917 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400919 info.container = container;
920 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700921 info.cellX = c.getInt(cellXIndex);
922 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923
Daniel Sandler8802e962010-05-26 16:28:16 -0400924 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400925 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400926 break;
927 }
928
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400930 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700931 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400932 break;
933 default:
934 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700935 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700936 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400937 folderInfo.add(info);
938 break;
939 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700940 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800941
942 // now that we've loaded everthing re-save it with the
943 // icon in case it disappears somehow.
944 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400945 } else {
946 // Failed to load the shortcut, probably because the
947 // activity manager couldn't resolve it (maybe the app
948 // was uninstalled), or the db row was somehow screwed up.
949 // Delete it.
950 id = c.getLong(idIndex);
951 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
952 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
953 id, false), null, null);
954 }
955 break;
956
Adam Cohendf2cc412011-04-27 16:56:57 -0700957 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -0400958 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700959 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400960
Winson Chungaafa03c2010-06-11 17:34:16 -0700961 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400962 folderInfo.id = id;
963 container = c.getInt(containerIndex);
964 folderInfo.container = container;
965 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700966 folderInfo.cellX = c.getInt(cellXIndex);
967 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400968
969 // check & update map of what's occupied
970 if (!checkItemPlacement(occupied, folderInfo)) {
971 break;
972 }
Joe Onorato36115782010-06-17 13:28:48 -0400973 switch (container) {
974 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700975 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400976 break;
977 }
978
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700979 sItemsIdMap.put(folderInfo.id, folderInfo);
980 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400981 break;
982
Joe Onorato36115782010-06-17 13:28:48 -0400983 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
984 // Read all Launcher-specific widget details
985 int appWidgetId = c.getInt(appWidgetIdIndex);
986 id = c.getLong(idIndex);
987
988 final AppWidgetProviderInfo provider =
989 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700990
Joe Onorato36115782010-06-17 13:28:48 -0400991 if (!isSafeMode && (provider == null || provider.provider == null ||
992 provider.provider.getPackageName() == null)) {
993 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
994 + id + " appWidgetId=" + appWidgetId);
995 itemsToRemove.add(id);
996 } else {
997 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
998 appWidgetInfo.id = id;
999 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001000 appWidgetInfo.cellX = c.getInt(cellXIndex);
1001 appWidgetInfo.cellY = c.getInt(cellYIndex);
1002 appWidgetInfo.spanX = c.getInt(spanXIndex);
1003 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001004
1005 container = c.getInt(containerIndex);
1006 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1007 Log.e(TAG, "Widget found where container "
1008 + "!= CONTAINER_DESKTOP -- ignoring!");
1009 continue;
1010 }
1011 appWidgetInfo.container = c.getInt(containerIndex);
1012
1013 // check & update map of what's occupied
1014 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1015 break;
1016 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001017 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1018 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001019 }
1020 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001021 }
Joe Onorato36115782010-06-17 13:28:48 -04001022 } catch (Exception e) {
1023 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001024 }
1025 }
Joe Onorato36115782010-06-17 13:28:48 -04001026 } finally {
1027 c.close();
1028 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001029
Joe Onorato36115782010-06-17 13:28:48 -04001030 if (itemsToRemove.size() > 0) {
1031 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1032 LauncherSettings.Favorites.CONTENT_URI);
1033 // Remove dead items
1034 for (long id : itemsToRemove) {
1035 if (DEBUG_LOADERS) {
1036 Log.d(TAG, "Removed id = " + id);
1037 }
1038 // Don't notify content observers
1039 try {
1040 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1041 null, null);
1042 } catch (RemoteException e) {
1043 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001044 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001045 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 }
1047
Joe Onorato36115782010-06-17 13:28:48 -04001048 if (DEBUG_LOADERS) {
1049 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1050 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001051 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001052 String line = "";
1053 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1054 if (s > 0) {
1055 line += " | ";
1056 }
Adam Cohend22015c2010-07-26 22:02:18 -07001057 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001058 line += ((occupied[s][x][y] != null) ? "#" : ".");
1059 }
1060 }
1061 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 }
Joe Onorato36115782010-06-17 13:28:48 -04001063 }
1064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065
Joe Onorato36115782010-06-17 13:28:48 -04001066 /**
1067 * Read everything out of our database.
1068 */
1069 private void bindWorkspace() {
1070 final long t = SystemClock.uptimeMillis();
1071
1072 // Don't use these two variables in any of the callback runnables.
1073 // Otherwise we hold a reference to them.
1074 final Callbacks oldCallbacks = mCallbacks.get();
1075 if (oldCallbacks == null) {
1076 // This launcher has exited and nobody bothered to tell us. Just bail.
1077 Log.w(TAG, "LoaderTask running with no launcher");
1078 return;
1079 }
1080
1081 int N;
1082 // Tell the workspace that we're about to start firing items at it
1083 mHandler.post(new Runnable() {
1084 public void run() {
1085 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1086 if (callbacks != null) {
1087 callbacks.startBinding();
1088 }
1089 }
1090 });
1091 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001092 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001093 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1094 final int start = i;
1095 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096 mHandler.post(new Runnable() {
1097 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001098 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001100 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101 }
1102 }
1103 });
Joe Onorato36115782010-06-17 13:28:48 -04001104 }
1105 mHandler.post(new Runnable() {
1106 public void run() {
1107 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1108 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001109 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001110 }
1111 }
1112 });
1113 // Wait until the queue goes empty.
1114 mHandler.post(new Runnable() {
1115 public void run() {
1116 if (DEBUG_LOADERS) {
1117 Log.d(TAG, "Going to start binding widgets soon.");
1118 }
1119 }
1120 });
1121 // Bind the widgets, one at a time.
1122 // WARNING: this is calling into the workspace from the background thread,
1123 // but since getCurrentScreen() just returns the int, we should be okay. This
1124 // is just a hint for the order, and if it's wrong, we'll be okay.
1125 // TODO: instead, we should have that push the current screen into here.
1126 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001127 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001128 // once for the current screen
1129 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001130 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001131 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 mHandler.post(new Runnable() {
1133 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001134 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001136 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 }
1138 }
1139 });
1140 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 }
Joe Onorato36115782010-06-17 13:28:48 -04001142 // once for the other screens
1143 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001144 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001145 if (widget.screen != currentScreen) {
1146 mHandler.post(new Runnable() {
1147 public void run() {
1148 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1149 if (callbacks != null) {
1150 callbacks.bindAppWidget(widget);
1151 }
1152 }
1153 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001154 }
1155 }
Joe Onorato36115782010-06-17 13:28:48 -04001156 // Tell the workspace that we're done.
1157 mHandler.post(new Runnable() {
1158 public void run() {
1159 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1160 if (callbacks != null) {
1161 callbacks.finishBindingItems();
1162 }
1163 }
1164 });
1165 // If we're profiling, this is the last thing in the queue.
1166 mHandler.post(new Runnable() {
1167 public void run() {
1168 if (DEBUG_LOADERS) {
1169 Log.d(TAG, "bound workspace in "
1170 + (SystemClock.uptimeMillis()-t) + "ms");
1171 }
1172 }
1173 });
1174 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001175
Joe Onorato36115782010-06-17 13:28:48 -04001176 private void loadAndBindAllApps() {
1177 if (DEBUG_LOADERS) {
1178 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1179 }
1180 if (!mAllAppsLoaded) {
1181 loadAllAppsByBatch();
1182 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001183 return;
1184 }
Joe Onorato36115782010-06-17 13:28:48 -04001185 mAllAppsLoaded = true;
1186 } else {
1187 onlyBindAllApps();
1188 }
1189 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001190
Joe Onorato36115782010-06-17 13:28:48 -04001191 private void onlyBindAllApps() {
1192 final Callbacks oldCallbacks = mCallbacks.get();
1193 if (oldCallbacks == null) {
1194 // This launcher has exited and nobody bothered to tell us. Just bail.
1195 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1196 return;
1197 }
1198
1199 // shallow copy
1200 final ArrayList<ApplicationInfo> list
1201 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1202 mHandler.post(new Runnable() {
1203 public void run() {
1204 final long t = SystemClock.uptimeMillis();
1205 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1206 if (callbacks != null) {
1207 callbacks.bindAllApplications(list);
1208 }
1209 if (DEBUG_LOADERS) {
1210 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1211 + (SystemClock.uptimeMillis()-t) + "ms");
1212 }
1213 }
1214 });
1215
1216 }
1217
1218 private void loadAllAppsByBatch() {
1219 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1220
1221 // Don't use these two variables in any of the callback runnables.
1222 // Otherwise we hold a reference to them.
1223 final Callbacks oldCallbacks = mCallbacks.get();
1224 if (oldCallbacks == null) {
1225 // This launcher has exited and nobody bothered to tell us. Just bail.
1226 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1227 return;
1228 }
1229
1230 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1231 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1232
1233 final PackageManager packageManager = mContext.getPackageManager();
1234 List<ResolveInfo> apps = null;
1235
1236 int N = Integer.MAX_VALUE;
1237
1238 int startIndex;
1239 int i=0;
1240 int batchSize = -1;
1241 while (i < N && !mStopped) {
1242 if (i == 0) {
1243 mAllAppsList.clear();
1244 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1245 apps = packageManager.queryIntentActivities(mainIntent, 0);
1246 if (DEBUG_LOADERS) {
1247 Log.d(TAG, "queryIntentActivities took "
1248 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1249 }
1250 if (apps == null) {
1251 return;
1252 }
1253 N = apps.size();
1254 if (DEBUG_LOADERS) {
1255 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1256 }
1257 if (N == 0) {
1258 // There are no apps?!?
1259 return;
1260 }
1261 if (mBatchSize == 0) {
1262 batchSize = N;
1263 } else {
1264 batchSize = mBatchSize;
1265 }
1266
1267 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1268 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001269 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001270 if (DEBUG_LOADERS) {
1271 Log.d(TAG, "sort took "
1272 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1273 }
1274 }
1275
1276 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1277
1278 startIndex = i;
1279 for (int j=0; i<N && j<batchSize; j++) {
1280 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001281 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1282 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001283 i++;
1284 }
1285
1286 final boolean first = i <= batchSize;
1287 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1288 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1289 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1290
Joe Onoratocc67f472010-06-08 10:54:30 -07001291 mHandler.post(new Runnable() {
1292 public void run() {
1293 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001294 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001295 if (first) {
1296 callbacks.bindAllApplications(added);
1297 } else {
1298 callbacks.bindAppsAdded(added);
1299 }
1300 if (DEBUG_LOADERS) {
1301 Log.d(TAG, "bound " + added.size() + " apps in "
1302 + (SystemClock.uptimeMillis() - t) + "ms");
1303 }
1304 } else {
1305 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001306 }
1307 }
1308 });
1309
Daniel Sandlerdca66122010-04-13 16:23:58 -04001310 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001311 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1312 + (SystemClock.uptimeMillis()-t2) + "ms");
1313 }
1314
1315 if (mAllAppsLoadDelay > 0 && i < N) {
1316 try {
1317 if (DEBUG_LOADERS) {
1318 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1319 }
1320 Thread.sleep(mAllAppsLoadDelay);
1321 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322 }
1323 }
1324
Joe Onorato36115782010-06-17 13:28:48 -04001325 if (DEBUG_LOADERS) {
1326 Log.d(TAG, "cached all " + N + " apps in "
1327 + (SystemClock.uptimeMillis()-t) + "ms"
1328 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001329 }
1330 }
1331
1332 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001333 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1334 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1335 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1336 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1337 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001338 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001339 }
1340 }
1341
1342 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001343 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001344 }
1345
1346 private class PackageUpdatedTask implements Runnable {
1347 int mOp;
1348 String[] mPackages;
1349
1350 public static final int OP_NONE = 0;
1351 public static final int OP_ADD = 1;
1352 public static final int OP_UPDATE = 2;
1353 public static final int OP_REMOVE = 3; // uninstlled
1354 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1355
1356
1357 public PackageUpdatedTask(int op, String[] packages) {
1358 mOp = op;
1359 mPackages = packages;
1360 }
1361
1362 public void run() {
1363 final Context context = mApp;
1364
1365 final String[] packages = mPackages;
1366 final int N = packages.length;
1367 switch (mOp) {
1368 case OP_ADD:
1369 for (int i=0; i<N; i++) {
1370 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1371 mAllAppsList.addPackage(context, packages[i]);
1372 }
1373 break;
1374 case OP_UPDATE:
1375 for (int i=0; i<N; i++) {
1376 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1377 mAllAppsList.updatePackage(context, packages[i]);
1378 }
1379 break;
1380 case OP_REMOVE:
1381 case OP_UNAVAILABLE:
1382 for (int i=0; i<N; i++) {
1383 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1384 mAllAppsList.removePackage(packages[i]);
1385 }
1386 break;
1387 }
1388
1389 ArrayList<ApplicationInfo> added = null;
1390 ArrayList<ApplicationInfo> removed = null;
1391 ArrayList<ApplicationInfo> modified = null;
1392
1393 if (mAllAppsList.added.size() > 0) {
1394 added = mAllAppsList.added;
1395 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1396 }
1397 if (mAllAppsList.removed.size() > 0) {
1398 removed = mAllAppsList.removed;
1399 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1400 for (ApplicationInfo info: removed) {
1401 mIconCache.remove(info.intent.getComponent());
1402 }
1403 }
1404 if (mAllAppsList.modified.size() > 0) {
1405 modified = mAllAppsList.modified;
1406 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1407 }
1408
1409 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1410 if (callbacks == null) {
1411 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1412 return;
1413 }
1414
1415 if (added != null) {
1416 final ArrayList<ApplicationInfo> addedFinal = added;
1417 mHandler.post(new Runnable() {
1418 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001419 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1420 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001421 callbacks.bindAppsAdded(addedFinal);
1422 }
1423 }
1424 });
1425 }
1426 if (modified != null) {
1427 final ArrayList<ApplicationInfo> modifiedFinal = modified;
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.bindAppsUpdated(modifiedFinal);
1433 }
1434 }
1435 });
1436 }
1437 if (removed != null) {
1438 final boolean permanent = mOp != OP_UNAVAILABLE;
1439 final ArrayList<ApplicationInfo> removedFinal = removed;
1440 mHandler.post(new Runnable() {
1441 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001442 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1443 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001444 callbacks.bindAppsRemoved(removedFinal, permanent);
1445 }
1446 }
1447 });
Joe Onoratobe386092009-11-17 17:32:16 -08001448 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001449
1450 mHandler.post(new Runnable() {
1451 @Override
1452 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001453 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1454 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001455 callbacks.bindPackagesUpdated();
1456 }
1457 }
1458 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459 }
1460 }
1461
1462 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001463 * This is called from the code that adds shortcuts from the intent receiver. This
1464 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001465 */
Joe Onorato56d82912010-03-07 14:32:10 -05001466 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001467 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001468 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469
Joe Onorato56d82912010-03-07 14:32:10 -05001470 /**
1471 * Make an ShortcutInfo object for a shortcut that is an application.
1472 *
1473 * If c is not null, then it will be used to fill in missing data like the title and icon.
1474 */
1475 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001476 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001477 Bitmap icon = null;
1478 final ShortcutInfo info = new ShortcutInfo();
1479
1480 ComponentName componentName = intent.getComponent();
1481 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001482 return null;
1483 }
1484
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001485 // TODO: See if the PackageManager knows about this case. If it doesn't
1486 // then return null & delete this.
1487
Joe Onorato56d82912010-03-07 14:32:10 -05001488 // the resource -- This may implicitly give us back the fallback icon,
1489 // but don't worry about that. All we're doing with usingFallbackIcon is
1490 // to avoid saving lots of copies of that in the database, and most apps
1491 // have icons anyway.
1492 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1493 if (resolveInfo != null) {
1494 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001495 }
Joe Onorato56d82912010-03-07 14:32:10 -05001496 // the db
1497 if (icon == null) {
1498 if (c != null) {
1499 icon = getIconFromCursor(c, iconIndex);
1500 }
1501 }
1502 // the fallback icon
1503 if (icon == null) {
1504 icon = getFallbackIcon();
1505 info.usingFallbackIcon = true;
1506 }
1507 info.setIcon(icon);
1508
1509 // from the resource
1510 if (resolveInfo != null) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001511 if (labelCache != null && labelCache.containsKey(resolveInfo)) {
1512 info.title = labelCache.get(resolveInfo);
1513 } else {
1514 info.title = resolveInfo.activityInfo.loadLabel(manager);
1515 if (labelCache != null) {
1516 labelCache.put(resolveInfo, info.title);
1517 }
1518 }
Joe Onorato56d82912010-03-07 14:32:10 -05001519 }
1520 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001521 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001522 if (c != null) {
1523 info.title = c.getString(titleIndex);
1524 }
1525 }
1526 // fall back to the class name of the activity
1527 if (info.title == null) {
1528 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001529 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001530 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1531 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001533
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001535 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001537 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001538 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1539 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540
Joe Onorato56d82912010-03-07 14:32:10 -05001541 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001542 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001543 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001545 // TODO: If there's an explicit component and we can't install that, delete it.
1546
Joe Onorato56d82912010-03-07 14:32:10 -05001547 info.title = c.getString(titleIndex);
1548
Joe Onorato9c1289c2009-08-17 11:03:03 -04001549 int iconType = c.getInt(iconTypeIndex);
1550 switch (iconType) {
1551 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1552 String packageName = c.getString(iconPackageIndex);
1553 String resourceName = c.getString(iconResourceIndex);
1554 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001555 info.customIcon = false;
1556 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001558 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001559 if (resources != null) {
1560 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001561 icon = Utilities.createIconBitmap(
1562 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001563 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001564 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001565 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Joe Onorato56d82912010-03-07 14:32:10 -05001567 // the db
1568 if (icon == null) {
1569 icon = getIconFromCursor(c, iconIndex);
1570 }
1571 // the fallback icon
1572 if (icon == null) {
1573 icon = getFallbackIcon();
1574 info.usingFallbackIcon = true;
1575 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 break;
1577 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001578 icon = getIconFromCursor(c, iconIndex);
1579 if (icon == null) {
1580 icon = getFallbackIcon();
1581 info.customIcon = false;
1582 info.usingFallbackIcon = true;
1583 } else {
1584 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586 break;
1587 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001588 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001589 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001590 info.customIcon = false;
1591 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001593 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001594 return info;
1595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596
Joe Onorato56d82912010-03-07 14:32:10 -05001597 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1598 if (false) {
1599 Log.d(TAG, "getIconFromCursor app="
1600 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1601 }
1602 byte[] data = c.getBlob(iconIndex);
1603 try {
1604 return BitmapFactory.decodeByteArray(data, 0, data.length);
1605 } catch (Exception e) {
1606 return null;
1607 }
1608 }
1609
Joe Onorato0589f0f2010-02-08 13:44:00 -08001610 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001611 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001612
Winson Chunga9abd0e2010-10-27 17:18:37 -07001613 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001614 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001615 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001616
1617 return info;
1618 }
1619
Winson Chunga9abd0e2010-10-27 17:18:37 -07001620 /**
Winson Chung55cef262010-10-28 14:14:18 -07001621 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1622 */
1623 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1624 ComponentName component) {
1625 List<AppWidgetProviderInfo> widgets =
1626 AppWidgetManager.getInstance(context).getInstalledProviders();
1627 for (AppWidgetProviderInfo info : widgets) {
1628 if (info.provider.equals(component)) {
1629 return info;
1630 }
1631 }
1632 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001633 }
1634
Winson Chung68846fd2010-10-29 11:00:27 -07001635 /**
1636 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1637 */
1638 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1639 final PackageManager packageManager = context.getPackageManager();
1640 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1641 new ArrayList<WidgetMimeTypeHandlerData>();
1642
1643 final Intent supportsIntent =
1644 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1645 supportsIntent.setType(mimeType);
1646
1647 // Create a set of widget configuration components that we can test against
1648 final List<AppWidgetProviderInfo> widgets =
1649 AppWidgetManager.getInstance(context).getInstalledProviders();
1650 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1651 new HashMap<ComponentName, AppWidgetProviderInfo>();
1652 for (AppWidgetProviderInfo info : widgets) {
1653 configurationComponentToWidget.put(info.configure, info);
1654 }
1655
1656 // Run through each of the intents that can handle this type of clip data, and cross
1657 // reference them with the components that are actual configuration components
1658 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1659 PackageManager.MATCH_DEFAULT_ONLY);
1660 for (ResolveInfo info : activities) {
1661 final ActivityInfo activityInfo = info.activityInfo;
1662 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1663 activityInfo.name);
1664 if (configurationComponentToWidget.containsKey(infoComponent)) {
1665 supportedConfigurationActivities.add(
1666 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1667 configurationComponentToWidget.get(infoComponent)));
1668 }
1669 }
1670 return supportedConfigurationActivities;
1671 }
1672
Winson Chunga9abd0e2010-10-27 17:18:37 -07001673 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001674 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1675 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1676 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1677
1678 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001679 boolean customIcon = false;
1680 ShortcutIconResource iconResource = null;
1681
1682 if (bitmap != null && bitmap instanceof Bitmap) {
1683 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001684 customIcon = true;
1685 } else {
1686 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1687 if (extra != null && extra instanceof ShortcutIconResource) {
1688 try {
1689 iconResource = (ShortcutIconResource) extra;
1690 final PackageManager packageManager = context.getPackageManager();
1691 Resources resources = packageManager.getResourcesForApplication(
1692 iconResource.packageName);
1693 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001694 icon = Utilities.createIconBitmap(
1695 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001696 } catch (Exception e) {
1697 Log.w(TAG, "Could not load shortcut icon: " + extra);
1698 }
1699 }
1700 }
1701
Joe Onorato0589f0f2010-02-08 13:44:00 -08001702 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001703
1704 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001705 if (fallbackIcon != null) {
1706 icon = fallbackIcon;
1707 } else {
1708 icon = getFallbackIcon();
1709 info.usingFallbackIcon = true;
1710 }
Joe Onorato56d82912010-03-07 14:32:10 -05001711 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001712 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001713
Joe Onorato0589f0f2010-02-08 13:44:00 -08001714 info.title = name;
1715 info.intent = intent;
1716 info.customIcon = customIcon;
1717 info.iconResource = iconResource;
1718
1719 return info;
1720 }
1721
Joe Onorato56d82912010-03-07 14:32:10 -05001722 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001723 // If apps can't be on SD, don't even bother.
1724 if (!mAppsCanBeOnExternalStorage) {
1725 return;
1726 }
Joe Onorato56d82912010-03-07 14:32:10 -05001727 // If this icon doesn't have a custom icon, check to see
1728 // what's stored in the DB, and if it doesn't match what
1729 // we're going to show, store what we are going to show back
1730 // into the DB. We do this so when we're loading, if the
1731 // package manager can't find an icon (for example because
1732 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001733 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001734 boolean needSave;
1735 byte[] data = c.getBlob(iconIndex);
1736 try {
1737 if (data != null) {
1738 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1739 Bitmap loaded = info.getIcon(mIconCache);
1740 needSave = !saved.sameAs(loaded);
1741 } else {
1742 needSave = true;
1743 }
1744 } catch (Exception e) {
1745 needSave = true;
1746 }
1747 if (needSave) {
1748 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001749 // This is slower than is ideal, but this only happens once
1750 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001751 updateItemInDatabase(context, info);
1752 }
1753 }
1754 }
1755
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001757 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 * or make a new one.
1759 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001760 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761 // See if a placeholder was created for us already
1762 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001763 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001764 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001765 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766 folders.put(id, folderInfo);
1767 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001768 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
1770
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001772 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773 = new Comparator<ApplicationInfo>() {
1774 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001775 int result = sCollator.compare(a.title.toString(), b.title.toString());
1776 if (result == 0) {
1777 result = a.componentName.compareTo(b.componentName);
1778 }
1779 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781 };
Winson Chung78403fe2011-01-21 15:38:02 -08001782 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1783 = new Comparator<ApplicationInfo>() {
1784 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1785 if (a.firstInstallTime < b.firstInstallTime) return 1;
1786 if (a.firstInstallTime > b.firstInstallTime) return -1;
1787 return 0;
1788 }
1789 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001790 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1791 = new Comparator<AppWidgetProviderInfo>() {
1792 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1793 return sCollator.compare(a.label.toString(), b.label.toString());
1794 }
1795 };
1796 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1797 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001798 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001799 ShortcutNameComparator(PackageManager pm) {
1800 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001801 mLabelCache = new HashMap<Object, CharSequence>();
1802 }
1803 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1804 mPackageManager = pm;
1805 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001806 }
1807 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001808 CharSequence labelA, labelB;
1809 if (mLabelCache.containsKey(a)) {
1810 labelA = mLabelCache.get(a);
1811 } else {
1812 labelA = a.loadLabel(mPackageManager).toString();
1813
1814 mLabelCache.put(a, labelA);
1815 }
1816 if (mLabelCache.containsKey(b)) {
1817 labelB = mLabelCache.get(b);
1818 } else {
1819 labelB = b.loadLabel(mPackageManager).toString();
1820
1821 mLabelCache.put(b, labelB);
1822 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001823 return sCollator.compare(labelA, labelB);
1824 }
1825 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001826 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1827 private PackageManager mPackageManager;
1828 private HashMap<Object, String> mLabelCache;
1829 WidgetAndShortcutNameComparator(PackageManager pm) {
1830 mPackageManager = pm;
1831 mLabelCache = new HashMap<Object, String>();
1832 }
1833 public final int compare(Object a, Object b) {
1834 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001835 if (mLabelCache.containsKey(a)) {
1836 labelA = mLabelCache.get(a);
1837 } else {
1838 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001839 ((AppWidgetProviderInfo) a).label :
1840 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001841 mLabelCache.put(a, labelA);
1842 }
1843 if (mLabelCache.containsKey(b)) {
1844 labelB = mLabelCache.get(b);
1845 } else {
1846 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001847 ((AppWidgetProviderInfo) b).label :
1848 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001849 mLabelCache.put(b, labelB);
1850 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001851 return sCollator.compare(labelA, labelB);
1852 }
1853 };
Joe Onoratobe386092009-11-17 17:32:16 -08001854
1855 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001856 Log.d(TAG, "mCallbacks=" + mCallbacks);
1857 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1858 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1859 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1860 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001861 if (mLoaderTask != null) {
1862 mLoaderTask.dumpState();
1863 } else {
1864 Log.d(TAG, "mLoaderTask=null");
1865 }
Joe Onoratobe386092009-11-17 17:32:16 -08001866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867}