blob: 1a097b5201fb75a96138465e4e0b473a2d70bf04 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chung68846fd2010-10-29 11:00:27 -070019import java.lang.ref.WeakReference;
20import java.net.URISyntaxException;
21import java.text.Collator;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.List;
Winson Chungaafa03c2010-06-11 17:34:16 -070027
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040030import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080032import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ContentResolver;
34import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.ActivityInfo;
39import android.content.pm.PackageManager;
40import android.content.pm.ResolveInfo;
41import android.content.res.Resources;
42import android.database.Cursor;
43import android.graphics.Bitmap;
44import android.graphics.BitmapFactory;
45import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080046import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040047import android.os.Handler;
48import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040052import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
Winson Chung68846fd2010-10-29 11:00:27 -070055import com.android.launcher.R;
56import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080057
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * Maintains in-memory state of the Launcher. It is expected that there should be only one
60 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070061 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040063public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080064 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070066
Joe Onorato36115782010-06-17 13:28:48 -040067 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080068 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040069 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040070 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040071
Joe Onoratof99f8c12009-10-31 17:27:36 -040072 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040073 private final Object mLock = new Object();
74 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040075 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070077 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
78 static {
79 sWorkerThread.start();
80 }
81 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
82
Joe Onoratocc67f472010-06-08 10:54:30 -070083 // We start off with everything not loaded. After that, we assume that
84 // our monitoring of the package manager provides all updates and we never
85 // need to do a requery. These are only ever touched from the loader thread.
86 private boolean mWorkspaceLoaded;
87 private boolean mAllAppsLoaded;
88
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Michael Jurkaa8c760d2011-04-28 14:59:33 -070091 // < only access in worker thread >
92 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080093
Michael Jurkaa8c760d2011-04-28 14:59:33 -070094 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
95 // LauncherModel to their ids
96 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
97
98 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
99 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
100 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700101 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700102
103 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
104 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
105 new ArrayList<LauncherAppWidgetInfo>();
106
107 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
108 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
109 // </ only access in worker thread >
110
111 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800112 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Adam Cohend22015c2010-07-26 22:02:18 -0700114 private static int mCellCountX;
115 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700118 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 public int getCurrentWorkspaceScreen();
120 public void startBinding();
121 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500122 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 public void finishBindingItems();
124 public void bindAppWidget(LauncherAppWidgetInfo info);
125 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500126 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
127 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400128 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700129 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400130 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato0589f0f2010-02-08 13:44:00 -0800133 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800134 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400135 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136 mAllAppsList = new AllAppsList(iconCache);
137 mIconCache = iconCache;
138
139 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700140 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400141
142 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400143
144 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 }
146
Joe Onorato56d82912010-03-07 14:32:10 -0500147 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400149 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Adam Cohen4eac29a2011-07-11 17:53:37 -0700151 public static void unbindWorkspaceItems() {
152 for (ItemInfo item: sWorkspaceItems) {
153 item.unbind();
154 }
155 }
156
Joe Onorato9c1289c2009-08-17 11:03:03 -0400157 /**
158 * Adds an item to the DB if it was not created previously, or move it to a new
159 * <container, screen, cellX, cellY>
160 */
161 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
162 int screen, int cellX, int cellY) {
163 if (item.container == ItemInfo.NO_ID) {
164 // From all apps
165 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
166 } else {
167 // From somewhere else
168 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 }
170 }
171
172 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700174 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
176 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 item.container = container;
178 item.screen = screen;
179 item.cellX = cellX;
180 item.cellY = cellY;
181
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700182 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 final ContentValues values = new ContentValues();
184 final ContentResolver cr = context.getContentResolver();
185
186 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700187 values.put(LauncherSettings.Favorites.CELLX, cellX);
188 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
190
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700191 sWorker.post(new Runnable() {
192 public void run() {
193 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700194 ItemInfo modelItem = sItemsIdMap.get(item.id);
195 if (item != modelItem) {
196 // the modelItem needs to match up perfectly with item if our model is to be
197 // consistent with the database-- for now, just require modelItem == item
198 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
199 "doesn't match original");
200 }
201
202 // Items are added/removed from the corresponding FolderInfo elsewhere, such
203 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
204 // that are on the desktop, as appropriate
205 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700206 if (!sWorkspaceItems.contains(modelItem)) {
207 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700208 }
209 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700210 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700211 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700212 }
213 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700214 }
215
216 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800217 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800218 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700219 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
220 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800221 item.spanX = spanX;
222 item.spanY = spanY;
223 item.cellX = cellX;
224 item.cellY = cellY;
225
226 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
227 final ContentValues values = new ContentValues();
228 final ContentResolver cr = context.getContentResolver();
229
230 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
231 values.put(LauncherSettings.Favorites.SPANX, spanX);
232 values.put(LauncherSettings.Favorites.SPANY, spanY);
233 values.put(LauncherSettings.Favorites.CELLX, cellX);
234 values.put(LauncherSettings.Favorites.CELLY, cellY);
235
236 sWorker.post(new Runnable() {
237 public void run() {
238 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700239 ItemInfo modelItem = sItemsIdMap.get(item.id);
240 if (item != modelItem) {
241 // the modelItem needs to match up perfectly with item if our model is to be
242 // consistent with the database-- for now, just require modelItem == item
243 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
244 "doesn't match original");
245 }
Adam Cohend4844c32011-02-18 19:25:06 -0800246 }
247 });
248 }
249
250 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 * Returns true if the shortcuts already exists in the database.
252 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 static boolean shortcutExists(Context context, String title, Intent intent) {
255 final ContentResolver cr = context.getContentResolver();
256 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
257 new String[] { "title", "intent" }, "title=? and intent=?",
258 new String[] { title, intent.toUri(0) }, null);
259 boolean result = false;
260 try {
261 result = c.moveToFirst();
262 } finally {
263 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700266 }
267
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700269 * Returns an ItemInfo array containing all the items in the LauncherModel.
270 * The ItemInfo.id is not set through this function.
271 */
272 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
273 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
274 final ContentResolver cr = context.getContentResolver();
275 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
276 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
277 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
278 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
279
280 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
281 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
282 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
283 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
284 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
285 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
286 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
287
288 try {
289 while (c.moveToNext()) {
290 ItemInfo item = new ItemInfo();
291 item.cellX = c.getInt(cellXIndex);
292 item.cellY = c.getInt(cellYIndex);
293 item.spanX = c.getInt(spanXIndex);
294 item.spanY = c.getInt(spanYIndex);
295 item.container = c.getInt(containerIndex);
296 item.itemType = c.getInt(itemTypeIndex);
297 item.screen = c.getInt(screenIndex);
298
299 items.add(item);
300 }
301 } catch (Exception e) {
302 items.clear();
303 } finally {
304 c.close();
305 }
306
307 return items;
308 }
309
310 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
312 */
313 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
314 final ContentResolver cr = context.getContentResolver();
315 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
316 "_id=? and (itemType=? or itemType=?)",
317 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700318 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700319
Joe Onorato9c1289c2009-08-17 11:03:03 -0400320 try {
321 if (c.moveToFirst()) {
322 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
323 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
324 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
325 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
326 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
327 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328
Joe Onorato9c1289c2009-08-17 11:03:03 -0400329 FolderInfo folderInfo = null;
330 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700331 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
332 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400333 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700334 }
335
Joe Onorato9c1289c2009-08-17 11:03:03 -0400336 folderInfo.title = c.getString(titleIndex);
337 folderInfo.id = id;
338 folderInfo.container = c.getInt(containerIndex);
339 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700340 folderInfo.cellX = c.getInt(cellXIndex);
341 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342
343 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700344 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400345 } finally {
346 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700347 }
348
349 return null;
350 }
351
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 /**
353 * Add an item to the database in a specified container. Sets the container, screen, cellX and
354 * cellY fields of the item. Also assigns an ID to the item.
355 */
Adam Cohend0445262011-07-04 23:53:22 -0700356 static void addItemToDatabase(Context context, final ItemInfo item, long container,
357 int screen, int cellX, int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 item.container = container;
359 item.screen = screen;
360 item.cellX = cellX;
361 item.cellY = cellY;
362
363 final ContentValues values = new ContentValues();
364 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 item.onAddToDatabase(values);
366
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700367 Launcher l = (Launcher) context;
368 LauncherApplication app = (LauncherApplication) l.getApplication();
369 item.id = app.getLauncherProvider().generateNewId();
370 values.put(LauncherSettings.Favorites._ID, item.id);
Adam Cohend22015c2010-07-26 22:02:18 -0700371 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700372
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700373 sWorker.post(new Runnable() {
374 public void run() {
375 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
376 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700378 sItemsIdMap.put(item.id, item);
379 switch (item.itemType) {
380 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
381 sFolders.put(item.id, (FolderInfo) item);
382 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700383 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700384 }
385 break;
386 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
387 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
388 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700389 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700390 }
391 break;
392 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
393 sAppWidgets.add((LauncherAppWidgetInfo) item);
394 break;
395 }
396 }
397 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700398 }
399
Joe Onorato9c1289c2009-08-17 11:03:03 -0400400 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700401 * Creates a new unique child id, for a given cell span across all layouts.
402 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700403 static int getCellLayoutChildId(
404 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
405 return ((cellId & 0xFF) << 24)
406 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700407 }
408
Adam Cohend22015c2010-07-26 22:02:18 -0700409 static int getCellCountX() {
410 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700411 }
412
Adam Cohend22015c2010-07-26 22:02:18 -0700413 static int getCellCountY() {
414 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700415 }
416
417 /**
418 * Updates the model orientation helper to take into account the current layout dimensions
419 * when performing local/canonical coordinate transformations.
420 */
421 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700422 mCellCountX = shortAxisCellCount;
423 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700424 }
425
426 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427 * Update an item to the database in a specified container.
428 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700429 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 final ContentValues values = new ContentValues();
431 final ContentResolver cr = context.getContentResolver();
432
433 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700434 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700435
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700436 sWorker.post(new Runnable() {
437 public void run() {
438 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
439 values, null, null);
440 final ItemInfo modelItem = sItemsIdMap.get(item.id);
441 if (item != modelItem) {
442 // the modelItem needs to match up perfectly with item if our model is to be
443 // consistent with the database-- for now, just require modelItem == item
444 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
445 "doesn't match original");
446 }
447 }
448 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 }
450
451 /**
452 * Removes the specified item from the database
453 * @param context
454 * @param item
455 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700456 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700458 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700459 sWorker.post(new Runnable() {
460 public void run() {
461 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700462 switch (item.itemType) {
463 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
464 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700465 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700466 break;
467 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
468 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700469 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700470 break;
471 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
472 sAppWidgets.remove((LauncherAppWidgetInfo) item);
473 break;
474 }
475 sItemsIdMap.remove(item.id);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700476 }
477 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400478 }
479
480 /**
481 * Remove the contents of the specified folder from the database
482 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700483 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400484 final ContentResolver cr = context.getContentResolver();
485
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700486 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700487 public void run() {
488 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700489 sItemsIdMap.remove(info.id);
490 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700491 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700492
Adam Cohenafb01ee2011-06-23 15:38:03 -0700493 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
494 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700495 for (ItemInfo childInfo : info.contents) {
496 sItemsIdMap.remove(childInfo.id);
497 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700498 }
499 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400500 }
501
502 /**
503 * Set this as the current Launcher activity object for the loader.
504 */
505 public void initialize(Callbacks callbacks) {
506 synchronized (mLock) {
507 mCallbacks = new WeakReference<Callbacks>(callbacks);
508 }
509 }
510
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700511 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
513 * ACTION_PACKAGE_CHANGED.
514 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400515 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400516 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700517
Joe Onorato36115782010-06-17 13:28:48 -0400518 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400519
Joe Onorato36115782010-06-17 13:28:48 -0400520 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
521 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
522 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
523 final String packageName = intent.getData().getSchemeSpecificPart();
524 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525
Joe Onorato36115782010-06-17 13:28:48 -0400526 int op = PackageUpdatedTask.OP_NONE;
527
528 if (packageName == null || packageName.length() == 0) {
529 // they sent us a bad intent
530 return;
531 }
532
533 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
534 op = PackageUpdatedTask.OP_UPDATE;
535 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
536 if (!replacing) {
537 op = PackageUpdatedTask.OP_REMOVE;
538 }
539 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
540 // later, we will update the package at this time
541 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
542 if (!replacing) {
543 op = PackageUpdatedTask.OP_ADD;
544 } else {
545 op = PackageUpdatedTask.OP_UPDATE;
546 }
547 }
548
549 if (op != PackageUpdatedTask.OP_NONE) {
550 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
551 }
552
553 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400554 // First, schedule to add these apps back in.
555 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
556 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
557 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700558 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400559 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
560 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
561 enqueuePackageUpdated(new PackageUpdatedTask(
562 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700563 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
564 // If we have changed locale we need to clear out the labels in all apps.
565 // Do this here because if the launcher activity is running it will be restarted.
566 // If it's not running startLoaderFromBackground will merely tell it that it needs
567 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
568 // next time.
569 mAllAppsLoaded = false;
570 startLoaderFromBackground();
571 }
572 }
573
574 /**
575 * When the launcher is in the background, it's possible for it to miss paired
576 * configuration changes. So whenever we trigger the loader from the background
577 * tell the launcher that it needs to re-run the loader when it comes back instead
578 * of doing it now.
579 */
580 public void startLoaderFromBackground() {
581 boolean runLoader = false;
582 if (mCallbacks != null) {
583 Callbacks callbacks = mCallbacks.get();
584 if (callbacks != null) {
585 // Only actually run the loader if they're not paused.
586 if (!callbacks.setLoadOnResume()) {
587 runLoader = true;
588 }
589 }
590 }
591 if (runLoader) {
592 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700593 }
Joe Onorato36115782010-06-17 13:28:48 -0400594 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400595
Joe Onorato36115782010-06-17 13:28:48 -0400596 public void startLoader(Context context, boolean isLaunching) {
597 synchronized (mLock) {
598 if (DEBUG_LOADERS) {
599 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601
Joe Onorato36115782010-06-17 13:28:48 -0400602 // Don't bother to start the thread if we know it's not going to do anything
603 if (mCallbacks != null && mCallbacks.get() != null) {
604 // If there is already one running, tell it to stop.
605 LoaderTask oldTask = mLoaderTask;
606 if (oldTask != null) {
607 if (oldTask.isLaunching()) {
608 // don't downgrade isLaunching if we're already running
609 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500610 }
Joe Onorato36115782010-06-17 13:28:48 -0400611 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500612 }
Joe Onorato36115782010-06-17 13:28:48 -0400613 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700614 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 }
617 }
618
Joe Onorato36115782010-06-17 13:28:48 -0400619 public void stopLoader() {
620 synchronized (mLock) {
621 if (mLoaderTask != null) {
622 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 }
624 }
Joe Onorato36115782010-06-17 13:28:48 -0400625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626
Joe Onorato36115782010-06-17 13:28:48 -0400627 /**
628 * Runnable for the thread that loads the contents of the launcher:
629 * - workspace icons
630 * - widgets
631 * - all apps icons
632 */
633 private class LoaderTask implements Runnable {
634 private Context mContext;
635 private Thread mWaitThread;
636 private boolean mIsLaunching;
637 private boolean mStopped;
638 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700639 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400640
641 LoaderTask(Context context, boolean isLaunching) {
642 mContext = context;
643 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700644 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645 }
646
Joe Onorato36115782010-06-17 13:28:48 -0400647 boolean isLaunching() {
648 return mIsLaunching;
649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650
Joe Onorato36115782010-06-17 13:28:48 -0400651 private void loadAndBindWorkspace() {
652 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400653 if (DEBUG_LOADERS) {
654 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700656 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400657 loadWorkspace();
658 if (mStopped) {
659 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 }
Joe Onorato36115782010-06-17 13:28:48 -0400661 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662 }
663
Joe Onorato36115782010-06-17 13:28:48 -0400664 // Bind the workspace
665 bindWorkspace();
666 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400667
Joe Onorato36115782010-06-17 13:28:48 -0400668 private void waitForIdle() {
669 // Wait until the either we're stopped or the other threads are done.
670 // This way we don't start loading all apps until the workspace has settled
671 // down.
672 synchronized (LoaderTask.this) {
673 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700674
Joe Onorato36115782010-06-17 13:28:48 -0400675 mHandler.postIdle(new Runnable() {
676 public void run() {
677 synchronized (LoaderTask.this) {
678 mLoadAndBindStepFinished = true;
679 if (DEBUG_LOADERS) {
680 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400681 }
Joe Onorato36115782010-06-17 13:28:48 -0400682 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400683 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400684 }
Joe Onorato36115782010-06-17 13:28:48 -0400685 });
686
687 while (!mStopped && !mLoadAndBindStepFinished) {
688 try {
689 this.wait();
690 } catch (InterruptedException ex) {
691 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400692 }
693 }
Joe Onorato36115782010-06-17 13:28:48 -0400694 if (DEBUG_LOADERS) {
695 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700696 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400697 + "ms for previous step to finish binding");
698 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400699 }
Joe Onorato36115782010-06-17 13:28:48 -0400700 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400701
Joe Onorato36115782010-06-17 13:28:48 -0400702 public void run() {
703 // Optimize for end-user experience: if the Launcher is up and // running with the
704 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
705 // workspace first (default).
706 final Callbacks cbk = mCallbacks.get();
707 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400708
Joe Onorato36115782010-06-17 13:28:48 -0400709 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400710 // Elevate priority when Home launches for the first time to avoid
711 // starving at boot time. Staring at a blank home is not cool.
712 synchronized (mLock) {
713 android.os.Process.setThreadPriority(mIsLaunching
714 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
715 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400716 if (loadWorkspaceFirst) {
717 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
718 loadAndBindWorkspace();
719 } else {
720 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700721 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400722 }
723
Joe Onorato36115782010-06-17 13:28:48 -0400724 if (mStopped) {
725 break keep_running;
726 }
727
728 // Whew! Hard work done. Slow us down, and wait until the UI thread has
729 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400730 synchronized (mLock) {
731 if (mIsLaunching) {
732 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
733 }
734 }
Joe Onorato36115782010-06-17 13:28:48 -0400735 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400736
737 // second step
738 if (loadWorkspaceFirst) {
739 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700740 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400741 } else {
742 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
743 loadAndBindWorkspace();
744 }
Joe Onorato36115782010-06-17 13:28:48 -0400745 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746
Joe Onorato36115782010-06-17 13:28:48 -0400747 // Clear out this reference, otherwise we end up holding it until all of the
748 // callback runnables are done.
749 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750
Joe Onorato36115782010-06-17 13:28:48 -0400751 synchronized (mLock) {
752 // If we are still the last one to be scheduled, remove ourselves.
753 if (mLoaderTask == this) {
754 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755 }
Joe Onorato36115782010-06-17 13:28:48 -0400756 }
Joe Onorato36115782010-06-17 13:28:48 -0400757 }
758
759 public void stopLocked() {
760 synchronized (LoaderTask.this) {
761 mStopped = true;
762 this.notify();
763 }
764 }
765
766 /**
767 * Gets the callbacks object. If we've been stopped, or if the launcher object
768 * has somehow been garbage collected, return null instead. Pass in the Callbacks
769 * object that was around when the deferred message was scheduled, and if there's
770 * a new Callbacks object around then also return null. This will save us from
771 * calling onto it with data that will be ignored.
772 */
773 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
774 synchronized (mLock) {
775 if (mStopped) {
776 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400777 }
Joe Onorato36115782010-06-17 13:28:48 -0400778
779 if (mCallbacks == null) {
780 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400781 }
Joe Onorato36115782010-06-17 13:28:48 -0400782
783 final Callbacks callbacks = mCallbacks.get();
784 if (callbacks != oldCallbacks) {
785 return null;
786 }
787 if (callbacks == null) {
788 Log.w(TAG, "no mCallbacks");
789 return null;
790 }
791
792 return callbacks;
793 }
794 }
795
796 // check & update map of what's occupied; used to discard overlapping/invalid items
797 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
798 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400799 return true;
800 }
Joe Onorato36115782010-06-17 13:28:48 -0400801 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
802 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
803 if (occupied[item.screen][x][y] != null) {
804 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700805 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400806 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700807 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400808 + occupied[item.screen][x][y]);
809 return false;
810 }
811 }
812 }
813 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
814 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
815 occupied[item.screen][x][y] = item;
816 }
817 }
818 return true;
819 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820
Joe Onorato36115782010-06-17 13:28:48 -0400821 private void loadWorkspace() {
822 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823
Joe Onorato36115782010-06-17 13:28:48 -0400824 final Context context = mContext;
825 final ContentResolver contentResolver = context.getContentResolver();
826 final PackageManager manager = context.getPackageManager();
827 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
828 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400829
Adam Cohen4eac29a2011-07-11 17:53:37 -0700830 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700831 sAppWidgets.clear();
832 sFolders.clear();
833 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800834
Joe Onorato36115782010-06-17 13:28:48 -0400835 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836
Joe Onorato36115782010-06-17 13:28:48 -0400837 final Cursor c = contentResolver.query(
838 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400839
Adam Cohend22015c2010-07-26 22:02:18 -0700840 final ItemInfo occupied[][][] =
841 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842
Joe Onorato36115782010-06-17 13:28:48 -0400843 try {
844 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
845 final int intentIndex = c.getColumnIndexOrThrow
846 (LauncherSettings.Favorites.INTENT);
847 final int titleIndex = c.getColumnIndexOrThrow
848 (LauncherSettings.Favorites.TITLE);
849 final int iconTypeIndex = c.getColumnIndexOrThrow(
850 LauncherSettings.Favorites.ICON_TYPE);
851 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
852 final int iconPackageIndex = c.getColumnIndexOrThrow(
853 LauncherSettings.Favorites.ICON_PACKAGE);
854 final int iconResourceIndex = c.getColumnIndexOrThrow(
855 LauncherSettings.Favorites.ICON_RESOURCE);
856 final int containerIndex = c.getColumnIndexOrThrow(
857 LauncherSettings.Favorites.CONTAINER);
858 final int itemTypeIndex = c.getColumnIndexOrThrow(
859 LauncherSettings.Favorites.ITEM_TYPE);
860 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
861 LauncherSettings.Favorites.APPWIDGET_ID);
862 final int screenIndex = c.getColumnIndexOrThrow(
863 LauncherSettings.Favorites.SCREEN);
864 final int cellXIndex = c.getColumnIndexOrThrow
865 (LauncherSettings.Favorites.CELLX);
866 final int cellYIndex = c.getColumnIndexOrThrow
867 (LauncherSettings.Favorites.CELLY);
868 final int spanXIndex = c.getColumnIndexOrThrow
869 (LauncherSettings.Favorites.SPANX);
870 final int spanYIndex = c.getColumnIndexOrThrow(
871 LauncherSettings.Favorites.SPANY);
872 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
873 final int displayModeIndex = c.getColumnIndexOrThrow(
874 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875
Joe Onorato36115782010-06-17 13:28:48 -0400876 ShortcutInfo info;
877 String intentDescription;
878 LauncherAppWidgetInfo appWidgetInfo;
879 int container;
880 long id;
881 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Joe Onorato36115782010-06-17 13:28:48 -0400883 while (!mStopped && c.moveToNext()) {
884 try {
885 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400886
Joe Onorato36115782010-06-17 13:28:48 -0400887 switch (itemType) {
888 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
889 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
890 intentDescription = c.getString(intentIndex);
891 try {
892 intent = Intent.parseUri(intentDescription, 0);
893 } catch (URISyntaxException e) {
894 continue;
895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400896
Joe Onorato36115782010-06-17 13:28:48 -0400897 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
898 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700899 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400900 } else {
901 info = getShortcutInfo(c, context, iconTypeIndex,
902 iconPackageIndex, iconResourceIndex, iconIndex,
903 titleIndex);
904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905
Joe Onorato36115782010-06-17 13:28:48 -0400906 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400907 info.intent = intent;
908 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400910 info.container = container;
911 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700912 info.cellX = c.getInt(cellXIndex);
913 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914
Daniel Sandler8802e962010-05-26 16:28:16 -0400915 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400916 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400917 break;
918 }
919
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400921 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700922 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400923 break;
924 default:
925 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700926 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700927 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400928 folderInfo.add(info);
929 break;
930 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700931 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800932
933 // now that we've loaded everthing re-save it with the
934 // icon in case it disappears somehow.
935 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400936 } else {
937 // Failed to load the shortcut, probably because the
938 // activity manager couldn't resolve it (maybe the app
939 // was uninstalled), or the db row was somehow screwed up.
940 // Delete it.
941 id = c.getLong(idIndex);
942 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
943 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
944 id, false), null, null);
945 }
946 break;
947
Adam Cohendf2cc412011-04-27 16:56:57 -0700948 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -0400949 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700950 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400951
Winson Chungaafa03c2010-06-11 17:34:16 -0700952 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400953 folderInfo.id = id;
954 container = c.getInt(containerIndex);
955 folderInfo.container = container;
956 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700957 folderInfo.cellX = c.getInt(cellXIndex);
958 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400959
960 // check & update map of what's occupied
961 if (!checkItemPlacement(occupied, folderInfo)) {
962 break;
963 }
Joe Onorato36115782010-06-17 13:28:48 -0400964 switch (container) {
965 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700966 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400967 break;
968 }
969
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700970 sItemsIdMap.put(folderInfo.id, folderInfo);
971 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400972 break;
973
Joe Onorato36115782010-06-17 13:28:48 -0400974 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
975 // Read all Launcher-specific widget details
976 int appWidgetId = c.getInt(appWidgetIdIndex);
977 id = c.getLong(idIndex);
978
979 final AppWidgetProviderInfo provider =
980 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700981
Joe Onorato36115782010-06-17 13:28:48 -0400982 if (!isSafeMode && (provider == null || provider.provider == null ||
983 provider.provider.getPackageName() == null)) {
984 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
985 + id + " appWidgetId=" + appWidgetId);
986 itemsToRemove.add(id);
987 } else {
988 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
989 appWidgetInfo.id = id;
990 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700991 appWidgetInfo.cellX = c.getInt(cellXIndex);
992 appWidgetInfo.cellY = c.getInt(cellYIndex);
993 appWidgetInfo.spanX = c.getInt(spanXIndex);
994 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400995
996 container = c.getInt(containerIndex);
997 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
998 Log.e(TAG, "Widget found where container "
999 + "!= CONTAINER_DESKTOP -- ignoring!");
1000 continue;
1001 }
1002 appWidgetInfo.container = c.getInt(containerIndex);
1003
1004 // check & update map of what's occupied
1005 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1006 break;
1007 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001008 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1009 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001010 }
1011 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001012 }
Joe Onorato36115782010-06-17 13:28:48 -04001013 } catch (Exception e) {
1014 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001015 }
1016 }
Joe Onorato36115782010-06-17 13:28:48 -04001017 } finally {
1018 c.close();
1019 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001020
Joe Onorato36115782010-06-17 13:28:48 -04001021 if (itemsToRemove.size() > 0) {
1022 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1023 LauncherSettings.Favorites.CONTENT_URI);
1024 // Remove dead items
1025 for (long id : itemsToRemove) {
1026 if (DEBUG_LOADERS) {
1027 Log.d(TAG, "Removed id = " + id);
1028 }
1029 // Don't notify content observers
1030 try {
1031 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1032 null, null);
1033 } catch (RemoteException e) {
1034 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001035 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001036 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 }
1038
Joe Onorato36115782010-06-17 13:28:48 -04001039 if (DEBUG_LOADERS) {
1040 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1041 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001042 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001043 String line = "";
1044 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1045 if (s > 0) {
1046 line += " | ";
1047 }
Adam Cohend22015c2010-07-26 22:02:18 -07001048 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001049 line += ((occupied[s][x][y] != null) ? "#" : ".");
1050 }
1051 }
1052 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 }
Joe Onorato36115782010-06-17 13:28:48 -04001054 }
1055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056
Joe Onorato36115782010-06-17 13:28:48 -04001057 /**
1058 * Read everything out of our database.
1059 */
1060 private void bindWorkspace() {
1061 final long t = SystemClock.uptimeMillis();
1062
1063 // Don't use these two variables in any of the callback runnables.
1064 // Otherwise we hold a reference to them.
1065 final Callbacks oldCallbacks = mCallbacks.get();
1066 if (oldCallbacks == null) {
1067 // This launcher has exited and nobody bothered to tell us. Just bail.
1068 Log.w(TAG, "LoaderTask running with no launcher");
1069 return;
1070 }
1071
1072 int N;
1073 // Tell the workspace that we're about to start firing items at it
1074 mHandler.post(new Runnable() {
1075 public void run() {
1076 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1077 if (callbacks != null) {
1078 callbacks.startBinding();
1079 }
1080 }
1081 });
1082 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001083 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001084 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1085 final int start = i;
1086 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 mHandler.post(new Runnable() {
1088 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001089 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001091 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092 }
1093 }
1094 });
Joe Onorato36115782010-06-17 13:28:48 -04001095 }
1096 mHandler.post(new Runnable() {
1097 public void run() {
1098 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1099 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001100 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001101 }
1102 }
1103 });
1104 // Wait until the queue goes empty.
1105 mHandler.post(new Runnable() {
1106 public void run() {
1107 if (DEBUG_LOADERS) {
1108 Log.d(TAG, "Going to start binding widgets soon.");
1109 }
1110 }
1111 });
1112 // Bind the widgets, one at a time.
1113 // WARNING: this is calling into the workspace from the background thread,
1114 // but since getCurrentScreen() just returns the int, we should be okay. This
1115 // is just a hint for the order, and if it's wrong, we'll be okay.
1116 // TODO: instead, we should have that push the current screen into here.
1117 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001118 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001119 // once for the current screen
1120 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001122 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 mHandler.post(new Runnable() {
1124 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001125 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001127 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 }
1129 }
1130 });
1131 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
Joe Onorato36115782010-06-17 13:28:48 -04001133 // once for the other screens
1134 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001135 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001136 if (widget.screen != currentScreen) {
1137 mHandler.post(new Runnable() {
1138 public void run() {
1139 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1140 if (callbacks != null) {
1141 callbacks.bindAppWidget(widget);
1142 }
1143 }
1144 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001145 }
1146 }
Joe Onorato36115782010-06-17 13:28:48 -04001147 // Tell the workspace that we're done.
1148 mHandler.post(new Runnable() {
1149 public void run() {
1150 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1151 if (callbacks != null) {
1152 callbacks.finishBindingItems();
1153 }
1154 }
1155 });
1156 // If we're profiling, this is the last thing in the queue.
1157 mHandler.post(new Runnable() {
1158 public void run() {
1159 if (DEBUG_LOADERS) {
1160 Log.d(TAG, "bound workspace in "
1161 + (SystemClock.uptimeMillis()-t) + "ms");
1162 }
1163 }
1164 });
1165 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001166
Joe Onorato36115782010-06-17 13:28:48 -04001167 private void loadAndBindAllApps() {
1168 if (DEBUG_LOADERS) {
1169 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1170 }
1171 if (!mAllAppsLoaded) {
1172 loadAllAppsByBatch();
1173 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001174 return;
1175 }
Joe Onorato36115782010-06-17 13:28:48 -04001176 mAllAppsLoaded = true;
1177 } else {
1178 onlyBindAllApps();
1179 }
1180 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001181
Joe Onorato36115782010-06-17 13:28:48 -04001182 private void onlyBindAllApps() {
1183 final Callbacks oldCallbacks = mCallbacks.get();
1184 if (oldCallbacks == null) {
1185 // This launcher has exited and nobody bothered to tell us. Just bail.
1186 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1187 return;
1188 }
1189
1190 // shallow copy
1191 final ArrayList<ApplicationInfo> list
1192 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1193 mHandler.post(new Runnable() {
1194 public void run() {
1195 final long t = SystemClock.uptimeMillis();
1196 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1197 if (callbacks != null) {
1198 callbacks.bindAllApplications(list);
1199 }
1200 if (DEBUG_LOADERS) {
1201 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1202 + (SystemClock.uptimeMillis()-t) + "ms");
1203 }
1204 }
1205 });
1206
1207 }
1208
1209 private void loadAllAppsByBatch() {
1210 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1211
1212 // Don't use these two variables in any of the callback runnables.
1213 // Otherwise we hold a reference to them.
1214 final Callbacks oldCallbacks = mCallbacks.get();
1215 if (oldCallbacks == null) {
1216 // This launcher has exited and nobody bothered to tell us. Just bail.
1217 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1218 return;
1219 }
1220
1221 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1222 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1223
1224 final PackageManager packageManager = mContext.getPackageManager();
1225 List<ResolveInfo> apps = null;
1226
1227 int N = Integer.MAX_VALUE;
1228
1229 int startIndex;
1230 int i=0;
1231 int batchSize = -1;
1232 while (i < N && !mStopped) {
1233 if (i == 0) {
1234 mAllAppsList.clear();
1235 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1236 apps = packageManager.queryIntentActivities(mainIntent, 0);
1237 if (DEBUG_LOADERS) {
1238 Log.d(TAG, "queryIntentActivities took "
1239 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1240 }
1241 if (apps == null) {
1242 return;
1243 }
1244 N = apps.size();
1245 if (DEBUG_LOADERS) {
1246 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1247 }
1248 if (N == 0) {
1249 // There are no apps?!?
1250 return;
1251 }
1252 if (mBatchSize == 0) {
1253 batchSize = N;
1254 } else {
1255 batchSize = mBatchSize;
1256 }
1257
1258 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1259 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001260 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001261 if (DEBUG_LOADERS) {
1262 Log.d(TAG, "sort took "
1263 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1264 }
1265 }
1266
1267 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1268
1269 startIndex = i;
1270 for (int j=0; i<N && j<batchSize; j++) {
1271 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001272 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1273 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001274 i++;
1275 }
1276
1277 final boolean first = i <= batchSize;
1278 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1279 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1280 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1281
Joe Onoratocc67f472010-06-08 10:54:30 -07001282 mHandler.post(new Runnable() {
1283 public void run() {
1284 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001285 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001286 if (first) {
1287 callbacks.bindAllApplications(added);
1288 } else {
1289 callbacks.bindAppsAdded(added);
1290 }
1291 if (DEBUG_LOADERS) {
1292 Log.d(TAG, "bound " + added.size() + " apps in "
1293 + (SystemClock.uptimeMillis() - t) + "ms");
1294 }
1295 } else {
1296 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001297 }
1298 }
1299 });
1300
Daniel Sandlerdca66122010-04-13 16:23:58 -04001301 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001302 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1303 + (SystemClock.uptimeMillis()-t2) + "ms");
1304 }
1305
1306 if (mAllAppsLoadDelay > 0 && i < N) {
1307 try {
1308 if (DEBUG_LOADERS) {
1309 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1310 }
1311 Thread.sleep(mAllAppsLoadDelay);
1312 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001313 }
1314 }
1315
Joe Onorato36115782010-06-17 13:28:48 -04001316 if (DEBUG_LOADERS) {
1317 Log.d(TAG, "cached all " + N + " apps in "
1318 + (SystemClock.uptimeMillis()-t) + "ms"
1319 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001320 }
1321 }
1322
1323 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001324 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1325 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1326 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1327 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1328 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001329 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001330 }
1331 }
1332
1333 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001334 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001335 }
1336
1337 private class PackageUpdatedTask implements Runnable {
1338 int mOp;
1339 String[] mPackages;
1340
1341 public static final int OP_NONE = 0;
1342 public static final int OP_ADD = 1;
1343 public static final int OP_UPDATE = 2;
1344 public static final int OP_REMOVE = 3; // uninstlled
1345 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1346
1347
1348 public PackageUpdatedTask(int op, String[] packages) {
1349 mOp = op;
1350 mPackages = packages;
1351 }
1352
1353 public void run() {
1354 final Context context = mApp;
1355
1356 final String[] packages = mPackages;
1357 final int N = packages.length;
1358 switch (mOp) {
1359 case OP_ADD:
1360 for (int i=0; i<N; i++) {
1361 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1362 mAllAppsList.addPackage(context, packages[i]);
1363 }
1364 break;
1365 case OP_UPDATE:
1366 for (int i=0; i<N; i++) {
1367 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1368 mAllAppsList.updatePackage(context, packages[i]);
1369 }
1370 break;
1371 case OP_REMOVE:
1372 case OP_UNAVAILABLE:
1373 for (int i=0; i<N; i++) {
1374 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1375 mAllAppsList.removePackage(packages[i]);
1376 }
1377 break;
1378 }
1379
1380 ArrayList<ApplicationInfo> added = null;
1381 ArrayList<ApplicationInfo> removed = null;
1382 ArrayList<ApplicationInfo> modified = null;
1383
1384 if (mAllAppsList.added.size() > 0) {
1385 added = mAllAppsList.added;
1386 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1387 }
1388 if (mAllAppsList.removed.size() > 0) {
1389 removed = mAllAppsList.removed;
1390 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1391 for (ApplicationInfo info: removed) {
1392 mIconCache.remove(info.intent.getComponent());
1393 }
1394 }
1395 if (mAllAppsList.modified.size() > 0) {
1396 modified = mAllAppsList.modified;
1397 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1398 }
1399
1400 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1401 if (callbacks == null) {
1402 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1403 return;
1404 }
1405
1406 if (added != null) {
1407 final ArrayList<ApplicationInfo> addedFinal = added;
1408 mHandler.post(new Runnable() {
1409 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001410 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1411 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001412 callbacks.bindAppsAdded(addedFinal);
1413 }
1414 }
1415 });
1416 }
1417 if (modified != null) {
1418 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1419 mHandler.post(new Runnable() {
1420 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001421 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1422 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001423 callbacks.bindAppsUpdated(modifiedFinal);
1424 }
1425 }
1426 });
1427 }
1428 if (removed != null) {
1429 final boolean permanent = mOp != OP_UNAVAILABLE;
1430 final ArrayList<ApplicationInfo> removedFinal = removed;
1431 mHandler.post(new Runnable() {
1432 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001433 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1434 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001435 callbacks.bindAppsRemoved(removedFinal, permanent);
1436 }
1437 }
1438 });
Joe Onoratobe386092009-11-17 17:32:16 -08001439 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001440
1441 mHandler.post(new Runnable() {
1442 @Override
1443 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001444 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1445 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001446 callbacks.bindPackagesUpdated();
1447 }
1448 }
1449 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 }
1451 }
1452
1453 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001454 * This is called from the code that adds shortcuts from the intent receiver. This
1455 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001456 */
Joe Onorato56d82912010-03-07 14:32:10 -05001457 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001458 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001459 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460
Joe Onorato56d82912010-03-07 14:32:10 -05001461 /**
1462 * Make an ShortcutInfo object for a shortcut that is an application.
1463 *
1464 * If c is not null, then it will be used to fill in missing data like the title and icon.
1465 */
1466 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001467 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001468 Bitmap icon = null;
1469 final ShortcutInfo info = new ShortcutInfo();
1470
1471 ComponentName componentName = intent.getComponent();
1472 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001473 return null;
1474 }
1475
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001476 // TODO: See if the PackageManager knows about this case. If it doesn't
1477 // then return null & delete this.
1478
Joe Onorato56d82912010-03-07 14:32:10 -05001479 // the resource -- This may implicitly give us back the fallback icon,
1480 // but don't worry about that. All we're doing with usingFallbackIcon is
1481 // to avoid saving lots of copies of that in the database, and most apps
1482 // have icons anyway.
1483 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1484 if (resolveInfo != null) {
1485 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001486 }
Joe Onorato56d82912010-03-07 14:32:10 -05001487 // the db
1488 if (icon == null) {
1489 if (c != null) {
1490 icon = getIconFromCursor(c, iconIndex);
1491 }
1492 }
1493 // the fallback icon
1494 if (icon == null) {
1495 icon = getFallbackIcon();
1496 info.usingFallbackIcon = true;
1497 }
1498 info.setIcon(icon);
1499
1500 // from the resource
1501 if (resolveInfo != null) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001502 if (labelCache != null && labelCache.containsKey(resolveInfo)) {
1503 info.title = labelCache.get(resolveInfo);
1504 } else {
1505 info.title = resolveInfo.activityInfo.loadLabel(manager);
1506 if (labelCache != null) {
1507 labelCache.put(resolveInfo, info.title);
1508 }
1509 }
Joe Onorato56d82912010-03-07 14:32:10 -05001510 }
1511 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001513 if (c != null) {
1514 info.title = c.getString(titleIndex);
1515 }
1516 }
1517 // fall back to the class name of the activity
1518 if (info.title == null) {
1519 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001520 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001521 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1522 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001526 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001528 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001529 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1530 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531
Joe Onorato56d82912010-03-07 14:32:10 -05001532 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001533 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001534 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001536 // TODO: If there's an explicit component and we can't install that, delete it.
1537
Joe Onorato56d82912010-03-07 14:32:10 -05001538 info.title = c.getString(titleIndex);
1539
Joe Onorato9c1289c2009-08-17 11:03:03 -04001540 int iconType = c.getInt(iconTypeIndex);
1541 switch (iconType) {
1542 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1543 String packageName = c.getString(iconPackageIndex);
1544 String resourceName = c.getString(iconResourceIndex);
1545 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001546 info.customIcon = false;
1547 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001549 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001550 if (resources != null) {
1551 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001552 icon = Utilities.createIconBitmap(
1553 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001555 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001556 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
Joe Onorato56d82912010-03-07 14:32:10 -05001558 // the db
1559 if (icon == null) {
1560 icon = getIconFromCursor(c, iconIndex);
1561 }
1562 // the fallback icon
1563 if (icon == null) {
1564 icon = getFallbackIcon();
1565 info.usingFallbackIcon = true;
1566 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567 break;
1568 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001569 icon = getIconFromCursor(c, iconIndex);
1570 if (icon == null) {
1571 icon = getFallbackIcon();
1572 info.customIcon = false;
1573 info.usingFallbackIcon = true;
1574 } else {
1575 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001577 break;
1578 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001579 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001580 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001581 info.customIcon = false;
1582 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001584 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 return info;
1586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587
Joe Onorato56d82912010-03-07 14:32:10 -05001588 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1589 if (false) {
1590 Log.d(TAG, "getIconFromCursor app="
1591 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1592 }
1593 byte[] data = c.getBlob(iconIndex);
1594 try {
1595 return BitmapFactory.decodeByteArray(data, 0, data.length);
1596 } catch (Exception e) {
1597 return null;
1598 }
1599 }
1600
Joe Onorato0589f0f2010-02-08 13:44:00 -08001601 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001602 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001603
Winson Chunga9abd0e2010-10-27 17:18:37 -07001604 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001605 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001606 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001607
1608 return info;
1609 }
1610
Winson Chunga9abd0e2010-10-27 17:18:37 -07001611 /**
Winson Chung55cef262010-10-28 14:14:18 -07001612 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1613 */
1614 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1615 ComponentName component) {
1616 List<AppWidgetProviderInfo> widgets =
1617 AppWidgetManager.getInstance(context).getInstalledProviders();
1618 for (AppWidgetProviderInfo info : widgets) {
1619 if (info.provider.equals(component)) {
1620 return info;
1621 }
1622 }
1623 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001624 }
1625
Winson Chung68846fd2010-10-29 11:00:27 -07001626 /**
1627 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1628 */
1629 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1630 final PackageManager packageManager = context.getPackageManager();
1631 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1632 new ArrayList<WidgetMimeTypeHandlerData>();
1633
1634 final Intent supportsIntent =
1635 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1636 supportsIntent.setType(mimeType);
1637
1638 // Create a set of widget configuration components that we can test against
1639 final List<AppWidgetProviderInfo> widgets =
1640 AppWidgetManager.getInstance(context).getInstalledProviders();
1641 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1642 new HashMap<ComponentName, AppWidgetProviderInfo>();
1643 for (AppWidgetProviderInfo info : widgets) {
1644 configurationComponentToWidget.put(info.configure, info);
1645 }
1646
1647 // Run through each of the intents that can handle this type of clip data, and cross
1648 // reference them with the components that are actual configuration components
1649 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1650 PackageManager.MATCH_DEFAULT_ONLY);
1651 for (ResolveInfo info : activities) {
1652 final ActivityInfo activityInfo = info.activityInfo;
1653 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1654 activityInfo.name);
1655 if (configurationComponentToWidget.containsKey(infoComponent)) {
1656 supportedConfigurationActivities.add(
1657 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1658 configurationComponentToWidget.get(infoComponent)));
1659 }
1660 }
1661 return supportedConfigurationActivities;
1662 }
1663
Winson Chunga9abd0e2010-10-27 17:18:37 -07001664 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001665 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1666 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1667 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1668
1669 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001670 boolean customIcon = false;
1671 ShortcutIconResource iconResource = null;
1672
1673 if (bitmap != null && bitmap instanceof Bitmap) {
1674 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001675 customIcon = true;
1676 } else {
1677 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1678 if (extra != null && extra instanceof ShortcutIconResource) {
1679 try {
1680 iconResource = (ShortcutIconResource) extra;
1681 final PackageManager packageManager = context.getPackageManager();
1682 Resources resources = packageManager.getResourcesForApplication(
1683 iconResource.packageName);
1684 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001685 icon = Utilities.createIconBitmap(
1686 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001687 } catch (Exception e) {
1688 Log.w(TAG, "Could not load shortcut icon: " + extra);
1689 }
1690 }
1691 }
1692
Joe Onorato0589f0f2010-02-08 13:44:00 -08001693 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001694
1695 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001696 if (fallbackIcon != null) {
1697 icon = fallbackIcon;
1698 } else {
1699 icon = getFallbackIcon();
1700 info.usingFallbackIcon = true;
1701 }
Joe Onorato56d82912010-03-07 14:32:10 -05001702 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001703 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001704
Joe Onorato0589f0f2010-02-08 13:44:00 -08001705 info.title = name;
1706 info.intent = intent;
1707 info.customIcon = customIcon;
1708 info.iconResource = iconResource;
1709
1710 return info;
1711 }
1712
Joe Onorato56d82912010-03-07 14:32:10 -05001713 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001714 // If apps can't be on SD, don't even bother.
1715 if (!mAppsCanBeOnExternalStorage) {
1716 return;
1717 }
Joe Onorato56d82912010-03-07 14:32:10 -05001718 // If this icon doesn't have a custom icon, check to see
1719 // what's stored in the DB, and if it doesn't match what
1720 // we're going to show, store what we are going to show back
1721 // into the DB. We do this so when we're loading, if the
1722 // package manager can't find an icon (for example because
1723 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001724 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001725 boolean needSave;
1726 byte[] data = c.getBlob(iconIndex);
1727 try {
1728 if (data != null) {
1729 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1730 Bitmap loaded = info.getIcon(mIconCache);
1731 needSave = !saved.sameAs(loaded);
1732 } else {
1733 needSave = true;
1734 }
1735 } catch (Exception e) {
1736 needSave = true;
1737 }
1738 if (needSave) {
1739 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001740 // This is slower than is ideal, but this only happens once
1741 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001742 updateItemInDatabase(context, info);
1743 }
1744 }
1745 }
1746
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001748 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749 * or make a new one.
1750 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001751 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752 // See if a placeholder was created for us already
1753 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001754 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001755 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001756 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757 folders.put(id, folderInfo);
1758 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001759 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 }
1761
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001763 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001764 = new Comparator<ApplicationInfo>() {
1765 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001766 int result = sCollator.compare(a.title.toString(), b.title.toString());
1767 if (result == 0) {
1768 result = a.componentName.compareTo(b.componentName);
1769 }
1770 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772 };
Winson Chung78403fe2011-01-21 15:38:02 -08001773 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1774 = new Comparator<ApplicationInfo>() {
1775 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1776 if (a.firstInstallTime < b.firstInstallTime) return 1;
1777 if (a.firstInstallTime > b.firstInstallTime) return -1;
1778 return 0;
1779 }
1780 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001781 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1782 = new Comparator<AppWidgetProviderInfo>() {
1783 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1784 return sCollator.compare(a.label.toString(), b.label.toString());
1785 }
1786 };
1787 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1788 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001789 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001790 ShortcutNameComparator(PackageManager pm) {
1791 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001792 mLabelCache = new HashMap<Object, CharSequence>();
1793 }
1794 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1795 mPackageManager = pm;
1796 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001797 }
1798 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001799 CharSequence labelA, labelB;
1800 if (mLabelCache.containsKey(a)) {
1801 labelA = mLabelCache.get(a);
1802 } else {
1803 labelA = a.loadLabel(mPackageManager).toString();
1804
1805 mLabelCache.put(a, labelA);
1806 }
1807 if (mLabelCache.containsKey(b)) {
1808 labelB = mLabelCache.get(b);
1809 } else {
1810 labelB = b.loadLabel(mPackageManager).toString();
1811
1812 mLabelCache.put(b, labelB);
1813 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001814 return sCollator.compare(labelA, labelB);
1815 }
1816 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001817 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1818 private PackageManager mPackageManager;
1819 private HashMap<Object, String> mLabelCache;
1820 WidgetAndShortcutNameComparator(PackageManager pm) {
1821 mPackageManager = pm;
1822 mLabelCache = new HashMap<Object, String>();
1823 }
1824 public final int compare(Object a, Object b) {
1825 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001826 if (mLabelCache.containsKey(a)) {
1827 labelA = mLabelCache.get(a);
1828 } else {
1829 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001830 ((AppWidgetProviderInfo) a).label :
1831 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001832 mLabelCache.put(a, labelA);
1833 }
1834 if (mLabelCache.containsKey(b)) {
1835 labelB = mLabelCache.get(b);
1836 } else {
1837 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001838 ((AppWidgetProviderInfo) b).label :
1839 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001840 mLabelCache.put(b, labelB);
1841 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001842 return sCollator.compare(labelA, labelB);
1843 }
1844 };
Joe Onoratobe386092009-11-17 17:32:16 -08001845
1846 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001847 Log.d(TAG, "mCallbacks=" + mCallbacks);
1848 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1849 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1850 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1851 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001852 if (mLoaderTask != null) {
1853 mLoaderTask.dumpState();
1854 } else {
1855 Log.d(TAG, "mLoaderTask=null");
1856 }
Joe Onoratobe386092009-11-17 17:32:16 -08001857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858}