blob: 1c5dc6cc3ed2bc32f42a1316efc7d86c3ee28ac8 [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
Winson Chung603bcb92011-09-02 11:45:39 -0700158 public void unbindWorkspaceItems() {
159 sWorker.post(new Runnable() {
160 @Override
161 public void run() {
162 unbindWorkspaceItemsOnMainThread();
163 }
164 });
165 }
166
167 /** Unbinds all the sWorkspaceItems on the main thread, and return a copy of sWorkspaceItems
168 * that is save to reference from the main thread. */
169 private ArrayList<ItemInfo> unbindWorkspaceItemsOnMainThread() {
170 // Ensure that we don't use the same workspace items data structure on the main thread
171 // by making a copy of workspace items first.
172 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(sWorkspaceItems);
173 mHandler.post(new Runnable() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700174 @Override
Winson Chung603bcb92011-09-02 11:45:39 -0700175 public void run() {
176 for (ItemInfo item : workspaceItems) {
177 item.unbind();
178 }
179 }
180 });
181
182 return workspaceItems;
Adam Cohen4eac29a2011-07-11 17:53:37 -0700183 }
184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 /**
186 * Adds an item to the DB if it was not created previously, or move it to a new
187 * <container, screen, cellX, cellY>
188 */
189 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
190 int screen, int cellX, int cellY) {
191 if (item.container == ItemInfo.NO_ID) {
192 // From all apps
193 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
194 } else {
195 // From somewhere else
196 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 }
198 }
199
Michael Jurkac9d95c52011-08-29 14:03:34 -0700200 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
201 final ItemInfo item, final String callingFunction) {
202 final long itemId = item.id;
203 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
204 final ContentResolver cr = context.getContentResolver();
205
206 Runnable r = new Runnable() {
207 public void run() {
208 cr.update(uri, values, null, null);
209
210 ItemInfo modelItem = sItemsIdMap.get(itemId);
211 if (item != modelItem) {
212 // the modelItem needs to match up perfectly with item if our model is to be
213 // consistent with the database-- for now, just require modelItem == item
214 String msg = "item: " + ((item != null) ? item.toString() : "null") +
215 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
216 "Error: ItemInfo passed to " + callingFunction + " doesn't match original";
217 throw new RuntimeException(msg);
218 }
219
220 // Items are added/removed from the corresponding FolderInfo elsewhere, such
221 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
222 // that are on the desktop, as appropriate
223 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
224 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
225 if (!sWorkspaceItems.contains(modelItem)) {
226 sWorkspaceItems.add(modelItem);
227 }
228 } else {
229 sWorkspaceItems.remove(modelItem);
230 }
231 }
232 };
233
234 if (sWorkerThread.getThreadId() == Process.myTid()) {
235 r.run();
236 } else {
237 sWorker.post(r);
238 }
239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700242 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700243 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
244 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246 item.cellX = cellX;
247 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700248
Winson Chung3d503fb2011-07-13 17:25:49 -0700249 // We store hotseat items in canonical form which is this orientation invariant position
250 // in the hotseat
251 if (context instanceof Launcher && screen < 0 &&
252 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
253 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
254 } else {
255 item.screen = screen;
256 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257
258 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700260 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
261 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
263
Michael Jurkac9d95c52011-08-29 14:03:34 -0700264 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700265 }
266
267 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800268 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800269 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700270 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
271 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800272 item.spanX = spanX;
273 item.spanY = spanY;
274 item.cellX = cellX;
275 item.cellY = cellY;
276
Adam Cohend4844c32011-02-18 19:25:06 -0800277 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800278 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
279 values.put(LauncherSettings.Favorites.SPANX, spanX);
280 values.put(LauncherSettings.Favorites.SPANY, spanY);
281 values.put(LauncherSettings.Favorites.CELLX, cellX);
282 values.put(LauncherSettings.Favorites.CELLY, cellY);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700283 updateItemInDatabaseHelper(context, values, item, "resizeItemInDatabase");
284 }
Adam Cohend4844c32011-02-18 19:25:06 -0800285
Michael Jurkac9d95c52011-08-29 14:03:34 -0700286
287 /**
288 * Update an item to the database in a specified container.
289 */
290 static void updateItemInDatabase(Context context, final ItemInfo item) {
291 final ContentValues values = new ContentValues();
292 item.onAddToDatabase(values);
293 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
294 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800295 }
296
297 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 * Returns true if the shortcuts already exists in the database.
299 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301 static boolean shortcutExists(Context context, String title, Intent intent) {
302 final ContentResolver cr = context.getContentResolver();
303 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
304 new String[] { "title", "intent" }, "title=? and intent=?",
305 new String[] { title, intent.toUri(0) }, null);
306 boolean result = false;
307 try {
308 result = c.moveToFirst();
309 } finally {
310 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700313 }
314
Joe Onorato9c1289c2009-08-17 11:03:03 -0400315 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700316 * Returns an ItemInfo array containing all the items in the LauncherModel.
317 * The ItemInfo.id is not set through this function.
318 */
319 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
320 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
321 final ContentResolver cr = context.getContentResolver();
322 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
323 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
324 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
325 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
326
327 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
328 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
329 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
330 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
331 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
332 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
333 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
334
335 try {
336 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700337 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700338 item.cellX = c.getInt(cellXIndex);
339 item.cellY = c.getInt(cellYIndex);
340 item.spanX = c.getInt(spanXIndex);
341 item.spanY = c.getInt(spanYIndex);
342 item.container = c.getInt(containerIndex);
343 item.itemType = c.getInt(itemTypeIndex);
344 item.screen = c.getInt(screenIndex);
345
346 items.add(item);
347 }
348 } catch (Exception e) {
349 items.clear();
350 } finally {
351 c.close();
352 }
353
354 return items;
355 }
356
357 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
359 */
360 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
361 final ContentResolver cr = context.getContentResolver();
362 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
363 "_id=? and (itemType=? or itemType=?)",
364 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700365 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700366
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 try {
368 if (c.moveToFirst()) {
369 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
370 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
371 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
372 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
373 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
374 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376 FolderInfo folderInfo = null;
377 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700378 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
379 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700381 }
382
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 folderInfo.title = c.getString(titleIndex);
384 folderInfo.id = id;
385 folderInfo.container = c.getInt(containerIndex);
386 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700387 folderInfo.cellX = c.getInt(cellXIndex);
388 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400389
390 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400392 } finally {
393 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700394 }
395
396 return null;
397 }
398
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 /**
400 * Add an item to the database in a specified container. Sets the container, screen, cellX and
401 * cellY fields of the item. Also assigns an ID to the item.
402 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700403 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
404 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 item.cellX = cellX;
407 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700408 // We store hotseat items in canonical form which is this orientation invariant position
409 // in the hotseat
410 if (context instanceof Launcher && screen < 0 &&
411 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
412 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
413 } else {
414 item.screen = screen;
415 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400416
417 final ContentValues values = new ContentValues();
418 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 item.onAddToDatabase(values);
420
Michael Jurka7578ec62011-08-03 14:11:54 -0700421 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700422 item.id = app.getLauncherProvider().generateNewId();
423 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700424 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700425
Michael Jurkac9d95c52011-08-29 14:03:34 -0700426 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700427 public void run() {
428 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
429 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430
Winson Chungb1094bd2011-08-24 16:14:08 -0700431 if (sItemsIdMap.containsKey(item.id)) {
432 // we should not be adding new items in the db with the same id
433 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
434 "addItemToDatabase already exists." + item.toString());
435 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700436 sItemsIdMap.put(item.id, item);
437 switch (item.itemType) {
438 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
439 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700440 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700441 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
442 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700443 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
444 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700445 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700446 }
447 break;
448 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
449 sAppWidgets.add((LauncherAppWidgetInfo) item);
450 break;
451 }
452 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700453 };
454
455 if (sWorkerThread.getThreadId() == Process.myTid()) {
456 r.run();
457 } else {
458 sWorker.post(r);
459 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700460 }
461
Joe Onorato9c1289c2009-08-17 11:03:03 -0400462 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700463 * Creates a new unique child id, for a given cell span across all layouts.
464 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700465 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700466 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
467 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700468 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700469 }
470
Adam Cohend22015c2010-07-26 22:02:18 -0700471 static int getCellCountX() {
472 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700473 }
474
Adam Cohend22015c2010-07-26 22:02:18 -0700475 static int getCellCountY() {
476 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700477 }
478
479 /**
480 * Updates the model orientation helper to take into account the current layout dimensions
481 * when performing local/canonical coordinate transformations.
482 */
483 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700484 mCellCountX = shortAxisCellCount;
485 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700486 }
487
488 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700489 * Removes the specified item from the database
490 * @param context
491 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400492 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700493 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400494 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700495 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Michael Jurka83df1882011-08-31 20:59:26 -0700496 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700497 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700498 cr.delete(uriToDelete, null, null);
499 switch (item.itemType) {
500 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
501 sFolders.remove(item.id);
502 sWorkspaceItems.remove(item);
503 break;
504 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
505 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
506 sWorkspaceItems.remove(item);
507 break;
508 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
509 sAppWidgets.remove((LauncherAppWidgetInfo) item);
510 break;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700511 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700512 sItemsIdMap.remove(item.id);
513 sDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700514 }
Michael Jurka83df1882011-08-31 20:59:26 -0700515 };
516 if (sWorkerThread.getThreadId() == Process.myTid()) {
517 r.run();
518 } else {
519 sWorker.post(r);
520 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400521 }
522
523 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 * Remove the contents of the specified folder from the database
525 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700526 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400527 final ContentResolver cr = context.getContentResolver();
528
Michael Jurkac9d95c52011-08-29 14:03:34 -0700529 Runnable r = new Runnable() {
530 public void run() {
531 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
532 sItemsIdMap.remove(info.id);
533 sFolders.remove(info.id);
534 sDbIconCache.remove(info);
535 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700536
Michael Jurkac9d95c52011-08-29 14:03:34 -0700537 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
538 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
539 for (ItemInfo childInfo : info.contents) {
540 sItemsIdMap.remove(childInfo.id);
541 sDbIconCache.remove(childInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700542 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700543 }
544 };
545 if (sWorkerThread.getThreadId() == Process.myTid()) {
546 r.run();
547 } else {
548 sWorker.post(r);
549 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550 }
551
552 /**
553 * Set this as the current Launcher activity object for the loader.
554 */
555 public void initialize(Callbacks callbacks) {
556 synchronized (mLock) {
557 mCallbacks = new WeakReference<Callbacks>(callbacks);
558 }
559 }
560
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700561 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
563 * ACTION_PACKAGE_CHANGED.
564 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100565 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400566 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400567 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700568
Joe Onorato36115782010-06-17 13:28:48 -0400569 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400570
Joe Onorato36115782010-06-17 13:28:48 -0400571 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
572 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
573 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
574 final String packageName = intent.getData().getSchemeSpecificPart();
575 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576
Joe Onorato36115782010-06-17 13:28:48 -0400577 int op = PackageUpdatedTask.OP_NONE;
578
579 if (packageName == null || packageName.length() == 0) {
580 // they sent us a bad intent
581 return;
582 }
583
584 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
585 op = PackageUpdatedTask.OP_UPDATE;
586 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
587 if (!replacing) {
588 op = PackageUpdatedTask.OP_REMOVE;
589 }
590 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
591 // later, we will update the package at this time
592 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
593 if (!replacing) {
594 op = PackageUpdatedTask.OP_ADD;
595 } else {
596 op = PackageUpdatedTask.OP_UPDATE;
597 }
598 }
599
600 if (op != PackageUpdatedTask.OP_NONE) {
601 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
602 }
603
604 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400605 // First, schedule to add these apps back in.
606 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
607 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
608 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700609 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400610 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
611 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
612 enqueuePackageUpdated(new PackageUpdatedTask(
613 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700614 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
615 // If we have changed locale we need to clear out the labels in all apps.
616 // Do this here because if the launcher activity is running it will be restarted.
617 // If it's not running startLoaderFromBackground will merely tell it that it needs
618 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
619 // next time.
620 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700621 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700622 startLoaderFromBackground();
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700623 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
624 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700625 if (mCallbacks != null) {
626 Callbacks callbacks = mCallbacks.get();
627 if (callbacks != null) {
628 callbacks.bindSearchablesChanged();
629 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700630 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700631 }
632 }
633
634 /**
635 * When the launcher is in the background, it's possible for it to miss paired
636 * configuration changes. So whenever we trigger the loader from the background
637 * tell the launcher that it needs to re-run the loader when it comes back instead
638 * of doing it now.
639 */
640 public void startLoaderFromBackground() {
641 boolean runLoader = false;
642 if (mCallbacks != null) {
643 Callbacks callbacks = mCallbacks.get();
644 if (callbacks != null) {
645 // Only actually run the loader if they're not paused.
646 if (!callbacks.setLoadOnResume()) {
647 runLoader = true;
648 }
649 }
650 }
651 if (runLoader) {
652 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700653 }
Joe Onorato36115782010-06-17 13:28:48 -0400654 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400655
Joe Onorato36115782010-06-17 13:28:48 -0400656 public void startLoader(Context context, boolean isLaunching) {
657 synchronized (mLock) {
658 if (DEBUG_LOADERS) {
659 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
660 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661
Joe Onorato36115782010-06-17 13:28:48 -0400662 // Don't bother to start the thread if we know it's not going to do anything
663 if (mCallbacks != null && mCallbacks.get() != null) {
664 // If there is already one running, tell it to stop.
665 LoaderTask oldTask = mLoaderTask;
666 if (oldTask != null) {
667 if (oldTask.isLaunching()) {
668 // don't downgrade isLaunching if we're already running
669 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500670 }
Joe Onorato36115782010-06-17 13:28:48 -0400671 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500672 }
Joe Onorato36115782010-06-17 13:28:48 -0400673 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700674 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 }
677 }
678
Joe Onorato36115782010-06-17 13:28:48 -0400679 public void stopLoader() {
680 synchronized (mLock) {
681 if (mLoaderTask != null) {
682 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400683 }
684 }
Joe Onorato36115782010-06-17 13:28:48 -0400685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400686
Michael Jurkac57b7a82011-08-09 22:02:20 -0700687 public boolean isAllAppsLoaded() {
688 return mAllAppsLoaded;
689 }
690
Joe Onorato36115782010-06-17 13:28:48 -0400691 /**
692 * Runnable for the thread that loads the contents of the launcher:
693 * - workspace icons
694 * - widgets
695 * - all apps icons
696 */
697 private class LoaderTask implements Runnable {
698 private Context mContext;
699 private Thread mWaitThread;
700 private boolean mIsLaunching;
701 private boolean mStopped;
702 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700703 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400704
705 LoaderTask(Context context, boolean isLaunching) {
706 mContext = context;
707 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700708 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 }
710
Joe Onorato36115782010-06-17 13:28:48 -0400711 boolean isLaunching() {
712 return mIsLaunching;
713 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714
Joe Onorato36115782010-06-17 13:28:48 -0400715 private void loadAndBindWorkspace() {
716 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400717 if (DEBUG_LOADERS) {
718 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700720
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700721 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400722 loadWorkspace();
723 if (mStopped) {
724 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 }
Joe Onorato36115782010-06-17 13:28:48 -0400726 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727 }
728
Joe Onorato36115782010-06-17 13:28:48 -0400729 // Bind the workspace
730 bindWorkspace();
731 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400732
Joe Onorato36115782010-06-17 13:28:48 -0400733 private void waitForIdle() {
734 // Wait until the either we're stopped or the other threads are done.
735 // This way we don't start loading all apps until the workspace has settled
736 // down.
737 synchronized (LoaderTask.this) {
738 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700739
Joe Onorato36115782010-06-17 13:28:48 -0400740 mHandler.postIdle(new Runnable() {
741 public void run() {
742 synchronized (LoaderTask.this) {
743 mLoadAndBindStepFinished = true;
744 if (DEBUG_LOADERS) {
745 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400746 }
Joe Onorato36115782010-06-17 13:28:48 -0400747 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400748 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400749 }
Joe Onorato36115782010-06-17 13:28:48 -0400750 });
751
752 while (!mStopped && !mLoadAndBindStepFinished) {
753 try {
754 this.wait();
755 } catch (InterruptedException ex) {
756 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400757 }
758 }
Joe Onorato36115782010-06-17 13:28:48 -0400759 if (DEBUG_LOADERS) {
760 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700761 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400762 + "ms for previous step to finish binding");
763 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400764 }
Joe Onorato36115782010-06-17 13:28:48 -0400765 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400766
Joe Onorato36115782010-06-17 13:28:48 -0400767 public void run() {
768 // Optimize for end-user experience: if the Launcher is up and // running with the
769 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
770 // workspace first (default).
771 final Callbacks cbk = mCallbacks.get();
772 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400773
Joe Onorato36115782010-06-17 13:28:48 -0400774 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400775 // Elevate priority when Home launches for the first time to avoid
776 // starving at boot time. Staring at a blank home is not cool.
777 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700778 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
779 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400780 android.os.Process.setThreadPriority(mIsLaunching
781 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
782 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400783 if (loadWorkspaceFirst) {
784 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
785 loadAndBindWorkspace();
786 } else {
787 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700788 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400789 }
790
Joe Onorato36115782010-06-17 13:28:48 -0400791 if (mStopped) {
792 break keep_running;
793 }
794
795 // Whew! Hard work done. Slow us down, and wait until the UI thread has
796 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400797 synchronized (mLock) {
798 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700799 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400800 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
801 }
802 }
Joe Onorato36115782010-06-17 13:28:48 -0400803 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400804
805 // second step
806 if (loadWorkspaceFirst) {
807 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700808 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400809 } else {
810 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
811 loadAndBindWorkspace();
812 }
Joe Onorato36115782010-06-17 13:28:48 -0400813 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814
Winson Chungaac01e12011-08-17 10:37:13 -0700815
816 // Update the saved icons if necessary
817 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700818 for (Object key : sDbIconCache.keySet()) {
819 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700820 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700821 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700822
Joe Onorato36115782010-06-17 13:28:48 -0400823 // Clear out this reference, otherwise we end up holding it until all of the
824 // callback runnables are done.
825 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826
Joe Onorato36115782010-06-17 13:28:48 -0400827 synchronized (mLock) {
828 // If we are still the last one to be scheduled, remove ourselves.
829 if (mLoaderTask == this) {
830 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831 }
Joe Onorato36115782010-06-17 13:28:48 -0400832 }
Joe Onorato36115782010-06-17 13:28:48 -0400833 }
834
835 public void stopLocked() {
836 synchronized (LoaderTask.this) {
837 mStopped = true;
838 this.notify();
839 }
840 }
841
842 /**
843 * Gets the callbacks object. If we've been stopped, or if the launcher object
844 * has somehow been garbage collected, return null instead. Pass in the Callbacks
845 * object that was around when the deferred message was scheduled, and if there's
846 * a new Callbacks object around then also return null. This will save us from
847 * calling onto it with data that will be ignored.
848 */
849 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
850 synchronized (mLock) {
851 if (mStopped) {
852 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400853 }
Joe Onorato36115782010-06-17 13:28:48 -0400854
855 if (mCallbacks == null) {
856 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400857 }
Joe Onorato36115782010-06-17 13:28:48 -0400858
859 final Callbacks callbacks = mCallbacks.get();
860 if (callbacks != oldCallbacks) {
861 return null;
862 }
863 if (callbacks == null) {
864 Log.w(TAG, "no mCallbacks");
865 return null;
866 }
867
868 return callbacks;
869 }
870 }
871
872 // check & update map of what's occupied; used to discard overlapping/invalid items
873 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700874 int containerIndex = item.screen;
875 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700876 // Return early if we detect that an item is under the hotseat button
877 if (Hotseat.isAllAppsButtonRank(item.screen)) {
878 return false;
879 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700880
881 // We use the last index to refer to the hotseat and the screen as the rank, so
882 // test and update the occupied state accordingly
883 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
884 Log.e(TAG, "Error loading shortcut into hotseat " + item
885 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
886 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
887 return false;
888 } else {
889 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
890 return true;
891 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700892 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
893 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400894 return true;
895 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700896
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700897 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -0400898 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
899 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700900 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400901 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700902 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400903 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700904 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700905 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400906 return false;
907 }
908 }
909 }
910 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
911 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700912 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400913 }
914 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700915
Joe Onorato36115782010-06-17 13:28:48 -0400916 return true;
917 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918
Joe Onorato36115782010-06-17 13:28:48 -0400919 private void loadWorkspace() {
920 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921
Joe Onorato36115782010-06-17 13:28:48 -0400922 final Context context = mContext;
923 final ContentResolver contentResolver = context.getContentResolver();
924 final PackageManager manager = context.getPackageManager();
925 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
926 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400927
Adam Cohen4eac29a2011-07-11 17:53:37 -0700928 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700929 sAppWidgets.clear();
930 sFolders.clear();
931 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700932 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800933
Joe Onorato36115782010-06-17 13:28:48 -0400934 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400935
Joe Onorato36115782010-06-17 13:28:48 -0400936 final Cursor c = contentResolver.query(
937 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400938
Winson Chungf30ad5f2011-08-08 10:55:42 -0700939 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700940 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700941 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942
Joe Onorato36115782010-06-17 13:28:48 -0400943 try {
944 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
945 final int intentIndex = c.getColumnIndexOrThrow
946 (LauncherSettings.Favorites.INTENT);
947 final int titleIndex = c.getColumnIndexOrThrow
948 (LauncherSettings.Favorites.TITLE);
949 final int iconTypeIndex = c.getColumnIndexOrThrow(
950 LauncherSettings.Favorites.ICON_TYPE);
951 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
952 final int iconPackageIndex = c.getColumnIndexOrThrow(
953 LauncherSettings.Favorites.ICON_PACKAGE);
954 final int iconResourceIndex = c.getColumnIndexOrThrow(
955 LauncherSettings.Favorites.ICON_RESOURCE);
956 final int containerIndex = c.getColumnIndexOrThrow(
957 LauncherSettings.Favorites.CONTAINER);
958 final int itemTypeIndex = c.getColumnIndexOrThrow(
959 LauncherSettings.Favorites.ITEM_TYPE);
960 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
961 LauncherSettings.Favorites.APPWIDGET_ID);
962 final int screenIndex = c.getColumnIndexOrThrow(
963 LauncherSettings.Favorites.SCREEN);
964 final int cellXIndex = c.getColumnIndexOrThrow
965 (LauncherSettings.Favorites.CELLX);
966 final int cellYIndex = c.getColumnIndexOrThrow
967 (LauncherSettings.Favorites.CELLY);
968 final int spanXIndex = c.getColumnIndexOrThrow
969 (LauncherSettings.Favorites.SPANX);
970 final int spanYIndex = c.getColumnIndexOrThrow(
971 LauncherSettings.Favorites.SPANY);
972 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
973 final int displayModeIndex = c.getColumnIndexOrThrow(
974 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975
Joe Onorato36115782010-06-17 13:28:48 -0400976 ShortcutInfo info;
977 String intentDescription;
978 LauncherAppWidgetInfo appWidgetInfo;
979 int container;
980 long id;
981 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982
Joe Onorato36115782010-06-17 13:28:48 -0400983 while (!mStopped && c.moveToNext()) {
984 try {
985 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986
Joe Onorato36115782010-06-17 13:28:48 -0400987 switch (itemType) {
988 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
989 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
990 intentDescription = c.getString(intentIndex);
991 try {
992 intent = Intent.parseUri(intentDescription, 0);
993 } catch (URISyntaxException e) {
994 continue;
995 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996
Joe Onorato36115782010-06-17 13:28:48 -0400997 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
998 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700999 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001000 } else {
1001 info = getShortcutInfo(c, context, iconTypeIndex,
1002 iconPackageIndex, iconResourceIndex, iconIndex,
1003 titleIndex);
1004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005
Joe Onorato36115782010-06-17 13:28:48 -04001006 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001007 info.intent = intent;
1008 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001010 info.container = container;
1011 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001012 info.cellX = c.getInt(cellXIndex);
1013 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001014
Daniel Sandler8802e962010-05-26 16:28:16 -04001015 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001016 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001017 break;
1018 }
1019
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001021 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001022 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001023 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001024 break;
1025 default:
1026 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001027 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001028 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001029 folderInfo.add(info);
1030 break;
1031 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001032 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001033
1034 // now that we've loaded everthing re-save it with the
1035 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001036 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001037 } else {
1038 // Failed to load the shortcut, probably because the
1039 // activity manager couldn't resolve it (maybe the app
1040 // was uninstalled), or the db row was somehow screwed up.
1041 // Delete it.
1042 id = c.getLong(idIndex);
1043 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1044 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1045 id, false), null, null);
1046 }
1047 break;
1048
Adam Cohendf2cc412011-04-27 16:56:57 -07001049 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001050 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001051 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001052
Winson Chungaafa03c2010-06-11 17:34:16 -07001053 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001054 folderInfo.id = id;
1055 container = c.getInt(containerIndex);
1056 folderInfo.container = container;
1057 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001058 folderInfo.cellX = c.getInt(cellXIndex);
1059 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001060
1061 // check & update map of what's occupied
1062 if (!checkItemPlacement(occupied, folderInfo)) {
1063 break;
1064 }
Joe Onorato36115782010-06-17 13:28:48 -04001065 switch (container) {
1066 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001067 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001068 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001069 break;
1070 }
1071
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001072 sItemsIdMap.put(folderInfo.id, folderInfo);
1073 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001074 break;
1075
Joe Onorato36115782010-06-17 13:28:48 -04001076 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1077 // Read all Launcher-specific widget details
1078 int appWidgetId = c.getInt(appWidgetIdIndex);
1079 id = c.getLong(idIndex);
1080
1081 final AppWidgetProviderInfo provider =
1082 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001083
Joe Onorato36115782010-06-17 13:28:48 -04001084 if (!isSafeMode && (provider == null || provider.provider == null ||
1085 provider.provider.getPackageName() == null)) {
1086 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1087 + id + " appWidgetId=" + appWidgetId);
1088 itemsToRemove.add(id);
1089 } else {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001090 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001091 appWidgetInfo.id = id;
1092 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001093 appWidgetInfo.cellX = c.getInt(cellXIndex);
1094 appWidgetInfo.cellY = c.getInt(cellYIndex);
1095 appWidgetInfo.spanX = c.getInt(spanXIndex);
1096 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001097
1098 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001099 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1100 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001101 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001102 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001103 continue;
1104 }
1105 appWidgetInfo.container = c.getInt(containerIndex);
1106
1107 // check & update map of what's occupied
1108 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1109 break;
1110 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001111 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1112 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001113 }
1114 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001115 }
Joe Onorato36115782010-06-17 13:28:48 -04001116 } catch (Exception e) {
1117 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001118 }
1119 }
Joe Onorato36115782010-06-17 13:28:48 -04001120 } finally {
1121 c.close();
1122 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001123
Joe Onorato36115782010-06-17 13:28:48 -04001124 if (itemsToRemove.size() > 0) {
1125 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1126 LauncherSettings.Favorites.CONTENT_URI);
1127 // Remove dead items
1128 for (long id : itemsToRemove) {
1129 if (DEBUG_LOADERS) {
1130 Log.d(TAG, "Removed id = " + id);
1131 }
1132 // Don't notify content observers
1133 try {
1134 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1135 null, null);
1136 } catch (RemoteException e) {
1137 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001138 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001139 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 }
1141
Joe Onorato36115782010-06-17 13:28:48 -04001142 if (DEBUG_LOADERS) {
1143 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1144 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001145 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001146 String line = "";
1147 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1148 if (s > 0) {
1149 line += " | ";
1150 }
Adam Cohend22015c2010-07-26 22:02:18 -07001151 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001152 line += ((occupied[s][x][y] != null) ? "#" : ".");
1153 }
1154 }
1155 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156 }
Joe Onorato36115782010-06-17 13:28:48 -04001157 }
1158 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159
Joe Onorato36115782010-06-17 13:28:48 -04001160 /**
1161 * Read everything out of our database.
1162 */
1163 private void bindWorkspace() {
1164 final long t = SystemClock.uptimeMillis();
1165
1166 // Don't use these two variables in any of the callback runnables.
1167 // Otherwise we hold a reference to them.
1168 final Callbacks oldCallbacks = mCallbacks.get();
1169 if (oldCallbacks == null) {
1170 // This launcher has exited and nobody bothered to tell us. Just bail.
1171 Log.w(TAG, "LoaderTask running with no launcher");
1172 return;
1173 }
1174
1175 int N;
1176 // Tell the workspace that we're about to start firing items at it
1177 mHandler.post(new Runnable() {
1178 public void run() {
1179 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1180 if (callbacks != null) {
1181 callbacks.startBinding();
1182 }
1183 }
1184 });
Winson Chung603bcb92011-09-02 11:45:39 -07001185
1186 // Unbind previously bound workspace items to prevent a leak of AppWidgetHostViews.
1187 final ArrayList<ItemInfo> workspaceItems = unbindWorkspaceItemsOnMainThread();
1188
Joe Onorato36115782010-06-17 13:28:48 -04001189 // Add the items to the workspace.
Winson Chung603bcb92011-09-02 11:45:39 -07001190 N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001191 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1192 final int start = i;
1193 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 mHandler.post(new Runnable() {
1195 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001196 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001198 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199 }
1200 }
1201 });
Joe Onorato36115782010-06-17 13:28:48 -04001202 }
Winson Chung603bcb92011-09-02 11:45:39 -07001203 // Ensure that we don't use the same folders data structure on the main thread
1204 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001205 mHandler.post(new Runnable() {
1206 public void run() {
1207 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1208 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001209 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001210 }
1211 }
1212 });
1213 // Wait until the queue goes empty.
1214 mHandler.post(new Runnable() {
1215 public void run() {
1216 if (DEBUG_LOADERS) {
1217 Log.d(TAG, "Going to start binding widgets soon.");
1218 }
1219 }
1220 });
1221 // Bind the widgets, one at a time.
1222 // WARNING: this is calling into the workspace from the background thread,
1223 // but since getCurrentScreen() just returns the int, we should be okay. This
1224 // is just a hint for the order, and if it's wrong, we'll be okay.
1225 // TODO: instead, we should have that push the current screen into here.
1226 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001227 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001228 // once for the current screen
1229 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001230 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001231 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 mHandler.post(new Runnable() {
1233 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001234 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001235 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001236 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 }
1238 }
1239 });
1240 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 }
Joe Onorato36115782010-06-17 13:28:48 -04001242 // once for the other screens
1243 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001244 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001245 if (widget.screen != currentScreen) {
1246 mHandler.post(new Runnable() {
1247 public void run() {
1248 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1249 if (callbacks != null) {
1250 callbacks.bindAppWidget(widget);
1251 }
1252 }
1253 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001254 }
1255 }
Joe Onorato36115782010-06-17 13:28:48 -04001256 // Tell the workspace that we're done.
1257 mHandler.post(new Runnable() {
1258 public void run() {
1259 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1260 if (callbacks != null) {
1261 callbacks.finishBindingItems();
1262 }
1263 }
1264 });
1265 // If we're profiling, this is the last thing in the queue.
1266 mHandler.post(new Runnable() {
1267 public void run() {
1268 if (DEBUG_LOADERS) {
1269 Log.d(TAG, "bound workspace in "
1270 + (SystemClock.uptimeMillis()-t) + "ms");
1271 }
1272 }
1273 });
1274 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001275
Joe Onorato36115782010-06-17 13:28:48 -04001276 private void loadAndBindAllApps() {
1277 if (DEBUG_LOADERS) {
1278 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1279 }
1280 if (!mAllAppsLoaded) {
1281 loadAllAppsByBatch();
1282 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001283 return;
1284 }
Joe Onorato36115782010-06-17 13:28:48 -04001285 mAllAppsLoaded = true;
1286 } else {
1287 onlyBindAllApps();
1288 }
1289 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001290
Joe Onorato36115782010-06-17 13:28:48 -04001291 private void onlyBindAllApps() {
1292 final Callbacks oldCallbacks = mCallbacks.get();
1293 if (oldCallbacks == null) {
1294 // This launcher has exited and nobody bothered to tell us. Just bail.
1295 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1296 return;
1297 }
1298
1299 // shallow copy
1300 final ArrayList<ApplicationInfo> list
1301 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1302 mHandler.post(new Runnable() {
1303 public void run() {
1304 final long t = SystemClock.uptimeMillis();
1305 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1306 if (callbacks != null) {
1307 callbacks.bindAllApplications(list);
1308 }
1309 if (DEBUG_LOADERS) {
1310 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1311 + (SystemClock.uptimeMillis()-t) + "ms");
1312 }
1313 }
1314 });
1315
1316 }
1317
1318 private void loadAllAppsByBatch() {
1319 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1320
1321 // Don't use these two variables in any of the callback runnables.
1322 // Otherwise we hold a reference to them.
1323 final Callbacks oldCallbacks = mCallbacks.get();
1324 if (oldCallbacks == null) {
1325 // This launcher has exited and nobody bothered to tell us. Just bail.
1326 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1327 return;
1328 }
1329
1330 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1331 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1332
1333 final PackageManager packageManager = mContext.getPackageManager();
1334 List<ResolveInfo> apps = null;
1335
1336 int N = Integer.MAX_VALUE;
1337
1338 int startIndex;
1339 int i=0;
1340 int batchSize = -1;
1341 while (i < N && !mStopped) {
1342 if (i == 0) {
1343 mAllAppsList.clear();
1344 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1345 apps = packageManager.queryIntentActivities(mainIntent, 0);
1346 if (DEBUG_LOADERS) {
1347 Log.d(TAG, "queryIntentActivities took "
1348 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1349 }
1350 if (apps == null) {
1351 return;
1352 }
1353 N = apps.size();
1354 if (DEBUG_LOADERS) {
1355 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1356 }
1357 if (N == 0) {
1358 // There are no apps?!?
1359 return;
1360 }
1361 if (mBatchSize == 0) {
1362 batchSize = N;
1363 } else {
1364 batchSize = mBatchSize;
1365 }
1366
1367 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1368 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001369 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001370 if (DEBUG_LOADERS) {
1371 Log.d(TAG, "sort took "
1372 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1373 }
1374 }
1375
1376 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1377
1378 startIndex = i;
1379 for (int j=0; i<N && j<batchSize; j++) {
1380 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001381 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001382 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001383 i++;
1384 }
1385
1386 final boolean first = i <= batchSize;
1387 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1388 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1389 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1390
Joe Onoratocc67f472010-06-08 10:54:30 -07001391 mHandler.post(new Runnable() {
1392 public void run() {
1393 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001394 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001395 if (first) {
1396 callbacks.bindAllApplications(added);
1397 } else {
1398 callbacks.bindAppsAdded(added);
1399 }
1400 if (DEBUG_LOADERS) {
1401 Log.d(TAG, "bound " + added.size() + " apps in "
1402 + (SystemClock.uptimeMillis() - t) + "ms");
1403 }
1404 } else {
1405 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001406 }
1407 }
1408 });
1409
Daniel Sandlerdca66122010-04-13 16:23:58 -04001410 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001411 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1412 + (SystemClock.uptimeMillis()-t2) + "ms");
1413 }
1414
1415 if (mAllAppsLoadDelay > 0 && i < N) {
1416 try {
1417 if (DEBUG_LOADERS) {
1418 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1419 }
1420 Thread.sleep(mAllAppsLoadDelay);
1421 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422 }
1423 }
1424
Joe Onorato36115782010-06-17 13:28:48 -04001425 if (DEBUG_LOADERS) {
1426 Log.d(TAG, "cached all " + N + " apps in "
1427 + (SystemClock.uptimeMillis()-t) + "ms"
1428 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001429 }
1430 }
1431
1432 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001433 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1434 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1435 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1436 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1437 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001438 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001439 }
1440 }
1441
1442 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001443 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001444 }
1445
1446 private class PackageUpdatedTask implements Runnable {
1447 int mOp;
1448 String[] mPackages;
1449
1450 public static final int OP_NONE = 0;
1451 public static final int OP_ADD = 1;
1452 public static final int OP_UPDATE = 2;
1453 public static final int OP_REMOVE = 3; // uninstlled
1454 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1455
1456
1457 public PackageUpdatedTask(int op, String[] packages) {
1458 mOp = op;
1459 mPackages = packages;
1460 }
1461
1462 public void run() {
1463 final Context context = mApp;
1464
1465 final String[] packages = mPackages;
1466 final int N = packages.length;
1467 switch (mOp) {
1468 case OP_ADD:
1469 for (int i=0; i<N; i++) {
1470 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1471 mAllAppsList.addPackage(context, packages[i]);
1472 }
1473 break;
1474 case OP_UPDATE:
1475 for (int i=0; i<N; i++) {
1476 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1477 mAllAppsList.updatePackage(context, packages[i]);
1478 }
1479 break;
1480 case OP_REMOVE:
1481 case OP_UNAVAILABLE:
1482 for (int i=0; i<N; i++) {
1483 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1484 mAllAppsList.removePackage(packages[i]);
1485 }
1486 break;
1487 }
1488
1489 ArrayList<ApplicationInfo> added = null;
1490 ArrayList<ApplicationInfo> removed = null;
1491 ArrayList<ApplicationInfo> modified = null;
1492
1493 if (mAllAppsList.added.size() > 0) {
1494 added = mAllAppsList.added;
1495 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1496 }
1497 if (mAllAppsList.removed.size() > 0) {
1498 removed = mAllAppsList.removed;
1499 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1500 for (ApplicationInfo info: removed) {
1501 mIconCache.remove(info.intent.getComponent());
1502 }
1503 }
1504 if (mAllAppsList.modified.size() > 0) {
1505 modified = mAllAppsList.modified;
1506 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1507 }
1508
1509 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1510 if (callbacks == null) {
1511 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1512 return;
1513 }
1514
1515 if (added != null) {
1516 final ArrayList<ApplicationInfo> addedFinal = added;
1517 mHandler.post(new Runnable() {
1518 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001519 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1520 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001521 callbacks.bindAppsAdded(addedFinal);
1522 }
1523 }
1524 });
1525 }
1526 if (modified != null) {
1527 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1528 mHandler.post(new Runnable() {
1529 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001530 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1531 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001532 callbacks.bindAppsUpdated(modifiedFinal);
1533 }
1534 }
1535 });
1536 }
1537 if (removed != null) {
1538 final boolean permanent = mOp != OP_UNAVAILABLE;
1539 final ArrayList<ApplicationInfo> removedFinal = removed;
1540 mHandler.post(new Runnable() {
1541 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001542 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1543 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001544 callbacks.bindAppsRemoved(removedFinal, permanent);
1545 }
1546 }
1547 });
Joe Onoratobe386092009-11-17 17:32:16 -08001548 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001549
1550 mHandler.post(new Runnable() {
1551 @Override
1552 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001553 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1554 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001555 callbacks.bindPackagesUpdated();
1556 }
1557 }
1558 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001559 }
1560 }
1561
1562 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001563 * This is called from the code that adds shortcuts from the intent receiver. This
1564 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 */
Joe Onorato56d82912010-03-07 14:32:10 -05001566 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001567 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001568 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569
Joe Onorato56d82912010-03-07 14:32:10 -05001570 /**
1571 * Make an ShortcutInfo object for a shortcut that is an application.
1572 *
1573 * If c is not null, then it will be used to fill in missing data like the title and icon.
1574 */
1575 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001576 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001577 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001578 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001579
1580 ComponentName componentName = intent.getComponent();
1581 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001582 return null;
1583 }
1584
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001585 // TODO: See if the PackageManager knows about this case. If it doesn't
1586 // then return null & delete this.
1587
Joe Onorato56d82912010-03-07 14:32:10 -05001588 // the resource -- This may implicitly give us back the fallback icon,
1589 // but don't worry about that. All we're doing with usingFallbackIcon is
1590 // to avoid saving lots of copies of that in the database, and most apps
1591 // have icons anyway.
1592 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1593 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001594 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001595 }
Joe Onorato56d82912010-03-07 14:32:10 -05001596 // the db
1597 if (icon == null) {
1598 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001599 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001600 }
1601 }
1602 // the fallback icon
1603 if (icon == null) {
1604 icon = getFallbackIcon();
1605 info.usingFallbackIcon = true;
1606 }
1607 info.setIcon(icon);
1608
1609 // from the resource
1610 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001611 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1612 if (labelCache != null && labelCache.containsKey(key)) {
1613 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001614 } else {
1615 info.title = resolveInfo.activityInfo.loadLabel(manager);
1616 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001617 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001618 }
1619 }
Joe Onorato56d82912010-03-07 14:32:10 -05001620 }
1621 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001623 if (c != null) {
1624 info.title = c.getString(titleIndex);
1625 }
1626 }
1627 // fall back to the class name of the activity
1628 if (info.title == null) {
1629 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001630 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1632 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001636 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001638 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001639 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1640 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641
Joe Onorato56d82912010-03-07 14:32:10 -05001642 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001643 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001644 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001646 // TODO: If there's an explicit component and we can't install that, delete it.
1647
Joe Onorato56d82912010-03-07 14:32:10 -05001648 info.title = c.getString(titleIndex);
1649
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650 int iconType = c.getInt(iconTypeIndex);
1651 switch (iconType) {
1652 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1653 String packageName = c.getString(iconPackageIndex);
1654 String resourceName = c.getString(iconResourceIndex);
1655 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001656 info.customIcon = false;
1657 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001660 if (resources != null) {
1661 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001662 icon = Utilities.createIconBitmap(
1663 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001664 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001665 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001666 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 }
Joe Onorato56d82912010-03-07 14:32:10 -05001668 // the db
1669 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001670 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001671 }
1672 // the fallback icon
1673 if (icon == null) {
1674 icon = getFallbackIcon();
1675 info.usingFallbackIcon = true;
1676 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677 break;
1678 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001679 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001680 if (icon == null) {
1681 icon = getFallbackIcon();
1682 info.customIcon = false;
1683 info.usingFallbackIcon = true;
1684 } else {
1685 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001686 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001687 break;
1688 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001689 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001690 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001691 info.customIcon = false;
1692 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001694 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001695 return info;
1696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697
Michael Jurka931dc972011-08-05 15:08:15 -07001698 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001699 if (false) {
1700 Log.d(TAG, "getIconFromCursor app="
1701 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1702 }
1703 byte[] data = c.getBlob(iconIndex);
1704 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001705 return Utilities.createIconBitmap(
1706 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001707 } catch (Exception e) {
1708 return null;
1709 }
1710 }
1711
Winson Chung3d503fb2011-07-13 17:25:49 -07001712 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1713 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001714 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001715 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001716
1717 return info;
1718 }
1719
Winson Chunga9abd0e2010-10-27 17:18:37 -07001720 /**
Winson Chung55cef262010-10-28 14:14:18 -07001721 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1722 */
1723 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1724 ComponentName component) {
1725 List<AppWidgetProviderInfo> widgets =
1726 AppWidgetManager.getInstance(context).getInstalledProviders();
1727 for (AppWidgetProviderInfo info : widgets) {
1728 if (info.provider.equals(component)) {
1729 return info;
1730 }
1731 }
1732 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001733 }
1734
Winson Chung68846fd2010-10-29 11:00:27 -07001735 /**
1736 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1737 */
1738 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1739 final PackageManager packageManager = context.getPackageManager();
1740 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1741 new ArrayList<WidgetMimeTypeHandlerData>();
1742
1743 final Intent supportsIntent =
1744 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1745 supportsIntent.setType(mimeType);
1746
1747 // Create a set of widget configuration components that we can test against
1748 final List<AppWidgetProviderInfo> widgets =
1749 AppWidgetManager.getInstance(context).getInstalledProviders();
1750 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1751 new HashMap<ComponentName, AppWidgetProviderInfo>();
1752 for (AppWidgetProviderInfo info : widgets) {
1753 configurationComponentToWidget.put(info.configure, info);
1754 }
1755
1756 // Run through each of the intents that can handle this type of clip data, and cross
1757 // reference them with the components that are actual configuration components
1758 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1759 PackageManager.MATCH_DEFAULT_ONLY);
1760 for (ResolveInfo info : activities) {
1761 final ActivityInfo activityInfo = info.activityInfo;
1762 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1763 activityInfo.name);
1764 if (configurationComponentToWidget.containsKey(infoComponent)) {
1765 supportedConfigurationActivities.add(
1766 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1767 configurationComponentToWidget.get(infoComponent)));
1768 }
1769 }
1770 return supportedConfigurationActivities;
1771 }
1772
Winson Chunga9abd0e2010-10-27 17:18:37 -07001773 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001774 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1775 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1776 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1777
1778 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001779 boolean customIcon = false;
1780 ShortcutIconResource iconResource = null;
1781
1782 if (bitmap != null && bitmap instanceof Bitmap) {
1783 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001784 customIcon = true;
1785 } else {
1786 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1787 if (extra != null && extra instanceof ShortcutIconResource) {
1788 try {
1789 iconResource = (ShortcutIconResource) extra;
1790 final PackageManager packageManager = context.getPackageManager();
1791 Resources resources = packageManager.getResourcesForApplication(
1792 iconResource.packageName);
1793 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001794 icon = Utilities.createIconBitmap(
1795 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001796 } catch (Exception e) {
1797 Log.w(TAG, "Could not load shortcut icon: " + extra);
1798 }
1799 }
1800 }
1801
Michael Jurkac9d95c52011-08-29 14:03:34 -07001802 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001803
1804 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001805 if (fallbackIcon != null) {
1806 icon = fallbackIcon;
1807 } else {
1808 icon = getFallbackIcon();
1809 info.usingFallbackIcon = true;
1810 }
Joe Onorato56d82912010-03-07 14:32:10 -05001811 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001812 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001813
Joe Onorato0589f0f2010-02-08 13:44:00 -08001814 info.title = name;
1815 info.intent = intent;
1816 info.customIcon = customIcon;
1817 info.iconResource = iconResource;
1818
1819 return info;
1820 }
1821
Winson Chungaac01e12011-08-17 10:37:13 -07001822 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1823 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001824 // If apps can't be on SD, don't even bother.
1825 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001826 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001827 }
Joe Onorato56d82912010-03-07 14:32:10 -05001828 // If this icon doesn't have a custom icon, check to see
1829 // what's stored in the DB, and if it doesn't match what
1830 // we're going to show, store what we are going to show back
1831 // into the DB. We do this so when we're loading, if the
1832 // package manager can't find an icon (for example because
1833 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001834 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001835 cache.put(info, c.getBlob(iconIndex));
1836 return true;
1837 }
1838 return false;
1839 }
1840 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1841 boolean needSave = false;
1842 try {
1843 if (data != null) {
1844 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1845 Bitmap loaded = info.getIcon(mIconCache);
1846 needSave = !saved.sameAs(loaded);
1847 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001848 needSave = true;
1849 }
Winson Chungaac01e12011-08-17 10:37:13 -07001850 } catch (Exception e) {
1851 needSave = true;
1852 }
1853 if (needSave) {
1854 Log.d(TAG, "going to save icon bitmap for info=" + info);
1855 // This is slower than is ideal, but this only happens once
1856 // or when the app is updated with a new icon.
1857 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001858 }
1859 }
1860
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001862 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 * or make a new one.
1864 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001865 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866 // See if a placeholder was created for us already
1867 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001868 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001869 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07001870 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001871 folders.put(id, folderInfo);
1872 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001873 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001877 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001878 = new Comparator<ApplicationInfo>() {
1879 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001880 int result = sCollator.compare(a.title.toString(), b.title.toString());
1881 if (result == 0) {
1882 result = a.componentName.compareTo(b.componentName);
1883 }
1884 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001886 };
Winson Chung78403fe2011-01-21 15:38:02 -08001887 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1888 = new Comparator<ApplicationInfo>() {
1889 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1890 if (a.firstInstallTime < b.firstInstallTime) return 1;
1891 if (a.firstInstallTime > b.firstInstallTime) return -1;
1892 return 0;
1893 }
1894 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001895 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1896 = new Comparator<AppWidgetProviderInfo>() {
1897 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1898 return sCollator.compare(a.label.toString(), b.label.toString());
1899 }
1900 };
Winson Chung5308f242011-08-18 12:12:41 -07001901 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1902 if (info.activityInfo != null) {
1903 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1904 } else {
1905 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1906 }
1907 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001908 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1909 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001910 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001911 ShortcutNameComparator(PackageManager pm) {
1912 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001913 mLabelCache = new HashMap<Object, CharSequence>();
1914 }
1915 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1916 mPackageManager = pm;
1917 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001918 }
1919 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001920 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001921 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1922 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1923 if (mLabelCache.containsKey(keyA)) {
1924 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001925 } else {
1926 labelA = a.loadLabel(mPackageManager).toString();
1927
Winson Chung5308f242011-08-18 12:12:41 -07001928 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001929 }
Winson Chung5308f242011-08-18 12:12:41 -07001930 if (mLabelCache.containsKey(keyB)) {
1931 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001932 } else {
1933 labelB = b.loadLabel(mPackageManager).toString();
1934
Winson Chung5308f242011-08-18 12:12:41 -07001935 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001936 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001937 return sCollator.compare(labelA, labelB);
1938 }
1939 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001940 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1941 private PackageManager mPackageManager;
1942 private HashMap<Object, String> mLabelCache;
1943 WidgetAndShortcutNameComparator(PackageManager pm) {
1944 mPackageManager = pm;
1945 mLabelCache = new HashMap<Object, String>();
1946 }
1947 public final int compare(Object a, Object b) {
1948 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001949 if (mLabelCache.containsKey(a)) {
1950 labelA = mLabelCache.get(a);
1951 } else {
1952 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001953 ((AppWidgetProviderInfo) a).label :
1954 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001955 mLabelCache.put(a, labelA);
1956 }
1957 if (mLabelCache.containsKey(b)) {
1958 labelB = mLabelCache.get(b);
1959 } else {
1960 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001961 ((AppWidgetProviderInfo) b).label :
1962 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001963 mLabelCache.put(b, labelB);
1964 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001965 return sCollator.compare(labelA, labelB);
1966 }
1967 };
Joe Onoratobe386092009-11-17 17:32:16 -08001968
1969 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001970 Log.d(TAG, "mCallbacks=" + mCallbacks);
1971 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1972 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1973 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1974 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001975 if (mLoaderTask != null) {
1976 mLoaderTask.dumpState();
1977 } else {
1978 Log.d(TAG, "mLoaderTask=null");
1979 }
Joe Onoratobe386092009-11-17 17:32:16 -08001980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981}