blob: 4ad8922f4f0e29d37de01ebc303f088f8fc25f77 [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)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700608 if (mCallbacks != null) {
609 Callbacks callbacks = mCallbacks.get();
610 if (callbacks != null) {
611 callbacks.bindSearchablesChanged();
612 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700613 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700614 }
615 }
616
617 /**
618 * When the launcher is in the background, it's possible for it to miss paired
619 * configuration changes. So whenever we trigger the loader from the background
620 * tell the launcher that it needs to re-run the loader when it comes back instead
621 * of doing it now.
622 */
623 public void startLoaderFromBackground() {
624 boolean runLoader = false;
625 if (mCallbacks != null) {
626 Callbacks callbacks = mCallbacks.get();
627 if (callbacks != null) {
628 // Only actually run the loader if they're not paused.
629 if (!callbacks.setLoadOnResume()) {
630 runLoader = true;
631 }
632 }
633 }
634 if (runLoader) {
635 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700636 }
Joe Onorato36115782010-06-17 13:28:48 -0400637 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400638
Joe Onorato36115782010-06-17 13:28:48 -0400639 public void startLoader(Context context, boolean isLaunching) {
640 synchronized (mLock) {
641 if (DEBUG_LOADERS) {
642 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644
Joe Onorato36115782010-06-17 13:28:48 -0400645 // Don't bother to start the thread if we know it's not going to do anything
646 if (mCallbacks != null && mCallbacks.get() != null) {
647 // If there is already one running, tell it to stop.
648 LoaderTask oldTask = mLoaderTask;
649 if (oldTask != null) {
650 if (oldTask.isLaunching()) {
651 // don't downgrade isLaunching if we're already running
652 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500653 }
Joe Onorato36115782010-06-17 13:28:48 -0400654 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500655 }
Joe Onorato36115782010-06-17 13:28:48 -0400656 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700657 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659 }
660 }
661
Joe Onorato36115782010-06-17 13:28:48 -0400662 public void stopLoader() {
663 synchronized (mLock) {
664 if (mLoaderTask != null) {
665 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 }
667 }
Joe Onorato36115782010-06-17 13:28:48 -0400668 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669
Michael Jurkac57b7a82011-08-09 22:02:20 -0700670 public boolean isAllAppsLoaded() {
671 return mAllAppsLoaded;
672 }
673
Joe Onorato36115782010-06-17 13:28:48 -0400674 /**
675 * Runnable for the thread that loads the contents of the launcher:
676 * - workspace icons
677 * - widgets
678 * - all apps icons
679 */
680 private class LoaderTask implements Runnable {
681 private Context mContext;
682 private Thread mWaitThread;
683 private boolean mIsLaunching;
684 private boolean mStopped;
685 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700686 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400687
688 LoaderTask(Context context, boolean isLaunching) {
689 mContext = context;
690 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700691 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 }
693
Joe Onorato36115782010-06-17 13:28:48 -0400694 boolean isLaunching() {
695 return mIsLaunching;
696 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697
Joe Onorato36115782010-06-17 13:28:48 -0400698 private void loadAndBindWorkspace() {
699 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400700 if (DEBUG_LOADERS) {
701 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700703
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700704 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400705 loadWorkspace();
706 if (mStopped) {
707 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 }
Joe Onorato36115782010-06-17 13:28:48 -0400709 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 }
711
Joe Onorato36115782010-06-17 13:28:48 -0400712 // Bind the workspace
713 bindWorkspace();
714 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400715
Joe Onorato36115782010-06-17 13:28:48 -0400716 private void waitForIdle() {
717 // Wait until the either we're stopped or the other threads are done.
718 // This way we don't start loading all apps until the workspace has settled
719 // down.
720 synchronized (LoaderTask.this) {
721 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700722
Joe Onorato36115782010-06-17 13:28:48 -0400723 mHandler.postIdle(new Runnable() {
724 public void run() {
725 synchronized (LoaderTask.this) {
726 mLoadAndBindStepFinished = true;
727 if (DEBUG_LOADERS) {
728 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400729 }
Joe Onorato36115782010-06-17 13:28:48 -0400730 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400731 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400732 }
Joe Onorato36115782010-06-17 13:28:48 -0400733 });
734
735 while (!mStopped && !mLoadAndBindStepFinished) {
736 try {
737 this.wait();
738 } catch (InterruptedException ex) {
739 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400740 }
741 }
Joe Onorato36115782010-06-17 13:28:48 -0400742 if (DEBUG_LOADERS) {
743 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700744 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400745 + "ms for previous step to finish binding");
746 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400747 }
Joe Onorato36115782010-06-17 13:28:48 -0400748 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400749
Joe Onorato36115782010-06-17 13:28:48 -0400750 public void run() {
751 // Optimize for end-user experience: if the Launcher is up and // running with the
752 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
753 // workspace first (default).
754 final Callbacks cbk = mCallbacks.get();
755 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400756
Joe Onorato36115782010-06-17 13:28:48 -0400757 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400758 // Elevate priority when Home launches for the first time to avoid
759 // starving at boot time. Staring at a blank home is not cool.
760 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700761 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
762 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400763 android.os.Process.setThreadPriority(mIsLaunching
764 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
765 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400766 if (loadWorkspaceFirst) {
767 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
768 loadAndBindWorkspace();
769 } else {
770 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700771 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400772 }
773
Joe Onorato36115782010-06-17 13:28:48 -0400774 if (mStopped) {
775 break keep_running;
776 }
777
778 // Whew! Hard work done. Slow us down, and wait until the UI thread has
779 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400780 synchronized (mLock) {
781 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700782 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400783 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
784 }
785 }
Joe Onorato36115782010-06-17 13:28:48 -0400786 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400787
788 // second step
789 if (loadWorkspaceFirst) {
790 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700791 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400792 } else {
793 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
794 loadAndBindWorkspace();
795 }
Joe Onorato36115782010-06-17 13:28:48 -0400796 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797
Winson Chungaac01e12011-08-17 10:37:13 -0700798
799 // Update the saved icons if necessary
800 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700801 for (Object key : sDbIconCache.keySet()) {
802 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700803 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700804 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700805
Joe Onorato36115782010-06-17 13:28:48 -0400806 // Clear out this reference, otherwise we end up holding it until all of the
807 // callback runnables are done.
808 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809
Joe Onorato36115782010-06-17 13:28:48 -0400810 synchronized (mLock) {
811 // If we are still the last one to be scheduled, remove ourselves.
812 if (mLoaderTask == this) {
813 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 }
Joe Onorato36115782010-06-17 13:28:48 -0400815 }
Joe Onorato36115782010-06-17 13:28:48 -0400816 }
817
818 public void stopLocked() {
819 synchronized (LoaderTask.this) {
820 mStopped = true;
821 this.notify();
822 }
823 }
824
825 /**
826 * Gets the callbacks object. If we've been stopped, or if the launcher object
827 * has somehow been garbage collected, return null instead. Pass in the Callbacks
828 * object that was around when the deferred message was scheduled, and if there's
829 * a new Callbacks object around then also return null. This will save us from
830 * calling onto it with data that will be ignored.
831 */
832 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
833 synchronized (mLock) {
834 if (mStopped) {
835 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400836 }
Joe Onorato36115782010-06-17 13:28:48 -0400837
838 if (mCallbacks == null) {
839 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400840 }
Joe Onorato36115782010-06-17 13:28:48 -0400841
842 final Callbacks callbacks = mCallbacks.get();
843 if (callbacks != oldCallbacks) {
844 return null;
845 }
846 if (callbacks == null) {
847 Log.w(TAG, "no mCallbacks");
848 return null;
849 }
850
851 return callbacks;
852 }
853 }
854
855 // check & update map of what's occupied; used to discard overlapping/invalid items
856 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700857 int containerIndex = item.screen;
858 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
859 // We use the last index to refer to the hotseat
860 containerIndex = Launcher.SCREEN_COUNT;
861 // Return early if we detect that an item is under the hotseat button
862 if (Hotseat.isAllAppsButtonRank(item.screen)) {
863 return false;
864 }
865 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
866 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400867 return true;
868 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700869
Joe Onorato36115782010-06-17 13:28:48 -0400870 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
871 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700872 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400873 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700874 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400875 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700876 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700877 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400878 return false;
879 }
880 }
881 }
882 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
883 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700884 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400885 }
886 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700887
Joe Onorato36115782010-06-17 13:28:48 -0400888 return true;
889 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890
Joe Onorato36115782010-06-17 13:28:48 -0400891 private void loadWorkspace() {
892 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893
Joe Onorato36115782010-06-17 13:28:48 -0400894 final Context context = mContext;
895 final ContentResolver contentResolver = context.getContentResolver();
896 final PackageManager manager = context.getPackageManager();
897 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
898 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400899
Adam Cohen4eac29a2011-07-11 17:53:37 -0700900 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700901 sAppWidgets.clear();
902 sFolders.clear();
903 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700904 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800905
Joe Onorato36115782010-06-17 13:28:48 -0400906 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907
Joe Onorato36115782010-06-17 13:28:48 -0400908 final Cursor c = contentResolver.query(
909 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400910
Winson Chungf30ad5f2011-08-08 10:55:42 -0700911 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700912 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700913 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914
Joe Onorato36115782010-06-17 13:28:48 -0400915 try {
916 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
917 final int intentIndex = c.getColumnIndexOrThrow
918 (LauncherSettings.Favorites.INTENT);
919 final int titleIndex = c.getColumnIndexOrThrow
920 (LauncherSettings.Favorites.TITLE);
921 final int iconTypeIndex = c.getColumnIndexOrThrow(
922 LauncherSettings.Favorites.ICON_TYPE);
923 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
924 final int iconPackageIndex = c.getColumnIndexOrThrow(
925 LauncherSettings.Favorites.ICON_PACKAGE);
926 final int iconResourceIndex = c.getColumnIndexOrThrow(
927 LauncherSettings.Favorites.ICON_RESOURCE);
928 final int containerIndex = c.getColumnIndexOrThrow(
929 LauncherSettings.Favorites.CONTAINER);
930 final int itemTypeIndex = c.getColumnIndexOrThrow(
931 LauncherSettings.Favorites.ITEM_TYPE);
932 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
933 LauncherSettings.Favorites.APPWIDGET_ID);
934 final int screenIndex = c.getColumnIndexOrThrow(
935 LauncherSettings.Favorites.SCREEN);
936 final int cellXIndex = c.getColumnIndexOrThrow
937 (LauncherSettings.Favorites.CELLX);
938 final int cellYIndex = c.getColumnIndexOrThrow
939 (LauncherSettings.Favorites.CELLY);
940 final int spanXIndex = c.getColumnIndexOrThrow
941 (LauncherSettings.Favorites.SPANX);
942 final int spanYIndex = c.getColumnIndexOrThrow(
943 LauncherSettings.Favorites.SPANY);
944 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
945 final int displayModeIndex = c.getColumnIndexOrThrow(
946 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947
Joe Onorato36115782010-06-17 13:28:48 -0400948 ShortcutInfo info;
949 String intentDescription;
950 LauncherAppWidgetInfo appWidgetInfo;
951 int container;
952 long id;
953 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954
Joe Onorato36115782010-06-17 13:28:48 -0400955 while (!mStopped && c.moveToNext()) {
956 try {
957 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958
Joe Onorato36115782010-06-17 13:28:48 -0400959 switch (itemType) {
960 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
961 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
962 intentDescription = c.getString(intentIndex);
963 try {
964 intent = Intent.parseUri(intentDescription, 0);
965 } catch (URISyntaxException e) {
966 continue;
967 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968
Joe Onorato36115782010-06-17 13:28:48 -0400969 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
970 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700971 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400972 } else {
973 info = getShortcutInfo(c, context, iconTypeIndex,
974 iconPackageIndex, iconResourceIndex, iconIndex,
975 titleIndex);
976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977
Joe Onorato36115782010-06-17 13:28:48 -0400978 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400979 info.intent = intent;
980 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400982 info.container = container;
983 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700984 info.cellX = c.getInt(cellXIndex);
985 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986
Daniel Sandler8802e962010-05-26 16:28:16 -0400987 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400988 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400989 break;
990 }
991
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400993 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -0700994 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700995 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400996 break;
997 default:
998 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700999 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001000 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001001 folderInfo.add(info);
1002 break;
1003 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001004 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001005
1006 // now that we've loaded everthing re-save it with the
1007 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001008 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001009 } else {
1010 // Failed to load the shortcut, probably because the
1011 // activity manager couldn't resolve it (maybe the app
1012 // was uninstalled), or the db row was somehow screwed up.
1013 // Delete it.
1014 id = c.getLong(idIndex);
1015 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1016 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1017 id, false), null, null);
1018 }
1019 break;
1020
Adam Cohendf2cc412011-04-27 16:56:57 -07001021 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001022 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001023 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001024
Winson Chungaafa03c2010-06-11 17:34:16 -07001025 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001026 folderInfo.id = id;
1027 container = c.getInt(containerIndex);
1028 folderInfo.container = container;
1029 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001030 folderInfo.cellX = c.getInt(cellXIndex);
1031 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001032
1033 // check & update map of what's occupied
1034 if (!checkItemPlacement(occupied, folderInfo)) {
1035 break;
1036 }
Joe Onorato36115782010-06-17 13:28:48 -04001037 switch (container) {
1038 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001039 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001040 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001041 break;
1042 }
1043
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001044 sItemsIdMap.put(folderInfo.id, folderInfo);
1045 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001046 break;
1047
Joe Onorato36115782010-06-17 13:28:48 -04001048 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1049 // Read all Launcher-specific widget details
1050 int appWidgetId = c.getInt(appWidgetIdIndex);
1051 id = c.getLong(idIndex);
1052
1053 final AppWidgetProviderInfo provider =
1054 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001055
Joe Onorato36115782010-06-17 13:28:48 -04001056 if (!isSafeMode && (provider == null || provider.provider == null ||
1057 provider.provider.getPackageName() == null)) {
1058 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1059 + id + " appWidgetId=" + appWidgetId);
1060 itemsToRemove.add(id);
1061 } else {
1062 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
1063 appWidgetInfo.id = id;
1064 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001065 appWidgetInfo.cellX = c.getInt(cellXIndex);
1066 appWidgetInfo.cellY = c.getInt(cellYIndex);
1067 appWidgetInfo.spanX = c.getInt(spanXIndex);
1068 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001069
1070 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1072 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001073 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001074 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001075 continue;
1076 }
1077 appWidgetInfo.container = c.getInt(containerIndex);
1078
1079 // check & update map of what's occupied
1080 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1081 break;
1082 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001083 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1084 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001085 }
1086 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001087 }
Joe Onorato36115782010-06-17 13:28:48 -04001088 } catch (Exception e) {
1089 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001090 }
1091 }
Joe Onorato36115782010-06-17 13:28:48 -04001092 } finally {
1093 c.close();
1094 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001095
Joe Onorato36115782010-06-17 13:28:48 -04001096 if (itemsToRemove.size() > 0) {
1097 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1098 LauncherSettings.Favorites.CONTENT_URI);
1099 // Remove dead items
1100 for (long id : itemsToRemove) {
1101 if (DEBUG_LOADERS) {
1102 Log.d(TAG, "Removed id = " + id);
1103 }
1104 // Don't notify content observers
1105 try {
1106 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1107 null, null);
1108 } catch (RemoteException e) {
1109 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001110 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001111 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112 }
1113
Joe Onorato36115782010-06-17 13:28:48 -04001114 if (DEBUG_LOADERS) {
1115 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1116 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001117 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001118 String line = "";
1119 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1120 if (s > 0) {
1121 line += " | ";
1122 }
Adam Cohend22015c2010-07-26 22:02:18 -07001123 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001124 line += ((occupied[s][x][y] != null) ? "#" : ".");
1125 }
1126 }
1127 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 }
Joe Onorato36115782010-06-17 13:28:48 -04001129 }
1130 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131
Joe Onorato36115782010-06-17 13:28:48 -04001132 /**
1133 * Read everything out of our database.
1134 */
1135 private void bindWorkspace() {
1136 final long t = SystemClock.uptimeMillis();
1137
1138 // Don't use these two variables in any of the callback runnables.
1139 // Otherwise we hold a reference to them.
1140 final Callbacks oldCallbacks = mCallbacks.get();
1141 if (oldCallbacks == null) {
1142 // This launcher has exited and nobody bothered to tell us. Just bail.
1143 Log.w(TAG, "LoaderTask running with no launcher");
1144 return;
1145 }
1146
1147 int N;
1148 // Tell the workspace that we're about to start firing items at it
1149 mHandler.post(new Runnable() {
1150 public void run() {
1151 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1152 if (callbacks != null) {
1153 callbacks.startBinding();
1154 }
1155 }
1156 });
1157 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001158 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001159 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1160 final int start = i;
1161 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 mHandler.post(new Runnable() {
1163 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001164 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001166 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 }
1168 }
1169 });
Joe Onorato36115782010-06-17 13:28:48 -04001170 }
1171 mHandler.post(new Runnable() {
1172 public void run() {
1173 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1174 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001175 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001176 }
1177 }
1178 });
1179 // Wait until the queue goes empty.
1180 mHandler.post(new Runnable() {
1181 public void run() {
1182 if (DEBUG_LOADERS) {
1183 Log.d(TAG, "Going to start binding widgets soon.");
1184 }
1185 }
1186 });
1187 // Bind the widgets, one at a time.
1188 // WARNING: this is calling into the workspace from the background thread,
1189 // but since getCurrentScreen() just returns the int, we should be okay. This
1190 // is just a hint for the order, and if it's wrong, we'll be okay.
1191 // TODO: instead, we should have that push the current screen into here.
1192 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001193 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001194 // once for the current screen
1195 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001196 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001197 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 mHandler.post(new Runnable() {
1199 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001200 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001202 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 }
1204 }
1205 });
1206 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 }
Joe Onorato36115782010-06-17 13:28:48 -04001208 // once for the other screens
1209 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001210 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001211 if (widget.screen != currentScreen) {
1212 mHandler.post(new Runnable() {
1213 public void run() {
1214 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1215 if (callbacks != null) {
1216 callbacks.bindAppWidget(widget);
1217 }
1218 }
1219 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001220 }
1221 }
Joe Onorato36115782010-06-17 13:28:48 -04001222 // Tell the workspace that we're done.
1223 mHandler.post(new Runnable() {
1224 public void run() {
1225 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1226 if (callbacks != null) {
1227 callbacks.finishBindingItems();
1228 }
1229 }
1230 });
1231 // If we're profiling, this is the last thing in the queue.
1232 mHandler.post(new Runnable() {
1233 public void run() {
1234 if (DEBUG_LOADERS) {
1235 Log.d(TAG, "bound workspace in "
1236 + (SystemClock.uptimeMillis()-t) + "ms");
1237 }
1238 }
1239 });
1240 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001241
Joe Onorato36115782010-06-17 13:28:48 -04001242 private void loadAndBindAllApps() {
1243 if (DEBUG_LOADERS) {
1244 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1245 }
1246 if (!mAllAppsLoaded) {
1247 loadAllAppsByBatch();
1248 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001249 return;
1250 }
Joe Onorato36115782010-06-17 13:28:48 -04001251 mAllAppsLoaded = true;
1252 } else {
1253 onlyBindAllApps();
1254 }
1255 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001256
Joe Onorato36115782010-06-17 13:28:48 -04001257 private void onlyBindAllApps() {
1258 final Callbacks oldCallbacks = mCallbacks.get();
1259 if (oldCallbacks == null) {
1260 // This launcher has exited and nobody bothered to tell us. Just bail.
1261 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1262 return;
1263 }
1264
1265 // shallow copy
1266 final ArrayList<ApplicationInfo> list
1267 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1268 mHandler.post(new Runnable() {
1269 public void run() {
1270 final long t = SystemClock.uptimeMillis();
1271 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1272 if (callbacks != null) {
1273 callbacks.bindAllApplications(list);
1274 }
1275 if (DEBUG_LOADERS) {
1276 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1277 + (SystemClock.uptimeMillis()-t) + "ms");
1278 }
1279 }
1280 });
1281
1282 }
1283
1284 private void loadAllAppsByBatch() {
1285 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1286
1287 // Don't use these two variables in any of the callback runnables.
1288 // Otherwise we hold a reference to them.
1289 final Callbacks oldCallbacks = mCallbacks.get();
1290 if (oldCallbacks == null) {
1291 // This launcher has exited and nobody bothered to tell us. Just bail.
1292 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1293 return;
1294 }
1295
1296 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1297 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1298
1299 final PackageManager packageManager = mContext.getPackageManager();
1300 List<ResolveInfo> apps = null;
1301
1302 int N = Integer.MAX_VALUE;
1303
1304 int startIndex;
1305 int i=0;
1306 int batchSize = -1;
1307 while (i < N && !mStopped) {
1308 if (i == 0) {
1309 mAllAppsList.clear();
1310 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1311 apps = packageManager.queryIntentActivities(mainIntent, 0);
1312 if (DEBUG_LOADERS) {
1313 Log.d(TAG, "queryIntentActivities took "
1314 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1315 }
1316 if (apps == null) {
1317 return;
1318 }
1319 N = apps.size();
1320 if (DEBUG_LOADERS) {
1321 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1322 }
1323 if (N == 0) {
1324 // There are no apps?!?
1325 return;
1326 }
1327 if (mBatchSize == 0) {
1328 batchSize = N;
1329 } else {
1330 batchSize = mBatchSize;
1331 }
1332
1333 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1334 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001335 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001336 if (DEBUG_LOADERS) {
1337 Log.d(TAG, "sort took "
1338 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1339 }
1340 }
1341
1342 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1343
1344 startIndex = i;
1345 for (int j=0; i<N && j<batchSize; j++) {
1346 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001347 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1348 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001349 i++;
1350 }
1351
1352 final boolean first = i <= batchSize;
1353 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1354 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1355 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1356
Joe Onoratocc67f472010-06-08 10:54:30 -07001357 mHandler.post(new Runnable() {
1358 public void run() {
1359 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001360 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001361 if (first) {
1362 callbacks.bindAllApplications(added);
1363 } else {
1364 callbacks.bindAppsAdded(added);
1365 }
1366 if (DEBUG_LOADERS) {
1367 Log.d(TAG, "bound " + added.size() + " apps in "
1368 + (SystemClock.uptimeMillis() - t) + "ms");
1369 }
1370 } else {
1371 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001372 }
1373 }
1374 });
1375
Daniel Sandlerdca66122010-04-13 16:23:58 -04001376 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001377 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1378 + (SystemClock.uptimeMillis()-t2) + "ms");
1379 }
1380
1381 if (mAllAppsLoadDelay > 0 && i < N) {
1382 try {
1383 if (DEBUG_LOADERS) {
1384 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1385 }
1386 Thread.sleep(mAllAppsLoadDelay);
1387 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 }
1389 }
1390
Joe Onorato36115782010-06-17 13:28:48 -04001391 if (DEBUG_LOADERS) {
1392 Log.d(TAG, "cached all " + N + " apps in "
1393 + (SystemClock.uptimeMillis()-t) + "ms"
1394 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001395 }
1396 }
1397
1398 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001399 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1400 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1401 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1402 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1403 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001404 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001405 }
1406 }
1407
1408 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001409 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001410 }
1411
1412 private class PackageUpdatedTask implements Runnable {
1413 int mOp;
1414 String[] mPackages;
1415
1416 public static final int OP_NONE = 0;
1417 public static final int OP_ADD = 1;
1418 public static final int OP_UPDATE = 2;
1419 public static final int OP_REMOVE = 3; // uninstlled
1420 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1421
1422
1423 public PackageUpdatedTask(int op, String[] packages) {
1424 mOp = op;
1425 mPackages = packages;
1426 }
1427
1428 public void run() {
1429 final Context context = mApp;
1430
1431 final String[] packages = mPackages;
1432 final int N = packages.length;
1433 switch (mOp) {
1434 case OP_ADD:
1435 for (int i=0; i<N; i++) {
1436 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1437 mAllAppsList.addPackage(context, packages[i]);
1438 }
1439 break;
1440 case OP_UPDATE:
1441 for (int i=0; i<N; i++) {
1442 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1443 mAllAppsList.updatePackage(context, packages[i]);
1444 }
1445 break;
1446 case OP_REMOVE:
1447 case OP_UNAVAILABLE:
1448 for (int i=0; i<N; i++) {
1449 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1450 mAllAppsList.removePackage(packages[i]);
1451 }
1452 break;
1453 }
1454
1455 ArrayList<ApplicationInfo> added = null;
1456 ArrayList<ApplicationInfo> removed = null;
1457 ArrayList<ApplicationInfo> modified = null;
1458
1459 if (mAllAppsList.added.size() > 0) {
1460 added = mAllAppsList.added;
1461 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1462 }
1463 if (mAllAppsList.removed.size() > 0) {
1464 removed = mAllAppsList.removed;
1465 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1466 for (ApplicationInfo info: removed) {
1467 mIconCache.remove(info.intent.getComponent());
1468 }
1469 }
1470 if (mAllAppsList.modified.size() > 0) {
1471 modified = mAllAppsList.modified;
1472 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1473 }
1474
1475 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1476 if (callbacks == null) {
1477 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1478 return;
1479 }
1480
1481 if (added != null) {
1482 final ArrayList<ApplicationInfo> addedFinal = added;
1483 mHandler.post(new Runnable() {
1484 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001485 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1486 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001487 callbacks.bindAppsAdded(addedFinal);
1488 }
1489 }
1490 });
1491 }
1492 if (modified != null) {
1493 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1494 mHandler.post(new Runnable() {
1495 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001496 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1497 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001498 callbacks.bindAppsUpdated(modifiedFinal);
1499 }
1500 }
1501 });
1502 }
1503 if (removed != null) {
1504 final boolean permanent = mOp != OP_UNAVAILABLE;
1505 final ArrayList<ApplicationInfo> removedFinal = removed;
1506 mHandler.post(new Runnable() {
1507 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001508 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1509 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001510 callbacks.bindAppsRemoved(removedFinal, permanent);
1511 }
1512 }
1513 });
Joe Onoratobe386092009-11-17 17:32:16 -08001514 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001515
1516 mHandler.post(new Runnable() {
1517 @Override
1518 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001519 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1520 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001521 callbacks.bindPackagesUpdated();
1522 }
1523 }
1524 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001525 }
1526 }
1527
1528 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001529 * This is called from the code that adds shortcuts from the intent receiver. This
1530 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001531 */
Joe Onorato56d82912010-03-07 14:32:10 -05001532 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001533 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001534 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001535
Joe Onorato56d82912010-03-07 14:32:10 -05001536 /**
1537 * Make an ShortcutInfo object for a shortcut that is an application.
1538 *
1539 * If c is not null, then it will be used to fill in missing data like the title and icon.
1540 */
1541 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001542 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001543 Bitmap icon = null;
1544 final ShortcutInfo info = new ShortcutInfo();
1545
1546 ComponentName componentName = intent.getComponent();
1547 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001548 return null;
1549 }
1550
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001551 // TODO: See if the PackageManager knows about this case. If it doesn't
1552 // then return null & delete this.
1553
Joe Onorato56d82912010-03-07 14:32:10 -05001554 // the resource -- This may implicitly give us back the fallback icon,
1555 // but don't worry about that. All we're doing with usingFallbackIcon is
1556 // to avoid saving lots of copies of that in the database, and most apps
1557 // have icons anyway.
1558 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1559 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001560 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001561 }
Joe Onorato56d82912010-03-07 14:32:10 -05001562 // the db
1563 if (icon == null) {
1564 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001565 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001566 }
1567 }
1568 // the fallback icon
1569 if (icon == null) {
1570 icon = getFallbackIcon();
1571 info.usingFallbackIcon = true;
1572 }
1573 info.setIcon(icon);
1574
1575 // from the resource
1576 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001577 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1578 if (labelCache != null && labelCache.containsKey(key)) {
1579 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001580 } else {
1581 info.title = resolveInfo.activityInfo.loadLabel(manager);
1582 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001583 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001584 }
1585 }
Joe Onorato56d82912010-03-07 14:32:10 -05001586 }
1587 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001589 if (c != null) {
1590 info.title = c.getString(titleIndex);
1591 }
1592 }
1593 // fall back to the class name of the activity
1594 if (info.title == null) {
1595 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001596 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1598 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001602 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001604 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001605 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1606 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607
Joe Onorato56d82912010-03-07 14:32:10 -05001608 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001609 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001610 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001612 // TODO: If there's an explicit component and we can't install that, delete it.
1613
Joe Onorato56d82912010-03-07 14:32:10 -05001614 info.title = c.getString(titleIndex);
1615
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 int iconType = c.getInt(iconTypeIndex);
1617 switch (iconType) {
1618 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1619 String packageName = c.getString(iconPackageIndex);
1620 String resourceName = c.getString(iconResourceIndex);
1621 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001622 info.customIcon = false;
1623 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001625 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001626 if (resources != null) {
1627 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001628 icon = Utilities.createIconBitmap(
1629 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001630 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001632 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 }
Joe Onorato56d82912010-03-07 14:32:10 -05001634 // the db
1635 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001636 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001637 }
1638 // the fallback icon
1639 if (icon == null) {
1640 icon = getFallbackIcon();
1641 info.usingFallbackIcon = true;
1642 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001643 break;
1644 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001645 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001646 if (icon == null) {
1647 icon = getFallbackIcon();
1648 info.customIcon = false;
1649 info.usingFallbackIcon = true;
1650 } else {
1651 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001652 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001653 break;
1654 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001655 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001656 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001657 info.customIcon = false;
1658 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001660 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001661 return info;
1662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663
Michael Jurka931dc972011-08-05 15:08:15 -07001664 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001665 if (false) {
1666 Log.d(TAG, "getIconFromCursor app="
1667 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1668 }
1669 byte[] data = c.getBlob(iconIndex);
1670 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001671 return Utilities.createIconBitmap(
1672 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001673 } catch (Exception e) {
1674 return null;
1675 }
1676 }
1677
Winson Chung3d503fb2011-07-13 17:25:49 -07001678 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1679 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001680 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001681 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001682
1683 return info;
1684 }
1685
Winson Chunga9abd0e2010-10-27 17:18:37 -07001686 /**
Winson Chung55cef262010-10-28 14:14:18 -07001687 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1688 */
1689 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1690 ComponentName component) {
1691 List<AppWidgetProviderInfo> widgets =
1692 AppWidgetManager.getInstance(context).getInstalledProviders();
1693 for (AppWidgetProviderInfo info : widgets) {
1694 if (info.provider.equals(component)) {
1695 return info;
1696 }
1697 }
1698 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001699 }
1700
Winson Chung68846fd2010-10-29 11:00:27 -07001701 /**
1702 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1703 */
1704 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1705 final PackageManager packageManager = context.getPackageManager();
1706 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1707 new ArrayList<WidgetMimeTypeHandlerData>();
1708
1709 final Intent supportsIntent =
1710 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1711 supportsIntent.setType(mimeType);
1712
1713 // Create a set of widget configuration components that we can test against
1714 final List<AppWidgetProviderInfo> widgets =
1715 AppWidgetManager.getInstance(context).getInstalledProviders();
1716 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1717 new HashMap<ComponentName, AppWidgetProviderInfo>();
1718 for (AppWidgetProviderInfo info : widgets) {
1719 configurationComponentToWidget.put(info.configure, info);
1720 }
1721
1722 // Run through each of the intents that can handle this type of clip data, and cross
1723 // reference them with the components that are actual configuration components
1724 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1725 PackageManager.MATCH_DEFAULT_ONLY);
1726 for (ResolveInfo info : activities) {
1727 final ActivityInfo activityInfo = info.activityInfo;
1728 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1729 activityInfo.name);
1730 if (configurationComponentToWidget.containsKey(infoComponent)) {
1731 supportedConfigurationActivities.add(
1732 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1733 configurationComponentToWidget.get(infoComponent)));
1734 }
1735 }
1736 return supportedConfigurationActivities;
1737 }
1738
Winson Chunga9abd0e2010-10-27 17:18:37 -07001739 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001740 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1741 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1742 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1743
1744 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001745 boolean customIcon = false;
1746 ShortcutIconResource iconResource = null;
1747
1748 if (bitmap != null && bitmap instanceof Bitmap) {
1749 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001750 customIcon = true;
1751 } else {
1752 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1753 if (extra != null && extra instanceof ShortcutIconResource) {
1754 try {
1755 iconResource = (ShortcutIconResource) extra;
1756 final PackageManager packageManager = context.getPackageManager();
1757 Resources resources = packageManager.getResourcesForApplication(
1758 iconResource.packageName);
1759 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001760 icon = Utilities.createIconBitmap(
1761 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001762 } catch (Exception e) {
1763 Log.w(TAG, "Could not load shortcut icon: " + extra);
1764 }
1765 }
1766 }
1767
Joe Onorato0589f0f2010-02-08 13:44:00 -08001768 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001769
1770 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001771 if (fallbackIcon != null) {
1772 icon = fallbackIcon;
1773 } else {
1774 icon = getFallbackIcon();
1775 info.usingFallbackIcon = true;
1776 }
Joe Onorato56d82912010-03-07 14:32:10 -05001777 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001778 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001779
Joe Onorato0589f0f2010-02-08 13:44:00 -08001780 info.title = name;
1781 info.intent = intent;
1782 info.customIcon = customIcon;
1783 info.iconResource = iconResource;
1784
1785 return info;
1786 }
1787
Winson Chungaac01e12011-08-17 10:37:13 -07001788 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1789 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001790 // If apps can't be on SD, don't even bother.
1791 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001792 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001793 }
Joe Onorato56d82912010-03-07 14:32:10 -05001794 // If this icon doesn't have a custom icon, check to see
1795 // what's stored in the DB, and if it doesn't match what
1796 // we're going to show, store what we are going to show back
1797 // into the DB. We do this so when we're loading, if the
1798 // package manager can't find an icon (for example because
1799 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001800 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001801 cache.put(info, c.getBlob(iconIndex));
1802 return true;
1803 }
1804 return false;
1805 }
1806 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1807 boolean needSave = false;
1808 try {
1809 if (data != null) {
1810 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1811 Bitmap loaded = info.getIcon(mIconCache);
1812 needSave = !saved.sameAs(loaded);
1813 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001814 needSave = true;
1815 }
Winson Chungaac01e12011-08-17 10:37:13 -07001816 } catch (Exception e) {
1817 needSave = true;
1818 }
1819 if (needSave) {
1820 Log.d(TAG, "going to save icon bitmap for info=" + info);
1821 // This is slower than is ideal, but this only happens once
1822 // or when the app is updated with a new icon.
1823 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001824 }
1825 }
1826
Joe Onorato9c1289c2009-08-17 11:03:03 -04001827 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001828 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829 * or make a new one.
1830 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001831 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832 // See if a placeholder was created for us already
1833 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001834 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001836 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 folders.put(id, folderInfo);
1838 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001839 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001843 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 = new Comparator<ApplicationInfo>() {
1845 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001846 int result = sCollator.compare(a.title.toString(), b.title.toString());
1847 if (result == 0) {
1848 result = a.componentName.compareTo(b.componentName);
1849 }
1850 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 };
Winson Chung78403fe2011-01-21 15:38:02 -08001853 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1854 = new Comparator<ApplicationInfo>() {
1855 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1856 if (a.firstInstallTime < b.firstInstallTime) return 1;
1857 if (a.firstInstallTime > b.firstInstallTime) return -1;
1858 return 0;
1859 }
1860 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001861 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1862 = new Comparator<AppWidgetProviderInfo>() {
1863 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1864 return sCollator.compare(a.label.toString(), b.label.toString());
1865 }
1866 };
Winson Chung5308f242011-08-18 12:12:41 -07001867 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1868 if (info.activityInfo != null) {
1869 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1870 } else {
1871 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1872 }
1873 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001874 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1875 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001876 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001877 ShortcutNameComparator(PackageManager pm) {
1878 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001879 mLabelCache = new HashMap<Object, CharSequence>();
1880 }
1881 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1882 mPackageManager = pm;
1883 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001884 }
1885 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001886 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001887 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1888 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1889 if (mLabelCache.containsKey(keyA)) {
1890 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001891 } else {
1892 labelA = a.loadLabel(mPackageManager).toString();
1893
Winson Chung5308f242011-08-18 12:12:41 -07001894 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001895 }
Winson Chung5308f242011-08-18 12:12:41 -07001896 if (mLabelCache.containsKey(keyB)) {
1897 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001898 } else {
1899 labelB = b.loadLabel(mPackageManager).toString();
1900
Winson Chung5308f242011-08-18 12:12:41 -07001901 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001902 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001903 return sCollator.compare(labelA, labelB);
1904 }
1905 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001906 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1907 private PackageManager mPackageManager;
1908 private HashMap<Object, String> mLabelCache;
1909 WidgetAndShortcutNameComparator(PackageManager pm) {
1910 mPackageManager = pm;
1911 mLabelCache = new HashMap<Object, String>();
1912 }
1913 public final int compare(Object a, Object b) {
1914 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001915 if (mLabelCache.containsKey(a)) {
1916 labelA = mLabelCache.get(a);
1917 } else {
1918 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001919 ((AppWidgetProviderInfo) a).label :
1920 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001921 mLabelCache.put(a, labelA);
1922 }
1923 if (mLabelCache.containsKey(b)) {
1924 labelB = mLabelCache.get(b);
1925 } else {
1926 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001927 ((AppWidgetProviderInfo) b).label :
1928 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001929 mLabelCache.put(b, labelB);
1930 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001931 return sCollator.compare(labelA, labelB);
1932 }
1933 };
Joe Onoratobe386092009-11-17 17:32:16 -08001934
1935 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001936 Log.d(TAG, "mCallbacks=" + mCallbacks);
1937 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1938 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1939 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1940 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001941 if (mLoaderTask != null) {
1942 mLoaderTask.dumpState();
1943 } else {
1944 Log.d(TAG, "mLoaderTask=null");
1945 }
Joe Onoratobe386092009-11-17 17:32:16 -08001946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947}