blob: b7f92ca47f1c076c5e3fd012acdab7bd81660f8c [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 Chungb1094bd2011-08-24 16:14:08 -0700212 Log.e(TAG, "item: " + ((item != null) ? item.toString() : "null"));
213 Log.e(TAG, "modelItem: " + ((modelItem != null) ? modelItem.toString() :
214 "null"));
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700215 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
216 "doesn't match original");
217 }
218
219 // Items are added/removed from the corresponding FolderInfo elsewhere, such
220 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
221 // that are on the desktop, as appropriate
Winson Chung3d503fb2011-07-13 17:25:49 -0700222 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
223 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700224 if (!sWorkspaceItems.contains(modelItem)) {
225 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700226 }
227 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700228 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700229 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700230 }
231 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700232 }
233
234 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800235 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800236 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700237 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
238 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800239 item.spanX = spanX;
240 item.spanY = spanY;
241 item.cellX = cellX;
242 item.cellY = cellY;
243
244 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
245 final ContentValues values = new ContentValues();
246 final ContentResolver cr = context.getContentResolver();
247
248 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
249 values.put(LauncherSettings.Favorites.SPANX, spanX);
250 values.put(LauncherSettings.Favorites.SPANY, spanY);
251 values.put(LauncherSettings.Favorites.CELLX, cellX);
252 values.put(LauncherSettings.Favorites.CELLY, cellY);
253
254 sWorker.post(new Runnable() {
255 public void run() {
256 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700257 ItemInfo modelItem = sItemsIdMap.get(item.id);
258 if (item != modelItem) {
259 // the modelItem needs to match up perfectly with item if our model is to be
260 // consistent with the database-- for now, just require modelItem == item
Winson Chungb1094bd2011-08-24 16:14:08 -0700261 Log.e(TAG, "item: " + ((item != null) ? item.toString() : "null"));
262 Log.e(TAG, "modelItem: " + ((modelItem != null) ? modelItem.toString() :
263 "null"));
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700264 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
265 "doesn't match original");
266 }
Adam Cohend4844c32011-02-18 19:25:06 -0800267 }
268 });
269 }
270
271 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 * Returns true if the shortcuts already exists in the database.
273 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 static boolean shortcutExists(Context context, String title, Intent intent) {
276 final ContentResolver cr = context.getContentResolver();
277 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
278 new String[] { "title", "intent" }, "title=? and intent=?",
279 new String[] { title, intent.toUri(0) }, null);
280 boolean result = false;
281 try {
282 result = c.moveToFirst();
283 } finally {
284 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700287 }
288
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700290 * Returns an ItemInfo array containing all the items in the LauncherModel.
291 * The ItemInfo.id is not set through this function.
292 */
293 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
294 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
295 final ContentResolver cr = context.getContentResolver();
296 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
297 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
298 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
299 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
300
301 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
302 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
303 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
304 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
305 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
306 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
307 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
308
309 try {
310 while (c.moveToNext()) {
311 ItemInfo item = new ItemInfo();
312 item.cellX = c.getInt(cellXIndex);
313 item.cellY = c.getInt(cellYIndex);
314 item.spanX = c.getInt(spanXIndex);
315 item.spanY = c.getInt(spanYIndex);
316 item.container = c.getInt(containerIndex);
317 item.itemType = c.getInt(itemTypeIndex);
318 item.screen = c.getInt(screenIndex);
319
320 items.add(item);
321 }
322 } catch (Exception e) {
323 items.clear();
324 } finally {
325 c.close();
326 }
327
328 return items;
329 }
330
331 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400332 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
333 */
334 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
335 final ContentResolver cr = context.getContentResolver();
336 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
337 "_id=? and (itemType=? or itemType=?)",
338 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700339 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700340
Joe Onorato9c1289c2009-08-17 11:03:03 -0400341 try {
342 if (c.moveToFirst()) {
343 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
344 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
345 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
346 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
347 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
348 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349
Joe Onorato9c1289c2009-08-17 11:03:03 -0400350 FolderInfo folderInfo = null;
351 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700352 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
353 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400354 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700355 }
356
Joe Onorato9c1289c2009-08-17 11:03:03 -0400357 folderInfo.title = c.getString(titleIndex);
358 folderInfo.id = id;
359 folderInfo.container = c.getInt(containerIndex);
360 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700361 folderInfo.cellX = c.getInt(cellXIndex);
362 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400363
364 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700365 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 } finally {
367 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700368 }
369
370 return null;
371 }
372
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 /**
374 * Add an item to the database in a specified container. Sets the container, screen, cellX and
375 * cellY fields of the item. Also assigns an ID to the item.
376 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700377 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
378 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 item.cellX = cellX;
381 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700382 // We store hotseat items in canonical form which is this orientation invariant position
383 // in the hotseat
384 if (context instanceof Launcher && screen < 0 &&
385 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
386 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
387 } else {
388 item.screen = screen;
389 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390
391 final ContentValues values = new ContentValues();
392 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400393 item.onAddToDatabase(values);
394
Michael Jurka7578ec62011-08-03 14:11:54 -0700395 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700396 item.id = app.getLauncherProvider().generateNewId();
397 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700398 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700399
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700400 sWorker.post(new Runnable() {
401 public void run() {
402 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
403 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400404
Winson Chungb1094bd2011-08-24 16:14:08 -0700405 if (sItemsIdMap.containsKey(item.id)) {
406 // we should not be adding new items in the db with the same id
407 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
408 "addItemToDatabase already exists." + item.toString());
409 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700410 sItemsIdMap.put(item.id, item);
411 switch (item.itemType) {
412 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
413 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700414 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700415 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
416 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700417 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
418 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700419 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700420 }
421 break;
422 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
423 sAppWidgets.add((LauncherAppWidgetInfo) item);
424 break;
425 }
426 }
427 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700428 }
429
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700431 * Creates a new unique child id, for a given cell span across all layouts.
432 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700433 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700434 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
435 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700436 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700437 }
438
Adam Cohend22015c2010-07-26 22:02:18 -0700439 static int getCellCountX() {
440 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700441 }
442
Adam Cohend22015c2010-07-26 22:02:18 -0700443 static int getCellCountY() {
444 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700445 }
446
447 /**
448 * Updates the model orientation helper to take into account the current layout dimensions
449 * when performing local/canonical coordinate transformations.
450 */
451 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700452 mCellCountX = shortAxisCellCount;
453 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700454 }
455
456 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 * Update an item to the database in a specified container.
458 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700459 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400460 final ContentValues values = new ContentValues();
461 final ContentResolver cr = context.getContentResolver();
462
463 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700464 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700465
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700466 sWorker.post(new Runnable() {
467 public void run() {
468 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
469 values, null, null);
470 final ItemInfo modelItem = sItemsIdMap.get(item.id);
471 if (item != modelItem) {
472 // the modelItem needs to match up perfectly with item if our model is to be
473 // consistent with the database-- for now, just require modelItem == item
Winson Chungb1094bd2011-08-24 16:14:08 -0700474 Log.e(TAG, "item: " + ((item != null) ? item.toString() : "null"));
475 Log.e(TAG, "modelItem: " + ((modelItem != null) ? modelItem.toString() :
476 "null"));
477 throw new RuntimeException("Error: ItemInfo passed to updateItemInDatabase " +
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700478 "doesn't match original");
479 }
480 }
481 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400482 }
483
484 /**
485 * Removes the specified item from the database
486 * @param context
487 * @param item
488 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700489 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400490 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700491 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700492 sWorker.post(new Runnable() {
493 public void run() {
494 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700495 switch (item.itemType) {
496 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
497 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700498 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700499 break;
500 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
501 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700502 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700503 break;
504 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
505 sAppWidgets.remove((LauncherAppWidgetInfo) item);
506 break;
507 }
508 sItemsIdMap.remove(item.id);
Winson Chungb1094bd2011-08-24 16:14:08 -0700509 sDbIconCache.remove(item);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700510 }
511 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 }
513
514 /**
515 * Remove the contents of the specified folder from the database
516 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700517 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400518 final ContentResolver cr = context.getContentResolver();
519
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700520 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700521 public void run() {
522 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700523 sItemsIdMap.remove(info.id);
524 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700525 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700526
Adam Cohen9932a9b2011-08-02 22:14:07 -0700527 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Adam Cohenafb01ee2011-06-23 15:38:03 -0700528 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700529 for (ItemInfo childInfo : info.contents) {
530 sItemsIdMap.remove(childInfo.id);
531 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700532 }
533 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400534 }
535
536 /**
537 * Set this as the current Launcher activity object for the loader.
538 */
539 public void initialize(Callbacks callbacks) {
540 synchronized (mLock) {
541 mCallbacks = new WeakReference<Callbacks>(callbacks);
542 }
543 }
544
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700545 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
547 * ACTION_PACKAGE_CHANGED.
548 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100549 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400550 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400551 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700552
Joe Onorato36115782010-06-17 13:28:48 -0400553 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400554
Joe Onorato36115782010-06-17 13:28:48 -0400555 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
556 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
557 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
558 final String packageName = intent.getData().getSchemeSpecificPart();
559 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560
Joe Onorato36115782010-06-17 13:28:48 -0400561 int op = PackageUpdatedTask.OP_NONE;
562
563 if (packageName == null || packageName.length() == 0) {
564 // they sent us a bad intent
565 return;
566 }
567
568 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
569 op = PackageUpdatedTask.OP_UPDATE;
570 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
571 if (!replacing) {
572 op = PackageUpdatedTask.OP_REMOVE;
573 }
574 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
575 // later, we will update the package at this time
576 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
577 if (!replacing) {
578 op = PackageUpdatedTask.OP_ADD;
579 } else {
580 op = PackageUpdatedTask.OP_UPDATE;
581 }
582 }
583
584 if (op != PackageUpdatedTask.OP_NONE) {
585 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
586 }
587
588 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400589 // First, schedule to add these apps back in.
590 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
591 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
592 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700593 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400594 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
595 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
596 enqueuePackageUpdated(new PackageUpdatedTask(
597 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700598 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
599 // If we have changed locale we need to clear out the labels in all apps.
600 // Do this here because if the launcher activity is running it will be restarted.
601 // If it's not running startLoaderFromBackground will merely tell it that it needs
602 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
603 // next time.
604 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700605 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700606 startLoaderFromBackground();
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700607 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
608 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100609 Callbacks callbacks = mCallbacks.get();
610 callbacks.bindSearchablesChanged();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700611 }
612 }
613
614 /**
615 * When the launcher is in the background, it's possible for it to miss paired
616 * configuration changes. So whenever we trigger the loader from the background
617 * tell the launcher that it needs to re-run the loader when it comes back instead
618 * of doing it now.
619 */
620 public void startLoaderFromBackground() {
621 boolean runLoader = false;
622 if (mCallbacks != null) {
623 Callbacks callbacks = mCallbacks.get();
624 if (callbacks != null) {
625 // Only actually run the loader if they're not paused.
626 if (!callbacks.setLoadOnResume()) {
627 runLoader = true;
628 }
629 }
630 }
631 if (runLoader) {
632 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700633 }
Joe Onorato36115782010-06-17 13:28:48 -0400634 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400635
Joe Onorato36115782010-06-17 13:28:48 -0400636 public void startLoader(Context context, boolean isLaunching) {
637 synchronized (mLock) {
638 if (DEBUG_LOADERS) {
639 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
640 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641
Joe Onorato36115782010-06-17 13:28:48 -0400642 // Don't bother to start the thread if we know it's not going to do anything
643 if (mCallbacks != null && mCallbacks.get() != null) {
644 // If there is already one running, tell it to stop.
645 LoaderTask oldTask = mLoaderTask;
646 if (oldTask != null) {
647 if (oldTask.isLaunching()) {
648 // don't downgrade isLaunching if we're already running
649 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500650 }
Joe Onorato36115782010-06-17 13:28:48 -0400651 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500652 }
Joe Onorato36115782010-06-17 13:28:48 -0400653 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700654 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 }
657 }
658
Joe Onorato36115782010-06-17 13:28:48 -0400659 public void stopLoader() {
660 synchronized (mLock) {
661 if (mLoaderTask != null) {
662 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663 }
664 }
Joe Onorato36115782010-06-17 13:28:48 -0400665 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666
Michael Jurkac57b7a82011-08-09 22:02:20 -0700667 public boolean isAllAppsLoaded() {
668 return mAllAppsLoaded;
669 }
670
Joe Onorato36115782010-06-17 13:28:48 -0400671 /**
672 * Runnable for the thread that loads the contents of the launcher:
673 * - workspace icons
674 * - widgets
675 * - all apps icons
676 */
677 private class LoaderTask implements Runnable {
678 private Context mContext;
679 private Thread mWaitThread;
680 private boolean mIsLaunching;
681 private boolean mStopped;
682 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700683 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400684
685 LoaderTask(Context context, boolean isLaunching) {
686 mContext = context;
687 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700688 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400689 }
690
Joe Onorato36115782010-06-17 13:28:48 -0400691 boolean isLaunching() {
692 return mIsLaunching;
693 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694
Joe Onorato36115782010-06-17 13:28:48 -0400695 private void loadAndBindWorkspace() {
696 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400697 if (DEBUG_LOADERS) {
698 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400699 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700700
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700701 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400702 loadWorkspace();
703 if (mStopped) {
704 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705 }
Joe Onorato36115782010-06-17 13:28:48 -0400706 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 }
708
Joe Onorato36115782010-06-17 13:28:48 -0400709 // Bind the workspace
710 bindWorkspace();
711 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400712
Joe Onorato36115782010-06-17 13:28:48 -0400713 private void waitForIdle() {
714 // Wait until the either we're stopped or the other threads are done.
715 // This way we don't start loading all apps until the workspace has settled
716 // down.
717 synchronized (LoaderTask.this) {
718 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700719
Joe Onorato36115782010-06-17 13:28:48 -0400720 mHandler.postIdle(new Runnable() {
721 public void run() {
722 synchronized (LoaderTask.this) {
723 mLoadAndBindStepFinished = true;
724 if (DEBUG_LOADERS) {
725 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400726 }
Joe Onorato36115782010-06-17 13:28:48 -0400727 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400728 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400729 }
Joe Onorato36115782010-06-17 13:28:48 -0400730 });
731
732 while (!mStopped && !mLoadAndBindStepFinished) {
733 try {
734 this.wait();
735 } catch (InterruptedException ex) {
736 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400737 }
738 }
Joe Onorato36115782010-06-17 13:28:48 -0400739 if (DEBUG_LOADERS) {
740 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700741 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400742 + "ms for previous step to finish binding");
743 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400744 }
Joe Onorato36115782010-06-17 13:28:48 -0400745 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400746
Joe Onorato36115782010-06-17 13:28:48 -0400747 public void run() {
748 // Optimize for end-user experience: if the Launcher is up and // running with the
749 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
750 // workspace first (default).
751 final Callbacks cbk = mCallbacks.get();
752 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400753
Joe Onorato36115782010-06-17 13:28:48 -0400754 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400755 // Elevate priority when Home launches for the first time to avoid
756 // starving at boot time. Staring at a blank home is not cool.
757 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700758 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
759 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400760 android.os.Process.setThreadPriority(mIsLaunching
761 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
762 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400763 if (loadWorkspaceFirst) {
764 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
765 loadAndBindWorkspace();
766 } else {
767 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700768 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400769 }
770
Joe Onorato36115782010-06-17 13:28:48 -0400771 if (mStopped) {
772 break keep_running;
773 }
774
775 // Whew! Hard work done. Slow us down, and wait until the UI thread has
776 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400777 synchronized (mLock) {
778 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700779 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400780 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
781 }
782 }
Joe Onorato36115782010-06-17 13:28:48 -0400783 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400784
785 // second step
786 if (loadWorkspaceFirst) {
787 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700788 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400789 } else {
790 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
791 loadAndBindWorkspace();
792 }
Joe Onorato36115782010-06-17 13:28:48 -0400793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794
Winson Chungaac01e12011-08-17 10:37:13 -0700795
796 // Update the saved icons if necessary
797 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700798 for (Object key : sDbIconCache.keySet()) {
799 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700800 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700801 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700802
Joe Onorato36115782010-06-17 13:28:48 -0400803 // Clear out this reference, otherwise we end up holding it until all of the
804 // callback runnables are done.
805 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400806
Joe Onorato36115782010-06-17 13:28:48 -0400807 synchronized (mLock) {
808 // If we are still the last one to be scheduled, remove ourselves.
809 if (mLoaderTask == this) {
810 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811 }
Joe Onorato36115782010-06-17 13:28:48 -0400812 }
Joe Onorato36115782010-06-17 13:28:48 -0400813 }
814
815 public void stopLocked() {
816 synchronized (LoaderTask.this) {
817 mStopped = true;
818 this.notify();
819 }
820 }
821
822 /**
823 * Gets the callbacks object. If we've been stopped, or if the launcher object
824 * has somehow been garbage collected, return null instead. Pass in the Callbacks
825 * object that was around when the deferred message was scheduled, and if there's
826 * a new Callbacks object around then also return null. This will save us from
827 * calling onto it with data that will be ignored.
828 */
829 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
830 synchronized (mLock) {
831 if (mStopped) {
832 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400833 }
Joe Onorato36115782010-06-17 13:28:48 -0400834
835 if (mCallbacks == null) {
836 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400837 }
Joe Onorato36115782010-06-17 13:28:48 -0400838
839 final Callbacks callbacks = mCallbacks.get();
840 if (callbacks != oldCallbacks) {
841 return null;
842 }
843 if (callbacks == null) {
844 Log.w(TAG, "no mCallbacks");
845 return null;
846 }
847
848 return callbacks;
849 }
850 }
851
852 // check & update map of what's occupied; used to discard overlapping/invalid items
853 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700854 int containerIndex = item.screen;
855 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
856 // We use the last index to refer to the hotseat
857 containerIndex = Launcher.SCREEN_COUNT;
858 // Return early if we detect that an item is under the hotseat button
859 if (Hotseat.isAllAppsButtonRank(item.screen)) {
860 return false;
861 }
862 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
863 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400864 return true;
865 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700866
Joe Onorato36115782010-06-17 13:28:48 -0400867 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
868 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700869 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400870 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700871 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400872 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700873 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700874 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400875 return false;
876 }
877 }
878 }
879 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
880 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700881 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400882 }
883 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700884
Joe Onorato36115782010-06-17 13:28:48 -0400885 return true;
886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887
Joe Onorato36115782010-06-17 13:28:48 -0400888 private void loadWorkspace() {
889 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890
Joe Onorato36115782010-06-17 13:28:48 -0400891 final Context context = mContext;
892 final ContentResolver contentResolver = context.getContentResolver();
893 final PackageManager manager = context.getPackageManager();
894 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
895 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400896
Adam Cohen4eac29a2011-07-11 17:53:37 -0700897 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700898 sAppWidgets.clear();
899 sFolders.clear();
900 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700901 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800902
Joe Onorato36115782010-06-17 13:28:48 -0400903 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904
Joe Onorato36115782010-06-17 13:28:48 -0400905 final Cursor c = contentResolver.query(
906 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400907
Winson Chungf30ad5f2011-08-08 10:55:42 -0700908 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700909 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700910 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911
Joe Onorato36115782010-06-17 13:28:48 -0400912 try {
913 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
914 final int intentIndex = c.getColumnIndexOrThrow
915 (LauncherSettings.Favorites.INTENT);
916 final int titleIndex = c.getColumnIndexOrThrow
917 (LauncherSettings.Favorites.TITLE);
918 final int iconTypeIndex = c.getColumnIndexOrThrow(
919 LauncherSettings.Favorites.ICON_TYPE);
920 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
921 final int iconPackageIndex = c.getColumnIndexOrThrow(
922 LauncherSettings.Favorites.ICON_PACKAGE);
923 final int iconResourceIndex = c.getColumnIndexOrThrow(
924 LauncherSettings.Favorites.ICON_RESOURCE);
925 final int containerIndex = c.getColumnIndexOrThrow(
926 LauncherSettings.Favorites.CONTAINER);
927 final int itemTypeIndex = c.getColumnIndexOrThrow(
928 LauncherSettings.Favorites.ITEM_TYPE);
929 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
930 LauncherSettings.Favorites.APPWIDGET_ID);
931 final int screenIndex = c.getColumnIndexOrThrow(
932 LauncherSettings.Favorites.SCREEN);
933 final int cellXIndex = c.getColumnIndexOrThrow
934 (LauncherSettings.Favorites.CELLX);
935 final int cellYIndex = c.getColumnIndexOrThrow
936 (LauncherSettings.Favorites.CELLY);
937 final int spanXIndex = c.getColumnIndexOrThrow
938 (LauncherSettings.Favorites.SPANX);
939 final int spanYIndex = c.getColumnIndexOrThrow(
940 LauncherSettings.Favorites.SPANY);
941 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
942 final int displayModeIndex = c.getColumnIndexOrThrow(
943 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944
Joe Onorato36115782010-06-17 13:28:48 -0400945 ShortcutInfo info;
946 String intentDescription;
947 LauncherAppWidgetInfo appWidgetInfo;
948 int container;
949 long id;
950 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951
Joe Onorato36115782010-06-17 13:28:48 -0400952 while (!mStopped && c.moveToNext()) {
953 try {
954 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955
Joe Onorato36115782010-06-17 13:28:48 -0400956 switch (itemType) {
957 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
958 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
959 intentDescription = c.getString(intentIndex);
960 try {
961 intent = Intent.parseUri(intentDescription, 0);
962 } catch (URISyntaxException e) {
963 continue;
964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965
Joe Onorato36115782010-06-17 13:28:48 -0400966 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
967 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700968 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400969 } else {
970 info = getShortcutInfo(c, context, iconTypeIndex,
971 iconPackageIndex, iconResourceIndex, iconIndex,
972 titleIndex);
973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974
Joe Onorato36115782010-06-17 13:28:48 -0400975 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400976 info.intent = intent;
977 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400979 info.container = container;
980 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700981 info.cellX = c.getInt(cellXIndex);
982 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983
Daniel Sandler8802e962010-05-26 16:28:16 -0400984 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400985 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400986 break;
987 }
988
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400990 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -0700991 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700992 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400993 break;
994 default:
995 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700996 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700997 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400998 folderInfo.add(info);
999 break;
1000 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001001 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001002
1003 // now that we've loaded everthing re-save it with the
1004 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001005 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001006 } else {
1007 // Failed to load the shortcut, probably because the
1008 // activity manager couldn't resolve it (maybe the app
1009 // was uninstalled), or the db row was somehow screwed up.
1010 // Delete it.
1011 id = c.getLong(idIndex);
1012 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1013 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1014 id, false), null, null);
1015 }
1016 break;
1017
Adam Cohendf2cc412011-04-27 16:56:57 -07001018 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001019 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001020 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001021
Winson Chungaafa03c2010-06-11 17:34:16 -07001022 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001023 folderInfo.id = id;
1024 container = c.getInt(containerIndex);
1025 folderInfo.container = container;
1026 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001027 folderInfo.cellX = c.getInt(cellXIndex);
1028 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001029
1030 // check & update map of what's occupied
1031 if (!checkItemPlacement(occupied, folderInfo)) {
1032 break;
1033 }
Joe Onorato36115782010-06-17 13:28:48 -04001034 switch (container) {
1035 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001036 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001037 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001038 break;
1039 }
1040
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001041 sItemsIdMap.put(folderInfo.id, folderInfo);
1042 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001043 break;
1044
Joe Onorato36115782010-06-17 13:28:48 -04001045 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1046 // Read all Launcher-specific widget details
1047 int appWidgetId = c.getInt(appWidgetIdIndex);
1048 id = c.getLong(idIndex);
1049
1050 final AppWidgetProviderInfo provider =
1051 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001052
Joe Onorato36115782010-06-17 13:28:48 -04001053 if (!isSafeMode && (provider == null || provider.provider == null ||
1054 provider.provider.getPackageName() == null)) {
1055 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1056 + id + " appWidgetId=" + appWidgetId);
1057 itemsToRemove.add(id);
1058 } else {
1059 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
1060 appWidgetInfo.id = id;
1061 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001062 appWidgetInfo.cellX = c.getInt(cellXIndex);
1063 appWidgetInfo.cellY = c.getInt(cellYIndex);
1064 appWidgetInfo.spanX = c.getInt(spanXIndex);
1065 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001066
1067 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1069 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001070 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001072 continue;
1073 }
1074 appWidgetInfo.container = c.getInt(containerIndex);
1075
1076 // check & update map of what's occupied
1077 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1078 break;
1079 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001080 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1081 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001082 }
1083 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001084 }
Joe Onorato36115782010-06-17 13:28:48 -04001085 } catch (Exception e) {
1086 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001087 }
1088 }
Joe Onorato36115782010-06-17 13:28:48 -04001089 } finally {
1090 c.close();
1091 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001092
Joe Onorato36115782010-06-17 13:28:48 -04001093 if (itemsToRemove.size() > 0) {
1094 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1095 LauncherSettings.Favorites.CONTENT_URI);
1096 // Remove dead items
1097 for (long id : itemsToRemove) {
1098 if (DEBUG_LOADERS) {
1099 Log.d(TAG, "Removed id = " + id);
1100 }
1101 // Don't notify content observers
1102 try {
1103 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1104 null, null);
1105 } catch (RemoteException e) {
1106 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001107 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001108 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001109 }
1110
Joe Onorato36115782010-06-17 13:28:48 -04001111 if (DEBUG_LOADERS) {
1112 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1113 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001114 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001115 String line = "";
1116 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1117 if (s > 0) {
1118 line += " | ";
1119 }
Adam Cohend22015c2010-07-26 22:02:18 -07001120 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001121 line += ((occupied[s][x][y] != null) ? "#" : ".");
1122 }
1123 }
1124 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 }
Joe Onorato36115782010-06-17 13:28:48 -04001126 }
1127 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128
Joe Onorato36115782010-06-17 13:28:48 -04001129 /**
1130 * Read everything out of our database.
1131 */
1132 private void bindWorkspace() {
1133 final long t = SystemClock.uptimeMillis();
1134
1135 // Don't use these two variables in any of the callback runnables.
1136 // Otherwise we hold a reference to them.
1137 final Callbacks oldCallbacks = mCallbacks.get();
1138 if (oldCallbacks == null) {
1139 // This launcher has exited and nobody bothered to tell us. Just bail.
1140 Log.w(TAG, "LoaderTask running with no launcher");
1141 return;
1142 }
1143
1144 int N;
1145 // Tell the workspace that we're about to start firing items at it
1146 mHandler.post(new Runnable() {
1147 public void run() {
1148 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1149 if (callbacks != null) {
1150 callbacks.startBinding();
1151 }
1152 }
1153 });
1154 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001155 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001156 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1157 final int start = i;
1158 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159 mHandler.post(new Runnable() {
1160 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001161 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001163 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001164 }
1165 }
1166 });
Joe Onorato36115782010-06-17 13:28:48 -04001167 }
1168 mHandler.post(new Runnable() {
1169 public void run() {
1170 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1171 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001172 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001173 }
1174 }
1175 });
1176 // Wait until the queue goes empty.
1177 mHandler.post(new Runnable() {
1178 public void run() {
1179 if (DEBUG_LOADERS) {
1180 Log.d(TAG, "Going to start binding widgets soon.");
1181 }
1182 }
1183 });
1184 // Bind the widgets, one at a time.
1185 // WARNING: this is calling into the workspace from the background thread,
1186 // but since getCurrentScreen() just returns the int, we should be okay. This
1187 // is just a hint for the order, and if it's wrong, we'll be okay.
1188 // TODO: instead, we should have that push the current screen into here.
1189 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001190 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001191 // once for the current screen
1192 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001193 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001194 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 mHandler.post(new Runnable() {
1196 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001197 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001199 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 }
1201 }
1202 });
1203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 }
Joe Onorato36115782010-06-17 13:28:48 -04001205 // once for the other screens
1206 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001207 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001208 if (widget.screen != currentScreen) {
1209 mHandler.post(new Runnable() {
1210 public void run() {
1211 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1212 if (callbacks != null) {
1213 callbacks.bindAppWidget(widget);
1214 }
1215 }
1216 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001217 }
1218 }
Joe Onorato36115782010-06-17 13:28:48 -04001219 // Tell the workspace that we're done.
1220 mHandler.post(new Runnable() {
1221 public void run() {
1222 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1223 if (callbacks != null) {
1224 callbacks.finishBindingItems();
1225 }
1226 }
1227 });
1228 // If we're profiling, this is the last thing in the queue.
1229 mHandler.post(new Runnable() {
1230 public void run() {
1231 if (DEBUG_LOADERS) {
1232 Log.d(TAG, "bound workspace in "
1233 + (SystemClock.uptimeMillis()-t) + "ms");
1234 }
1235 }
1236 });
1237 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001238
Joe Onorato36115782010-06-17 13:28:48 -04001239 private void loadAndBindAllApps() {
1240 if (DEBUG_LOADERS) {
1241 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1242 }
1243 if (!mAllAppsLoaded) {
1244 loadAllAppsByBatch();
1245 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001246 return;
1247 }
Joe Onorato36115782010-06-17 13:28:48 -04001248 mAllAppsLoaded = true;
1249 } else {
1250 onlyBindAllApps();
1251 }
1252 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001253
Joe Onorato36115782010-06-17 13:28:48 -04001254 private void onlyBindAllApps() {
1255 final Callbacks oldCallbacks = mCallbacks.get();
1256 if (oldCallbacks == null) {
1257 // This launcher has exited and nobody bothered to tell us. Just bail.
1258 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1259 return;
1260 }
1261
1262 // shallow copy
1263 final ArrayList<ApplicationInfo> list
1264 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1265 mHandler.post(new Runnable() {
1266 public void run() {
1267 final long t = SystemClock.uptimeMillis();
1268 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1269 if (callbacks != null) {
1270 callbacks.bindAllApplications(list);
1271 }
1272 if (DEBUG_LOADERS) {
1273 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1274 + (SystemClock.uptimeMillis()-t) + "ms");
1275 }
1276 }
1277 });
1278
1279 }
1280
1281 private void loadAllAppsByBatch() {
1282 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1283
1284 // Don't use these two variables in any of the callback runnables.
1285 // Otherwise we hold a reference to them.
1286 final Callbacks oldCallbacks = mCallbacks.get();
1287 if (oldCallbacks == null) {
1288 // This launcher has exited and nobody bothered to tell us. Just bail.
1289 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1290 return;
1291 }
1292
1293 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1294 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1295
1296 final PackageManager packageManager = mContext.getPackageManager();
1297 List<ResolveInfo> apps = null;
1298
1299 int N = Integer.MAX_VALUE;
1300
1301 int startIndex;
1302 int i=0;
1303 int batchSize = -1;
1304 while (i < N && !mStopped) {
1305 if (i == 0) {
1306 mAllAppsList.clear();
1307 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1308 apps = packageManager.queryIntentActivities(mainIntent, 0);
1309 if (DEBUG_LOADERS) {
1310 Log.d(TAG, "queryIntentActivities took "
1311 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1312 }
1313 if (apps == null) {
1314 return;
1315 }
1316 N = apps.size();
1317 if (DEBUG_LOADERS) {
1318 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1319 }
1320 if (N == 0) {
1321 // There are no apps?!?
1322 return;
1323 }
1324 if (mBatchSize == 0) {
1325 batchSize = N;
1326 } else {
1327 batchSize = mBatchSize;
1328 }
1329
1330 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1331 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001332 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001333 if (DEBUG_LOADERS) {
1334 Log.d(TAG, "sort took "
1335 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1336 }
1337 }
1338
1339 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1340
1341 startIndex = i;
1342 for (int j=0; i<N && j<batchSize; j++) {
1343 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001344 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1345 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001346 i++;
1347 }
1348
1349 final boolean first = i <= batchSize;
1350 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1351 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1352 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1353
Joe Onoratocc67f472010-06-08 10:54:30 -07001354 mHandler.post(new Runnable() {
1355 public void run() {
1356 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001357 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001358 if (first) {
1359 callbacks.bindAllApplications(added);
1360 } else {
1361 callbacks.bindAppsAdded(added);
1362 }
1363 if (DEBUG_LOADERS) {
1364 Log.d(TAG, "bound " + added.size() + " apps in "
1365 + (SystemClock.uptimeMillis() - t) + "ms");
1366 }
1367 } else {
1368 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001369 }
1370 }
1371 });
1372
Daniel Sandlerdca66122010-04-13 16:23:58 -04001373 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001374 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1375 + (SystemClock.uptimeMillis()-t2) + "ms");
1376 }
1377
1378 if (mAllAppsLoadDelay > 0 && i < N) {
1379 try {
1380 if (DEBUG_LOADERS) {
1381 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1382 }
1383 Thread.sleep(mAllAppsLoadDelay);
1384 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001385 }
1386 }
1387
Joe Onorato36115782010-06-17 13:28:48 -04001388 if (DEBUG_LOADERS) {
1389 Log.d(TAG, "cached all " + N + " apps in "
1390 + (SystemClock.uptimeMillis()-t) + "ms"
1391 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001392 }
1393 }
1394
1395 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001396 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1397 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1398 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1399 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1400 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001401 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001402 }
1403 }
1404
1405 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001406 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001407 }
1408
1409 private class PackageUpdatedTask implements Runnable {
1410 int mOp;
1411 String[] mPackages;
1412
1413 public static final int OP_NONE = 0;
1414 public static final int OP_ADD = 1;
1415 public static final int OP_UPDATE = 2;
1416 public static final int OP_REMOVE = 3; // uninstlled
1417 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1418
1419
1420 public PackageUpdatedTask(int op, String[] packages) {
1421 mOp = op;
1422 mPackages = packages;
1423 }
1424
1425 public void run() {
1426 final Context context = mApp;
1427
1428 final String[] packages = mPackages;
1429 final int N = packages.length;
1430 switch (mOp) {
1431 case OP_ADD:
1432 for (int i=0; i<N; i++) {
1433 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1434 mAllAppsList.addPackage(context, packages[i]);
1435 }
1436 break;
1437 case OP_UPDATE:
1438 for (int i=0; i<N; i++) {
1439 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1440 mAllAppsList.updatePackage(context, packages[i]);
1441 }
1442 break;
1443 case OP_REMOVE:
1444 case OP_UNAVAILABLE:
1445 for (int i=0; i<N; i++) {
1446 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1447 mAllAppsList.removePackage(packages[i]);
1448 }
1449 break;
1450 }
1451
1452 ArrayList<ApplicationInfo> added = null;
1453 ArrayList<ApplicationInfo> removed = null;
1454 ArrayList<ApplicationInfo> modified = null;
1455
1456 if (mAllAppsList.added.size() > 0) {
1457 added = mAllAppsList.added;
1458 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1459 }
1460 if (mAllAppsList.removed.size() > 0) {
1461 removed = mAllAppsList.removed;
1462 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1463 for (ApplicationInfo info: removed) {
1464 mIconCache.remove(info.intent.getComponent());
1465 }
1466 }
1467 if (mAllAppsList.modified.size() > 0) {
1468 modified = mAllAppsList.modified;
1469 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1470 }
1471
1472 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1473 if (callbacks == null) {
1474 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1475 return;
1476 }
1477
1478 if (added != null) {
1479 final ArrayList<ApplicationInfo> addedFinal = added;
1480 mHandler.post(new Runnable() {
1481 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001482 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1483 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001484 callbacks.bindAppsAdded(addedFinal);
1485 }
1486 }
1487 });
1488 }
1489 if (modified != null) {
1490 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1491 mHandler.post(new Runnable() {
1492 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001493 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1494 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001495 callbacks.bindAppsUpdated(modifiedFinal);
1496 }
1497 }
1498 });
1499 }
1500 if (removed != null) {
1501 final boolean permanent = mOp != OP_UNAVAILABLE;
1502 final ArrayList<ApplicationInfo> removedFinal = removed;
1503 mHandler.post(new Runnable() {
1504 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001505 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1506 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001507 callbacks.bindAppsRemoved(removedFinal, permanent);
1508 }
1509 }
1510 });
Joe Onoratobe386092009-11-17 17:32:16 -08001511 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001512
1513 mHandler.post(new Runnable() {
1514 @Override
1515 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001516 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1517 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001518 callbacks.bindPackagesUpdated();
1519 }
1520 }
1521 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001522 }
1523 }
1524
1525 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001526 * This is called from the code that adds shortcuts from the intent receiver. This
1527 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001528 */
Joe Onorato56d82912010-03-07 14:32:10 -05001529 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001530 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001532
Joe Onorato56d82912010-03-07 14:32:10 -05001533 /**
1534 * Make an ShortcutInfo object for a shortcut that is an application.
1535 *
1536 * If c is not null, then it will be used to fill in missing data like the title and icon.
1537 */
1538 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001539 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001540 Bitmap icon = null;
1541 final ShortcutInfo info = new ShortcutInfo();
1542
1543 ComponentName componentName = intent.getComponent();
1544 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001545 return null;
1546 }
1547
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001548 // TODO: See if the PackageManager knows about this case. If it doesn't
1549 // then return null & delete this.
1550
Joe Onorato56d82912010-03-07 14:32:10 -05001551 // the resource -- This may implicitly give us back the fallback icon,
1552 // but don't worry about that. All we're doing with usingFallbackIcon is
1553 // to avoid saving lots of copies of that in the database, and most apps
1554 // have icons anyway.
1555 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1556 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001557 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001558 }
Joe Onorato56d82912010-03-07 14:32:10 -05001559 // the db
1560 if (icon == null) {
1561 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001562 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001563 }
1564 }
1565 // the fallback icon
1566 if (icon == null) {
1567 icon = getFallbackIcon();
1568 info.usingFallbackIcon = true;
1569 }
1570 info.setIcon(icon);
1571
1572 // from the resource
1573 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001574 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1575 if (labelCache != null && labelCache.containsKey(key)) {
1576 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001577 } else {
1578 info.title = resolveInfo.activityInfo.loadLabel(manager);
1579 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001580 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001581 }
1582 }
Joe Onorato56d82912010-03-07 14:32:10 -05001583 }
1584 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001586 if (c != null) {
1587 info.title = c.getString(titleIndex);
1588 }
1589 }
1590 // fall back to the class name of the activity
1591 if (info.title == null) {
1592 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001593 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001594 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1595 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001597
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001599 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001601 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001602 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1603 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604
Joe Onorato56d82912010-03-07 14:32:10 -05001605 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001606 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001609 // TODO: If there's an explicit component and we can't install that, delete it.
1610
Joe Onorato56d82912010-03-07 14:32:10 -05001611 info.title = c.getString(titleIndex);
1612
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613 int iconType = c.getInt(iconTypeIndex);
1614 switch (iconType) {
1615 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1616 String packageName = c.getString(iconPackageIndex);
1617 String resourceName = c.getString(iconResourceIndex);
1618 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001619 info.customIcon = false;
1620 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001623 if (resources != null) {
1624 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001625 icon = Utilities.createIconBitmap(
1626 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001627 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001628 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001629 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
Joe Onorato56d82912010-03-07 14:32:10 -05001631 // the db
1632 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001633 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001634 }
1635 // the fallback icon
1636 if (icon == null) {
1637 icon = getFallbackIcon();
1638 info.usingFallbackIcon = true;
1639 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640 break;
1641 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001642 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001643 if (icon == null) {
1644 icon = getFallbackIcon();
1645 info.customIcon = false;
1646 info.usingFallbackIcon = true;
1647 } else {
1648 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650 break;
1651 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001652 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001653 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001654 info.customIcon = false;
1655 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001657 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 return info;
1659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660
Michael Jurka931dc972011-08-05 15:08:15 -07001661 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001662 if (false) {
1663 Log.d(TAG, "getIconFromCursor app="
1664 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1665 }
1666 byte[] data = c.getBlob(iconIndex);
1667 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001668 return Utilities.createIconBitmap(
1669 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001670 } catch (Exception e) {
1671 return null;
1672 }
1673 }
1674
Winson Chung3d503fb2011-07-13 17:25:49 -07001675 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1676 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001677 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001678 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001679
1680 return info;
1681 }
1682
Winson Chunga9abd0e2010-10-27 17:18:37 -07001683 /**
Winson Chung55cef262010-10-28 14:14:18 -07001684 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1685 */
1686 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1687 ComponentName component) {
1688 List<AppWidgetProviderInfo> widgets =
1689 AppWidgetManager.getInstance(context).getInstalledProviders();
1690 for (AppWidgetProviderInfo info : widgets) {
1691 if (info.provider.equals(component)) {
1692 return info;
1693 }
1694 }
1695 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001696 }
1697
Winson Chung68846fd2010-10-29 11:00:27 -07001698 /**
1699 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1700 */
1701 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1702 final PackageManager packageManager = context.getPackageManager();
1703 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1704 new ArrayList<WidgetMimeTypeHandlerData>();
1705
1706 final Intent supportsIntent =
1707 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1708 supportsIntent.setType(mimeType);
1709
1710 // Create a set of widget configuration components that we can test against
1711 final List<AppWidgetProviderInfo> widgets =
1712 AppWidgetManager.getInstance(context).getInstalledProviders();
1713 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1714 new HashMap<ComponentName, AppWidgetProviderInfo>();
1715 for (AppWidgetProviderInfo info : widgets) {
1716 configurationComponentToWidget.put(info.configure, info);
1717 }
1718
1719 // Run through each of the intents that can handle this type of clip data, and cross
1720 // reference them with the components that are actual configuration components
1721 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1722 PackageManager.MATCH_DEFAULT_ONLY);
1723 for (ResolveInfo info : activities) {
1724 final ActivityInfo activityInfo = info.activityInfo;
1725 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1726 activityInfo.name);
1727 if (configurationComponentToWidget.containsKey(infoComponent)) {
1728 supportedConfigurationActivities.add(
1729 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1730 configurationComponentToWidget.get(infoComponent)));
1731 }
1732 }
1733 return supportedConfigurationActivities;
1734 }
1735
Winson Chunga9abd0e2010-10-27 17:18:37 -07001736 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001737 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1738 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1739 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1740
1741 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001742 boolean customIcon = false;
1743 ShortcutIconResource iconResource = null;
1744
1745 if (bitmap != null && bitmap instanceof Bitmap) {
1746 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001747 customIcon = true;
1748 } else {
1749 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1750 if (extra != null && extra instanceof ShortcutIconResource) {
1751 try {
1752 iconResource = (ShortcutIconResource) extra;
1753 final PackageManager packageManager = context.getPackageManager();
1754 Resources resources = packageManager.getResourcesForApplication(
1755 iconResource.packageName);
1756 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001757 icon = Utilities.createIconBitmap(
1758 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001759 } catch (Exception e) {
1760 Log.w(TAG, "Could not load shortcut icon: " + extra);
1761 }
1762 }
1763 }
1764
Joe Onorato0589f0f2010-02-08 13:44:00 -08001765 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001766
1767 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001768 if (fallbackIcon != null) {
1769 icon = fallbackIcon;
1770 } else {
1771 icon = getFallbackIcon();
1772 info.usingFallbackIcon = true;
1773 }
Joe Onorato56d82912010-03-07 14:32:10 -05001774 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001775 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001776
Joe Onorato0589f0f2010-02-08 13:44:00 -08001777 info.title = name;
1778 info.intent = intent;
1779 info.customIcon = customIcon;
1780 info.iconResource = iconResource;
1781
1782 return info;
1783 }
1784
Winson Chungaac01e12011-08-17 10:37:13 -07001785 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1786 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001787 // If apps can't be on SD, don't even bother.
1788 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001789 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001790 }
Joe Onorato56d82912010-03-07 14:32:10 -05001791 // If this icon doesn't have a custom icon, check to see
1792 // what's stored in the DB, and if it doesn't match what
1793 // we're going to show, store what we are going to show back
1794 // into the DB. We do this so when we're loading, if the
1795 // package manager can't find an icon (for example because
1796 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001797 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001798 cache.put(info, c.getBlob(iconIndex));
1799 return true;
1800 }
1801 return false;
1802 }
1803 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1804 boolean needSave = false;
1805 try {
1806 if (data != null) {
1807 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1808 Bitmap loaded = info.getIcon(mIconCache);
1809 needSave = !saved.sameAs(loaded);
1810 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001811 needSave = true;
1812 }
Winson Chungaac01e12011-08-17 10:37:13 -07001813 } catch (Exception e) {
1814 needSave = true;
1815 }
1816 if (needSave) {
1817 Log.d(TAG, "going to save icon bitmap for info=" + info);
1818 // This is slower than is ideal, but this only happens once
1819 // or when the app is updated with a new icon.
1820 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001821 }
1822 }
1823
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001825 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826 * or make a new one.
1827 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001828 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829 // See if a placeholder was created for us already
1830 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001831 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001833 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834 folders.put(id, folderInfo);
1835 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001836 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
1838
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001840 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 = new Comparator<ApplicationInfo>() {
1842 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001843 int result = sCollator.compare(a.title.toString(), b.title.toString());
1844 if (result == 0) {
1845 result = a.componentName.compareTo(b.componentName);
1846 }
1847 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 };
Winson Chung78403fe2011-01-21 15:38:02 -08001850 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1851 = new Comparator<ApplicationInfo>() {
1852 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1853 if (a.firstInstallTime < b.firstInstallTime) return 1;
1854 if (a.firstInstallTime > b.firstInstallTime) return -1;
1855 return 0;
1856 }
1857 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001858 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1859 = new Comparator<AppWidgetProviderInfo>() {
1860 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1861 return sCollator.compare(a.label.toString(), b.label.toString());
1862 }
1863 };
Winson Chung5308f242011-08-18 12:12:41 -07001864 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1865 if (info.activityInfo != null) {
1866 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1867 } else {
1868 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1869 }
1870 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001871 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1872 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001873 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001874 ShortcutNameComparator(PackageManager pm) {
1875 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001876 mLabelCache = new HashMap<Object, CharSequence>();
1877 }
1878 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1879 mPackageManager = pm;
1880 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001881 }
1882 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001883 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001884 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1885 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1886 if (mLabelCache.containsKey(keyA)) {
1887 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001888 } else {
1889 labelA = a.loadLabel(mPackageManager).toString();
1890
Winson Chung5308f242011-08-18 12:12:41 -07001891 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001892 }
Winson Chung5308f242011-08-18 12:12:41 -07001893 if (mLabelCache.containsKey(keyB)) {
1894 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001895 } else {
1896 labelB = b.loadLabel(mPackageManager).toString();
1897
Winson Chung5308f242011-08-18 12:12:41 -07001898 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001899 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001900 return sCollator.compare(labelA, labelB);
1901 }
1902 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001903 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1904 private PackageManager mPackageManager;
1905 private HashMap<Object, String> mLabelCache;
1906 WidgetAndShortcutNameComparator(PackageManager pm) {
1907 mPackageManager = pm;
1908 mLabelCache = new HashMap<Object, String>();
1909 }
1910 public final int compare(Object a, Object b) {
1911 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001912 if (mLabelCache.containsKey(a)) {
1913 labelA = mLabelCache.get(a);
1914 } else {
1915 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001916 ((AppWidgetProviderInfo) a).label :
1917 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001918 mLabelCache.put(a, labelA);
1919 }
1920 if (mLabelCache.containsKey(b)) {
1921 labelB = mLabelCache.get(b);
1922 } else {
1923 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001924 ((AppWidgetProviderInfo) b).label :
1925 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001926 mLabelCache.put(b, labelB);
1927 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001928 return sCollator.compare(labelA, labelB);
1929 }
1930 };
Joe Onoratobe386092009-11-17 17:32:16 -08001931
1932 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001933 Log.d(TAG, "mCallbacks=" + mCallbacks);
1934 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1935 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1936 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1937 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001938 if (mLoaderTask != null) {
1939 mLoaderTask.dumpState();
1940 } else {
1941 Log.d(TAG, "mLoaderTask=null");
1942 }
Joe Onoratobe386092009-11-17 17:32:16 -08001943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944}