blob: 6658f07c267eb536a686a82d27f6d5e07dce9914 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040022import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080024import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
26import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
37import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Winson Chung68846fd2010-10-29 11:00:27 -070047import com.android.launcher.R;
48import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080049
Michael Jurkac2f801e2011-07-12 14:19:46 -070050import java.lang.ref.WeakReference;
51import java.net.URISyntaxException;
52import java.text.Collator;
53import java.util.ArrayList;
54import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
57import java.util.List;
58import java.util.Locale;
59
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060/**
61 * Maintains in-memory state of the Launcher. It is expected that there should be only one
62 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070063 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040065public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080066 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040067 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070068
Joe Onorato36115782010-06-17 13:28:48 -040069 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080070 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040071 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040072 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040073
Joe Onoratof99f8c12009-10-31 17:27:36 -040074 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075 private final Object mLock = new Object();
76 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040077 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070079 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
80 static {
81 sWorkerThread.start();
82 }
83 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
84
Joe Onoratocc67f472010-06-08 10:54:30 -070085 // We start off with everything not loaded. After that, we assume that
86 // our monitoring of the package manager provides all updates and we never
87 // need to do a requery. These are only ever touched from the loader thread.
88 private boolean mWorkspaceLoaded;
89 private boolean mAllAppsLoaded;
90
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Michael Jurkaa8c760d2011-04-28 14:59:33 -070093 // < only access in worker thread >
94 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080095
Michael Jurkaa8c760d2011-04-28 14:59:33 -070096 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
97 // LauncherModel to their ids
98 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
99
100 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
101 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
102 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700103 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700104
105 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
106 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
107 new ArrayList<LauncherAppWidgetInfo>();
108
109 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
110 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
111 // </ only access in worker thread >
112
113 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Adam Cohend22015c2010-07-26 22:02:18 -0700116 private static int mCellCountX;
117 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700120 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 public int getCurrentWorkspaceScreen();
122 public void startBinding();
123 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500124 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 public void finishBindingItems();
126 public void bindAppWidget(LauncherAppWidgetInfo info);
127 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500128 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
129 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400130 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700131 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400132 public boolean isAllAppsVisible();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100133 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800137 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400138 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800139 mAllAppsList = new AllAppsList(iconCache);
140 mIconCache = iconCache;
141
142 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700143 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400144
145 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400146
147 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 }
149
Joe Onorato56d82912010-03-07 14:32:10 -0500150 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Adam Cohen4eac29a2011-07-11 17:53:37 -0700154 public static void unbindWorkspaceItems() {
155 for (ItemInfo item: sWorkspaceItems) {
156 item.unbind();
157 }
158 }
159
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 /**
161 * Adds an item to the DB if it was not created previously, or move it to a new
162 * <container, screen, cellX, cellY>
163 */
164 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
165 int screen, int cellX, int cellY) {
166 if (item.container == ItemInfo.NO_ID) {
167 // From all apps
168 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
169 } else {
170 // From somewhere else
171 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 }
173 }
174
175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700177 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700178 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
179 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 item.cellX = cellX;
182 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 // We store hotseat items in canonical form which is this orientation invariant position
184 // in the hotseat
185 if (context instanceof Launcher && screen < 0 &&
186 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
187 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
188 } else {
189 item.screen = screen;
190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700192 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 final ContentValues values = new ContentValues();
194 final ContentResolver cr = context.getContentResolver();
195
196 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700197 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
198 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
200
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700201 sWorker.post(new Runnable() {
202 public void run() {
203 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700204 ItemInfo modelItem = sItemsIdMap.get(item.id);
205 if (item != modelItem) {
206 // the modelItem needs to match up perfectly with item if our model is to be
207 // consistent with the database-- for now, just require modelItem == item
208 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
209 "doesn't match original");
210 }
211
212 // Items are added/removed from the corresponding FolderInfo elsewhere, such
213 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
214 // that are on the desktop, as appropriate
Winson Chung3d503fb2011-07-13 17:25:49 -0700215 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
216 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700217 if (!sWorkspaceItems.contains(modelItem)) {
218 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700219 }
220 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700221 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700222 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700223 }
224 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700225 }
226
227 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800228 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800229 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700230 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
231 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800232 item.spanX = spanX;
233 item.spanY = spanY;
234 item.cellX = cellX;
235 item.cellY = cellY;
236
237 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
238 final ContentValues values = new ContentValues();
239 final ContentResolver cr = context.getContentResolver();
240
241 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
242 values.put(LauncherSettings.Favorites.SPANX, spanX);
243 values.put(LauncherSettings.Favorites.SPANY, spanY);
244 values.put(LauncherSettings.Favorites.CELLX, cellX);
245 values.put(LauncherSettings.Favorites.CELLY, cellY);
246
247 sWorker.post(new Runnable() {
248 public void run() {
249 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700250 ItemInfo modelItem = sItemsIdMap.get(item.id);
251 if (item != modelItem) {
252 // the modelItem needs to match up perfectly with item if our model is to be
253 // consistent with the database-- for now, just require modelItem == item
254 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
255 "doesn't match original");
256 }
Adam Cohend4844c32011-02-18 19:25:06 -0800257 }
258 });
259 }
260
261 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 * Returns true if the shortcuts already exists in the database.
263 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 static boolean shortcutExists(Context context, String title, Intent intent) {
266 final ContentResolver cr = context.getContentResolver();
267 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
268 new String[] { "title", "intent" }, "title=? and intent=?",
269 new String[] { title, intent.toUri(0) }, null);
270 boolean result = false;
271 try {
272 result = c.moveToFirst();
273 } finally {
274 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700277 }
278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700280 * Returns an ItemInfo array containing all the items in the LauncherModel.
281 * The ItemInfo.id is not set through this function.
282 */
283 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
284 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
285 final ContentResolver cr = context.getContentResolver();
286 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
287 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
288 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
289 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
290
291 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
292 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
293 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
294 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
295 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
296 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
297 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
298
299 try {
300 while (c.moveToNext()) {
301 ItemInfo item = new ItemInfo();
302 item.cellX = c.getInt(cellXIndex);
303 item.cellY = c.getInt(cellYIndex);
304 item.spanX = c.getInt(spanXIndex);
305 item.spanY = c.getInt(spanYIndex);
306 item.container = c.getInt(containerIndex);
307 item.itemType = c.getInt(itemTypeIndex);
308 item.screen = c.getInt(screenIndex);
309
310 items.add(item);
311 }
312 } catch (Exception e) {
313 items.clear();
314 } finally {
315 c.close();
316 }
317
318 return items;
319 }
320
321 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400322 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
323 */
324 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
325 final ContentResolver cr = context.getContentResolver();
326 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
327 "_id=? and (itemType=? or itemType=?)",
328 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700329 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700330
Joe Onorato9c1289c2009-08-17 11:03:03 -0400331 try {
332 if (c.moveToFirst()) {
333 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
334 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
335 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
336 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
337 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
338 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339
Joe Onorato9c1289c2009-08-17 11:03:03 -0400340 FolderInfo folderInfo = null;
341 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700342 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
343 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400344 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700345 }
346
Joe Onorato9c1289c2009-08-17 11:03:03 -0400347 folderInfo.title = c.getString(titleIndex);
348 folderInfo.id = id;
349 folderInfo.container = c.getInt(containerIndex);
350 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700351 folderInfo.cellX = c.getInt(cellXIndex);
352 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400353
354 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400356 } finally {
357 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700358 }
359
360 return null;
361 }
362
Joe Onorato9c1289c2009-08-17 11:03:03 -0400363 /**
364 * Add an item to the database in a specified container. Sets the container, screen, cellX and
365 * cellY fields of the item. Also assigns an ID to the item.
366 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700367 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
368 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 item.cellX = cellX;
371 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700372 // We store hotseat items in canonical form which is this orientation invariant position
373 // in the hotseat
374 if (context instanceof Launcher && screen < 0 &&
375 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
376 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
377 } else {
378 item.screen = screen;
379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380
381 final ContentValues values = new ContentValues();
382 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 item.onAddToDatabase(values);
384
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700385 Launcher l = (Launcher) context;
386 LauncherApplication app = (LauncherApplication) l.getApplication();
387 item.id = app.getLauncherProvider().generateNewId();
388 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700389 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700390
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700391 sWorker.post(new Runnable() {
392 public void run() {
393 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
394 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700396 sItemsIdMap.put(item.id, item);
397 switch (item.itemType) {
398 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
399 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700400 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700401 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
402 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700403 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
404 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700405 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700406 }
407 break;
408 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
409 sAppWidgets.add((LauncherAppWidgetInfo) item);
410 break;
411 }
412 }
413 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700414 }
415
Joe Onorato9c1289c2009-08-17 11:03:03 -0400416 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700417 * Creates a new unique child id, for a given cell span across all layouts.
418 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700419 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700420 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
421 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700422 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700423 }
424
Adam Cohend22015c2010-07-26 22:02:18 -0700425 static int getCellCountX() {
426 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700427 }
428
Adam Cohend22015c2010-07-26 22:02:18 -0700429 static int getCellCountY() {
430 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700431 }
432
433 /**
434 * Updates the model orientation helper to take into account the current layout dimensions
435 * when performing local/canonical coordinate transformations.
436 */
437 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700438 mCellCountX = shortAxisCellCount;
439 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700440 }
441
442 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 * Update an item to the database in a specified container.
444 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700445 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446 final ContentValues values = new ContentValues();
447 final ContentResolver cr = context.getContentResolver();
448
449 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700450 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700451
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700452 sWorker.post(new Runnable() {
453 public void run() {
454 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
455 values, null, null);
456 final ItemInfo modelItem = sItemsIdMap.get(item.id);
457 if (item != modelItem) {
458 // the modelItem needs to match up perfectly with item if our model is to be
459 // consistent with the database-- for now, just require modelItem == item
460 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
461 "doesn't match original");
462 }
463 }
464 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400465 }
466
467 /**
468 * Removes the specified item from the database
469 * @param context
470 * @param item
471 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700472 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400473 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700474 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700475 sWorker.post(new Runnable() {
476 public void run() {
477 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700478 switch (item.itemType) {
479 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
480 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700481 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700482 break;
483 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
484 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700485 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700486 break;
487 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
488 sAppWidgets.remove((LauncherAppWidgetInfo) item);
489 break;
490 }
491 sItemsIdMap.remove(item.id);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700492 }
493 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400494 }
495
496 /**
497 * Remove the contents of the specified folder from the database
498 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700499 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400500 final ContentResolver cr = context.getContentResolver();
501
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700502 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700503 public void run() {
504 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700505 sItemsIdMap.remove(info.id);
506 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700507 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700508
Adam Cohen9932a9b2011-08-02 22:14:07 -0700509 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Adam Cohenafb01ee2011-06-23 15:38:03 -0700510 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700511 for (ItemInfo childInfo : info.contents) {
512 sItemsIdMap.remove(childInfo.id);
513 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700514 }
515 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 }
517
518 /**
519 * Set this as the current Launcher activity object for the loader.
520 */
521 public void initialize(Callbacks callbacks) {
522 synchronized (mLock) {
523 mCallbacks = new WeakReference<Callbacks>(callbacks);
524 }
525 }
526
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700527 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400528 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
529 * ACTION_PACKAGE_CHANGED.
530 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100531 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400532 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400533 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700534
Joe Onorato36115782010-06-17 13:28:48 -0400535 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400536
Joe Onorato36115782010-06-17 13:28:48 -0400537 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
538 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
539 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
540 final String packageName = intent.getData().getSchemeSpecificPart();
541 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542
Joe Onorato36115782010-06-17 13:28:48 -0400543 int op = PackageUpdatedTask.OP_NONE;
544
545 if (packageName == null || packageName.length() == 0) {
546 // they sent us a bad intent
547 return;
548 }
549
550 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
551 op = PackageUpdatedTask.OP_UPDATE;
552 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
553 if (!replacing) {
554 op = PackageUpdatedTask.OP_REMOVE;
555 }
556 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
557 // later, we will update the package at this time
558 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
559 if (!replacing) {
560 op = PackageUpdatedTask.OP_ADD;
561 } else {
562 op = PackageUpdatedTask.OP_UPDATE;
563 }
564 }
565
566 if (op != PackageUpdatedTask.OP_NONE) {
567 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
568 }
569
570 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400571 // First, schedule to add these apps back in.
572 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
573 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
574 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700575 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400576 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
577 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
578 enqueuePackageUpdated(new PackageUpdatedTask(
579 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700580 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
581 // If we have changed locale we need to clear out the labels in all apps.
582 // Do this here because if the launcher activity is running it will be restarted.
583 // If it's not running startLoaderFromBackground will merely tell it that it needs
584 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
585 // next time.
586 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700587 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700588 startLoaderFromBackground();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100589 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
590 Callbacks callbacks = mCallbacks.get();
591 callbacks.bindSearchablesChanged();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700592 }
593 }
594
595 /**
596 * When the launcher is in the background, it's possible for it to miss paired
597 * configuration changes. So whenever we trigger the loader from the background
598 * tell the launcher that it needs to re-run the loader when it comes back instead
599 * of doing it now.
600 */
601 public void startLoaderFromBackground() {
602 boolean runLoader = false;
603 if (mCallbacks != null) {
604 Callbacks callbacks = mCallbacks.get();
605 if (callbacks != null) {
606 // Only actually run the loader if they're not paused.
607 if (!callbacks.setLoadOnResume()) {
608 runLoader = true;
609 }
610 }
611 }
612 if (runLoader) {
613 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700614 }
Joe Onorato36115782010-06-17 13:28:48 -0400615 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400616
Joe Onorato36115782010-06-17 13:28:48 -0400617 public void startLoader(Context context, boolean isLaunching) {
618 synchronized (mLock) {
619 if (DEBUG_LOADERS) {
620 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
621 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622
Joe Onorato36115782010-06-17 13:28:48 -0400623 // Don't bother to start the thread if we know it's not going to do anything
624 if (mCallbacks != null && mCallbacks.get() != null) {
625 // If there is already one running, tell it to stop.
626 LoaderTask oldTask = mLoaderTask;
627 if (oldTask != null) {
628 if (oldTask.isLaunching()) {
629 // don't downgrade isLaunching if we're already running
630 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500631 }
Joe Onorato36115782010-06-17 13:28:48 -0400632 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500633 }
Joe Onorato36115782010-06-17 13:28:48 -0400634 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700635 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 }
638 }
639
Joe Onorato36115782010-06-17 13:28:48 -0400640 public void stopLoader() {
641 synchronized (mLock) {
642 if (mLoaderTask != null) {
643 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 }
645 }
Joe Onorato36115782010-06-17 13:28:48 -0400646 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647
Joe Onorato36115782010-06-17 13:28:48 -0400648 /**
649 * Runnable for the thread that loads the contents of the launcher:
650 * - workspace icons
651 * - widgets
652 * - all apps icons
653 */
654 private class LoaderTask implements Runnable {
655 private Context mContext;
656 private Thread mWaitThread;
657 private boolean mIsLaunching;
658 private boolean mStopped;
659 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700660 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400661
662 LoaderTask(Context context, boolean isLaunching) {
663 mContext = context;
664 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700665 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 }
667
Joe Onorato36115782010-06-17 13:28:48 -0400668 boolean isLaunching() {
669 return mIsLaunching;
670 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671
Joe Onorato36115782010-06-17 13:28:48 -0400672 private void loadAndBindWorkspace() {
673 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400674 if (DEBUG_LOADERS) {
675 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700677
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700678 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400679 loadWorkspace();
680 if (mStopped) {
681 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400682 }
Joe Onorato36115782010-06-17 13:28:48 -0400683 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684 }
685
Joe Onorato36115782010-06-17 13:28:48 -0400686 // Bind the workspace
687 bindWorkspace();
688 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400689
Joe Onorato36115782010-06-17 13:28:48 -0400690 private void waitForIdle() {
691 // Wait until the either we're stopped or the other threads are done.
692 // This way we don't start loading all apps until the workspace has settled
693 // down.
694 synchronized (LoaderTask.this) {
695 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700696
Joe Onorato36115782010-06-17 13:28:48 -0400697 mHandler.postIdle(new Runnable() {
698 public void run() {
699 synchronized (LoaderTask.this) {
700 mLoadAndBindStepFinished = true;
701 if (DEBUG_LOADERS) {
702 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400703 }
Joe Onorato36115782010-06-17 13:28:48 -0400704 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400705 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400706 }
Joe Onorato36115782010-06-17 13:28:48 -0400707 });
708
709 while (!mStopped && !mLoadAndBindStepFinished) {
710 try {
711 this.wait();
712 } catch (InterruptedException ex) {
713 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400714 }
715 }
Joe Onorato36115782010-06-17 13:28:48 -0400716 if (DEBUG_LOADERS) {
717 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700718 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400719 + "ms for previous step to finish binding");
720 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400721 }
Joe Onorato36115782010-06-17 13:28:48 -0400722 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400723
Joe Onorato36115782010-06-17 13:28:48 -0400724 public void run() {
725 // Optimize for end-user experience: if the Launcher is up and // running with the
726 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
727 // workspace first (default).
728 final Callbacks cbk = mCallbacks.get();
729 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400730
Joe Onorato36115782010-06-17 13:28:48 -0400731 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400732 // Elevate priority when Home launches for the first time to avoid
733 // starving at boot time. Staring at a blank home is not cool.
734 synchronized (mLock) {
735 android.os.Process.setThreadPriority(mIsLaunching
736 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
737 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400738 if (loadWorkspaceFirst) {
739 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
740 loadAndBindWorkspace();
741 } else {
742 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700743 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400744 }
745
Joe Onorato36115782010-06-17 13:28:48 -0400746 if (mStopped) {
747 break keep_running;
748 }
749
750 // Whew! Hard work done. Slow us down, and wait until the UI thread has
751 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400752 synchronized (mLock) {
753 if (mIsLaunching) {
754 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
755 }
756 }
Joe Onorato36115782010-06-17 13:28:48 -0400757 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400758
759 // second step
760 if (loadWorkspaceFirst) {
761 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700762 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400763 } else {
764 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
765 loadAndBindWorkspace();
766 }
Joe Onorato36115782010-06-17 13:28:48 -0400767 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768
Joe Onorato36115782010-06-17 13:28:48 -0400769 // Clear out this reference, otherwise we end up holding it until all of the
770 // callback runnables are done.
771 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772
Joe Onorato36115782010-06-17 13:28:48 -0400773 synchronized (mLock) {
774 // If we are still the last one to be scheduled, remove ourselves.
775 if (mLoaderTask == this) {
776 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400777 }
Joe Onorato36115782010-06-17 13:28:48 -0400778 }
Joe Onorato36115782010-06-17 13:28:48 -0400779 }
780
781 public void stopLocked() {
782 synchronized (LoaderTask.this) {
783 mStopped = true;
784 this.notify();
785 }
786 }
787
788 /**
789 * Gets the callbacks object. If we've been stopped, or if the launcher object
790 * has somehow been garbage collected, return null instead. Pass in the Callbacks
791 * object that was around when the deferred message was scheduled, and if there's
792 * a new Callbacks object around then also return null. This will save us from
793 * calling onto it with data that will be ignored.
794 */
795 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
796 synchronized (mLock) {
797 if (mStopped) {
798 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400799 }
Joe Onorato36115782010-06-17 13:28:48 -0400800
801 if (mCallbacks == null) {
802 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400803 }
Joe Onorato36115782010-06-17 13:28:48 -0400804
805 final Callbacks callbacks = mCallbacks.get();
806 if (callbacks != oldCallbacks) {
807 return null;
808 }
809 if (callbacks == null) {
810 Log.w(TAG, "no mCallbacks");
811 return null;
812 }
813
814 return callbacks;
815 }
816 }
817
818 // check & update map of what's occupied; used to discard overlapping/invalid items
819 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
820 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400821 return true;
822 }
Joe Onorato36115782010-06-17 13:28:48 -0400823 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
824 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
825 if (occupied[item.screen][x][y] != null) {
826 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700827 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400828 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700829 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400830 + occupied[item.screen][x][y]);
831 return false;
832 }
833 }
834 }
835 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
836 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
837 occupied[item.screen][x][y] = item;
838 }
839 }
840 return true;
841 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842
Joe Onorato36115782010-06-17 13:28:48 -0400843 private void loadWorkspace() {
844 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845
Joe Onorato36115782010-06-17 13:28:48 -0400846 final Context context = mContext;
847 final ContentResolver contentResolver = context.getContentResolver();
848 final PackageManager manager = context.getPackageManager();
849 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
850 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400851
Adam Cohen4eac29a2011-07-11 17:53:37 -0700852 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700853 sAppWidgets.clear();
854 sFolders.clear();
855 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800856
Joe Onorato36115782010-06-17 13:28:48 -0400857 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858
Joe Onorato36115782010-06-17 13:28:48 -0400859 final Cursor c = contentResolver.query(
860 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400861
Adam Cohend22015c2010-07-26 22:02:18 -0700862 final ItemInfo occupied[][][] =
863 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864
Joe Onorato36115782010-06-17 13:28:48 -0400865 try {
866 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
867 final int intentIndex = c.getColumnIndexOrThrow
868 (LauncherSettings.Favorites.INTENT);
869 final int titleIndex = c.getColumnIndexOrThrow
870 (LauncherSettings.Favorites.TITLE);
871 final int iconTypeIndex = c.getColumnIndexOrThrow(
872 LauncherSettings.Favorites.ICON_TYPE);
873 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
874 final int iconPackageIndex = c.getColumnIndexOrThrow(
875 LauncherSettings.Favorites.ICON_PACKAGE);
876 final int iconResourceIndex = c.getColumnIndexOrThrow(
877 LauncherSettings.Favorites.ICON_RESOURCE);
878 final int containerIndex = c.getColumnIndexOrThrow(
879 LauncherSettings.Favorites.CONTAINER);
880 final int itemTypeIndex = c.getColumnIndexOrThrow(
881 LauncherSettings.Favorites.ITEM_TYPE);
882 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
883 LauncherSettings.Favorites.APPWIDGET_ID);
884 final int screenIndex = c.getColumnIndexOrThrow(
885 LauncherSettings.Favorites.SCREEN);
886 final int cellXIndex = c.getColumnIndexOrThrow
887 (LauncherSettings.Favorites.CELLX);
888 final int cellYIndex = c.getColumnIndexOrThrow
889 (LauncherSettings.Favorites.CELLY);
890 final int spanXIndex = c.getColumnIndexOrThrow
891 (LauncherSettings.Favorites.SPANX);
892 final int spanYIndex = c.getColumnIndexOrThrow(
893 LauncherSettings.Favorites.SPANY);
894 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
895 final int displayModeIndex = c.getColumnIndexOrThrow(
896 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897
Joe Onorato36115782010-06-17 13:28:48 -0400898 ShortcutInfo info;
899 String intentDescription;
900 LauncherAppWidgetInfo appWidgetInfo;
901 int container;
902 long id;
903 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904
Joe Onorato36115782010-06-17 13:28:48 -0400905 while (!mStopped && c.moveToNext()) {
906 try {
907 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908
Joe Onorato36115782010-06-17 13:28:48 -0400909 switch (itemType) {
910 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
911 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
912 intentDescription = c.getString(intentIndex);
913 try {
914 intent = Intent.parseUri(intentDescription, 0);
915 } catch (URISyntaxException e) {
916 continue;
917 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918
Joe Onorato36115782010-06-17 13:28:48 -0400919 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
920 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700921 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400922 } else {
923 info = getShortcutInfo(c, context, iconTypeIndex,
924 iconPackageIndex, iconResourceIndex, iconIndex,
925 titleIndex);
926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927
Joe Onorato36115782010-06-17 13:28:48 -0400928 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400929 info.intent = intent;
930 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400932 info.container = container;
933 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700934 info.cellX = c.getInt(cellXIndex);
935 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936
Daniel Sandler8802e962010-05-26 16:28:16 -0400937 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400938 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400939 break;
940 }
941
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400943 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700945 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400946 break;
947 default:
948 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700949 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700950 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400951 folderInfo.add(info);
952 break;
953 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700954 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800955
956 // now that we've loaded everthing re-save it with the
957 // icon in case it disappears somehow.
958 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400959 } else {
960 // Failed to load the shortcut, probably because the
961 // activity manager couldn't resolve it (maybe the app
962 // was uninstalled), or the db row was somehow screwed up.
963 // Delete it.
964 id = c.getLong(idIndex);
965 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
966 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
967 id, false), null, null);
968 }
969 break;
970
Adam Cohendf2cc412011-04-27 16:56:57 -0700971 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -0400972 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700973 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400974
Winson Chungaafa03c2010-06-11 17:34:16 -0700975 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400976 folderInfo.id = id;
977 container = c.getInt(containerIndex);
978 folderInfo.container = container;
979 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700980 folderInfo.cellX = c.getInt(cellXIndex);
981 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400982
983 // check & update map of what's occupied
984 if (!checkItemPlacement(occupied, folderInfo)) {
985 break;
986 }
Joe Onorato36115782010-06-17 13:28:48 -0400987 switch (container) {
988 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -0700989 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700990 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400991 break;
992 }
993
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700994 sItemsIdMap.put(folderInfo.id, folderInfo);
995 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -0400996 break;
997
Joe Onorato36115782010-06-17 13:28:48 -0400998 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
999 // Read all Launcher-specific widget details
1000 int appWidgetId = c.getInt(appWidgetIdIndex);
1001 id = c.getLong(idIndex);
1002
1003 final AppWidgetProviderInfo provider =
1004 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001005
Joe Onorato36115782010-06-17 13:28:48 -04001006 if (!isSafeMode && (provider == null || provider.provider == null ||
1007 provider.provider.getPackageName() == null)) {
1008 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1009 + id + " appWidgetId=" + appWidgetId);
1010 itemsToRemove.add(id);
1011 } else {
1012 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
1013 appWidgetInfo.id = id;
1014 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001015 appWidgetInfo.cellX = c.getInt(cellXIndex);
1016 appWidgetInfo.cellY = c.getInt(cellYIndex);
1017 appWidgetInfo.spanX = c.getInt(spanXIndex);
1018 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001019
1020 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001021 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1022 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001023 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001024 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001025 continue;
1026 }
1027 appWidgetInfo.container = c.getInt(containerIndex);
1028
1029 // check & update map of what's occupied
1030 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1031 break;
1032 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001033 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1034 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001035 }
1036 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001037 }
Joe Onorato36115782010-06-17 13:28:48 -04001038 } catch (Exception e) {
1039 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001040 }
1041 }
Joe Onorato36115782010-06-17 13:28:48 -04001042 } finally {
1043 c.close();
1044 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001045
Joe Onorato36115782010-06-17 13:28:48 -04001046 if (itemsToRemove.size() > 0) {
1047 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1048 LauncherSettings.Favorites.CONTENT_URI);
1049 // Remove dead items
1050 for (long id : itemsToRemove) {
1051 if (DEBUG_LOADERS) {
1052 Log.d(TAG, "Removed id = " + id);
1053 }
1054 // Don't notify content observers
1055 try {
1056 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1057 null, null);
1058 } catch (RemoteException e) {
1059 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001060 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 }
1063
Joe Onorato36115782010-06-17 13:28:48 -04001064 if (DEBUG_LOADERS) {
1065 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1066 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001067 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001068 String line = "";
1069 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1070 if (s > 0) {
1071 line += " | ";
1072 }
Adam Cohend22015c2010-07-26 22:02:18 -07001073 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001074 line += ((occupied[s][x][y] != null) ? "#" : ".");
1075 }
1076 }
1077 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001078 }
Joe Onorato36115782010-06-17 13:28:48 -04001079 }
1080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081
Joe Onorato36115782010-06-17 13:28:48 -04001082 /**
1083 * Read everything out of our database.
1084 */
1085 private void bindWorkspace() {
1086 final long t = SystemClock.uptimeMillis();
1087
1088 // Don't use these two variables in any of the callback runnables.
1089 // Otherwise we hold a reference to them.
1090 final Callbacks oldCallbacks = mCallbacks.get();
1091 if (oldCallbacks == null) {
1092 // This launcher has exited and nobody bothered to tell us. Just bail.
1093 Log.w(TAG, "LoaderTask running with no launcher");
1094 return;
1095 }
1096
1097 int N;
1098 // Tell the workspace that we're about to start firing items at it
1099 mHandler.post(new Runnable() {
1100 public void run() {
1101 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1102 if (callbacks != null) {
1103 callbacks.startBinding();
1104 }
1105 }
1106 });
1107 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001108 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001109 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1110 final int start = i;
1111 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112 mHandler.post(new Runnable() {
1113 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001114 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001116 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 }
1118 }
1119 });
Joe Onorato36115782010-06-17 13:28:48 -04001120 }
1121 mHandler.post(new Runnable() {
1122 public void run() {
1123 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1124 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001125 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001126 }
1127 }
1128 });
1129 // Wait until the queue goes empty.
1130 mHandler.post(new Runnable() {
1131 public void run() {
1132 if (DEBUG_LOADERS) {
1133 Log.d(TAG, "Going to start binding widgets soon.");
1134 }
1135 }
1136 });
1137 // Bind the widgets, one at a time.
1138 // WARNING: this is calling into the workspace from the background thread,
1139 // but since getCurrentScreen() just returns the int, we should be okay. This
1140 // is just a hint for the order, and if it's wrong, we'll be okay.
1141 // TODO: instead, we should have that push the current screen into here.
1142 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001143 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001144 // once for the current screen
1145 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001146 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001147 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148 mHandler.post(new Runnable() {
1149 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001150 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001151 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001152 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 }
1154 }
1155 });
1156 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001157 }
Joe Onorato36115782010-06-17 13:28:48 -04001158 // once for the other screens
1159 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001160 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001161 if (widget.screen != currentScreen) {
1162 mHandler.post(new Runnable() {
1163 public void run() {
1164 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1165 if (callbacks != null) {
1166 callbacks.bindAppWidget(widget);
1167 }
1168 }
1169 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001170 }
1171 }
Joe Onorato36115782010-06-17 13:28:48 -04001172 // Tell the workspace that we're done.
1173 mHandler.post(new Runnable() {
1174 public void run() {
1175 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1176 if (callbacks != null) {
1177 callbacks.finishBindingItems();
1178 }
1179 }
1180 });
1181 // If we're profiling, this is the last thing in the queue.
1182 mHandler.post(new Runnable() {
1183 public void run() {
1184 if (DEBUG_LOADERS) {
1185 Log.d(TAG, "bound workspace in "
1186 + (SystemClock.uptimeMillis()-t) + "ms");
1187 }
1188 }
1189 });
1190 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001191
Joe Onorato36115782010-06-17 13:28:48 -04001192 private void loadAndBindAllApps() {
1193 if (DEBUG_LOADERS) {
1194 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1195 }
1196 if (!mAllAppsLoaded) {
1197 loadAllAppsByBatch();
1198 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001199 return;
1200 }
Joe Onorato36115782010-06-17 13:28:48 -04001201 mAllAppsLoaded = true;
1202 } else {
1203 onlyBindAllApps();
1204 }
1205 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001206
Joe Onorato36115782010-06-17 13:28:48 -04001207 private void onlyBindAllApps() {
1208 final Callbacks oldCallbacks = mCallbacks.get();
1209 if (oldCallbacks == null) {
1210 // This launcher has exited and nobody bothered to tell us. Just bail.
1211 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1212 return;
1213 }
1214
1215 // shallow copy
1216 final ArrayList<ApplicationInfo> list
1217 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1218 mHandler.post(new Runnable() {
1219 public void run() {
1220 final long t = SystemClock.uptimeMillis();
1221 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1222 if (callbacks != null) {
1223 callbacks.bindAllApplications(list);
1224 }
1225 if (DEBUG_LOADERS) {
1226 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1227 + (SystemClock.uptimeMillis()-t) + "ms");
1228 }
1229 }
1230 });
1231
1232 }
1233
1234 private void loadAllAppsByBatch() {
1235 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1236
1237 // Don't use these two variables in any of the callback runnables.
1238 // Otherwise we hold a reference to them.
1239 final Callbacks oldCallbacks = mCallbacks.get();
1240 if (oldCallbacks == null) {
1241 // This launcher has exited and nobody bothered to tell us. Just bail.
1242 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1243 return;
1244 }
1245
1246 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1247 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1248
1249 final PackageManager packageManager = mContext.getPackageManager();
1250 List<ResolveInfo> apps = null;
1251
1252 int N = Integer.MAX_VALUE;
1253
1254 int startIndex;
1255 int i=0;
1256 int batchSize = -1;
1257 while (i < N && !mStopped) {
1258 if (i == 0) {
1259 mAllAppsList.clear();
1260 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1261 apps = packageManager.queryIntentActivities(mainIntent, 0);
1262 if (DEBUG_LOADERS) {
1263 Log.d(TAG, "queryIntentActivities took "
1264 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1265 }
1266 if (apps == null) {
1267 return;
1268 }
1269 N = apps.size();
1270 if (DEBUG_LOADERS) {
1271 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1272 }
1273 if (N == 0) {
1274 // There are no apps?!?
1275 return;
1276 }
1277 if (mBatchSize == 0) {
1278 batchSize = N;
1279 } else {
1280 batchSize = mBatchSize;
1281 }
1282
1283 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1284 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001285 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001286 if (DEBUG_LOADERS) {
1287 Log.d(TAG, "sort took "
1288 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1289 }
1290 }
1291
1292 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1293
1294 startIndex = i;
1295 for (int j=0; i<N && j<batchSize; j++) {
1296 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001297 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1298 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001299 i++;
1300 }
1301
1302 final boolean first = i <= batchSize;
1303 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1304 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1305 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1306
Joe Onoratocc67f472010-06-08 10:54:30 -07001307 mHandler.post(new Runnable() {
1308 public void run() {
1309 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001310 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001311 if (first) {
1312 callbacks.bindAllApplications(added);
1313 } else {
1314 callbacks.bindAppsAdded(added);
1315 }
1316 if (DEBUG_LOADERS) {
1317 Log.d(TAG, "bound " + added.size() + " apps in "
1318 + (SystemClock.uptimeMillis() - t) + "ms");
1319 }
1320 } else {
1321 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001322 }
1323 }
1324 });
1325
Daniel Sandlerdca66122010-04-13 16:23:58 -04001326 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001327 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1328 + (SystemClock.uptimeMillis()-t2) + "ms");
1329 }
1330
1331 if (mAllAppsLoadDelay > 0 && i < N) {
1332 try {
1333 if (DEBUG_LOADERS) {
1334 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1335 }
1336 Thread.sleep(mAllAppsLoadDelay);
1337 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338 }
1339 }
1340
Joe Onorato36115782010-06-17 13:28:48 -04001341 if (DEBUG_LOADERS) {
1342 Log.d(TAG, "cached all " + N + " apps in "
1343 + (SystemClock.uptimeMillis()-t) + "ms"
1344 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001345 }
1346 }
1347
1348 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001349 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1350 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1351 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1352 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1353 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001354 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001355 }
1356 }
1357
1358 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001359 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001360 }
1361
1362 private class PackageUpdatedTask implements Runnable {
1363 int mOp;
1364 String[] mPackages;
1365
1366 public static final int OP_NONE = 0;
1367 public static final int OP_ADD = 1;
1368 public static final int OP_UPDATE = 2;
1369 public static final int OP_REMOVE = 3; // uninstlled
1370 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1371
1372
1373 public PackageUpdatedTask(int op, String[] packages) {
1374 mOp = op;
1375 mPackages = packages;
1376 }
1377
1378 public void run() {
1379 final Context context = mApp;
1380
1381 final String[] packages = mPackages;
1382 final int N = packages.length;
1383 switch (mOp) {
1384 case OP_ADD:
1385 for (int i=0; i<N; i++) {
1386 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1387 mAllAppsList.addPackage(context, packages[i]);
1388 }
1389 break;
1390 case OP_UPDATE:
1391 for (int i=0; i<N; i++) {
1392 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1393 mAllAppsList.updatePackage(context, packages[i]);
1394 }
1395 break;
1396 case OP_REMOVE:
1397 case OP_UNAVAILABLE:
1398 for (int i=0; i<N; i++) {
1399 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1400 mAllAppsList.removePackage(packages[i]);
1401 }
1402 break;
1403 }
1404
1405 ArrayList<ApplicationInfo> added = null;
1406 ArrayList<ApplicationInfo> removed = null;
1407 ArrayList<ApplicationInfo> modified = null;
1408
1409 if (mAllAppsList.added.size() > 0) {
1410 added = mAllAppsList.added;
1411 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1412 }
1413 if (mAllAppsList.removed.size() > 0) {
1414 removed = mAllAppsList.removed;
1415 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1416 for (ApplicationInfo info: removed) {
1417 mIconCache.remove(info.intent.getComponent());
1418 }
1419 }
1420 if (mAllAppsList.modified.size() > 0) {
1421 modified = mAllAppsList.modified;
1422 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1423 }
1424
1425 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1426 if (callbacks == null) {
1427 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1428 return;
1429 }
1430
1431 if (added != null) {
1432 final ArrayList<ApplicationInfo> addedFinal = added;
1433 mHandler.post(new Runnable() {
1434 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001435 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1436 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001437 callbacks.bindAppsAdded(addedFinal);
1438 }
1439 }
1440 });
1441 }
1442 if (modified != null) {
1443 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1444 mHandler.post(new Runnable() {
1445 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001446 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1447 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001448 callbacks.bindAppsUpdated(modifiedFinal);
1449 }
1450 }
1451 });
1452 }
1453 if (removed != null) {
1454 final boolean permanent = mOp != OP_UNAVAILABLE;
1455 final ArrayList<ApplicationInfo> removedFinal = removed;
1456 mHandler.post(new Runnable() {
1457 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001458 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1459 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001460 callbacks.bindAppsRemoved(removedFinal, permanent);
1461 }
1462 }
1463 });
Joe Onoratobe386092009-11-17 17:32:16 -08001464 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001465
1466 mHandler.post(new Runnable() {
1467 @Override
1468 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001469 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1470 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001471 callbacks.bindPackagesUpdated();
1472 }
1473 }
1474 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001475 }
1476 }
1477
1478 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001479 * This is called from the code that adds shortcuts from the intent receiver. This
1480 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001481 */
Joe Onorato56d82912010-03-07 14:32:10 -05001482 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001483 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001484 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485
Joe Onorato56d82912010-03-07 14:32:10 -05001486 /**
1487 * Make an ShortcutInfo object for a shortcut that is an application.
1488 *
1489 * If c is not null, then it will be used to fill in missing data like the title and icon.
1490 */
1491 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001492 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001493 Bitmap icon = null;
1494 final ShortcutInfo info = new ShortcutInfo();
1495
1496 ComponentName componentName = intent.getComponent();
1497 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001498 return null;
1499 }
1500
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001501 // TODO: See if the PackageManager knows about this case. If it doesn't
1502 // then return null & delete this.
1503
Joe Onorato56d82912010-03-07 14:32:10 -05001504 // the resource -- This may implicitly give us back the fallback icon,
1505 // but don't worry about that. All we're doing with usingFallbackIcon is
1506 // to avoid saving lots of copies of that in the database, and most apps
1507 // have icons anyway.
1508 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1509 if (resolveInfo != null) {
1510 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001511 }
Joe Onorato56d82912010-03-07 14:32:10 -05001512 // the db
1513 if (icon == null) {
1514 if (c != null) {
1515 icon = getIconFromCursor(c, iconIndex);
1516 }
1517 }
1518 // the fallback icon
1519 if (icon == null) {
1520 icon = getFallbackIcon();
1521 info.usingFallbackIcon = true;
1522 }
1523 info.setIcon(icon);
1524
1525 // from the resource
1526 if (resolveInfo != null) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001527 if (labelCache != null && labelCache.containsKey(resolveInfo)) {
1528 info.title = labelCache.get(resolveInfo);
1529 } else {
1530 info.title = resolveInfo.activityInfo.loadLabel(manager);
1531 if (labelCache != null) {
1532 labelCache.put(resolveInfo, info.title);
1533 }
1534 }
Joe Onorato56d82912010-03-07 14:32:10 -05001535 }
1536 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001538 if (c != null) {
1539 info.title = c.getString(titleIndex);
1540 }
1541 }
1542 // fall back to the class name of the activity
1543 if (info.title == null) {
1544 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001546 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1547 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001549
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001551 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001553 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001554 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1555 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556
Joe Onorato56d82912010-03-07 14:32:10 -05001557 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001558 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001559 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001561 // TODO: If there's an explicit component and we can't install that, delete it.
1562
Joe Onorato56d82912010-03-07 14:32:10 -05001563 info.title = c.getString(titleIndex);
1564
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 int iconType = c.getInt(iconTypeIndex);
1566 switch (iconType) {
1567 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1568 String packageName = c.getString(iconPackageIndex);
1569 String resourceName = c.getString(iconResourceIndex);
1570 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001571 info.customIcon = false;
1572 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001575 if (resources != null) {
1576 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001577 icon = Utilities.createIconBitmap(
1578 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001581 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
Joe Onorato56d82912010-03-07 14:32:10 -05001583 // the db
1584 if (icon == null) {
1585 icon = getIconFromCursor(c, iconIndex);
1586 }
1587 // the fallback icon
1588 if (icon == null) {
1589 icon = getFallbackIcon();
1590 info.usingFallbackIcon = true;
1591 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001592 break;
1593 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001594 icon = getIconFromCursor(c, iconIndex);
1595 if (icon == null) {
1596 icon = getFallbackIcon();
1597 info.customIcon = false;
1598 info.usingFallbackIcon = true;
1599 } else {
1600 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001601 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001602 break;
1603 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001604 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001605 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001606 info.customIcon = false;
1607 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001609 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001610 return info;
1611 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
Joe Onorato56d82912010-03-07 14:32:10 -05001613 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1614 if (false) {
1615 Log.d(TAG, "getIconFromCursor app="
1616 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1617 }
1618 byte[] data = c.getBlob(iconIndex);
1619 try {
1620 return BitmapFactory.decodeByteArray(data, 0, data.length);
1621 } catch (Exception e) {
1622 return null;
1623 }
1624 }
1625
Winson Chung3d503fb2011-07-13 17:25:49 -07001626 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1627 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001628 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001629 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001630
1631 return info;
1632 }
1633
Winson Chunga9abd0e2010-10-27 17:18:37 -07001634 /**
Winson Chung55cef262010-10-28 14:14:18 -07001635 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1636 */
1637 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1638 ComponentName component) {
1639 List<AppWidgetProviderInfo> widgets =
1640 AppWidgetManager.getInstance(context).getInstalledProviders();
1641 for (AppWidgetProviderInfo info : widgets) {
1642 if (info.provider.equals(component)) {
1643 return info;
1644 }
1645 }
1646 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001647 }
1648
Winson Chung68846fd2010-10-29 11:00:27 -07001649 /**
1650 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1651 */
1652 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1653 final PackageManager packageManager = context.getPackageManager();
1654 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1655 new ArrayList<WidgetMimeTypeHandlerData>();
1656
1657 final Intent supportsIntent =
1658 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1659 supportsIntent.setType(mimeType);
1660
1661 // Create a set of widget configuration components that we can test against
1662 final List<AppWidgetProviderInfo> widgets =
1663 AppWidgetManager.getInstance(context).getInstalledProviders();
1664 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1665 new HashMap<ComponentName, AppWidgetProviderInfo>();
1666 for (AppWidgetProviderInfo info : widgets) {
1667 configurationComponentToWidget.put(info.configure, info);
1668 }
1669
1670 // Run through each of the intents that can handle this type of clip data, and cross
1671 // reference them with the components that are actual configuration components
1672 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1673 PackageManager.MATCH_DEFAULT_ONLY);
1674 for (ResolveInfo info : activities) {
1675 final ActivityInfo activityInfo = info.activityInfo;
1676 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1677 activityInfo.name);
1678 if (configurationComponentToWidget.containsKey(infoComponent)) {
1679 supportedConfigurationActivities.add(
1680 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1681 configurationComponentToWidget.get(infoComponent)));
1682 }
1683 }
1684 return supportedConfigurationActivities;
1685 }
1686
Winson Chunga9abd0e2010-10-27 17:18:37 -07001687 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001688 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1689 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1690 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1691
1692 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001693 boolean customIcon = false;
1694 ShortcutIconResource iconResource = null;
1695
1696 if (bitmap != null && bitmap instanceof Bitmap) {
1697 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001698 customIcon = true;
1699 } else {
1700 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1701 if (extra != null && extra instanceof ShortcutIconResource) {
1702 try {
1703 iconResource = (ShortcutIconResource) extra;
1704 final PackageManager packageManager = context.getPackageManager();
1705 Resources resources = packageManager.getResourcesForApplication(
1706 iconResource.packageName);
1707 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001708 icon = Utilities.createIconBitmap(
1709 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001710 } catch (Exception e) {
1711 Log.w(TAG, "Could not load shortcut icon: " + extra);
1712 }
1713 }
1714 }
1715
Joe Onorato0589f0f2010-02-08 13:44:00 -08001716 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001717
1718 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001719 if (fallbackIcon != null) {
1720 icon = fallbackIcon;
1721 } else {
1722 icon = getFallbackIcon();
1723 info.usingFallbackIcon = true;
1724 }
Joe Onorato56d82912010-03-07 14:32:10 -05001725 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001726 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001727
Joe Onorato0589f0f2010-02-08 13:44:00 -08001728 info.title = name;
1729 info.intent = intent;
1730 info.customIcon = customIcon;
1731 info.iconResource = iconResource;
1732
1733 return info;
1734 }
1735
Joe Onorato56d82912010-03-07 14:32:10 -05001736 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001737 // If apps can't be on SD, don't even bother.
1738 if (!mAppsCanBeOnExternalStorage) {
1739 return;
1740 }
Joe Onorato56d82912010-03-07 14:32:10 -05001741 // If this icon doesn't have a custom icon, check to see
1742 // what's stored in the DB, and if it doesn't match what
1743 // we're going to show, store what we are going to show back
1744 // into the DB. We do this so when we're loading, if the
1745 // package manager can't find an icon (for example because
1746 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001747 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001748 boolean needSave;
1749 byte[] data = c.getBlob(iconIndex);
1750 try {
1751 if (data != null) {
1752 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1753 Bitmap loaded = info.getIcon(mIconCache);
1754 needSave = !saved.sameAs(loaded);
1755 } else {
1756 needSave = true;
1757 }
1758 } catch (Exception e) {
1759 needSave = true;
1760 }
1761 if (needSave) {
1762 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001763 // This is slower than is ideal, but this only happens once
1764 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001765 updateItemInDatabase(context, info);
1766 }
1767 }
1768 }
1769
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001771 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772 * or make a new one.
1773 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001774 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 // See if a placeholder was created for us already
1776 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001777 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001779 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 folders.put(id, folderInfo);
1781 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001782 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
1784
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001786 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787 = new Comparator<ApplicationInfo>() {
1788 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001789 int result = sCollator.compare(a.title.toString(), b.title.toString());
1790 if (result == 0) {
1791 result = a.componentName.compareTo(b.componentName);
1792 }
1793 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795 };
Winson Chung78403fe2011-01-21 15:38:02 -08001796 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1797 = new Comparator<ApplicationInfo>() {
1798 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1799 if (a.firstInstallTime < b.firstInstallTime) return 1;
1800 if (a.firstInstallTime > b.firstInstallTime) return -1;
1801 return 0;
1802 }
1803 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001804 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1805 = new Comparator<AppWidgetProviderInfo>() {
1806 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1807 return sCollator.compare(a.label.toString(), b.label.toString());
1808 }
1809 };
1810 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1811 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001812 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001813 ShortcutNameComparator(PackageManager pm) {
1814 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001815 mLabelCache = new HashMap<Object, CharSequence>();
1816 }
1817 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1818 mPackageManager = pm;
1819 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001820 }
1821 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001822 CharSequence labelA, labelB;
1823 if (mLabelCache.containsKey(a)) {
1824 labelA = mLabelCache.get(a);
1825 } else {
1826 labelA = a.loadLabel(mPackageManager).toString();
1827
1828 mLabelCache.put(a, labelA);
1829 }
1830 if (mLabelCache.containsKey(b)) {
1831 labelB = mLabelCache.get(b);
1832 } else {
1833 labelB = b.loadLabel(mPackageManager).toString();
1834
1835 mLabelCache.put(b, labelB);
1836 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001837 return sCollator.compare(labelA, labelB);
1838 }
1839 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001840 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1841 private PackageManager mPackageManager;
1842 private HashMap<Object, String> mLabelCache;
1843 WidgetAndShortcutNameComparator(PackageManager pm) {
1844 mPackageManager = pm;
1845 mLabelCache = new HashMap<Object, String>();
1846 }
1847 public final int compare(Object a, Object b) {
1848 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001849 if (mLabelCache.containsKey(a)) {
1850 labelA = mLabelCache.get(a);
1851 } else {
1852 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001853 ((AppWidgetProviderInfo) a).label :
1854 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001855 mLabelCache.put(a, labelA);
1856 }
1857 if (mLabelCache.containsKey(b)) {
1858 labelB = mLabelCache.get(b);
1859 } else {
1860 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001861 ((AppWidgetProviderInfo) b).label :
1862 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001863 mLabelCache.put(b, labelB);
1864 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001865 return sCollator.compare(labelA, labelB);
1866 }
1867 };
Joe Onoratobe386092009-11-17 17:32:16 -08001868
1869 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001870 Log.d(TAG, "mCallbacks=" + mCallbacks);
1871 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1872 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1873 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1874 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001875 if (mLoaderTask != null) {
1876 mLoaderTask.dumpState();
1877 } else {
1878 Log.d(TAG, "mLoaderTask=null");
1879 }
Joe Onoratobe386092009-11-17 17:32:16 -08001880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881}