blob: 7d6206222a68ea199af237e38ee3abe90055c834 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040022import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080024import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
26import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
37import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Winson Chung68846fd2010-10-29 11:00:27 -070047import com.android.launcher.R;
48import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080049
Michael Jurkac2f801e2011-07-12 14:19:46 -070050import java.lang.ref.WeakReference;
51import java.net.URISyntaxException;
52import java.text.Collator;
53import java.util.ArrayList;
54import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
57import java.util.List;
58import java.util.Locale;
59
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060/**
61 * Maintains in-memory state of the Launcher. It is expected that there should be only one
62 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070063 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040065public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080066 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040067 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070068
Joe Onorato36115782010-06-17 13:28:48 -040069 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080070 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040071 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040072 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040073
Joe Onoratof99f8c12009-10-31 17:27:36 -040074 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075 private final Object mLock = new Object();
76 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040077 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070079 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
80 static {
81 sWorkerThread.start();
82 }
83 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
84
Joe Onoratocc67f472010-06-08 10:54:30 -070085 // We start off with everything not loaded. After that, we assume that
86 // our monitoring of the package manager provides all updates and we never
87 // need to do a requery. These are only ever touched from the loader thread.
88 private boolean mWorkspaceLoaded;
89 private boolean mAllAppsLoaded;
90
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Michael Jurkaa8c760d2011-04-28 14:59:33 -070093 // < only access in worker thread >
94 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080095
Michael Jurkaa8c760d2011-04-28 14:59:33 -070096 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
97 // LauncherModel to their ids
98 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
99
100 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
101 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
102 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700103 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700104
105 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
106 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
107 new ArrayList<LauncherAppWidgetInfo>();
108
109 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
110 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700111
112 // sDbIconCache is the set of ItemInfos that need to have their icons updated in the database
113 static final HashMap<Object, byte[]> sDbIconCache = new HashMap<Object, byte[]>();
114
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700115 // </ only access in worker thread >
116
117 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800118 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohend22015c2010-07-26 22:02:18 -0700120 private static int mCellCountX;
121 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700122
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700124 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 public int getCurrentWorkspaceScreen();
126 public void startBinding();
127 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500128 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400129 public void finishBindingItems();
130 public void bindAppWidget(LauncherAppWidgetInfo info);
131 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500132 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
133 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400134 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700135 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400136 public boolean isAllAppsVisible();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100137 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Joe Onorato0589f0f2010-02-08 13:44:00 -0800140 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800141 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400142 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143 mAllAppsList = new AllAppsList(iconCache);
144 mIconCache = iconCache;
145
146 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700147 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400148
149 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400150
151 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800152 }
153
Joe Onorato56d82912010-03-07 14:32:10 -0500154 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800155 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Adam Cohen4eac29a2011-07-11 17:53:37 -0700158 public static void unbindWorkspaceItems() {
159 for (ItemInfo item: sWorkspaceItems) {
160 item.unbind();
161 }
162 }
163
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 /**
165 * Adds an item to the DB if it was not created previously, or move it to a new
166 * <container, screen, cellX, cellY>
167 */
168 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
169 int screen, int cellX, int cellY) {
170 if (item.container == ItemInfo.NO_ID) {
171 // From all apps
172 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
173 } else {
174 // From somewhere else
175 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 }
177 }
178
179 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700181 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700182 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
183 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 item.cellX = cellX;
186 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700187 // We store hotseat items in canonical form which is this orientation invariant position
188 // in the hotseat
189 if (context instanceof Launcher && screen < 0 &&
190 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
191 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
192 } else {
193 item.screen = screen;
194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700196 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 final ContentValues values = new ContentValues();
198 final ContentResolver cr = context.getContentResolver();
199
200 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700201 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
202 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
204
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700205 sWorker.post(new Runnable() {
206 public void run() {
207 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700208 ItemInfo modelItem = sItemsIdMap.get(item.id);
209 if (item != modelItem) {
210 // the modelItem needs to match up perfectly with item if our model is to be
211 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700212 String msg = "item: " + ((item != null) ? item.toString() : "null") +
213 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
214 "Error: ItemInfo passed to moveItemInDatabase doesn't match original";
215 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700216 }
217
218 // Items are added/removed from the corresponding FolderInfo elsewhere, such
219 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
220 // that are on the desktop, as appropriate
Winson Chung3d503fb2011-07-13 17:25:49 -0700221 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
222 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700223 if (!sWorkspaceItems.contains(modelItem)) {
224 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700225 }
226 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700227 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700228 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700229 }
230 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700231 }
232
233 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800234 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800235 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700236 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
237 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800238 item.spanX = spanX;
239 item.spanY = spanY;
240 item.cellX = cellX;
241 item.cellY = cellY;
242
243 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
244 final ContentValues values = new ContentValues();
245 final ContentResolver cr = context.getContentResolver();
246
247 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
248 values.put(LauncherSettings.Favorites.SPANX, spanX);
249 values.put(LauncherSettings.Favorites.SPANY, spanY);
250 values.put(LauncherSettings.Favorites.CELLX, cellX);
251 values.put(LauncherSettings.Favorites.CELLY, cellY);
252
253 sWorker.post(new Runnable() {
254 public void run() {
255 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700256 ItemInfo modelItem = sItemsIdMap.get(item.id);
257 if (item != modelItem) {
258 // the modelItem needs to match up perfectly with item if our model is to be
259 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700260 String msg = "item: " + ((item != null) ? item.toString() : "null") +
261 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
262 "Error: ItemInfo passed to resizeItemInDatabase doesn't match original";
263 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700264 }
Adam Cohend4844c32011-02-18 19:25:06 -0800265 }
266 });
267 }
268
269 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 * Returns true if the shortcuts already exists in the database.
271 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 static boolean shortcutExists(Context context, String title, Intent intent) {
274 final ContentResolver cr = context.getContentResolver();
275 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
276 new String[] { "title", "intent" }, "title=? and intent=?",
277 new String[] { title, intent.toUri(0) }, null);
278 boolean result = false;
279 try {
280 result = c.moveToFirst();
281 } finally {
282 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700285 }
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700288 * Returns an ItemInfo array containing all the items in the LauncherModel.
289 * The ItemInfo.id is not set through this function.
290 */
291 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
292 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
293 final ContentResolver cr = context.getContentResolver();
294 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
295 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
296 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
297 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
298
299 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
300 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
301 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
302 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
303 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
304 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
305 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
306
307 try {
308 while (c.moveToNext()) {
309 ItemInfo item = new ItemInfo();
310 item.cellX = c.getInt(cellXIndex);
311 item.cellY = c.getInt(cellYIndex);
312 item.spanX = c.getInt(spanXIndex);
313 item.spanY = c.getInt(spanYIndex);
314 item.container = c.getInt(containerIndex);
315 item.itemType = c.getInt(itemTypeIndex);
316 item.screen = c.getInt(screenIndex);
317
318 items.add(item);
319 }
320 } catch (Exception e) {
321 items.clear();
322 } finally {
323 c.close();
324 }
325
326 return items;
327 }
328
329 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400330 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
331 */
332 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
333 final ContentResolver cr = context.getContentResolver();
334 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
335 "_id=? and (itemType=? or itemType=?)",
336 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700337 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700338
Joe Onorato9c1289c2009-08-17 11:03:03 -0400339 try {
340 if (c.moveToFirst()) {
341 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
342 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
343 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
344 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
345 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
346 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347
Joe Onorato9c1289c2009-08-17 11:03:03 -0400348 FolderInfo folderInfo = null;
349 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700350 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
351 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700353 }
354
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 folderInfo.title = c.getString(titleIndex);
356 folderInfo.id = id;
357 folderInfo.container = c.getInt(containerIndex);
358 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700359 folderInfo.cellX = c.getInt(cellXIndex);
360 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361
362 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 } finally {
365 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700366 }
367
368 return null;
369 }
370
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371 /**
372 * Add an item to the database in a specified container. Sets the container, screen, cellX and
373 * cellY fields of the item. Also assigns an ID to the item.
374 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700375 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
376 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 item.cellX = cellX;
379 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700380 // We store hotseat items in canonical form which is this orientation invariant position
381 // in the hotseat
382 if (context instanceof Launcher && screen < 0 &&
383 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
384 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
385 } else {
386 item.screen = screen;
387 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400388
389 final ContentValues values = new ContentValues();
390 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400391 item.onAddToDatabase(values);
392
Michael Jurka7578ec62011-08-03 14:11:54 -0700393 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700394 item.id = app.getLauncherProvider().generateNewId();
395 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700396 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700397
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700398 sWorker.post(new Runnable() {
399 public void run() {
400 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
401 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400402
Winson Chungb1094bd2011-08-24 16:14:08 -0700403 if (sItemsIdMap.containsKey(item.id)) {
404 // we should not be adding new items in the db with the same id
405 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
406 "addItemToDatabase already exists." + item.toString());
407 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700408 sItemsIdMap.put(item.id, item);
409 switch (item.itemType) {
410 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
411 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700412 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700413 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
414 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700415 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
416 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700417 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700418 }
419 break;
420 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
421 sAppWidgets.add((LauncherAppWidgetInfo) item);
422 break;
423 }
424 }
425 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700426 }
427
Joe Onorato9c1289c2009-08-17 11:03:03 -0400428 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700429 * Creates a new unique child id, for a given cell span across all layouts.
430 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700431 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700432 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
433 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700434 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700435 }
436
Adam Cohend22015c2010-07-26 22:02:18 -0700437 static int getCellCountX() {
438 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700439 }
440
Adam Cohend22015c2010-07-26 22:02:18 -0700441 static int getCellCountY() {
442 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700443 }
444
445 /**
446 * Updates the model orientation helper to take into account the current layout dimensions
447 * when performing local/canonical coordinate transformations.
448 */
449 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700450 mCellCountX = shortAxisCellCount;
451 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700452 }
453
454 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455 * Update an item to the database in a specified container.
456 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700457 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400458 final ContentValues values = new ContentValues();
459 final ContentResolver cr = context.getContentResolver();
460
461 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700462 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700463
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700464 sWorker.post(new Runnable() {
465 public void run() {
466 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
467 values, null, null);
468 final ItemInfo modelItem = sItemsIdMap.get(item.id);
469 if (item != modelItem) {
470 // the modelItem needs to match up perfectly with item if our model is to be
471 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700472 String msg = "item: " + ((item != null) ? item.toString() : "null") +
473 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
474 "Error: ItemInfo passed to updateItemInDatabase doesn't match original";
475 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700476 }
477 }
478 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400479 }
480
481 /**
482 * Removes the specified item from the database
483 * @param context
484 * @param item
485 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700486 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400487 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700488 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700489 sWorker.post(new Runnable() {
490 public void run() {
491 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700492 switch (item.itemType) {
493 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
494 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700495 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700496 break;
497 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
498 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700499 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700500 break;
501 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
502 sAppWidgets.remove((LauncherAppWidgetInfo) item);
503 break;
504 }
505 sItemsIdMap.remove(item.id);
Winson Chungb1094bd2011-08-24 16:14:08 -0700506 sDbIconCache.remove(item);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700507 }
508 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400509 }
510
511 /**
512 * Remove the contents of the specified folder from the database
513 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700514 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 final ContentResolver cr = context.getContentResolver();
516
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700517 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700518 public void run() {
519 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700520 sItemsIdMap.remove(info.id);
521 sFolders.remove(info.id);
Winson Chungc22a54d2011-08-25 13:51:25 -0700522 sDbIconCache.remove(info);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700523 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700524
Adam Cohen9932a9b2011-08-02 22:14:07 -0700525 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Adam Cohenafb01ee2011-06-23 15:38:03 -0700526 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700527 for (ItemInfo childInfo : info.contents) {
528 sItemsIdMap.remove(childInfo.id);
Winson Chungc22a54d2011-08-25 13:51:25 -0700529 sDbIconCache.remove(childInfo);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700530 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700531 }
532 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 }
534
535 /**
536 * Set this as the current Launcher activity object for the loader.
537 */
538 public void initialize(Callbacks callbacks) {
539 synchronized (mLock) {
540 mCallbacks = new WeakReference<Callbacks>(callbacks);
541 }
542 }
543
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700544 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400545 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
546 * ACTION_PACKAGE_CHANGED.
547 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100548 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400549 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400550 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700551
Joe Onorato36115782010-06-17 13:28:48 -0400552 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400553
Joe Onorato36115782010-06-17 13:28:48 -0400554 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
555 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
556 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
557 final String packageName = intent.getData().getSchemeSpecificPart();
558 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400559
Joe Onorato36115782010-06-17 13:28:48 -0400560 int op = PackageUpdatedTask.OP_NONE;
561
562 if (packageName == null || packageName.length() == 0) {
563 // they sent us a bad intent
564 return;
565 }
566
567 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
568 op = PackageUpdatedTask.OP_UPDATE;
569 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
570 if (!replacing) {
571 op = PackageUpdatedTask.OP_REMOVE;
572 }
573 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
574 // later, we will update the package at this time
575 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
576 if (!replacing) {
577 op = PackageUpdatedTask.OP_ADD;
578 } else {
579 op = PackageUpdatedTask.OP_UPDATE;
580 }
581 }
582
583 if (op != PackageUpdatedTask.OP_NONE) {
584 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
585 }
586
587 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400588 // First, schedule to add these apps back in.
589 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
590 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
591 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700592 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400593 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
594 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
595 enqueuePackageUpdated(new PackageUpdatedTask(
596 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700597 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
598 // If we have changed locale we need to clear out the labels in all apps.
599 // Do this here because if the launcher activity is running it will be restarted.
600 // If it's not running startLoaderFromBackground will merely tell it that it needs
601 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
602 // next time.
603 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700604 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700605 startLoaderFromBackground();
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700606 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
607 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100608 Callbacks callbacks = mCallbacks.get();
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700609 if (callbacks != null) {
610 callbacks.bindSearchablesChanged();
611 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700612 }
613 }
614
615 /**
616 * When the launcher is in the background, it's possible for it to miss paired
617 * configuration changes. So whenever we trigger the loader from the background
618 * tell the launcher that it needs to re-run the loader when it comes back instead
619 * of doing it now.
620 */
621 public void startLoaderFromBackground() {
622 boolean runLoader = false;
623 if (mCallbacks != null) {
624 Callbacks callbacks = mCallbacks.get();
625 if (callbacks != null) {
626 // Only actually run the loader if they're not paused.
627 if (!callbacks.setLoadOnResume()) {
628 runLoader = true;
629 }
630 }
631 }
632 if (runLoader) {
633 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700634 }
Joe Onorato36115782010-06-17 13:28:48 -0400635 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400636
Joe Onorato36115782010-06-17 13:28:48 -0400637 public void startLoader(Context context, boolean isLaunching) {
638 synchronized (mLock) {
639 if (DEBUG_LOADERS) {
640 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
641 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642
Joe Onorato36115782010-06-17 13:28:48 -0400643 // Don't bother to start the thread if we know it's not going to do anything
644 if (mCallbacks != null && mCallbacks.get() != null) {
645 // If there is already one running, tell it to stop.
646 LoaderTask oldTask = mLoaderTask;
647 if (oldTask != null) {
648 if (oldTask.isLaunching()) {
649 // don't downgrade isLaunching if we're already running
650 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500651 }
Joe Onorato36115782010-06-17 13:28:48 -0400652 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500653 }
Joe Onorato36115782010-06-17 13:28:48 -0400654 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700655 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 }
658 }
659
Joe Onorato36115782010-06-17 13:28:48 -0400660 public void stopLoader() {
661 synchronized (mLock) {
662 if (mLoaderTask != null) {
663 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664 }
665 }
Joe Onorato36115782010-06-17 13:28:48 -0400666 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400667
Michael Jurkac57b7a82011-08-09 22:02:20 -0700668 public boolean isAllAppsLoaded() {
669 return mAllAppsLoaded;
670 }
671
Joe Onorato36115782010-06-17 13:28:48 -0400672 /**
673 * Runnable for the thread that loads the contents of the launcher:
674 * - workspace icons
675 * - widgets
676 * - all apps icons
677 */
678 private class LoaderTask implements Runnable {
679 private Context mContext;
680 private Thread mWaitThread;
681 private boolean mIsLaunching;
682 private boolean mStopped;
683 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700684 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400685
686 LoaderTask(Context context, boolean isLaunching) {
687 mContext = context;
688 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700689 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 }
691
Joe Onorato36115782010-06-17 13:28:48 -0400692 boolean isLaunching() {
693 return mIsLaunching;
694 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695
Joe Onorato36115782010-06-17 13:28:48 -0400696 private void loadAndBindWorkspace() {
697 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400698 if (DEBUG_LOADERS) {
699 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700701
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700702 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400703 loadWorkspace();
704 if (mStopped) {
705 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 }
Joe Onorato36115782010-06-17 13:28:48 -0400707 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 }
709
Joe Onorato36115782010-06-17 13:28:48 -0400710 // Bind the workspace
711 bindWorkspace();
712 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400713
Joe Onorato36115782010-06-17 13:28:48 -0400714 private void waitForIdle() {
715 // Wait until the either we're stopped or the other threads are done.
716 // This way we don't start loading all apps until the workspace has settled
717 // down.
718 synchronized (LoaderTask.this) {
719 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700720
Joe Onorato36115782010-06-17 13:28:48 -0400721 mHandler.postIdle(new Runnable() {
722 public void run() {
723 synchronized (LoaderTask.this) {
724 mLoadAndBindStepFinished = true;
725 if (DEBUG_LOADERS) {
726 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400727 }
Joe Onorato36115782010-06-17 13:28:48 -0400728 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400729 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400730 }
Joe Onorato36115782010-06-17 13:28:48 -0400731 });
732
733 while (!mStopped && !mLoadAndBindStepFinished) {
734 try {
735 this.wait();
736 } catch (InterruptedException ex) {
737 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400738 }
739 }
Joe Onorato36115782010-06-17 13:28:48 -0400740 if (DEBUG_LOADERS) {
741 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700742 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400743 + "ms for previous step to finish binding");
744 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400745 }
Joe Onorato36115782010-06-17 13:28:48 -0400746 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400747
Joe Onorato36115782010-06-17 13:28:48 -0400748 public void run() {
749 // Optimize for end-user experience: if the Launcher is up and // running with the
750 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
751 // workspace first (default).
752 final Callbacks cbk = mCallbacks.get();
753 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400754
Joe Onorato36115782010-06-17 13:28:48 -0400755 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400756 // Elevate priority when Home launches for the first time to avoid
757 // starving at boot time. Staring at a blank home is not cool.
758 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700759 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
760 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400761 android.os.Process.setThreadPriority(mIsLaunching
762 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
763 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400764 if (loadWorkspaceFirst) {
765 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
766 loadAndBindWorkspace();
767 } else {
768 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700769 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400770 }
771
Joe Onorato36115782010-06-17 13:28:48 -0400772 if (mStopped) {
773 break keep_running;
774 }
775
776 // Whew! Hard work done. Slow us down, and wait until the UI thread has
777 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400778 synchronized (mLock) {
779 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700780 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400781 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
782 }
783 }
Joe Onorato36115782010-06-17 13:28:48 -0400784 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400785
786 // second step
787 if (loadWorkspaceFirst) {
788 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700789 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400790 } else {
791 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
792 loadAndBindWorkspace();
793 }
Joe Onorato36115782010-06-17 13:28:48 -0400794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795
Winson Chungaac01e12011-08-17 10:37:13 -0700796
797 // Update the saved icons if necessary
798 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700799 for (Object key : sDbIconCache.keySet()) {
800 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700801 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700802 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700803
Joe Onorato36115782010-06-17 13:28:48 -0400804 // Clear out this reference, otherwise we end up holding it until all of the
805 // callback runnables are done.
806 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400807
Joe Onorato36115782010-06-17 13:28:48 -0400808 synchronized (mLock) {
809 // If we are still the last one to be scheduled, remove ourselves.
810 if (mLoaderTask == this) {
811 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 }
Joe Onorato36115782010-06-17 13:28:48 -0400813 }
Joe Onorato36115782010-06-17 13:28:48 -0400814 }
815
816 public void stopLocked() {
817 synchronized (LoaderTask.this) {
818 mStopped = true;
819 this.notify();
820 }
821 }
822
823 /**
824 * Gets the callbacks object. If we've been stopped, or if the launcher object
825 * has somehow been garbage collected, return null instead. Pass in the Callbacks
826 * object that was around when the deferred message was scheduled, and if there's
827 * a new Callbacks object around then also return null. This will save us from
828 * calling onto it with data that will be ignored.
829 */
830 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
831 synchronized (mLock) {
832 if (mStopped) {
833 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400834 }
Joe Onorato36115782010-06-17 13:28:48 -0400835
836 if (mCallbacks == null) {
837 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400838 }
Joe Onorato36115782010-06-17 13:28:48 -0400839
840 final Callbacks callbacks = mCallbacks.get();
841 if (callbacks != oldCallbacks) {
842 return null;
843 }
844 if (callbacks == null) {
845 Log.w(TAG, "no mCallbacks");
846 return null;
847 }
848
849 return callbacks;
850 }
851 }
852
853 // check & update map of what's occupied; used to discard overlapping/invalid items
854 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700855 int containerIndex = item.screen;
856 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
857 // We use the last index to refer to the hotseat
858 containerIndex = Launcher.SCREEN_COUNT;
859 // Return early if we detect that an item is under the hotseat button
860 if (Hotseat.isAllAppsButtonRank(item.screen)) {
861 return false;
862 }
863 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
864 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400865 return true;
866 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700867
Joe Onorato36115782010-06-17 13:28:48 -0400868 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
869 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700870 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400871 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700872 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400873 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700874 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700875 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400876 return false;
877 }
878 }
879 }
880 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
881 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700882 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400883 }
884 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700885
Joe Onorato36115782010-06-17 13:28:48 -0400886 return true;
887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888
Joe Onorato36115782010-06-17 13:28:48 -0400889 private void loadWorkspace() {
890 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891
Joe Onorato36115782010-06-17 13:28:48 -0400892 final Context context = mContext;
893 final ContentResolver contentResolver = context.getContentResolver();
894 final PackageManager manager = context.getPackageManager();
895 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
896 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400897
Adam Cohen4eac29a2011-07-11 17:53:37 -0700898 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700899 sAppWidgets.clear();
900 sFolders.clear();
901 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700902 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800903
Joe Onorato36115782010-06-17 13:28:48 -0400904 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905
Joe Onorato36115782010-06-17 13:28:48 -0400906 final Cursor c = contentResolver.query(
907 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400908
Winson Chungf30ad5f2011-08-08 10:55:42 -0700909 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700910 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700911 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912
Joe Onorato36115782010-06-17 13:28:48 -0400913 try {
914 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
915 final int intentIndex = c.getColumnIndexOrThrow
916 (LauncherSettings.Favorites.INTENT);
917 final int titleIndex = c.getColumnIndexOrThrow
918 (LauncherSettings.Favorites.TITLE);
919 final int iconTypeIndex = c.getColumnIndexOrThrow(
920 LauncherSettings.Favorites.ICON_TYPE);
921 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
922 final int iconPackageIndex = c.getColumnIndexOrThrow(
923 LauncherSettings.Favorites.ICON_PACKAGE);
924 final int iconResourceIndex = c.getColumnIndexOrThrow(
925 LauncherSettings.Favorites.ICON_RESOURCE);
926 final int containerIndex = c.getColumnIndexOrThrow(
927 LauncherSettings.Favorites.CONTAINER);
928 final int itemTypeIndex = c.getColumnIndexOrThrow(
929 LauncherSettings.Favorites.ITEM_TYPE);
930 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
931 LauncherSettings.Favorites.APPWIDGET_ID);
932 final int screenIndex = c.getColumnIndexOrThrow(
933 LauncherSettings.Favorites.SCREEN);
934 final int cellXIndex = c.getColumnIndexOrThrow
935 (LauncherSettings.Favorites.CELLX);
936 final int cellYIndex = c.getColumnIndexOrThrow
937 (LauncherSettings.Favorites.CELLY);
938 final int spanXIndex = c.getColumnIndexOrThrow
939 (LauncherSettings.Favorites.SPANX);
940 final int spanYIndex = c.getColumnIndexOrThrow(
941 LauncherSettings.Favorites.SPANY);
942 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
943 final int displayModeIndex = c.getColumnIndexOrThrow(
944 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945
Joe Onorato36115782010-06-17 13:28:48 -0400946 ShortcutInfo info;
947 String intentDescription;
948 LauncherAppWidgetInfo appWidgetInfo;
949 int container;
950 long id;
951 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952
Joe Onorato36115782010-06-17 13:28:48 -0400953 while (!mStopped && c.moveToNext()) {
954 try {
955 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956
Joe Onorato36115782010-06-17 13:28:48 -0400957 switch (itemType) {
958 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
959 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
960 intentDescription = c.getString(intentIndex);
961 try {
962 intent = Intent.parseUri(intentDescription, 0);
963 } catch (URISyntaxException e) {
964 continue;
965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966
Joe Onorato36115782010-06-17 13:28:48 -0400967 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
968 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700969 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400970 } else {
971 info = getShortcutInfo(c, context, iconTypeIndex,
972 iconPackageIndex, iconResourceIndex, iconIndex,
973 titleIndex);
974 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975
Joe Onorato36115782010-06-17 13:28:48 -0400976 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400977 info.intent = intent;
978 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400980 info.container = container;
981 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700982 info.cellX = c.getInt(cellXIndex);
983 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984
Daniel Sandler8802e962010-05-26 16:28:16 -0400985 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400986 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400987 break;
988 }
989
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400991 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700993 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400994 break;
995 default:
996 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700997 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700998 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400999 folderInfo.add(info);
1000 break;
1001 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001002 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001003
1004 // now that we've loaded everthing re-save it with the
1005 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001006 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001007 } else {
1008 // Failed to load the shortcut, probably because the
1009 // activity manager couldn't resolve it (maybe the app
1010 // was uninstalled), or the db row was somehow screwed up.
1011 // Delete it.
1012 id = c.getLong(idIndex);
1013 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1014 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1015 id, false), null, null);
1016 }
1017 break;
1018
Adam Cohendf2cc412011-04-27 16:56:57 -07001019 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001020 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001021 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001022
Winson Chungaafa03c2010-06-11 17:34:16 -07001023 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001024 folderInfo.id = id;
1025 container = c.getInt(containerIndex);
1026 folderInfo.container = container;
1027 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001028 folderInfo.cellX = c.getInt(cellXIndex);
1029 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001030
1031 // check & update map of what's occupied
1032 if (!checkItemPlacement(occupied, folderInfo)) {
1033 break;
1034 }
Joe Onorato36115782010-06-17 13:28:48 -04001035 switch (container) {
1036 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001037 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001038 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001039 break;
1040 }
1041
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001042 sItemsIdMap.put(folderInfo.id, folderInfo);
1043 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001044 break;
1045
Joe Onorato36115782010-06-17 13:28:48 -04001046 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1047 // Read all Launcher-specific widget details
1048 int appWidgetId = c.getInt(appWidgetIdIndex);
1049 id = c.getLong(idIndex);
1050
1051 final AppWidgetProviderInfo provider =
1052 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001053
Joe Onorato36115782010-06-17 13:28:48 -04001054 if (!isSafeMode && (provider == null || provider.provider == null ||
1055 provider.provider.getPackageName() == null)) {
1056 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1057 + id + " appWidgetId=" + appWidgetId);
1058 itemsToRemove.add(id);
1059 } else {
1060 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
1061 appWidgetInfo.id = id;
1062 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001063 appWidgetInfo.cellX = c.getInt(cellXIndex);
1064 appWidgetInfo.cellY = c.getInt(cellYIndex);
1065 appWidgetInfo.spanX = c.getInt(spanXIndex);
1066 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001067
1068 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001069 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1070 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001071 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001072 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001073 continue;
1074 }
1075 appWidgetInfo.container = c.getInt(containerIndex);
1076
1077 // check & update map of what's occupied
1078 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1079 break;
1080 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001081 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1082 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001083 }
1084 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001085 }
Joe Onorato36115782010-06-17 13:28:48 -04001086 } catch (Exception e) {
1087 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001088 }
1089 }
Joe Onorato36115782010-06-17 13:28:48 -04001090 } finally {
1091 c.close();
1092 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001093
Joe Onorato36115782010-06-17 13:28:48 -04001094 if (itemsToRemove.size() > 0) {
1095 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1096 LauncherSettings.Favorites.CONTENT_URI);
1097 // Remove dead items
1098 for (long id : itemsToRemove) {
1099 if (DEBUG_LOADERS) {
1100 Log.d(TAG, "Removed id = " + id);
1101 }
1102 // Don't notify content observers
1103 try {
1104 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1105 null, null);
1106 } catch (RemoteException e) {
1107 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001108 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001109 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 }
1111
Joe Onorato36115782010-06-17 13:28:48 -04001112 if (DEBUG_LOADERS) {
1113 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1114 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001115 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001116 String line = "";
1117 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1118 if (s > 0) {
1119 line += " | ";
1120 }
Adam Cohend22015c2010-07-26 22:02:18 -07001121 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001122 line += ((occupied[s][x][y] != null) ? "#" : ".");
1123 }
1124 }
1125 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 }
Joe Onorato36115782010-06-17 13:28:48 -04001127 }
1128 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129
Joe Onorato36115782010-06-17 13:28:48 -04001130 /**
1131 * Read everything out of our database.
1132 */
1133 private void bindWorkspace() {
1134 final long t = SystemClock.uptimeMillis();
1135
1136 // Don't use these two variables in any of the callback runnables.
1137 // Otherwise we hold a reference to them.
1138 final Callbacks oldCallbacks = mCallbacks.get();
1139 if (oldCallbacks == null) {
1140 // This launcher has exited and nobody bothered to tell us. Just bail.
1141 Log.w(TAG, "LoaderTask running with no launcher");
1142 return;
1143 }
1144
1145 int N;
1146 // Tell the workspace that we're about to start firing items at it
1147 mHandler.post(new Runnable() {
1148 public void run() {
1149 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1150 if (callbacks != null) {
1151 callbacks.startBinding();
1152 }
1153 }
1154 });
1155 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001156 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001157 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1158 final int start = i;
1159 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001160 mHandler.post(new Runnable() {
1161 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001162 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001163 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001164 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 }
1166 }
1167 });
Joe Onorato36115782010-06-17 13:28:48 -04001168 }
1169 mHandler.post(new Runnable() {
1170 public void run() {
1171 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1172 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001173 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001174 }
1175 }
1176 });
1177 // Wait until the queue goes empty.
1178 mHandler.post(new Runnable() {
1179 public void run() {
1180 if (DEBUG_LOADERS) {
1181 Log.d(TAG, "Going to start binding widgets soon.");
1182 }
1183 }
1184 });
1185 // Bind the widgets, one at a time.
1186 // WARNING: this is calling into the workspace from the background thread,
1187 // but since getCurrentScreen() just returns the int, we should be okay. This
1188 // is just a hint for the order, and if it's wrong, we'll be okay.
1189 // TODO: instead, we should have that push the current screen into here.
1190 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001191 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001192 // once for the current screen
1193 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001194 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001195 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 mHandler.post(new Runnable() {
1197 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001198 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001200 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
1202 }
1203 });
1204 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 }
Joe Onorato36115782010-06-17 13:28:48 -04001206 // once for the other screens
1207 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001208 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001209 if (widget.screen != currentScreen) {
1210 mHandler.post(new Runnable() {
1211 public void run() {
1212 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1213 if (callbacks != null) {
1214 callbacks.bindAppWidget(widget);
1215 }
1216 }
1217 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001218 }
1219 }
Joe Onorato36115782010-06-17 13:28:48 -04001220 // Tell the workspace that we're done.
1221 mHandler.post(new Runnable() {
1222 public void run() {
1223 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1224 if (callbacks != null) {
1225 callbacks.finishBindingItems();
1226 }
1227 }
1228 });
1229 // If we're profiling, this is the last thing in the queue.
1230 mHandler.post(new Runnable() {
1231 public void run() {
1232 if (DEBUG_LOADERS) {
1233 Log.d(TAG, "bound workspace in "
1234 + (SystemClock.uptimeMillis()-t) + "ms");
1235 }
1236 }
1237 });
1238 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001239
Joe Onorato36115782010-06-17 13:28:48 -04001240 private void loadAndBindAllApps() {
1241 if (DEBUG_LOADERS) {
1242 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1243 }
1244 if (!mAllAppsLoaded) {
1245 loadAllAppsByBatch();
1246 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001247 return;
1248 }
Joe Onorato36115782010-06-17 13:28:48 -04001249 mAllAppsLoaded = true;
1250 } else {
1251 onlyBindAllApps();
1252 }
1253 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001254
Joe Onorato36115782010-06-17 13:28:48 -04001255 private void onlyBindAllApps() {
1256 final Callbacks oldCallbacks = mCallbacks.get();
1257 if (oldCallbacks == null) {
1258 // This launcher has exited and nobody bothered to tell us. Just bail.
1259 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1260 return;
1261 }
1262
1263 // shallow copy
1264 final ArrayList<ApplicationInfo> list
1265 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1266 mHandler.post(new Runnable() {
1267 public void run() {
1268 final long t = SystemClock.uptimeMillis();
1269 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1270 if (callbacks != null) {
1271 callbacks.bindAllApplications(list);
1272 }
1273 if (DEBUG_LOADERS) {
1274 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1275 + (SystemClock.uptimeMillis()-t) + "ms");
1276 }
1277 }
1278 });
1279
1280 }
1281
1282 private void loadAllAppsByBatch() {
1283 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1284
1285 // Don't use these two variables in any of the callback runnables.
1286 // Otherwise we hold a reference to them.
1287 final Callbacks oldCallbacks = mCallbacks.get();
1288 if (oldCallbacks == null) {
1289 // This launcher has exited and nobody bothered to tell us. Just bail.
1290 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1291 return;
1292 }
1293
1294 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1295 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1296
1297 final PackageManager packageManager = mContext.getPackageManager();
1298 List<ResolveInfo> apps = null;
1299
1300 int N = Integer.MAX_VALUE;
1301
1302 int startIndex;
1303 int i=0;
1304 int batchSize = -1;
1305 while (i < N && !mStopped) {
1306 if (i == 0) {
1307 mAllAppsList.clear();
1308 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1309 apps = packageManager.queryIntentActivities(mainIntent, 0);
1310 if (DEBUG_LOADERS) {
1311 Log.d(TAG, "queryIntentActivities took "
1312 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1313 }
1314 if (apps == null) {
1315 return;
1316 }
1317 N = apps.size();
1318 if (DEBUG_LOADERS) {
1319 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1320 }
1321 if (N == 0) {
1322 // There are no apps?!?
1323 return;
1324 }
1325 if (mBatchSize == 0) {
1326 batchSize = N;
1327 } else {
1328 batchSize = mBatchSize;
1329 }
1330
1331 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1332 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001333 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001334 if (DEBUG_LOADERS) {
1335 Log.d(TAG, "sort took "
1336 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1337 }
1338 }
1339
1340 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1341
1342 startIndex = i;
1343 for (int j=0; i<N && j<batchSize; j++) {
1344 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001345 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1346 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001347 i++;
1348 }
1349
1350 final boolean first = i <= batchSize;
1351 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1352 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1353 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1354
Joe Onoratocc67f472010-06-08 10:54:30 -07001355 mHandler.post(new Runnable() {
1356 public void run() {
1357 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001358 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001359 if (first) {
1360 callbacks.bindAllApplications(added);
1361 } else {
1362 callbacks.bindAppsAdded(added);
1363 }
1364 if (DEBUG_LOADERS) {
1365 Log.d(TAG, "bound " + added.size() + " apps in "
1366 + (SystemClock.uptimeMillis() - t) + "ms");
1367 }
1368 } else {
1369 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001370 }
1371 }
1372 });
1373
Daniel Sandlerdca66122010-04-13 16:23:58 -04001374 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001375 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1376 + (SystemClock.uptimeMillis()-t2) + "ms");
1377 }
1378
1379 if (mAllAppsLoadDelay > 0 && i < N) {
1380 try {
1381 if (DEBUG_LOADERS) {
1382 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1383 }
1384 Thread.sleep(mAllAppsLoadDelay);
1385 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 }
1387 }
1388
Joe Onorato36115782010-06-17 13:28:48 -04001389 if (DEBUG_LOADERS) {
1390 Log.d(TAG, "cached all " + N + " apps in "
1391 + (SystemClock.uptimeMillis()-t) + "ms"
1392 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001393 }
1394 }
1395
1396 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001397 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1398 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1399 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1400 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1401 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001402 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001403 }
1404 }
1405
1406 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001407 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001408 }
1409
1410 private class PackageUpdatedTask implements Runnable {
1411 int mOp;
1412 String[] mPackages;
1413
1414 public static final int OP_NONE = 0;
1415 public static final int OP_ADD = 1;
1416 public static final int OP_UPDATE = 2;
1417 public static final int OP_REMOVE = 3; // uninstlled
1418 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1419
1420
1421 public PackageUpdatedTask(int op, String[] packages) {
1422 mOp = op;
1423 mPackages = packages;
1424 }
1425
1426 public void run() {
1427 final Context context = mApp;
1428
1429 final String[] packages = mPackages;
1430 final int N = packages.length;
1431 switch (mOp) {
1432 case OP_ADD:
1433 for (int i=0; i<N; i++) {
1434 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1435 mAllAppsList.addPackage(context, packages[i]);
1436 }
1437 break;
1438 case OP_UPDATE:
1439 for (int i=0; i<N; i++) {
1440 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1441 mAllAppsList.updatePackage(context, packages[i]);
1442 }
1443 break;
1444 case OP_REMOVE:
1445 case OP_UNAVAILABLE:
1446 for (int i=0; i<N; i++) {
1447 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1448 mAllAppsList.removePackage(packages[i]);
1449 }
1450 break;
1451 }
1452
1453 ArrayList<ApplicationInfo> added = null;
1454 ArrayList<ApplicationInfo> removed = null;
1455 ArrayList<ApplicationInfo> modified = null;
1456
1457 if (mAllAppsList.added.size() > 0) {
1458 added = mAllAppsList.added;
1459 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1460 }
1461 if (mAllAppsList.removed.size() > 0) {
1462 removed = mAllAppsList.removed;
1463 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1464 for (ApplicationInfo info: removed) {
1465 mIconCache.remove(info.intent.getComponent());
1466 }
1467 }
1468 if (mAllAppsList.modified.size() > 0) {
1469 modified = mAllAppsList.modified;
1470 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1471 }
1472
1473 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1474 if (callbacks == null) {
1475 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1476 return;
1477 }
1478
1479 if (added != null) {
1480 final ArrayList<ApplicationInfo> addedFinal = added;
1481 mHandler.post(new Runnable() {
1482 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001483 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1484 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001485 callbacks.bindAppsAdded(addedFinal);
1486 }
1487 }
1488 });
1489 }
1490 if (modified != null) {
1491 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1492 mHandler.post(new Runnable() {
1493 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001494 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1495 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001496 callbacks.bindAppsUpdated(modifiedFinal);
1497 }
1498 }
1499 });
1500 }
1501 if (removed != null) {
1502 final boolean permanent = mOp != OP_UNAVAILABLE;
1503 final ArrayList<ApplicationInfo> removedFinal = removed;
1504 mHandler.post(new Runnable() {
1505 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001506 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1507 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001508 callbacks.bindAppsRemoved(removedFinal, permanent);
1509 }
1510 }
1511 });
Joe Onoratobe386092009-11-17 17:32:16 -08001512 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001513
1514 mHandler.post(new Runnable() {
1515 @Override
1516 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001517 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1518 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001519 callbacks.bindPackagesUpdated();
1520 }
1521 }
1522 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001523 }
1524 }
1525
1526 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001527 * This is called from the code that adds shortcuts from the intent receiver. This
1528 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001529 */
Joe Onorato56d82912010-03-07 14:32:10 -05001530 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001531 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001532 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533
Joe Onorato56d82912010-03-07 14:32:10 -05001534 /**
1535 * Make an ShortcutInfo object for a shortcut that is an application.
1536 *
1537 * If c is not null, then it will be used to fill in missing data like the title and icon.
1538 */
1539 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001540 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001541 Bitmap icon = null;
1542 final ShortcutInfo info = new ShortcutInfo();
1543
1544 ComponentName componentName = intent.getComponent();
1545 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001546 return null;
1547 }
1548
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001549 // TODO: See if the PackageManager knows about this case. If it doesn't
1550 // then return null & delete this.
1551
Joe Onorato56d82912010-03-07 14:32:10 -05001552 // the resource -- This may implicitly give us back the fallback icon,
1553 // but don't worry about that. All we're doing with usingFallbackIcon is
1554 // to avoid saving lots of copies of that in the database, and most apps
1555 // have icons anyway.
1556 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1557 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001558 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001559 }
Joe Onorato56d82912010-03-07 14:32:10 -05001560 // the db
1561 if (icon == null) {
1562 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001563 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001564 }
1565 }
1566 // the fallback icon
1567 if (icon == null) {
1568 icon = getFallbackIcon();
1569 info.usingFallbackIcon = true;
1570 }
1571 info.setIcon(icon);
1572
1573 // from the resource
1574 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001575 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1576 if (labelCache != null && labelCache.containsKey(key)) {
1577 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001578 } else {
1579 info.title = resolveInfo.activityInfo.loadLabel(manager);
1580 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001581 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001582 }
1583 }
Joe Onorato56d82912010-03-07 14:32:10 -05001584 }
1585 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001587 if (c != null) {
1588 info.title = c.getString(titleIndex);
1589 }
1590 }
1591 // fall back to the class name of the activity
1592 if (info.title == null) {
1593 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001594 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1596 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001600 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001602 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001603 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1604 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605
Joe Onorato56d82912010-03-07 14:32:10 -05001606 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001607 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001608 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001610 // TODO: If there's an explicit component and we can't install that, delete it.
1611
Joe Onorato56d82912010-03-07 14:32:10 -05001612 info.title = c.getString(titleIndex);
1613
Joe Onorato9c1289c2009-08-17 11:03:03 -04001614 int iconType = c.getInt(iconTypeIndex);
1615 switch (iconType) {
1616 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1617 String packageName = c.getString(iconPackageIndex);
1618 String resourceName = c.getString(iconResourceIndex);
1619 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001620 info.customIcon = false;
1621 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001623 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001624 if (resources != null) {
1625 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001626 icon = Utilities.createIconBitmap(
1627 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001628 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001629 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001630 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 }
Joe Onorato56d82912010-03-07 14:32:10 -05001632 // the db
1633 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001634 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001635 }
1636 // the fallback icon
1637 if (icon == null) {
1638 icon = getFallbackIcon();
1639 info.usingFallbackIcon = true;
1640 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001641 break;
1642 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001643 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001644 if (icon == null) {
1645 icon = getFallbackIcon();
1646 info.customIcon = false;
1647 info.usingFallbackIcon = true;
1648 } else {
1649 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001651 break;
1652 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001653 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001654 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001655 info.customIcon = false;
1656 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001658 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 return info;
1660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661
Michael Jurka931dc972011-08-05 15:08:15 -07001662 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001663 if (false) {
1664 Log.d(TAG, "getIconFromCursor app="
1665 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1666 }
1667 byte[] data = c.getBlob(iconIndex);
1668 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001669 return Utilities.createIconBitmap(
1670 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001671 } catch (Exception e) {
1672 return null;
1673 }
1674 }
1675
Winson Chung3d503fb2011-07-13 17:25:49 -07001676 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1677 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001678 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001679 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001680
1681 return info;
1682 }
1683
Winson Chunga9abd0e2010-10-27 17:18:37 -07001684 /**
Winson Chung55cef262010-10-28 14:14:18 -07001685 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1686 */
1687 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1688 ComponentName component) {
1689 List<AppWidgetProviderInfo> widgets =
1690 AppWidgetManager.getInstance(context).getInstalledProviders();
1691 for (AppWidgetProviderInfo info : widgets) {
1692 if (info.provider.equals(component)) {
1693 return info;
1694 }
1695 }
1696 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001697 }
1698
Winson Chung68846fd2010-10-29 11:00:27 -07001699 /**
1700 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1701 */
1702 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1703 final PackageManager packageManager = context.getPackageManager();
1704 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1705 new ArrayList<WidgetMimeTypeHandlerData>();
1706
1707 final Intent supportsIntent =
1708 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1709 supportsIntent.setType(mimeType);
1710
1711 // Create a set of widget configuration components that we can test against
1712 final List<AppWidgetProviderInfo> widgets =
1713 AppWidgetManager.getInstance(context).getInstalledProviders();
1714 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1715 new HashMap<ComponentName, AppWidgetProviderInfo>();
1716 for (AppWidgetProviderInfo info : widgets) {
1717 configurationComponentToWidget.put(info.configure, info);
1718 }
1719
1720 // Run through each of the intents that can handle this type of clip data, and cross
1721 // reference them with the components that are actual configuration components
1722 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1723 PackageManager.MATCH_DEFAULT_ONLY);
1724 for (ResolveInfo info : activities) {
1725 final ActivityInfo activityInfo = info.activityInfo;
1726 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1727 activityInfo.name);
1728 if (configurationComponentToWidget.containsKey(infoComponent)) {
1729 supportedConfigurationActivities.add(
1730 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1731 configurationComponentToWidget.get(infoComponent)));
1732 }
1733 }
1734 return supportedConfigurationActivities;
1735 }
1736
Winson Chunga9abd0e2010-10-27 17:18:37 -07001737 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001738 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1739 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1740 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1741
1742 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001743 boolean customIcon = false;
1744 ShortcutIconResource iconResource = null;
1745
1746 if (bitmap != null && bitmap instanceof Bitmap) {
1747 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001748 customIcon = true;
1749 } else {
1750 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1751 if (extra != null && extra instanceof ShortcutIconResource) {
1752 try {
1753 iconResource = (ShortcutIconResource) extra;
1754 final PackageManager packageManager = context.getPackageManager();
1755 Resources resources = packageManager.getResourcesForApplication(
1756 iconResource.packageName);
1757 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001758 icon = Utilities.createIconBitmap(
1759 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001760 } catch (Exception e) {
1761 Log.w(TAG, "Could not load shortcut icon: " + extra);
1762 }
1763 }
1764 }
1765
Joe Onorato0589f0f2010-02-08 13:44:00 -08001766 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001767
1768 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001769 if (fallbackIcon != null) {
1770 icon = fallbackIcon;
1771 } else {
1772 icon = getFallbackIcon();
1773 info.usingFallbackIcon = true;
1774 }
Joe Onorato56d82912010-03-07 14:32:10 -05001775 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001776 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001777
Joe Onorato0589f0f2010-02-08 13:44:00 -08001778 info.title = name;
1779 info.intent = intent;
1780 info.customIcon = customIcon;
1781 info.iconResource = iconResource;
1782
1783 return info;
1784 }
1785
Winson Chungaac01e12011-08-17 10:37:13 -07001786 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1787 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001788 // If apps can't be on SD, don't even bother.
1789 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001790 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001791 }
Joe Onorato56d82912010-03-07 14:32:10 -05001792 // If this icon doesn't have a custom icon, check to see
1793 // what's stored in the DB, and if it doesn't match what
1794 // we're going to show, store what we are going to show back
1795 // into the DB. We do this so when we're loading, if the
1796 // package manager can't find an icon (for example because
1797 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001798 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001799 cache.put(info, c.getBlob(iconIndex));
1800 return true;
1801 }
1802 return false;
1803 }
1804 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1805 boolean needSave = false;
1806 try {
1807 if (data != null) {
1808 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1809 Bitmap loaded = info.getIcon(mIconCache);
1810 needSave = !saved.sameAs(loaded);
1811 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001812 needSave = true;
1813 }
Winson Chungaac01e12011-08-17 10:37:13 -07001814 } catch (Exception e) {
1815 needSave = true;
1816 }
1817 if (needSave) {
1818 Log.d(TAG, "going to save icon bitmap for info=" + info);
1819 // This is slower than is ideal, but this only happens once
1820 // or when the app is updated with a new icon.
1821 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001822 }
1823 }
1824
Joe Onorato9c1289c2009-08-17 11:03:03 -04001825 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001826 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001827 * or make a new one.
1828 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001829 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001830 // See if a placeholder was created for us already
1831 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001832 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001834 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 folders.put(id, folderInfo);
1836 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001837 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 }
1839
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001841 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 = new Comparator<ApplicationInfo>() {
1843 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001844 int result = sCollator.compare(a.title.toString(), b.title.toString());
1845 if (result == 0) {
1846 result = a.componentName.compareTo(b.componentName);
1847 }
1848 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001850 };
Winson Chung78403fe2011-01-21 15:38:02 -08001851 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1852 = new Comparator<ApplicationInfo>() {
1853 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1854 if (a.firstInstallTime < b.firstInstallTime) return 1;
1855 if (a.firstInstallTime > b.firstInstallTime) return -1;
1856 return 0;
1857 }
1858 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001859 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1860 = new Comparator<AppWidgetProviderInfo>() {
1861 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1862 return sCollator.compare(a.label.toString(), b.label.toString());
1863 }
1864 };
Winson Chung5308f242011-08-18 12:12:41 -07001865 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1866 if (info.activityInfo != null) {
1867 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1868 } else {
1869 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1870 }
1871 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001872 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1873 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001874 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001875 ShortcutNameComparator(PackageManager pm) {
1876 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001877 mLabelCache = new HashMap<Object, CharSequence>();
1878 }
1879 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1880 mPackageManager = pm;
1881 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001882 }
1883 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001884 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001885 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1886 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1887 if (mLabelCache.containsKey(keyA)) {
1888 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001889 } else {
1890 labelA = a.loadLabel(mPackageManager).toString();
1891
Winson Chung5308f242011-08-18 12:12:41 -07001892 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001893 }
Winson Chung5308f242011-08-18 12:12:41 -07001894 if (mLabelCache.containsKey(keyB)) {
1895 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001896 } else {
1897 labelB = b.loadLabel(mPackageManager).toString();
1898
Winson Chung5308f242011-08-18 12:12:41 -07001899 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001900 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001901 return sCollator.compare(labelA, labelB);
1902 }
1903 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001904 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1905 private PackageManager mPackageManager;
1906 private HashMap<Object, String> mLabelCache;
1907 WidgetAndShortcutNameComparator(PackageManager pm) {
1908 mPackageManager = pm;
1909 mLabelCache = new HashMap<Object, String>();
1910 }
1911 public final int compare(Object a, Object b) {
1912 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001913 if (mLabelCache.containsKey(a)) {
1914 labelA = mLabelCache.get(a);
1915 } else {
1916 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001917 ((AppWidgetProviderInfo) a).label :
1918 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001919 mLabelCache.put(a, labelA);
1920 }
1921 if (mLabelCache.containsKey(b)) {
1922 labelB = mLabelCache.get(b);
1923 } else {
1924 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001925 ((AppWidgetProviderInfo) b).label :
1926 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001927 mLabelCache.put(b, labelB);
1928 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001929 return sCollator.compare(labelA, labelB);
1930 }
1931 };
Joe Onoratobe386092009-11-17 17:32:16 -08001932
1933 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001934 Log.d(TAG, "mCallbacks=" + mCallbacks);
1935 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1936 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1937 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1938 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001939 if (mLoaderTask != null) {
1940 mLoaderTask.dumpState();
1941 } else {
1942 Log.d(TAG, "mLoaderTask=null");
1943 }
Joe Onoratobe386092009-11-17 17:32:16 -08001944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945}