blob: 331f1242c9162c65981ddec34768fc671f1f63a1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetManager;
20import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040021import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080023import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.ContentResolver;
25import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080028import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.ActivityInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Resources;
33import android.database.Cursor;
34import android.graphics.Bitmap;
35import android.graphics.BitmapFactory;
36import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080037import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040038import android.os.Handler;
39import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040043import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045
Winson Chung68846fd2010-10-29 11:00:27 -070046import com.android.launcher.R;
47import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080048
Michael Jurkac2f801e2011-07-12 14:19:46 -070049import java.lang.ref.WeakReference;
50import java.net.URISyntaxException;
51import java.text.Collator;
52import java.util.ArrayList;
53import java.util.Collections;
54import java.util.Comparator;
55import java.util.HashMap;
56import java.util.List;
57import java.util.Locale;
58
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059/**
60 * Maintains in-memory state of the Launcher. It is expected that there should be only one
61 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070062 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040064public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080065 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040066 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070067
Joe Onorato36115782010-06-17 13:28:48 -040068 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080069 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040070 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040071 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040072
Joe Onoratof99f8c12009-10-31 17:27:36 -040073 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 private final Object mLock = new Object();
75 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040076 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070078 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
79 static {
80 sWorkerThread.start();
81 }
82 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
83
Joe Onoratocc67f472010-06-08 10:54:30 -070084 // We start off with everything not loaded. After that, we assume that
85 // our monitoring of the package manager provides all updates and we never
86 // need to do a requery. These are only ever touched from the loader thread.
87 private boolean mWorkspaceLoaded;
88 private boolean mAllAppsLoaded;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Michael Jurkaa8c760d2011-04-28 14:59:33 -070092 // < only access in worker thread >
93 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080094
Michael Jurkaa8c760d2011-04-28 14:59:33 -070095 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
96 // LauncherModel to their ids
97 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
98
99 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
100 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
101 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700102 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700103
104 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
105 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
106 new ArrayList<LauncherAppWidgetInfo>();
107
108 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
109 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
110 // </ only access in worker thread >
111
112 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800113 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Adam Cohend22015c2010-07-26 22:02:18 -0700115 private static int mCellCountX;
116 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700117
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700119 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 public int getCurrentWorkspaceScreen();
121 public void startBinding();
122 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500123 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 public void finishBindingItems();
125 public void bindAppWidget(LauncherAppWidgetInfo info);
126 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500127 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
128 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400129 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700130 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400131 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato0589f0f2010-02-08 13:44:00 -0800134 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800135 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400136 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800137 mAllAppsList = new AllAppsList(iconCache);
138 mIconCache = iconCache;
139
140 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700141 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400142
143 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400144
145 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146 }
147
Joe Onorato56d82912010-03-07 14:32:10 -0500148 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800149 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Adam Cohen4eac29a2011-07-11 17:53:37 -0700152 public static void unbindWorkspaceItems() {
153 for (ItemInfo item: sWorkspaceItems) {
154 item.unbind();
155 }
156 }
157
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 /**
159 * Adds an item to the DB if it was not created previously, or move it to a new
160 * <container, screen, cellX, cellY>
161 */
162 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
163 int screen, int cellX, int cellY) {
164 if (item.container == ItemInfo.NO_ID) {
165 // From all apps
166 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
167 } else {
168 // From somewhere else
169 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 }
171 }
172
173 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700175 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700176 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
177 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 item.container = container;
179 item.screen = screen;
180 item.cellX = cellX;
181 item.cellY = cellY;
182
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700183 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 final ContentValues values = new ContentValues();
185 final ContentResolver cr = context.getContentResolver();
186
187 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700188 values.put(LauncherSettings.Favorites.CELLX, cellX);
189 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
191
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700192 sWorker.post(new Runnable() {
193 public void run() {
194 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700195 ItemInfo modelItem = sItemsIdMap.get(item.id);
196 if (item != modelItem) {
197 // the modelItem needs to match up perfectly with item if our model is to be
198 // consistent with the database-- for now, just require modelItem == item
199 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
200 "doesn't match original");
201 }
202
203 // Items are added/removed from the corresponding FolderInfo elsewhere, such
204 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
205 // that are on the desktop, as appropriate
206 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700207 if (!sWorkspaceItems.contains(modelItem)) {
208 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700209 }
210 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700211 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700212 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700213 }
214 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700215 }
216
217 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800218 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800219 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700220 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
221 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800222 item.spanX = spanX;
223 item.spanY = spanY;
224 item.cellX = cellX;
225 item.cellY = cellY;
226
227 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
228 final ContentValues values = new ContentValues();
229 final ContentResolver cr = context.getContentResolver();
230
231 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
232 values.put(LauncherSettings.Favorites.SPANX, spanX);
233 values.put(LauncherSettings.Favorites.SPANY, spanY);
234 values.put(LauncherSettings.Favorites.CELLX, cellX);
235 values.put(LauncherSettings.Favorites.CELLY, cellY);
236
237 sWorker.post(new Runnable() {
238 public void run() {
239 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700240 ItemInfo modelItem = sItemsIdMap.get(item.id);
241 if (item != modelItem) {
242 // the modelItem needs to match up perfectly with item if our model is to be
243 // consistent with the database-- for now, just require modelItem == item
244 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
245 "doesn't match original");
246 }
Adam Cohend4844c32011-02-18 19:25:06 -0800247 }
248 });
249 }
250
251 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252 * Returns true if the shortcuts already exists in the database.
253 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 static boolean shortcutExists(Context context, String title, Intent intent) {
256 final ContentResolver cr = context.getContentResolver();
257 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
258 new String[] { "title", "intent" }, "title=? and intent=?",
259 new String[] { title, intent.toUri(0) }, null);
260 boolean result = false;
261 try {
262 result = c.moveToFirst();
263 } finally {
264 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700267 }
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700270 * Returns an ItemInfo array containing all the items in the LauncherModel.
271 * The ItemInfo.id is not set through this function.
272 */
273 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
274 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
275 final ContentResolver cr = context.getContentResolver();
276 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
277 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
278 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
279 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
280
281 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
282 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
283 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
284 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
285 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
286 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
287 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
288
289 try {
290 while (c.moveToNext()) {
291 ItemInfo item = new ItemInfo();
292 item.cellX = c.getInt(cellXIndex);
293 item.cellY = c.getInt(cellYIndex);
294 item.spanX = c.getInt(spanXIndex);
295 item.spanY = c.getInt(spanYIndex);
296 item.container = c.getInt(containerIndex);
297 item.itemType = c.getInt(itemTypeIndex);
298 item.screen = c.getInt(screenIndex);
299
300 items.add(item);
301 }
302 } catch (Exception e) {
303 items.clear();
304 } finally {
305 c.close();
306 }
307
308 return items;
309 }
310
311 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
313 */
314 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
315 final ContentResolver cr = context.getContentResolver();
316 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
317 "_id=? and (itemType=? or itemType=?)",
318 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700319 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700320
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 try {
322 if (c.moveToFirst()) {
323 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
324 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
325 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
326 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
327 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
328 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329
Joe Onorato9c1289c2009-08-17 11:03:03 -0400330 FolderInfo folderInfo = null;
331 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700332 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
333 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400334 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700335 }
336
Joe Onorato9c1289c2009-08-17 11:03:03 -0400337 folderInfo.title = c.getString(titleIndex);
338 folderInfo.id = id;
339 folderInfo.container = c.getInt(containerIndex);
340 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700341 folderInfo.cellX = c.getInt(cellXIndex);
342 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400343
344 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700345 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400346 } finally {
347 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700348 }
349
350 return null;
351 }
352
Joe Onorato9c1289c2009-08-17 11:03:03 -0400353 /**
354 * Add an item to the database in a specified container. Sets the container, screen, cellX and
355 * cellY fields of the item. Also assigns an ID to the item.
356 */
Adam Cohend0445262011-07-04 23:53:22 -0700357 static void addItemToDatabase(Context context, final ItemInfo item, long container,
358 int screen, int cellX, int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359 item.container = container;
360 item.screen = screen;
361 item.cellX = cellX;
362 item.cellY = cellY;
363
364 final ContentValues values = new ContentValues();
365 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 item.onAddToDatabase(values);
367
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700368 Launcher l = (Launcher) context;
369 LauncherApplication app = (LauncherApplication) l.getApplication();
370 item.id = app.getLauncherProvider().generateNewId();
371 values.put(LauncherSettings.Favorites._ID, item.id);
Adam Cohend22015c2010-07-26 22:02:18 -0700372 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700373
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700374 sWorker.post(new Runnable() {
375 public void run() {
376 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
377 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700379 sItemsIdMap.put(item.id, item);
380 switch (item.itemType) {
381 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
382 sFolders.put(item.id, (FolderInfo) item);
383 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700384 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700385 }
386 break;
387 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
388 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
389 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700390 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700391 }
392 break;
393 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
394 sAppWidgets.add((LauncherAppWidgetInfo) item);
395 break;
396 }
397 }
398 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700399 }
400
Joe Onorato9c1289c2009-08-17 11:03:03 -0400401 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700402 * Creates a new unique child id, for a given cell span across all layouts.
403 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700404 static int getCellLayoutChildId(
405 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
406 return ((cellId & 0xFF) << 24)
407 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700408 }
409
Adam Cohend22015c2010-07-26 22:02:18 -0700410 static int getCellCountX() {
411 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700412 }
413
Adam Cohend22015c2010-07-26 22:02:18 -0700414 static int getCellCountY() {
415 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700416 }
417
418 /**
419 * Updates the model orientation helper to take into account the current layout dimensions
420 * when performing local/canonical coordinate transformations.
421 */
422 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700423 mCellCountX = shortAxisCellCount;
424 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700425 }
426
427 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400428 * Update an item to the database in a specified container.
429 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700430 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400431 final ContentValues values = new ContentValues();
432 final ContentResolver cr = context.getContentResolver();
433
434 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700435 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700436
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700437 sWorker.post(new Runnable() {
438 public void run() {
439 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
440 values, null, null);
441 final ItemInfo modelItem = sItemsIdMap.get(item.id);
442 if (item != modelItem) {
443 // the modelItem needs to match up perfectly with item if our model is to be
444 // consistent with the database-- for now, just require modelItem == item
445 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
446 "doesn't match original");
447 }
448 }
449 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450 }
451
452 /**
453 * Removes the specified item from the database
454 * @param context
455 * @param item
456 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700457 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400458 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700459 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700460 sWorker.post(new Runnable() {
461 public void run() {
462 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700463 switch (item.itemType) {
464 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
465 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700466 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700467 break;
468 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
469 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700470 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700471 break;
472 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
473 sAppWidgets.remove((LauncherAppWidgetInfo) item);
474 break;
475 }
476 sItemsIdMap.remove(item.id);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700477 }
478 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400479 }
480
481 /**
482 * Remove the contents of the specified folder from the database
483 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700484 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400485 final ContentResolver cr = context.getContentResolver();
486
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700487 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700488 public void run() {
489 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700490 sItemsIdMap.remove(info.id);
491 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700492 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700493
Adam Cohenafb01ee2011-06-23 15:38:03 -0700494 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
495 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700496 for (ItemInfo childInfo : info.contents) {
497 sItemsIdMap.remove(childInfo.id);
498 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700499 }
500 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400501 }
502
503 /**
504 * Set this as the current Launcher activity object for the loader.
505 */
506 public void initialize(Callbacks callbacks) {
507 synchronized (mLock) {
508 mCallbacks = new WeakReference<Callbacks>(callbacks);
509 }
510 }
511
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700512 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400513 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
514 * ACTION_PACKAGE_CHANGED.
515 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400516 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400517 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700518
Joe Onorato36115782010-06-17 13:28:48 -0400519 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400520
Joe Onorato36115782010-06-17 13:28:48 -0400521 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
522 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
523 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
524 final String packageName = intent.getData().getSchemeSpecificPart();
525 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526
Joe Onorato36115782010-06-17 13:28:48 -0400527 int op = PackageUpdatedTask.OP_NONE;
528
529 if (packageName == null || packageName.length() == 0) {
530 // they sent us a bad intent
531 return;
532 }
533
534 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
535 op = PackageUpdatedTask.OP_UPDATE;
536 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
537 if (!replacing) {
538 op = PackageUpdatedTask.OP_REMOVE;
539 }
540 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
541 // later, we will update the package at this time
542 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
543 if (!replacing) {
544 op = PackageUpdatedTask.OP_ADD;
545 } else {
546 op = PackageUpdatedTask.OP_UPDATE;
547 }
548 }
549
550 if (op != PackageUpdatedTask.OP_NONE) {
551 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
552 }
553
554 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400555 // First, schedule to add these apps back in.
556 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
557 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
558 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700559 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400560 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
561 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
562 enqueuePackageUpdated(new PackageUpdatedTask(
563 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700564 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
565 // If we have changed locale we need to clear out the labels in all apps.
566 // Do this here because if the launcher activity is running it will be restarted.
567 // If it's not running startLoaderFromBackground will merely tell it that it needs
568 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
569 // next time.
570 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700571 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700572 startLoaderFromBackground();
573 }
574 }
575
576 /**
577 * When the launcher is in the background, it's possible for it to miss paired
578 * configuration changes. So whenever we trigger the loader from the background
579 * tell the launcher that it needs to re-run the loader when it comes back instead
580 * of doing it now.
581 */
582 public void startLoaderFromBackground() {
583 boolean runLoader = false;
584 if (mCallbacks != null) {
585 Callbacks callbacks = mCallbacks.get();
586 if (callbacks != null) {
587 // Only actually run the loader if they're not paused.
588 if (!callbacks.setLoadOnResume()) {
589 runLoader = true;
590 }
591 }
592 }
593 if (runLoader) {
594 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700595 }
Joe Onorato36115782010-06-17 13:28:48 -0400596 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400597
Joe Onorato36115782010-06-17 13:28:48 -0400598 public void startLoader(Context context, boolean isLaunching) {
599 synchronized (mLock) {
600 if (DEBUG_LOADERS) {
601 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400603
Joe Onorato36115782010-06-17 13:28:48 -0400604 // Don't bother to start the thread if we know it's not going to do anything
605 if (mCallbacks != null && mCallbacks.get() != null) {
606 // If there is already one running, tell it to stop.
607 LoaderTask oldTask = mLoaderTask;
608 if (oldTask != null) {
609 if (oldTask.isLaunching()) {
610 // don't downgrade isLaunching if we're already running
611 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500612 }
Joe Onorato36115782010-06-17 13:28:48 -0400613 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500614 }
Joe Onorato36115782010-06-17 13:28:48 -0400615 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700616 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400618 }
619 }
620
Joe Onorato36115782010-06-17 13:28:48 -0400621 public void stopLoader() {
622 synchronized (mLock) {
623 if (mLoaderTask != null) {
624 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 }
626 }
Joe Onorato36115782010-06-17 13:28:48 -0400627 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628
Joe Onorato36115782010-06-17 13:28:48 -0400629 /**
630 * Runnable for the thread that loads the contents of the launcher:
631 * - workspace icons
632 * - widgets
633 * - all apps icons
634 */
635 private class LoaderTask implements Runnable {
636 private Context mContext;
637 private Thread mWaitThread;
638 private boolean mIsLaunching;
639 private boolean mStopped;
640 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700641 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400642
643 LoaderTask(Context context, boolean isLaunching) {
644 mContext = context;
645 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700646 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647 }
648
Joe Onorato36115782010-06-17 13:28:48 -0400649 boolean isLaunching() {
650 return mIsLaunching;
651 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652
Joe Onorato36115782010-06-17 13:28:48 -0400653 private void loadAndBindWorkspace() {
654 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400655 if (DEBUG_LOADERS) {
656 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700658
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700659 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400660 loadWorkspace();
661 if (mStopped) {
662 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663 }
Joe Onorato36115782010-06-17 13:28:48 -0400664 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665 }
666
Joe Onorato36115782010-06-17 13:28:48 -0400667 // Bind the workspace
668 bindWorkspace();
669 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400670
Joe Onorato36115782010-06-17 13:28:48 -0400671 private void waitForIdle() {
672 // Wait until the either we're stopped or the other threads are done.
673 // This way we don't start loading all apps until the workspace has settled
674 // down.
675 synchronized (LoaderTask.this) {
676 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700677
Joe Onorato36115782010-06-17 13:28:48 -0400678 mHandler.postIdle(new Runnable() {
679 public void run() {
680 synchronized (LoaderTask.this) {
681 mLoadAndBindStepFinished = true;
682 if (DEBUG_LOADERS) {
683 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400684 }
Joe Onorato36115782010-06-17 13:28:48 -0400685 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400686 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400687 }
Joe Onorato36115782010-06-17 13:28:48 -0400688 });
689
690 while (!mStopped && !mLoadAndBindStepFinished) {
691 try {
692 this.wait();
693 } catch (InterruptedException ex) {
694 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400695 }
696 }
Joe Onorato36115782010-06-17 13:28:48 -0400697 if (DEBUG_LOADERS) {
698 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700699 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400700 + "ms for previous step to finish binding");
701 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400702 }
Joe Onorato36115782010-06-17 13:28:48 -0400703 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400704
Joe Onorato36115782010-06-17 13:28:48 -0400705 public void run() {
706 // Optimize for end-user experience: if the Launcher is up and // running with the
707 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
708 // workspace first (default).
709 final Callbacks cbk = mCallbacks.get();
710 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400711
Joe Onorato36115782010-06-17 13:28:48 -0400712 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400713 // Elevate priority when Home launches for the first time to avoid
714 // starving at boot time. Staring at a blank home is not cool.
715 synchronized (mLock) {
716 android.os.Process.setThreadPriority(mIsLaunching
717 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
718 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400719 if (loadWorkspaceFirst) {
720 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
721 loadAndBindWorkspace();
722 } else {
723 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700724 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400725 }
726
Joe Onorato36115782010-06-17 13:28:48 -0400727 if (mStopped) {
728 break keep_running;
729 }
730
731 // Whew! Hard work done. Slow us down, and wait until the UI thread has
732 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400733 synchronized (mLock) {
734 if (mIsLaunching) {
735 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
736 }
737 }
Joe Onorato36115782010-06-17 13:28:48 -0400738 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400739
740 // second step
741 if (loadWorkspaceFirst) {
742 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700743 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400744 } else {
745 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
746 loadAndBindWorkspace();
747 }
Joe Onorato36115782010-06-17 13:28:48 -0400748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749
Joe Onorato36115782010-06-17 13:28:48 -0400750 // Clear out this reference, otherwise we end up holding it until all of the
751 // callback runnables are done.
752 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753
Joe Onorato36115782010-06-17 13:28:48 -0400754 synchronized (mLock) {
755 // If we are still the last one to be scheduled, remove ourselves.
756 if (mLoaderTask == this) {
757 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 }
Joe Onorato36115782010-06-17 13:28:48 -0400759 }
Joe Onorato36115782010-06-17 13:28:48 -0400760 }
761
762 public void stopLocked() {
763 synchronized (LoaderTask.this) {
764 mStopped = true;
765 this.notify();
766 }
767 }
768
769 /**
770 * Gets the callbacks object. If we've been stopped, or if the launcher object
771 * has somehow been garbage collected, return null instead. Pass in the Callbacks
772 * object that was around when the deferred message was scheduled, and if there's
773 * a new Callbacks object around then also return null. This will save us from
774 * calling onto it with data that will be ignored.
775 */
776 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
777 synchronized (mLock) {
778 if (mStopped) {
779 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400780 }
Joe Onorato36115782010-06-17 13:28:48 -0400781
782 if (mCallbacks == null) {
783 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400784 }
Joe Onorato36115782010-06-17 13:28:48 -0400785
786 final Callbacks callbacks = mCallbacks.get();
787 if (callbacks != oldCallbacks) {
788 return null;
789 }
790 if (callbacks == null) {
791 Log.w(TAG, "no mCallbacks");
792 return null;
793 }
794
795 return callbacks;
796 }
797 }
798
799 // check & update map of what's occupied; used to discard overlapping/invalid items
800 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
801 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400802 return true;
803 }
Joe Onorato36115782010-06-17 13:28:48 -0400804 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
805 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
806 if (occupied[item.screen][x][y] != null) {
807 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700808 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400809 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700810 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400811 + occupied[item.screen][x][y]);
812 return false;
813 }
814 }
815 }
816 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
817 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
818 occupied[item.screen][x][y] = item;
819 }
820 }
821 return true;
822 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823
Joe Onorato36115782010-06-17 13:28:48 -0400824 private void loadWorkspace() {
825 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826
Joe Onorato36115782010-06-17 13:28:48 -0400827 final Context context = mContext;
828 final ContentResolver contentResolver = context.getContentResolver();
829 final PackageManager manager = context.getPackageManager();
830 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
831 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400832
Adam Cohen4eac29a2011-07-11 17:53:37 -0700833 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700834 sAppWidgets.clear();
835 sFolders.clear();
836 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800837
Joe Onorato36115782010-06-17 13:28:48 -0400838 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839
Joe Onorato36115782010-06-17 13:28:48 -0400840 final Cursor c = contentResolver.query(
841 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400842
Adam Cohend22015c2010-07-26 22:02:18 -0700843 final ItemInfo occupied[][][] =
844 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845
Joe Onorato36115782010-06-17 13:28:48 -0400846 try {
847 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
848 final int intentIndex = c.getColumnIndexOrThrow
849 (LauncherSettings.Favorites.INTENT);
850 final int titleIndex = c.getColumnIndexOrThrow
851 (LauncherSettings.Favorites.TITLE);
852 final int iconTypeIndex = c.getColumnIndexOrThrow(
853 LauncherSettings.Favorites.ICON_TYPE);
854 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
855 final int iconPackageIndex = c.getColumnIndexOrThrow(
856 LauncherSettings.Favorites.ICON_PACKAGE);
857 final int iconResourceIndex = c.getColumnIndexOrThrow(
858 LauncherSettings.Favorites.ICON_RESOURCE);
859 final int containerIndex = c.getColumnIndexOrThrow(
860 LauncherSettings.Favorites.CONTAINER);
861 final int itemTypeIndex = c.getColumnIndexOrThrow(
862 LauncherSettings.Favorites.ITEM_TYPE);
863 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
864 LauncherSettings.Favorites.APPWIDGET_ID);
865 final int screenIndex = c.getColumnIndexOrThrow(
866 LauncherSettings.Favorites.SCREEN);
867 final int cellXIndex = c.getColumnIndexOrThrow
868 (LauncherSettings.Favorites.CELLX);
869 final int cellYIndex = c.getColumnIndexOrThrow
870 (LauncherSettings.Favorites.CELLY);
871 final int spanXIndex = c.getColumnIndexOrThrow
872 (LauncherSettings.Favorites.SPANX);
873 final int spanYIndex = c.getColumnIndexOrThrow(
874 LauncherSettings.Favorites.SPANY);
875 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
876 final int displayModeIndex = c.getColumnIndexOrThrow(
877 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878
Joe Onorato36115782010-06-17 13:28:48 -0400879 ShortcutInfo info;
880 String intentDescription;
881 LauncherAppWidgetInfo appWidgetInfo;
882 int container;
883 long id;
884 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
Joe Onorato36115782010-06-17 13:28:48 -0400886 while (!mStopped && c.moveToNext()) {
887 try {
888 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889
Joe Onorato36115782010-06-17 13:28:48 -0400890 switch (itemType) {
891 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
892 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
893 intentDescription = c.getString(intentIndex);
894 try {
895 intent = Intent.parseUri(intentDescription, 0);
896 } catch (URISyntaxException e) {
897 continue;
898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899
Joe Onorato36115782010-06-17 13:28:48 -0400900 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
901 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700902 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400903 } else {
904 info = getShortcutInfo(c, context, iconTypeIndex,
905 iconPackageIndex, iconResourceIndex, iconIndex,
906 titleIndex);
907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908
Joe Onorato36115782010-06-17 13:28:48 -0400909 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400910 info.intent = intent;
911 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400913 info.container = container;
914 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700915 info.cellX = c.getInt(cellXIndex);
916 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917
Daniel Sandler8802e962010-05-26 16:28:16 -0400918 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400919 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400920 break;
921 }
922
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400924 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700925 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400926 break;
927 default:
928 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700929 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700930 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400931 folderInfo.add(info);
932 break;
933 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700934 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800935
936 // now that we've loaded everthing re-save it with the
937 // icon in case it disappears somehow.
938 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400939 } else {
940 // Failed to load the shortcut, probably because the
941 // activity manager couldn't resolve it (maybe the app
942 // was uninstalled), or the db row was somehow screwed up.
943 // Delete it.
944 id = c.getLong(idIndex);
945 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
946 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
947 id, false), null, null);
948 }
949 break;
950
Adam Cohendf2cc412011-04-27 16:56:57 -0700951 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -0400952 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700953 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400954
Winson Chungaafa03c2010-06-11 17:34:16 -0700955 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400956 folderInfo.id = id;
957 container = c.getInt(containerIndex);
958 folderInfo.container = container;
959 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700960 folderInfo.cellX = c.getInt(cellXIndex);
961 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400962
963 // check & update map of what's occupied
964 if (!checkItemPlacement(occupied, folderInfo)) {
965 break;
966 }
Joe Onorato36115782010-06-17 13:28:48 -0400967 switch (container) {
968 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700969 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400970 break;
971 }
972
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700973 sItemsIdMap.put(folderInfo.id, folderInfo);
974 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400975 break;
976
Joe Onorato36115782010-06-17 13:28:48 -0400977 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
978 // Read all Launcher-specific widget details
979 int appWidgetId = c.getInt(appWidgetIdIndex);
980 id = c.getLong(idIndex);
981
982 final AppWidgetProviderInfo provider =
983 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700984
Joe Onorato36115782010-06-17 13:28:48 -0400985 if (!isSafeMode && (provider == null || provider.provider == null ||
986 provider.provider.getPackageName() == null)) {
987 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
988 + id + " appWidgetId=" + appWidgetId);
989 itemsToRemove.add(id);
990 } else {
991 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
992 appWidgetInfo.id = id;
993 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700994 appWidgetInfo.cellX = c.getInt(cellXIndex);
995 appWidgetInfo.cellY = c.getInt(cellYIndex);
996 appWidgetInfo.spanX = c.getInt(spanXIndex);
997 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400998
999 container = c.getInt(containerIndex);
1000 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1001 Log.e(TAG, "Widget found where container "
1002 + "!= CONTAINER_DESKTOP -- ignoring!");
1003 continue;
1004 }
1005 appWidgetInfo.container = c.getInt(containerIndex);
1006
1007 // check & update map of what's occupied
1008 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1009 break;
1010 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001011 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1012 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001013 }
1014 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001015 }
Joe Onorato36115782010-06-17 13:28:48 -04001016 } catch (Exception e) {
1017 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001018 }
1019 }
Joe Onorato36115782010-06-17 13:28:48 -04001020 } finally {
1021 c.close();
1022 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001023
Joe Onorato36115782010-06-17 13:28:48 -04001024 if (itemsToRemove.size() > 0) {
1025 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1026 LauncherSettings.Favorites.CONTENT_URI);
1027 // Remove dead items
1028 for (long id : itemsToRemove) {
1029 if (DEBUG_LOADERS) {
1030 Log.d(TAG, "Removed id = " + id);
1031 }
1032 // Don't notify content observers
1033 try {
1034 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1035 null, null);
1036 } catch (RemoteException e) {
1037 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001038 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 }
1041
Joe Onorato36115782010-06-17 13:28:48 -04001042 if (DEBUG_LOADERS) {
1043 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1044 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001045 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001046 String line = "";
1047 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1048 if (s > 0) {
1049 line += " | ";
1050 }
Adam Cohend22015c2010-07-26 22:02:18 -07001051 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001052 line += ((occupied[s][x][y] != null) ? "#" : ".");
1053 }
1054 }
1055 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056 }
Joe Onorato36115782010-06-17 13:28:48 -04001057 }
1058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059
Joe Onorato36115782010-06-17 13:28:48 -04001060 /**
1061 * Read everything out of our database.
1062 */
1063 private void bindWorkspace() {
1064 final long t = SystemClock.uptimeMillis();
1065
1066 // Don't use these two variables in any of the callback runnables.
1067 // Otherwise we hold a reference to them.
1068 final Callbacks oldCallbacks = mCallbacks.get();
1069 if (oldCallbacks == null) {
1070 // This launcher has exited and nobody bothered to tell us. Just bail.
1071 Log.w(TAG, "LoaderTask running with no launcher");
1072 return;
1073 }
1074
1075 int N;
1076 // Tell the workspace that we're about to start firing items at it
1077 mHandler.post(new Runnable() {
1078 public void run() {
1079 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1080 if (callbacks != null) {
1081 callbacks.startBinding();
1082 }
1083 }
1084 });
1085 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001086 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001087 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1088 final int start = i;
1089 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 mHandler.post(new Runnable() {
1091 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001092 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001094 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095 }
1096 }
1097 });
Joe Onorato36115782010-06-17 13:28:48 -04001098 }
1099 mHandler.post(new Runnable() {
1100 public void run() {
1101 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1102 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001103 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001104 }
1105 }
1106 });
1107 // Wait until the queue goes empty.
1108 mHandler.post(new Runnable() {
1109 public void run() {
1110 if (DEBUG_LOADERS) {
1111 Log.d(TAG, "Going to start binding widgets soon.");
1112 }
1113 }
1114 });
1115 // Bind the widgets, one at a time.
1116 // WARNING: this is calling into the workspace from the background thread,
1117 // but since getCurrentScreen() just returns the int, we should be okay. This
1118 // is just a hint for the order, and if it's wrong, we'll be okay.
1119 // TODO: instead, we should have that push the current screen into here.
1120 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001122 // once for the current screen
1123 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001124 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001125 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 mHandler.post(new Runnable() {
1127 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001128 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001130 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 }
1132 }
1133 });
1134 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 }
Joe Onorato36115782010-06-17 13:28:48 -04001136 // once for the other screens
1137 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001138 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001139 if (widget.screen != currentScreen) {
1140 mHandler.post(new Runnable() {
1141 public void run() {
1142 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1143 if (callbacks != null) {
1144 callbacks.bindAppWidget(widget);
1145 }
1146 }
1147 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001148 }
1149 }
Joe Onorato36115782010-06-17 13:28:48 -04001150 // Tell the workspace that we're done.
1151 mHandler.post(new Runnable() {
1152 public void run() {
1153 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1154 if (callbacks != null) {
1155 callbacks.finishBindingItems();
1156 }
1157 }
1158 });
1159 // If we're profiling, this is the last thing in the queue.
1160 mHandler.post(new Runnable() {
1161 public void run() {
1162 if (DEBUG_LOADERS) {
1163 Log.d(TAG, "bound workspace in "
1164 + (SystemClock.uptimeMillis()-t) + "ms");
1165 }
1166 }
1167 });
1168 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001169
Joe Onorato36115782010-06-17 13:28:48 -04001170 private void loadAndBindAllApps() {
1171 if (DEBUG_LOADERS) {
1172 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1173 }
1174 if (!mAllAppsLoaded) {
1175 loadAllAppsByBatch();
1176 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001177 return;
1178 }
Joe Onorato36115782010-06-17 13:28:48 -04001179 mAllAppsLoaded = true;
1180 } else {
1181 onlyBindAllApps();
1182 }
1183 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001184
Joe Onorato36115782010-06-17 13:28:48 -04001185 private void onlyBindAllApps() {
1186 final Callbacks oldCallbacks = mCallbacks.get();
1187 if (oldCallbacks == null) {
1188 // This launcher has exited and nobody bothered to tell us. Just bail.
1189 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1190 return;
1191 }
1192
1193 // shallow copy
1194 final ArrayList<ApplicationInfo> list
1195 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1196 mHandler.post(new Runnable() {
1197 public void run() {
1198 final long t = SystemClock.uptimeMillis();
1199 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1200 if (callbacks != null) {
1201 callbacks.bindAllApplications(list);
1202 }
1203 if (DEBUG_LOADERS) {
1204 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1205 + (SystemClock.uptimeMillis()-t) + "ms");
1206 }
1207 }
1208 });
1209
1210 }
1211
1212 private void loadAllAppsByBatch() {
1213 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1214
1215 // Don't use these two variables in any of the callback runnables.
1216 // Otherwise we hold a reference to them.
1217 final Callbacks oldCallbacks = mCallbacks.get();
1218 if (oldCallbacks == null) {
1219 // This launcher has exited and nobody bothered to tell us. Just bail.
1220 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1221 return;
1222 }
1223
1224 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1225 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1226
1227 final PackageManager packageManager = mContext.getPackageManager();
1228 List<ResolveInfo> apps = null;
1229
1230 int N = Integer.MAX_VALUE;
1231
1232 int startIndex;
1233 int i=0;
1234 int batchSize = -1;
1235 while (i < N && !mStopped) {
1236 if (i == 0) {
1237 mAllAppsList.clear();
1238 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1239 apps = packageManager.queryIntentActivities(mainIntent, 0);
1240 if (DEBUG_LOADERS) {
1241 Log.d(TAG, "queryIntentActivities took "
1242 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1243 }
1244 if (apps == null) {
1245 return;
1246 }
1247 N = apps.size();
1248 if (DEBUG_LOADERS) {
1249 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1250 }
1251 if (N == 0) {
1252 // There are no apps?!?
1253 return;
1254 }
1255 if (mBatchSize == 0) {
1256 batchSize = N;
1257 } else {
1258 batchSize = mBatchSize;
1259 }
1260
1261 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1262 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001263 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001264 if (DEBUG_LOADERS) {
1265 Log.d(TAG, "sort took "
1266 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1267 }
1268 }
1269
1270 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1271
1272 startIndex = i;
1273 for (int j=0; i<N && j<batchSize; j++) {
1274 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001275 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1276 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001277 i++;
1278 }
1279
1280 final boolean first = i <= batchSize;
1281 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1282 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1283 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1284
Joe Onoratocc67f472010-06-08 10:54:30 -07001285 mHandler.post(new Runnable() {
1286 public void run() {
1287 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001288 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001289 if (first) {
1290 callbacks.bindAllApplications(added);
1291 } else {
1292 callbacks.bindAppsAdded(added);
1293 }
1294 if (DEBUG_LOADERS) {
1295 Log.d(TAG, "bound " + added.size() + " apps in "
1296 + (SystemClock.uptimeMillis() - t) + "ms");
1297 }
1298 } else {
1299 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001300 }
1301 }
1302 });
1303
Daniel Sandlerdca66122010-04-13 16:23:58 -04001304 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001305 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1306 + (SystemClock.uptimeMillis()-t2) + "ms");
1307 }
1308
1309 if (mAllAppsLoadDelay > 0 && i < N) {
1310 try {
1311 if (DEBUG_LOADERS) {
1312 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1313 }
1314 Thread.sleep(mAllAppsLoadDelay);
1315 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 }
1317 }
1318
Joe Onorato36115782010-06-17 13:28:48 -04001319 if (DEBUG_LOADERS) {
1320 Log.d(TAG, "cached all " + N + " apps in "
1321 + (SystemClock.uptimeMillis()-t) + "ms"
1322 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001323 }
1324 }
1325
1326 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001327 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1328 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1329 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1330 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1331 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001332 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001333 }
1334 }
1335
1336 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001337 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001338 }
1339
1340 private class PackageUpdatedTask implements Runnable {
1341 int mOp;
1342 String[] mPackages;
1343
1344 public static final int OP_NONE = 0;
1345 public static final int OP_ADD = 1;
1346 public static final int OP_UPDATE = 2;
1347 public static final int OP_REMOVE = 3; // uninstlled
1348 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1349
1350
1351 public PackageUpdatedTask(int op, String[] packages) {
1352 mOp = op;
1353 mPackages = packages;
1354 }
1355
1356 public void run() {
1357 final Context context = mApp;
1358
1359 final String[] packages = mPackages;
1360 final int N = packages.length;
1361 switch (mOp) {
1362 case OP_ADD:
1363 for (int i=0; i<N; i++) {
1364 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1365 mAllAppsList.addPackage(context, packages[i]);
1366 }
1367 break;
1368 case OP_UPDATE:
1369 for (int i=0; i<N; i++) {
1370 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1371 mAllAppsList.updatePackage(context, packages[i]);
1372 }
1373 break;
1374 case OP_REMOVE:
1375 case OP_UNAVAILABLE:
1376 for (int i=0; i<N; i++) {
1377 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1378 mAllAppsList.removePackage(packages[i]);
1379 }
1380 break;
1381 }
1382
1383 ArrayList<ApplicationInfo> added = null;
1384 ArrayList<ApplicationInfo> removed = null;
1385 ArrayList<ApplicationInfo> modified = null;
1386
1387 if (mAllAppsList.added.size() > 0) {
1388 added = mAllAppsList.added;
1389 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1390 }
1391 if (mAllAppsList.removed.size() > 0) {
1392 removed = mAllAppsList.removed;
1393 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1394 for (ApplicationInfo info: removed) {
1395 mIconCache.remove(info.intent.getComponent());
1396 }
1397 }
1398 if (mAllAppsList.modified.size() > 0) {
1399 modified = mAllAppsList.modified;
1400 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1401 }
1402
1403 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1404 if (callbacks == null) {
1405 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1406 return;
1407 }
1408
1409 if (added != null) {
1410 final ArrayList<ApplicationInfo> addedFinal = added;
1411 mHandler.post(new Runnable() {
1412 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001413 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1414 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001415 callbacks.bindAppsAdded(addedFinal);
1416 }
1417 }
1418 });
1419 }
1420 if (modified != null) {
1421 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1422 mHandler.post(new Runnable() {
1423 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001424 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1425 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001426 callbacks.bindAppsUpdated(modifiedFinal);
1427 }
1428 }
1429 });
1430 }
1431 if (removed != null) {
1432 final boolean permanent = mOp != OP_UNAVAILABLE;
1433 final ArrayList<ApplicationInfo> removedFinal = removed;
1434 mHandler.post(new Runnable() {
1435 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001436 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1437 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001438 callbacks.bindAppsRemoved(removedFinal, permanent);
1439 }
1440 }
1441 });
Joe Onoratobe386092009-11-17 17:32:16 -08001442 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001443
1444 mHandler.post(new Runnable() {
1445 @Override
1446 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001447 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1448 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001449 callbacks.bindPackagesUpdated();
1450 }
1451 }
1452 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 }
1454 }
1455
1456 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001457 * This is called from the code that adds shortcuts from the intent receiver. This
1458 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459 */
Joe Onorato56d82912010-03-07 14:32:10 -05001460 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001461 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463
Joe Onorato56d82912010-03-07 14:32:10 -05001464 /**
1465 * Make an ShortcutInfo object for a shortcut that is an application.
1466 *
1467 * If c is not null, then it will be used to fill in missing data like the title and icon.
1468 */
1469 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001470 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001471 Bitmap icon = null;
1472 final ShortcutInfo info = new ShortcutInfo();
1473
1474 ComponentName componentName = intent.getComponent();
1475 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001476 return null;
1477 }
1478
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001479 // TODO: See if the PackageManager knows about this case. If it doesn't
1480 // then return null & delete this.
1481
Joe Onorato56d82912010-03-07 14:32:10 -05001482 // the resource -- This may implicitly give us back the fallback icon,
1483 // but don't worry about that. All we're doing with usingFallbackIcon is
1484 // to avoid saving lots of copies of that in the database, and most apps
1485 // have icons anyway.
1486 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1487 if (resolveInfo != null) {
1488 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001489 }
Joe Onorato56d82912010-03-07 14:32:10 -05001490 // the db
1491 if (icon == null) {
1492 if (c != null) {
1493 icon = getIconFromCursor(c, iconIndex);
1494 }
1495 }
1496 // the fallback icon
1497 if (icon == null) {
1498 icon = getFallbackIcon();
1499 info.usingFallbackIcon = true;
1500 }
1501 info.setIcon(icon);
1502
1503 // from the resource
1504 if (resolveInfo != null) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001505 if (labelCache != null && labelCache.containsKey(resolveInfo)) {
1506 info.title = labelCache.get(resolveInfo);
1507 } else {
1508 info.title = resolveInfo.activityInfo.loadLabel(manager);
1509 if (labelCache != null) {
1510 labelCache.put(resolveInfo, info.title);
1511 }
1512 }
Joe Onorato56d82912010-03-07 14:32:10 -05001513 }
1514 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001515 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001516 if (c != null) {
1517 info.title = c.getString(titleIndex);
1518 }
1519 }
1520 // fall back to the class name of the activity
1521 if (info.title == null) {
1522 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001523 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001524 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1525 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001529 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001531 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001532 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1533 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534
Joe Onorato56d82912010-03-07 14:32:10 -05001535 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001536 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001539 // TODO: If there's an explicit component and we can't install that, delete it.
1540
Joe Onorato56d82912010-03-07 14:32:10 -05001541 info.title = c.getString(titleIndex);
1542
Joe Onorato9c1289c2009-08-17 11:03:03 -04001543 int iconType = c.getInt(iconTypeIndex);
1544 switch (iconType) {
1545 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1546 String packageName = c.getString(iconPackageIndex);
1547 String resourceName = c.getString(iconResourceIndex);
1548 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001549 info.customIcon = false;
1550 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001553 if (resources != null) {
1554 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001555 icon = Utilities.createIconBitmap(
1556 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001557 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001558 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001559 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
Joe Onorato56d82912010-03-07 14:32:10 -05001561 // the db
1562 if (icon == null) {
1563 icon = getIconFromCursor(c, iconIndex);
1564 }
1565 // the fallback icon
1566 if (icon == null) {
1567 icon = getFallbackIcon();
1568 info.usingFallbackIcon = true;
1569 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 break;
1571 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001572 icon = getIconFromCursor(c, iconIndex);
1573 if (icon == null) {
1574 icon = getFallbackIcon();
1575 info.customIcon = false;
1576 info.usingFallbackIcon = true;
1577 } else {
1578 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 break;
1581 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001582 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001583 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001584 info.customIcon = false;
1585 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001587 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 return info;
1589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590
Joe Onorato56d82912010-03-07 14:32:10 -05001591 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1592 if (false) {
1593 Log.d(TAG, "getIconFromCursor app="
1594 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1595 }
1596 byte[] data = c.getBlob(iconIndex);
1597 try {
1598 return BitmapFactory.decodeByteArray(data, 0, data.length);
1599 } catch (Exception e) {
1600 return null;
1601 }
1602 }
1603
Joe Onorato0589f0f2010-02-08 13:44:00 -08001604 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001605 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001606
Winson Chunga9abd0e2010-10-27 17:18:37 -07001607 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001608 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001609 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001610
1611 return info;
1612 }
1613
Winson Chunga9abd0e2010-10-27 17:18:37 -07001614 /**
Winson Chung55cef262010-10-28 14:14:18 -07001615 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1616 */
1617 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1618 ComponentName component) {
1619 List<AppWidgetProviderInfo> widgets =
1620 AppWidgetManager.getInstance(context).getInstalledProviders();
1621 for (AppWidgetProviderInfo info : widgets) {
1622 if (info.provider.equals(component)) {
1623 return info;
1624 }
1625 }
1626 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001627 }
1628
Winson Chung68846fd2010-10-29 11:00:27 -07001629 /**
1630 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1631 */
1632 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1633 final PackageManager packageManager = context.getPackageManager();
1634 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1635 new ArrayList<WidgetMimeTypeHandlerData>();
1636
1637 final Intent supportsIntent =
1638 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1639 supportsIntent.setType(mimeType);
1640
1641 // Create a set of widget configuration components that we can test against
1642 final List<AppWidgetProviderInfo> widgets =
1643 AppWidgetManager.getInstance(context).getInstalledProviders();
1644 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1645 new HashMap<ComponentName, AppWidgetProviderInfo>();
1646 for (AppWidgetProviderInfo info : widgets) {
1647 configurationComponentToWidget.put(info.configure, info);
1648 }
1649
1650 // Run through each of the intents that can handle this type of clip data, and cross
1651 // reference them with the components that are actual configuration components
1652 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1653 PackageManager.MATCH_DEFAULT_ONLY);
1654 for (ResolveInfo info : activities) {
1655 final ActivityInfo activityInfo = info.activityInfo;
1656 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1657 activityInfo.name);
1658 if (configurationComponentToWidget.containsKey(infoComponent)) {
1659 supportedConfigurationActivities.add(
1660 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1661 configurationComponentToWidget.get(infoComponent)));
1662 }
1663 }
1664 return supportedConfigurationActivities;
1665 }
1666
Winson Chunga9abd0e2010-10-27 17:18:37 -07001667 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001668 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1669 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1670 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1671
1672 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001673 boolean customIcon = false;
1674 ShortcutIconResource iconResource = null;
1675
1676 if (bitmap != null && bitmap instanceof Bitmap) {
1677 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001678 customIcon = true;
1679 } else {
1680 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1681 if (extra != null && extra instanceof ShortcutIconResource) {
1682 try {
1683 iconResource = (ShortcutIconResource) extra;
1684 final PackageManager packageManager = context.getPackageManager();
1685 Resources resources = packageManager.getResourcesForApplication(
1686 iconResource.packageName);
1687 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001688 icon = Utilities.createIconBitmap(
1689 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001690 } catch (Exception e) {
1691 Log.w(TAG, "Could not load shortcut icon: " + extra);
1692 }
1693 }
1694 }
1695
Joe Onorato0589f0f2010-02-08 13:44:00 -08001696 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001697
1698 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001699 if (fallbackIcon != null) {
1700 icon = fallbackIcon;
1701 } else {
1702 icon = getFallbackIcon();
1703 info.usingFallbackIcon = true;
1704 }
Joe Onorato56d82912010-03-07 14:32:10 -05001705 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001706 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001707
Joe Onorato0589f0f2010-02-08 13:44:00 -08001708 info.title = name;
1709 info.intent = intent;
1710 info.customIcon = customIcon;
1711 info.iconResource = iconResource;
1712
1713 return info;
1714 }
1715
Joe Onorato56d82912010-03-07 14:32:10 -05001716 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001717 // If apps can't be on SD, don't even bother.
1718 if (!mAppsCanBeOnExternalStorage) {
1719 return;
1720 }
Joe Onorato56d82912010-03-07 14:32:10 -05001721 // If this icon doesn't have a custom icon, check to see
1722 // what's stored in the DB, and if it doesn't match what
1723 // we're going to show, store what we are going to show back
1724 // into the DB. We do this so when we're loading, if the
1725 // package manager can't find an icon (for example because
1726 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001727 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001728 boolean needSave;
1729 byte[] data = c.getBlob(iconIndex);
1730 try {
1731 if (data != null) {
1732 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1733 Bitmap loaded = info.getIcon(mIconCache);
1734 needSave = !saved.sameAs(loaded);
1735 } else {
1736 needSave = true;
1737 }
1738 } catch (Exception e) {
1739 needSave = true;
1740 }
1741 if (needSave) {
1742 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001743 // This is slower than is ideal, but this only happens once
1744 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001745 updateItemInDatabase(context, info);
1746 }
1747 }
1748 }
1749
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001751 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752 * or make a new one.
1753 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001754 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001755 // See if a placeholder was created for us already
1756 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001757 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001759 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001760 folders.put(id, folderInfo);
1761 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001762 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 }
1764
Joe Onorato9c1289c2009-08-17 11:03:03 -04001765 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001766 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001767 = new Comparator<ApplicationInfo>() {
1768 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001769 int result = sCollator.compare(a.title.toString(), b.title.toString());
1770 if (result == 0) {
1771 result = a.componentName.compareTo(b.componentName);
1772 }
1773 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 };
Winson Chung78403fe2011-01-21 15:38:02 -08001776 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1777 = new Comparator<ApplicationInfo>() {
1778 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1779 if (a.firstInstallTime < b.firstInstallTime) return 1;
1780 if (a.firstInstallTime > b.firstInstallTime) return -1;
1781 return 0;
1782 }
1783 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001784 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1785 = new Comparator<AppWidgetProviderInfo>() {
1786 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1787 return sCollator.compare(a.label.toString(), b.label.toString());
1788 }
1789 };
1790 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1791 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001792 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001793 ShortcutNameComparator(PackageManager pm) {
1794 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001795 mLabelCache = new HashMap<Object, CharSequence>();
1796 }
1797 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1798 mPackageManager = pm;
1799 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001800 }
1801 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001802 CharSequence labelA, labelB;
1803 if (mLabelCache.containsKey(a)) {
1804 labelA = mLabelCache.get(a);
1805 } else {
1806 labelA = a.loadLabel(mPackageManager).toString();
1807
1808 mLabelCache.put(a, labelA);
1809 }
1810 if (mLabelCache.containsKey(b)) {
1811 labelB = mLabelCache.get(b);
1812 } else {
1813 labelB = b.loadLabel(mPackageManager).toString();
1814
1815 mLabelCache.put(b, labelB);
1816 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001817 return sCollator.compare(labelA, labelB);
1818 }
1819 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001820 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1821 private PackageManager mPackageManager;
1822 private HashMap<Object, String> mLabelCache;
1823 WidgetAndShortcutNameComparator(PackageManager pm) {
1824 mPackageManager = pm;
1825 mLabelCache = new HashMap<Object, String>();
1826 }
1827 public final int compare(Object a, Object b) {
1828 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001829 if (mLabelCache.containsKey(a)) {
1830 labelA = mLabelCache.get(a);
1831 } else {
1832 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001833 ((AppWidgetProviderInfo) a).label :
1834 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001835 mLabelCache.put(a, labelA);
1836 }
1837 if (mLabelCache.containsKey(b)) {
1838 labelB = mLabelCache.get(b);
1839 } else {
1840 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001841 ((AppWidgetProviderInfo) b).label :
1842 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001843 mLabelCache.put(b, labelB);
1844 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001845 return sCollator.compare(labelA, labelB);
1846 }
1847 };
Joe Onoratobe386092009-11-17 17:32:16 -08001848
1849 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001850 Log.d(TAG, "mCallbacks=" + mCallbacks);
1851 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1852 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1853 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1854 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001855 if (mLoaderTask != null) {
1856 mLoaderTask.dumpState();
1857 } else {
1858 Log.d(TAG, "mLoaderTask=null");
1859 }
Joe Onoratobe386092009-11-17 17:32:16 -08001860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861}