blob: e0b04dacb31acd46ec364fbff0d6d71a768e9e71 [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() {
174 @Override
175 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
200 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700202 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700203 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
204 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 item.cellX = cellX;
207 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700208 // We store hotseat items in canonical form which is this orientation invariant position
209 // in the hotseat
210 if (context instanceof Launcher && screen < 0 &&
211 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
212 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
213 } else {
214 item.screen = screen;
215 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700217 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 final ContentValues values = new ContentValues();
219 final ContentResolver cr = context.getContentResolver();
220
221 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700222 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
223 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
225
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700226 sWorker.post(new Runnable() {
227 public void run() {
228 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700229 ItemInfo modelItem = sItemsIdMap.get(item.id);
230 if (item != modelItem) {
231 // the modelItem needs to match up perfectly with item if our model is to be
232 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700233 String msg = "item: " + ((item != null) ? item.toString() : "null") +
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700234 " modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
235 " creation tag of item: " + ((item != null) ? item.whereCreated : "null") +
236 " creation tag of modelItem: " + ((modelItem != null) ? modelItem.whereCreated : "null") +
237 " Error: ItemInfo passed to moveItemInDatabase doesn't match original";
Winson Chungc22a54d2011-08-25 13:51:25 -0700238 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700239 }
240
241 // Items are added/removed from the corresponding FolderInfo elsewhere, such
242 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
243 // that are on the desktop, as appropriate
Winson Chung3d503fb2011-07-13 17:25:49 -0700244 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
245 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700246 if (!sWorkspaceItems.contains(modelItem)) {
247 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700248 }
249 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700250 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700251 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700252 }
253 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700254 }
255
256 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800257 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800258 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700259 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
260 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800261 item.spanX = spanX;
262 item.spanY = spanY;
263 item.cellX = cellX;
264 item.cellY = cellY;
265
266 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
267 final ContentValues values = new ContentValues();
268 final ContentResolver cr = context.getContentResolver();
269
270 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
271 values.put(LauncherSettings.Favorites.SPANX, spanX);
272 values.put(LauncherSettings.Favorites.SPANY, spanY);
273 values.put(LauncherSettings.Favorites.CELLX, cellX);
274 values.put(LauncherSettings.Favorites.CELLY, cellY);
275
276 sWorker.post(new Runnable() {
277 public void run() {
278 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700279 ItemInfo modelItem = sItemsIdMap.get(item.id);
280 if (item != modelItem) {
281 // the modelItem needs to match up perfectly with item if our model is to be
282 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700283 String msg = "item: " + ((item != null) ? item.toString() : "null") +
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700284 " modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
285 " creation tag of item: " + ((item != null) ? item.whereCreated : "null") +
286 " creation tag of modelItem: " + ((modelItem != null) ? modelItem.whereCreated : "null") +
287 " Error: ItemInfo passed to resizeItemInDatabase doesn't match original";
Winson Chungc22a54d2011-08-25 13:51:25 -0700288 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700289 }
Adam Cohend4844c32011-02-18 19:25:06 -0800290 }
291 });
292 }
293
294 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 * Returns true if the shortcuts already exists in the database.
296 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 static boolean shortcutExists(Context context, String title, Intent intent) {
299 final ContentResolver cr = context.getContentResolver();
300 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
301 new String[] { "title", "intent" }, "title=? and intent=?",
302 new String[] { title, intent.toUri(0) }, null);
303 boolean result = false;
304 try {
305 result = c.moveToFirst();
306 } finally {
307 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700310 }
311
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700313 * Returns an ItemInfo array containing all the items in the LauncherModel.
314 * The ItemInfo.id is not set through this function.
315 */
316 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
317 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
318 final ContentResolver cr = context.getContentResolver();
319 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
320 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
321 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
322 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
323
324 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
325 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
326 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
327 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
328 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
329 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
330 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
331
332 try {
333 while (c.moveToNext()) {
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700334 ItemInfo item = new ItemInfo("17");
Winson Chungaafa03c2010-06-11 17:34:16 -0700335 item.cellX = c.getInt(cellXIndex);
336 item.cellY = c.getInt(cellYIndex);
337 item.spanX = c.getInt(spanXIndex);
338 item.spanY = c.getInt(spanYIndex);
339 item.container = c.getInt(containerIndex);
340 item.itemType = c.getInt(itemTypeIndex);
341 item.screen = c.getInt(screenIndex);
342
343 items.add(item);
344 }
345 } catch (Exception e) {
346 items.clear();
347 } finally {
348 c.close();
349 }
350
351 return items;
352 }
353
354 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
356 */
357 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
358 final ContentResolver cr = context.getContentResolver();
359 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
360 "_id=? and (itemType=? or itemType=?)",
361 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700362 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700363
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 try {
365 if (c.moveToFirst()) {
366 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
367 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
368 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
369 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
370 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
371 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 FolderInfo folderInfo = null;
374 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700375 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
376 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700378 }
379
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 folderInfo.title = c.getString(titleIndex);
381 folderInfo.id = id;
382 folderInfo.container = c.getInt(containerIndex);
383 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700384 folderInfo.cellX = c.getInt(cellXIndex);
385 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386
387 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700388 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400389 } finally {
390 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700391 }
392
393 return null;
394 }
395
Joe Onorato9c1289c2009-08-17 11:03:03 -0400396 /**
397 * Add an item to the database in a specified container. Sets the container, screen, cellX and
398 * cellY fields of the item. Also assigns an ID to the item.
399 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700400 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
401 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400402 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 item.cellX = cellX;
404 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700405 // We store hotseat items in canonical form which is this orientation invariant position
406 // in the hotseat
407 if (context instanceof Launcher && screen < 0 &&
408 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
409 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
410 } else {
411 item.screen = screen;
412 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400413
414 final ContentValues values = new ContentValues();
415 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400416 item.onAddToDatabase(values);
417
Michael Jurka7578ec62011-08-03 14:11:54 -0700418 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700419 item.id = app.getLauncherProvider().generateNewId();
420 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700421 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700422
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700423 sWorker.post(new Runnable() {
424 public void run() {
425 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
426 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427
Winson Chungb1094bd2011-08-24 16:14:08 -0700428 if (sItemsIdMap.containsKey(item.id)) {
429 // we should not be adding new items in the db with the same id
430 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
431 "addItemToDatabase already exists." + item.toString());
432 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700433 sItemsIdMap.put(item.id, item);
434 switch (item.itemType) {
435 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
436 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700437 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700438 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
439 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700440 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
441 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700442 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700443 }
444 break;
445 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
446 sAppWidgets.add((LauncherAppWidgetInfo) item);
447 break;
448 }
449 }
450 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700451 }
452
Joe Onorato9c1289c2009-08-17 11:03:03 -0400453 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700454 * Creates a new unique child id, for a given cell span across all layouts.
455 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700456 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700457 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
458 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700459 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700460 }
461
Adam Cohend22015c2010-07-26 22:02:18 -0700462 static int getCellCountX() {
463 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700464 }
465
Adam Cohend22015c2010-07-26 22:02:18 -0700466 static int getCellCountY() {
467 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700468 }
469
470 /**
471 * Updates the model orientation helper to take into account the current layout dimensions
472 * when performing local/canonical coordinate transformations.
473 */
474 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700475 mCellCountX = shortAxisCellCount;
476 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700477 }
478
479 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400480 * Update an item to the database in a specified container.
481 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700482 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400483 final ContentValues values = new ContentValues();
484 final ContentResolver cr = context.getContentResolver();
485
486 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700487 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700488
Michael Jurka83df1882011-08-31 20:59:26 -0700489 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700490 public void run() {
491 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
492 values, null, null);
493 final ItemInfo modelItem = sItemsIdMap.get(item.id);
494 if (item != modelItem) {
495 // the modelItem needs to match up perfectly with item if our model is to be
496 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700497 String msg = "item: " + ((item != null) ? item.toString() : "null") +
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700498 " modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
499 " creation tag of item: " + ((item != null) ? item.whereCreated : "null") +
500 " creation tag of modelItem: " + ((modelItem != null) ? modelItem.whereCreated : "null") +
501 " Error: ItemInfo passed to updateItemInDatabase doesn't match original";
Winson Chungc22a54d2011-08-25 13:51:25 -0700502 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700503 }
504 }
Michael Jurka83df1882011-08-31 20:59:26 -0700505 };
506 if (sWorkerThread.getThreadId() == Process.myTid()) {
507 r.run();
508 } else {
509 sWorker.post(r);
510 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400511 }
512
513 /**
514 * Removes the specified item from the database
515 * @param context
516 * @param item
517 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700518 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700520 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700521 sWorker.post(new Runnable() {
522 public void run() {
523 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700524 switch (item.itemType) {
525 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
526 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700527 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700528 break;
529 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
530 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700531 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700532 break;
533 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
534 sAppWidgets.remove((LauncherAppWidgetInfo) item);
535 break;
536 }
537 sItemsIdMap.remove(item.id);
Winson Chungb1094bd2011-08-24 16:14:08 -0700538 sDbIconCache.remove(item);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700539 }
540 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541 }
542
543 /**
544 * Remove the contents of the specified folder from the database
545 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700546 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 final ContentResolver cr = context.getContentResolver();
548
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700549 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700550 public void run() {
551 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700552 sItemsIdMap.remove(info.id);
553 sFolders.remove(info.id);
Winson Chungc22a54d2011-08-25 13:51:25 -0700554 sDbIconCache.remove(info);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700555 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700556
Adam Cohen9932a9b2011-08-02 22:14:07 -0700557 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Adam Cohenafb01ee2011-06-23 15:38:03 -0700558 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700559 for (ItemInfo childInfo : info.contents) {
560 sItemsIdMap.remove(childInfo.id);
Winson Chungc22a54d2011-08-25 13:51:25 -0700561 sDbIconCache.remove(childInfo);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700562 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700563 }
564 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565 }
566
567 /**
568 * Set this as the current Launcher activity object for the loader.
569 */
570 public void initialize(Callbacks callbacks) {
571 synchronized (mLock) {
572 mCallbacks = new WeakReference<Callbacks>(callbacks);
573 }
574 }
575
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700576 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
578 * ACTION_PACKAGE_CHANGED.
579 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100580 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400581 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400582 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700583
Joe Onorato36115782010-06-17 13:28:48 -0400584 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400585
Joe Onorato36115782010-06-17 13:28:48 -0400586 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
587 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
588 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
589 final String packageName = intent.getData().getSchemeSpecificPart();
590 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400591
Joe Onorato36115782010-06-17 13:28:48 -0400592 int op = PackageUpdatedTask.OP_NONE;
593
594 if (packageName == null || packageName.length() == 0) {
595 // they sent us a bad intent
596 return;
597 }
598
599 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
600 op = PackageUpdatedTask.OP_UPDATE;
601 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
602 if (!replacing) {
603 op = PackageUpdatedTask.OP_REMOVE;
604 }
605 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
606 // later, we will update the package at this time
607 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
608 if (!replacing) {
609 op = PackageUpdatedTask.OP_ADD;
610 } else {
611 op = PackageUpdatedTask.OP_UPDATE;
612 }
613 }
614
615 if (op != PackageUpdatedTask.OP_NONE) {
616 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
617 }
618
619 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400620 // First, schedule to add these apps back in.
621 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
622 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
623 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700624 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400625 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
626 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
627 enqueuePackageUpdated(new PackageUpdatedTask(
628 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700629 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
630 // If we have changed locale we need to clear out the labels in all apps.
631 // Do this here because if the launcher activity is running it will be restarted.
632 // If it's not running startLoaderFromBackground will merely tell it that it needs
633 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
634 // next time.
635 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700636 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700637 startLoaderFromBackground();
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700638 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
639 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700640 if (mCallbacks != null) {
641 Callbacks callbacks = mCallbacks.get();
642 if (callbacks != null) {
643 callbacks.bindSearchablesChanged();
644 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700645 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700646 }
647 }
648
649 /**
650 * When the launcher is in the background, it's possible for it to miss paired
651 * configuration changes. So whenever we trigger the loader from the background
652 * tell the launcher that it needs to re-run the loader when it comes back instead
653 * of doing it now.
654 */
655 public void startLoaderFromBackground() {
656 boolean runLoader = false;
657 if (mCallbacks != null) {
658 Callbacks callbacks = mCallbacks.get();
659 if (callbacks != null) {
660 // Only actually run the loader if they're not paused.
661 if (!callbacks.setLoadOnResume()) {
662 runLoader = true;
663 }
664 }
665 }
666 if (runLoader) {
667 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700668 }
Joe Onorato36115782010-06-17 13:28:48 -0400669 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400670
Joe Onorato36115782010-06-17 13:28:48 -0400671 public void startLoader(Context context, boolean isLaunching) {
672 synchronized (mLock) {
673 if (DEBUG_LOADERS) {
674 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
675 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676
Joe Onorato36115782010-06-17 13:28:48 -0400677 // Don't bother to start the thread if we know it's not going to do anything
678 if (mCallbacks != null && mCallbacks.get() != null) {
679 // If there is already one running, tell it to stop.
680 LoaderTask oldTask = mLoaderTask;
681 if (oldTask != null) {
682 if (oldTask.isLaunching()) {
683 // don't downgrade isLaunching if we're already running
684 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500685 }
Joe Onorato36115782010-06-17 13:28:48 -0400686 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500687 }
Joe Onorato36115782010-06-17 13:28:48 -0400688 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700689 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 }
692 }
693
Joe Onorato36115782010-06-17 13:28:48 -0400694 public void stopLoader() {
695 synchronized (mLock) {
696 if (mLoaderTask != null) {
697 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 }
699 }
Joe Onorato36115782010-06-17 13:28:48 -0400700 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701
Michael Jurkac57b7a82011-08-09 22:02:20 -0700702 public boolean isAllAppsLoaded() {
703 return mAllAppsLoaded;
704 }
705
Joe Onorato36115782010-06-17 13:28:48 -0400706 /**
707 * Runnable for the thread that loads the contents of the launcher:
708 * - workspace icons
709 * - widgets
710 * - all apps icons
711 */
712 private class LoaderTask implements Runnable {
713 private Context mContext;
714 private Thread mWaitThread;
715 private boolean mIsLaunching;
716 private boolean mStopped;
717 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700718 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400719
720 LoaderTask(Context context, boolean isLaunching) {
721 mContext = context;
722 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700723 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 }
725
Joe Onorato36115782010-06-17 13:28:48 -0400726 boolean isLaunching() {
727 return mIsLaunching;
728 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729
Joe Onorato36115782010-06-17 13:28:48 -0400730 private void loadAndBindWorkspace() {
731 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400732 if (DEBUG_LOADERS) {
733 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700735
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700736 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400737 loadWorkspace();
738 if (mStopped) {
739 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 }
Joe Onorato36115782010-06-17 13:28:48 -0400741 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 }
743
Joe Onorato36115782010-06-17 13:28:48 -0400744 // Bind the workspace
745 bindWorkspace();
746 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400747
Joe Onorato36115782010-06-17 13:28:48 -0400748 private void waitForIdle() {
749 // Wait until the either we're stopped or the other threads are done.
750 // This way we don't start loading all apps until the workspace has settled
751 // down.
752 synchronized (LoaderTask.this) {
753 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700754
Joe Onorato36115782010-06-17 13:28:48 -0400755 mHandler.postIdle(new Runnable() {
756 public void run() {
757 synchronized (LoaderTask.this) {
758 mLoadAndBindStepFinished = true;
759 if (DEBUG_LOADERS) {
760 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400761 }
Joe Onorato36115782010-06-17 13:28:48 -0400762 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400763 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400764 }
Joe Onorato36115782010-06-17 13:28:48 -0400765 });
766
767 while (!mStopped && !mLoadAndBindStepFinished) {
768 try {
769 this.wait();
770 } catch (InterruptedException ex) {
771 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400772 }
773 }
Joe Onorato36115782010-06-17 13:28:48 -0400774 if (DEBUG_LOADERS) {
775 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700776 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400777 + "ms for previous step to finish binding");
778 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400779 }
Joe Onorato36115782010-06-17 13:28:48 -0400780 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400781
Joe Onorato36115782010-06-17 13:28:48 -0400782 public void run() {
783 // Optimize for end-user experience: if the Launcher is up and // running with the
784 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
785 // workspace first (default).
786 final Callbacks cbk = mCallbacks.get();
787 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400788
Joe Onorato36115782010-06-17 13:28:48 -0400789 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400790 // Elevate priority when Home launches for the first time to avoid
791 // starving at boot time. Staring at a blank home is not cool.
792 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700793 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
794 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400795 android.os.Process.setThreadPriority(mIsLaunching
796 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
797 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400798 if (loadWorkspaceFirst) {
799 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
800 loadAndBindWorkspace();
801 } else {
802 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700803 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400804 }
805
Joe Onorato36115782010-06-17 13:28:48 -0400806 if (mStopped) {
807 break keep_running;
808 }
809
810 // Whew! Hard work done. Slow us down, and wait until the UI thread has
811 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400812 synchronized (mLock) {
813 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700814 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400815 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
816 }
817 }
Joe Onorato36115782010-06-17 13:28:48 -0400818 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400819
820 // second step
821 if (loadWorkspaceFirst) {
822 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700823 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400824 } else {
825 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
826 loadAndBindWorkspace();
827 }
Joe Onorato36115782010-06-17 13:28:48 -0400828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829
Winson Chungaac01e12011-08-17 10:37:13 -0700830
831 // Update the saved icons if necessary
832 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700833 for (Object key : sDbIconCache.keySet()) {
834 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700835 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700836 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700837
Joe Onorato36115782010-06-17 13:28:48 -0400838 // Clear out this reference, otherwise we end up holding it until all of the
839 // callback runnables are done.
840 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841
Joe Onorato36115782010-06-17 13:28:48 -0400842 synchronized (mLock) {
843 // If we are still the last one to be scheduled, remove ourselves.
844 if (mLoaderTask == this) {
845 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846 }
Joe Onorato36115782010-06-17 13:28:48 -0400847 }
Joe Onorato36115782010-06-17 13:28:48 -0400848 }
849
850 public void stopLocked() {
851 synchronized (LoaderTask.this) {
852 mStopped = true;
853 this.notify();
854 }
855 }
856
857 /**
858 * Gets the callbacks object. If we've been stopped, or if the launcher object
859 * has somehow been garbage collected, return null instead. Pass in the Callbacks
860 * object that was around when the deferred message was scheduled, and if there's
861 * a new Callbacks object around then also return null. This will save us from
862 * calling onto it with data that will be ignored.
863 */
864 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
865 synchronized (mLock) {
866 if (mStopped) {
867 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400868 }
Joe Onorato36115782010-06-17 13:28:48 -0400869
870 if (mCallbacks == null) {
871 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400872 }
Joe Onorato36115782010-06-17 13:28:48 -0400873
874 final Callbacks callbacks = mCallbacks.get();
875 if (callbacks != oldCallbacks) {
876 return null;
877 }
878 if (callbacks == null) {
879 Log.w(TAG, "no mCallbacks");
880 return null;
881 }
882
883 return callbacks;
884 }
885 }
886
887 // check & update map of what's occupied; used to discard overlapping/invalid items
888 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700889 int containerIndex = item.screen;
890 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700891 // Return early if we detect that an item is under the hotseat button
892 if (Hotseat.isAllAppsButtonRank(item.screen)) {
893 return false;
894 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700895
896 // We use the last index to refer to the hotseat and the screen as the rank, so
897 // test and update the occupied state accordingly
898 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
899 Log.e(TAG, "Error loading shortcut into hotseat " + item
900 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
901 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
902 return false;
903 } else {
904 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
905 return true;
906 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700907 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
908 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400909 return true;
910 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700911
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700912 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -0400913 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
914 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700915 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400916 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700917 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400918 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700919 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700920 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400921 return false;
922 }
923 }
924 }
925 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
926 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700927 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400928 }
929 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700930
Joe Onorato36115782010-06-17 13:28:48 -0400931 return true;
932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933
Joe Onorato36115782010-06-17 13:28:48 -0400934 private void loadWorkspace() {
935 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936
Joe Onorato36115782010-06-17 13:28:48 -0400937 final Context context = mContext;
938 final ContentResolver contentResolver = context.getContentResolver();
939 final PackageManager manager = context.getPackageManager();
940 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
941 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400942
Adam Cohen4eac29a2011-07-11 17:53:37 -0700943 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700944 sAppWidgets.clear();
945 sFolders.clear();
946 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700947 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800948
Joe Onorato36115782010-06-17 13:28:48 -0400949 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950
Joe Onorato36115782010-06-17 13:28:48 -0400951 final Cursor c = contentResolver.query(
952 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400953
Winson Chungf30ad5f2011-08-08 10:55:42 -0700954 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700955 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700956 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957
Joe Onorato36115782010-06-17 13:28:48 -0400958 try {
959 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
960 final int intentIndex = c.getColumnIndexOrThrow
961 (LauncherSettings.Favorites.INTENT);
962 final int titleIndex = c.getColumnIndexOrThrow
963 (LauncherSettings.Favorites.TITLE);
964 final int iconTypeIndex = c.getColumnIndexOrThrow(
965 LauncherSettings.Favorites.ICON_TYPE);
966 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
967 final int iconPackageIndex = c.getColumnIndexOrThrow(
968 LauncherSettings.Favorites.ICON_PACKAGE);
969 final int iconResourceIndex = c.getColumnIndexOrThrow(
970 LauncherSettings.Favorites.ICON_RESOURCE);
971 final int containerIndex = c.getColumnIndexOrThrow(
972 LauncherSettings.Favorites.CONTAINER);
973 final int itemTypeIndex = c.getColumnIndexOrThrow(
974 LauncherSettings.Favorites.ITEM_TYPE);
975 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
976 LauncherSettings.Favorites.APPWIDGET_ID);
977 final int screenIndex = c.getColumnIndexOrThrow(
978 LauncherSettings.Favorites.SCREEN);
979 final int cellXIndex = c.getColumnIndexOrThrow
980 (LauncherSettings.Favorites.CELLX);
981 final int cellYIndex = c.getColumnIndexOrThrow
982 (LauncherSettings.Favorites.CELLY);
983 final int spanXIndex = c.getColumnIndexOrThrow
984 (LauncherSettings.Favorites.SPANX);
985 final int spanYIndex = c.getColumnIndexOrThrow(
986 LauncherSettings.Favorites.SPANY);
987 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
988 final int displayModeIndex = c.getColumnIndexOrThrow(
989 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990
Joe Onorato36115782010-06-17 13:28:48 -0400991 ShortcutInfo info;
992 String intentDescription;
993 LauncherAppWidgetInfo appWidgetInfo;
994 int container;
995 long id;
996 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997
Joe Onorato36115782010-06-17 13:28:48 -0400998 while (!mStopped && c.moveToNext()) {
999 try {
1000 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001
Joe Onorato36115782010-06-17 13:28:48 -04001002 switch (itemType) {
1003 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1004 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1005 intentDescription = c.getString(intentIndex);
1006 try {
1007 intent = Intent.parseUri(intentDescription, 0);
1008 } catch (URISyntaxException e) {
1009 continue;
1010 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011
Joe Onorato36115782010-06-17 13:28:48 -04001012 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1013 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -07001014 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001015 } else {
1016 info = getShortcutInfo(c, context, iconTypeIndex,
1017 iconPackageIndex, iconResourceIndex, iconIndex,
1018 titleIndex);
1019 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020
Joe Onorato36115782010-06-17 13:28:48 -04001021 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001022 info.intent = intent;
1023 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001025 info.container = container;
1026 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001027 info.cellX = c.getInt(cellXIndex);
1028 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001029
Daniel Sandler8802e962010-05-26 16:28:16 -04001030 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001031 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001032 break;
1033 }
1034
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001036 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001037 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001038 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001039 break;
1040 default:
1041 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001042 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001043 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001044 folderInfo.add(info);
1045 break;
1046 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001047 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001048
1049 // now that we've loaded everthing re-save it with the
1050 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001051 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001052 } else {
1053 // Failed to load the shortcut, probably because the
1054 // activity manager couldn't resolve it (maybe the app
1055 // was uninstalled), or the db row was somehow screwed up.
1056 // Delete it.
1057 id = c.getLong(idIndex);
1058 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1059 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1060 id, false), null, null);
1061 }
1062 break;
1063
Adam Cohendf2cc412011-04-27 16:56:57 -07001064 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001065 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001066 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001067
Winson Chungaafa03c2010-06-11 17:34:16 -07001068 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001069 folderInfo.id = id;
1070 container = c.getInt(containerIndex);
1071 folderInfo.container = container;
1072 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001073 folderInfo.cellX = c.getInt(cellXIndex);
1074 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001075
1076 // check & update map of what's occupied
1077 if (!checkItemPlacement(occupied, folderInfo)) {
1078 break;
1079 }
Joe Onorato36115782010-06-17 13:28:48 -04001080 switch (container) {
1081 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001082 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001083 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001084 break;
1085 }
1086
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001087 sItemsIdMap.put(folderInfo.id, folderInfo);
1088 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001089 break;
1090
Joe Onorato36115782010-06-17 13:28:48 -04001091 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1092 // Read all Launcher-specific widget details
1093 int appWidgetId = c.getInt(appWidgetIdIndex);
1094 id = c.getLong(idIndex);
1095
1096 final AppWidgetProviderInfo provider =
1097 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001098
Joe Onorato36115782010-06-17 13:28:48 -04001099 if (!isSafeMode && (provider == null || provider.provider == null ||
1100 provider.provider.getPackageName() == null)) {
1101 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1102 + id + " appWidgetId=" + appWidgetId);
1103 itemsToRemove.add(id);
1104 } else {
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001105 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, "5");
Joe Onorato36115782010-06-17 13:28:48 -04001106 appWidgetInfo.id = id;
1107 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001108 appWidgetInfo.cellX = c.getInt(cellXIndex);
1109 appWidgetInfo.cellY = c.getInt(cellYIndex);
1110 appWidgetInfo.spanX = c.getInt(spanXIndex);
1111 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001112
1113 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001114 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1115 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001116 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001117 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001118 continue;
1119 }
1120 appWidgetInfo.container = c.getInt(containerIndex);
1121
1122 // check & update map of what's occupied
1123 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1124 break;
1125 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001126 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1127 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001128 }
1129 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001130 }
Joe Onorato36115782010-06-17 13:28:48 -04001131 } catch (Exception e) {
1132 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001133 }
1134 }
Joe Onorato36115782010-06-17 13:28:48 -04001135 } finally {
1136 c.close();
1137 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001138
Joe Onorato36115782010-06-17 13:28:48 -04001139 if (itemsToRemove.size() > 0) {
1140 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1141 LauncherSettings.Favorites.CONTENT_URI);
1142 // Remove dead items
1143 for (long id : itemsToRemove) {
1144 if (DEBUG_LOADERS) {
1145 Log.d(TAG, "Removed id = " + id);
1146 }
1147 // Don't notify content observers
1148 try {
1149 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1150 null, null);
1151 } catch (RemoteException e) {
1152 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001153 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001155 }
1156
Joe Onorato36115782010-06-17 13:28:48 -04001157 if (DEBUG_LOADERS) {
1158 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1159 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001160 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001161 String line = "";
1162 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1163 if (s > 0) {
1164 line += " | ";
1165 }
Adam Cohend22015c2010-07-26 22:02:18 -07001166 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001167 line += ((occupied[s][x][y] != null) ? "#" : ".");
1168 }
1169 }
1170 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001171 }
Joe Onorato36115782010-06-17 13:28:48 -04001172 }
1173 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001174
Joe Onorato36115782010-06-17 13:28:48 -04001175 /**
1176 * Read everything out of our database.
1177 */
1178 private void bindWorkspace() {
1179 final long t = SystemClock.uptimeMillis();
1180
1181 // Don't use these two variables in any of the callback runnables.
1182 // Otherwise we hold a reference to them.
1183 final Callbacks oldCallbacks = mCallbacks.get();
1184 if (oldCallbacks == null) {
1185 // This launcher has exited and nobody bothered to tell us. Just bail.
1186 Log.w(TAG, "LoaderTask running with no launcher");
1187 return;
1188 }
1189
1190 int N;
1191 // Tell the workspace that we're about to start firing items at it
1192 mHandler.post(new Runnable() {
1193 public void run() {
1194 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1195 if (callbacks != null) {
1196 callbacks.startBinding();
1197 }
1198 }
1199 });
Winson Chung603bcb92011-09-02 11:45:39 -07001200
1201 // Unbind previously bound workspace items to prevent a leak of AppWidgetHostViews.
1202 final ArrayList<ItemInfo> workspaceItems = unbindWorkspaceItemsOnMainThread();
1203
Joe Onorato36115782010-06-17 13:28:48 -04001204 // Add the items to the workspace.
Winson Chung603bcb92011-09-02 11:45:39 -07001205 N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001206 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1207 final int start = i;
1208 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 mHandler.post(new Runnable() {
1210 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001211 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001213 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 }
1215 }
1216 });
Joe Onorato36115782010-06-17 13:28:48 -04001217 }
Winson Chung603bcb92011-09-02 11:45:39 -07001218 // Ensure that we don't use the same folders data structure on the main thread
1219 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001220 mHandler.post(new Runnable() {
1221 public void run() {
1222 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1223 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001224 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001225 }
1226 }
1227 });
1228 // Wait until the queue goes empty.
1229 mHandler.post(new Runnable() {
1230 public void run() {
1231 if (DEBUG_LOADERS) {
1232 Log.d(TAG, "Going to start binding widgets soon.");
1233 }
1234 }
1235 });
1236 // Bind the widgets, one at a time.
1237 // WARNING: this is calling into the workspace from the background thread,
1238 // but since getCurrentScreen() just returns the int, we should be okay. This
1239 // is just a hint for the order, and if it's wrong, we'll be okay.
1240 // TODO: instead, we should have that push the current screen into here.
1241 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001242 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001243 // once for the current screen
1244 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001245 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001246 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 mHandler.post(new Runnable() {
1248 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001249 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001251 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252 }
1253 }
1254 });
1255 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 }
Joe Onorato36115782010-06-17 13:28:48 -04001257 // once for the other screens
1258 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001259 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001260 if (widget.screen != currentScreen) {
1261 mHandler.post(new Runnable() {
1262 public void run() {
1263 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1264 if (callbacks != null) {
1265 callbacks.bindAppWidget(widget);
1266 }
1267 }
1268 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001269 }
1270 }
Joe Onorato36115782010-06-17 13:28:48 -04001271 // Tell the workspace that we're done.
1272 mHandler.post(new Runnable() {
1273 public void run() {
1274 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1275 if (callbacks != null) {
1276 callbacks.finishBindingItems();
1277 }
1278 }
1279 });
1280 // If we're profiling, this is the last thing in the queue.
1281 mHandler.post(new Runnable() {
1282 public void run() {
1283 if (DEBUG_LOADERS) {
1284 Log.d(TAG, "bound workspace in "
1285 + (SystemClock.uptimeMillis()-t) + "ms");
1286 }
1287 }
1288 });
1289 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001290
Joe Onorato36115782010-06-17 13:28:48 -04001291 private void loadAndBindAllApps() {
1292 if (DEBUG_LOADERS) {
1293 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1294 }
1295 if (!mAllAppsLoaded) {
1296 loadAllAppsByBatch();
1297 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001298 return;
1299 }
Joe Onorato36115782010-06-17 13:28:48 -04001300 mAllAppsLoaded = true;
1301 } else {
1302 onlyBindAllApps();
1303 }
1304 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001305
Joe Onorato36115782010-06-17 13:28:48 -04001306 private void onlyBindAllApps() {
1307 final Callbacks oldCallbacks = mCallbacks.get();
1308 if (oldCallbacks == null) {
1309 // This launcher has exited and nobody bothered to tell us. Just bail.
1310 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1311 return;
1312 }
1313
1314 // shallow copy
1315 final ArrayList<ApplicationInfo> list
1316 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1317 mHandler.post(new Runnable() {
1318 public void run() {
1319 final long t = SystemClock.uptimeMillis();
1320 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1321 if (callbacks != null) {
1322 callbacks.bindAllApplications(list);
1323 }
1324 if (DEBUG_LOADERS) {
1325 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1326 + (SystemClock.uptimeMillis()-t) + "ms");
1327 }
1328 }
1329 });
1330
1331 }
1332
1333 private void loadAllAppsByBatch() {
1334 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1335
1336 // Don't use these two variables in any of the callback runnables.
1337 // Otherwise we hold a reference to them.
1338 final Callbacks oldCallbacks = mCallbacks.get();
1339 if (oldCallbacks == null) {
1340 // This launcher has exited and nobody bothered to tell us. Just bail.
1341 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1342 return;
1343 }
1344
1345 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1346 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1347
1348 final PackageManager packageManager = mContext.getPackageManager();
1349 List<ResolveInfo> apps = null;
1350
1351 int N = Integer.MAX_VALUE;
1352
1353 int startIndex;
1354 int i=0;
1355 int batchSize = -1;
1356 while (i < N && !mStopped) {
1357 if (i == 0) {
1358 mAllAppsList.clear();
1359 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1360 apps = packageManager.queryIntentActivities(mainIntent, 0);
1361 if (DEBUG_LOADERS) {
1362 Log.d(TAG, "queryIntentActivities took "
1363 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1364 }
1365 if (apps == null) {
1366 return;
1367 }
1368 N = apps.size();
1369 if (DEBUG_LOADERS) {
1370 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1371 }
1372 if (N == 0) {
1373 // There are no apps?!?
1374 return;
1375 }
1376 if (mBatchSize == 0) {
1377 batchSize = N;
1378 } else {
1379 batchSize = mBatchSize;
1380 }
1381
1382 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1383 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001384 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001385 if (DEBUG_LOADERS) {
1386 Log.d(TAG, "sort took "
1387 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1388 }
1389 }
1390
1391 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1392
1393 startIndex = i;
1394 for (int j=0; i<N && j<batchSize; j++) {
1395 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001396 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001397 mIconCache, mLabelCache, "6"));
Joe Onorato36115782010-06-17 13:28:48 -04001398 i++;
1399 }
1400
1401 final boolean first = i <= batchSize;
1402 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1403 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1404 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1405
Joe Onoratocc67f472010-06-08 10:54:30 -07001406 mHandler.post(new Runnable() {
1407 public void run() {
1408 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001409 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001410 if (first) {
1411 callbacks.bindAllApplications(added);
1412 } else {
1413 callbacks.bindAppsAdded(added);
1414 }
1415 if (DEBUG_LOADERS) {
1416 Log.d(TAG, "bound " + added.size() + " apps in "
1417 + (SystemClock.uptimeMillis() - t) + "ms");
1418 }
1419 } else {
1420 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001421 }
1422 }
1423 });
1424
Daniel Sandlerdca66122010-04-13 16:23:58 -04001425 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001426 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1427 + (SystemClock.uptimeMillis()-t2) + "ms");
1428 }
1429
1430 if (mAllAppsLoadDelay > 0 && i < N) {
1431 try {
1432 if (DEBUG_LOADERS) {
1433 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1434 }
1435 Thread.sleep(mAllAppsLoadDelay);
1436 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 }
1438 }
1439
Joe Onorato36115782010-06-17 13:28:48 -04001440 if (DEBUG_LOADERS) {
1441 Log.d(TAG, "cached all " + N + " apps in "
1442 + (SystemClock.uptimeMillis()-t) + "ms"
1443 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001444 }
1445 }
1446
1447 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001448 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1449 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1450 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1451 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1452 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001453 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001454 }
1455 }
1456
1457 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001458 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001459 }
1460
1461 private class PackageUpdatedTask implements Runnable {
1462 int mOp;
1463 String[] mPackages;
1464
1465 public static final int OP_NONE = 0;
1466 public static final int OP_ADD = 1;
1467 public static final int OP_UPDATE = 2;
1468 public static final int OP_REMOVE = 3; // uninstlled
1469 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1470
1471
1472 public PackageUpdatedTask(int op, String[] packages) {
1473 mOp = op;
1474 mPackages = packages;
1475 }
1476
1477 public void run() {
1478 final Context context = mApp;
1479
1480 final String[] packages = mPackages;
1481 final int N = packages.length;
1482 switch (mOp) {
1483 case OP_ADD:
1484 for (int i=0; i<N; i++) {
1485 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1486 mAllAppsList.addPackage(context, packages[i]);
1487 }
1488 break;
1489 case OP_UPDATE:
1490 for (int i=0; i<N; i++) {
1491 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1492 mAllAppsList.updatePackage(context, packages[i]);
1493 }
1494 break;
1495 case OP_REMOVE:
1496 case OP_UNAVAILABLE:
1497 for (int i=0; i<N; i++) {
1498 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1499 mAllAppsList.removePackage(packages[i]);
1500 }
1501 break;
1502 }
1503
1504 ArrayList<ApplicationInfo> added = null;
1505 ArrayList<ApplicationInfo> removed = null;
1506 ArrayList<ApplicationInfo> modified = null;
1507
1508 if (mAllAppsList.added.size() > 0) {
1509 added = mAllAppsList.added;
1510 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1511 }
1512 if (mAllAppsList.removed.size() > 0) {
1513 removed = mAllAppsList.removed;
1514 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1515 for (ApplicationInfo info: removed) {
1516 mIconCache.remove(info.intent.getComponent());
1517 }
1518 }
1519 if (mAllAppsList.modified.size() > 0) {
1520 modified = mAllAppsList.modified;
1521 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1522 }
1523
1524 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1525 if (callbacks == null) {
1526 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1527 return;
1528 }
1529
1530 if (added != null) {
1531 final ArrayList<ApplicationInfo> addedFinal = added;
1532 mHandler.post(new Runnable() {
1533 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001534 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1535 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001536 callbacks.bindAppsAdded(addedFinal);
1537 }
1538 }
1539 });
1540 }
1541 if (modified != null) {
1542 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1543 mHandler.post(new Runnable() {
1544 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001545 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1546 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001547 callbacks.bindAppsUpdated(modifiedFinal);
1548 }
1549 }
1550 });
1551 }
1552 if (removed != null) {
1553 final boolean permanent = mOp != OP_UNAVAILABLE;
1554 final ArrayList<ApplicationInfo> removedFinal = removed;
1555 mHandler.post(new Runnable() {
1556 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001557 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1558 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001559 callbacks.bindAppsRemoved(removedFinal, permanent);
1560 }
1561 }
1562 });
Joe Onoratobe386092009-11-17 17:32:16 -08001563 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001564
1565 mHandler.post(new Runnable() {
1566 @Override
1567 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001568 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1569 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001570 callbacks.bindPackagesUpdated();
1571 }
1572 }
1573 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 }
1575 }
1576
1577 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001578 * This is called from the code that adds shortcuts from the intent receiver. This
1579 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 */
Joe Onorato56d82912010-03-07 14:32:10 -05001581 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001582 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001583 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001584
Joe Onorato56d82912010-03-07 14:32:10 -05001585 /**
1586 * Make an ShortcutInfo object for a shortcut that is an application.
1587 *
1588 * If c is not null, then it will be used to fill in missing data like the title and icon.
1589 */
1590 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001591 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001592 Bitmap icon = null;
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001593 final ShortcutInfo info = new ShortcutInfo("7");
Joe Onorato56d82912010-03-07 14:32:10 -05001594
1595 ComponentName componentName = intent.getComponent();
1596 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001597 return null;
1598 }
1599
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001600 // TODO: See if the PackageManager knows about this case. If it doesn't
1601 // then return null & delete this.
1602
Joe Onorato56d82912010-03-07 14:32:10 -05001603 // the resource -- This may implicitly give us back the fallback icon,
1604 // but don't worry about that. All we're doing with usingFallbackIcon is
1605 // to avoid saving lots of copies of that in the database, and most apps
1606 // have icons anyway.
1607 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1608 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001609 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001610 }
Joe Onorato56d82912010-03-07 14:32:10 -05001611 // the db
1612 if (icon == null) {
1613 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001614 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001615 }
1616 }
1617 // the fallback icon
1618 if (icon == null) {
1619 icon = getFallbackIcon();
1620 info.usingFallbackIcon = true;
1621 }
1622 info.setIcon(icon);
1623
1624 // from the resource
1625 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001626 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1627 if (labelCache != null && labelCache.containsKey(key)) {
1628 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001629 } else {
1630 info.title = resolveInfo.activityInfo.loadLabel(manager);
1631 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001632 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001633 }
1634 }
Joe Onorato56d82912010-03-07 14:32:10 -05001635 }
1636 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001637 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001638 if (c != null) {
1639 info.title = c.getString(titleIndex);
1640 }
1641 }
1642 // fall back to the class name of the activity
1643 if (info.title == null) {
1644 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001646 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1647 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001651 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001653 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001654 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1655 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656
Joe Onorato56d82912010-03-07 14:32:10 -05001657 Bitmap icon = null;
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001658 final ShortcutInfo info = new ShortcutInfo("8");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001661 // TODO: If there's an explicit component and we can't install that, delete it.
1662
Joe Onorato56d82912010-03-07 14:32:10 -05001663 info.title = c.getString(titleIndex);
1664
Joe Onorato9c1289c2009-08-17 11:03:03 -04001665 int iconType = c.getInt(iconTypeIndex);
1666 switch (iconType) {
1667 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1668 String packageName = c.getString(iconPackageIndex);
1669 String resourceName = c.getString(iconResourceIndex);
1670 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001671 info.customIcon = false;
1672 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001674 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001675 if (resources != null) {
1676 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001677 icon = Utilities.createIconBitmap(
1678 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001679 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001680 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001681 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
Joe Onorato56d82912010-03-07 14:32:10 -05001683 // the db
1684 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001685 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001686 }
1687 // the fallback icon
1688 if (icon == null) {
1689 icon = getFallbackIcon();
1690 info.usingFallbackIcon = true;
1691 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001692 break;
1693 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001694 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001695 if (icon == null) {
1696 icon = getFallbackIcon();
1697 info.customIcon = false;
1698 info.usingFallbackIcon = true;
1699 } else {
1700 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001701 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001702 break;
1703 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001704 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001705 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001706 info.customIcon = false;
1707 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001709 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001710 return info;
1711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712
Michael Jurka931dc972011-08-05 15:08:15 -07001713 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001714 if (false) {
1715 Log.d(TAG, "getIconFromCursor app="
1716 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1717 }
1718 byte[] data = c.getBlob(iconIndex);
1719 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001720 return Utilities.createIconBitmap(
1721 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001722 } catch (Exception e) {
1723 return null;
1724 }
1725 }
1726
Winson Chung3d503fb2011-07-13 17:25:49 -07001727 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1728 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001729 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001730 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001731
1732 return info;
1733 }
1734
Winson Chunga9abd0e2010-10-27 17:18:37 -07001735 /**
Winson Chung55cef262010-10-28 14:14:18 -07001736 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1737 */
1738 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1739 ComponentName component) {
1740 List<AppWidgetProviderInfo> widgets =
1741 AppWidgetManager.getInstance(context).getInstalledProviders();
1742 for (AppWidgetProviderInfo info : widgets) {
1743 if (info.provider.equals(component)) {
1744 return info;
1745 }
1746 }
1747 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001748 }
1749
Winson Chung68846fd2010-10-29 11:00:27 -07001750 /**
1751 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1752 */
1753 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1754 final PackageManager packageManager = context.getPackageManager();
1755 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1756 new ArrayList<WidgetMimeTypeHandlerData>();
1757
1758 final Intent supportsIntent =
1759 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1760 supportsIntent.setType(mimeType);
1761
1762 // Create a set of widget configuration components that we can test against
1763 final List<AppWidgetProviderInfo> widgets =
1764 AppWidgetManager.getInstance(context).getInstalledProviders();
1765 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1766 new HashMap<ComponentName, AppWidgetProviderInfo>();
1767 for (AppWidgetProviderInfo info : widgets) {
1768 configurationComponentToWidget.put(info.configure, info);
1769 }
1770
1771 // Run through each of the intents that can handle this type of clip data, and cross
1772 // reference them with the components that are actual configuration components
1773 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1774 PackageManager.MATCH_DEFAULT_ONLY);
1775 for (ResolveInfo info : activities) {
1776 final ActivityInfo activityInfo = info.activityInfo;
1777 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1778 activityInfo.name);
1779 if (configurationComponentToWidget.containsKey(infoComponent)) {
1780 supportedConfigurationActivities.add(
1781 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1782 configurationComponentToWidget.get(infoComponent)));
1783 }
1784 }
1785 return supportedConfigurationActivities;
1786 }
1787
Winson Chunga9abd0e2010-10-27 17:18:37 -07001788 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001789 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1790 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1791 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1792
1793 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001794 boolean customIcon = false;
1795 ShortcutIconResource iconResource = null;
1796
1797 if (bitmap != null && bitmap instanceof Bitmap) {
1798 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001799 customIcon = true;
1800 } else {
1801 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1802 if (extra != null && extra instanceof ShortcutIconResource) {
1803 try {
1804 iconResource = (ShortcutIconResource) extra;
1805 final PackageManager packageManager = context.getPackageManager();
1806 Resources resources = packageManager.getResourcesForApplication(
1807 iconResource.packageName);
1808 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001809 icon = Utilities.createIconBitmap(
1810 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001811 } catch (Exception e) {
1812 Log.w(TAG, "Could not load shortcut icon: " + extra);
1813 }
1814 }
1815 }
1816
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001817 final ShortcutInfo info = new ShortcutInfo("9");
Joe Onorato56d82912010-03-07 14:32:10 -05001818
1819 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001820 if (fallbackIcon != null) {
1821 icon = fallbackIcon;
1822 } else {
1823 icon = getFallbackIcon();
1824 info.usingFallbackIcon = true;
1825 }
Joe Onorato56d82912010-03-07 14:32:10 -05001826 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001827 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001828
Joe Onorato0589f0f2010-02-08 13:44:00 -08001829 info.title = name;
1830 info.intent = intent;
1831 info.customIcon = customIcon;
1832 info.iconResource = iconResource;
1833
1834 return info;
1835 }
1836
Winson Chungaac01e12011-08-17 10:37:13 -07001837 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1838 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001839 // If apps can't be on SD, don't even bother.
1840 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001841 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001842 }
Joe Onorato56d82912010-03-07 14:32:10 -05001843 // If this icon doesn't have a custom icon, check to see
1844 // what's stored in the DB, and if it doesn't match what
1845 // we're going to show, store what we are going to show back
1846 // into the DB. We do this so when we're loading, if the
1847 // package manager can't find an icon (for example because
1848 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001849 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001850 cache.put(info, c.getBlob(iconIndex));
1851 return true;
1852 }
1853 return false;
1854 }
1855 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1856 boolean needSave = false;
1857 try {
1858 if (data != null) {
1859 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1860 Bitmap loaded = info.getIcon(mIconCache);
1861 needSave = !saved.sameAs(loaded);
1862 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001863 needSave = true;
1864 }
Winson Chungaac01e12011-08-17 10:37:13 -07001865 } catch (Exception e) {
1866 needSave = true;
1867 }
1868 if (needSave) {
1869 Log.d(TAG, "going to save icon bitmap for info=" + info);
1870 // This is slower than is ideal, but this only happens once
1871 // or when the app is updated with a new icon.
1872 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001873 }
1874 }
1875
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001877 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001878 * or make a new one.
1879 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001880 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001881 // See if a placeholder was created for us already
1882 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001883 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001884 // No placeholder -- create a new instance
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001885 folderInfo = new FolderInfo("10");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001886 folders.put(id, folderInfo);
1887 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001888 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890
Joe Onorato9c1289c2009-08-17 11:03:03 -04001891 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001892 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001893 = new Comparator<ApplicationInfo>() {
1894 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001895 int result = sCollator.compare(a.title.toString(), b.title.toString());
1896 if (result == 0) {
1897 result = a.componentName.compareTo(b.componentName);
1898 }
1899 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001901 };
Winson Chung78403fe2011-01-21 15:38:02 -08001902 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1903 = new Comparator<ApplicationInfo>() {
1904 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1905 if (a.firstInstallTime < b.firstInstallTime) return 1;
1906 if (a.firstInstallTime > b.firstInstallTime) return -1;
1907 return 0;
1908 }
1909 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001910 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1911 = new Comparator<AppWidgetProviderInfo>() {
1912 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1913 return sCollator.compare(a.label.toString(), b.label.toString());
1914 }
1915 };
Winson Chung5308f242011-08-18 12:12:41 -07001916 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1917 if (info.activityInfo != null) {
1918 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1919 } else {
1920 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1921 }
1922 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001923 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1924 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001925 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001926 ShortcutNameComparator(PackageManager pm) {
1927 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001928 mLabelCache = new HashMap<Object, CharSequence>();
1929 }
1930 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1931 mPackageManager = pm;
1932 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001933 }
1934 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001935 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001936 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1937 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1938 if (mLabelCache.containsKey(keyA)) {
1939 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001940 } else {
1941 labelA = a.loadLabel(mPackageManager).toString();
1942
Winson Chung5308f242011-08-18 12:12:41 -07001943 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001944 }
Winson Chung5308f242011-08-18 12:12:41 -07001945 if (mLabelCache.containsKey(keyB)) {
1946 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001947 } else {
1948 labelB = b.loadLabel(mPackageManager).toString();
1949
Winson Chung5308f242011-08-18 12:12:41 -07001950 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001951 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001952 return sCollator.compare(labelA, labelB);
1953 }
1954 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001955 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1956 private PackageManager mPackageManager;
1957 private HashMap<Object, String> mLabelCache;
1958 WidgetAndShortcutNameComparator(PackageManager pm) {
1959 mPackageManager = pm;
1960 mLabelCache = new HashMap<Object, String>();
1961 }
1962 public final int compare(Object a, Object b) {
1963 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001964 if (mLabelCache.containsKey(a)) {
1965 labelA = mLabelCache.get(a);
1966 } else {
1967 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001968 ((AppWidgetProviderInfo) a).label :
1969 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001970 mLabelCache.put(a, labelA);
1971 }
1972 if (mLabelCache.containsKey(b)) {
1973 labelB = mLabelCache.get(b);
1974 } else {
1975 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001976 ((AppWidgetProviderInfo) b).label :
1977 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001978 mLabelCache.put(b, labelB);
1979 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001980 return sCollator.compare(labelA, labelB);
1981 }
1982 };
Joe Onoratobe386092009-11-17 17:32:16 -08001983
1984 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001985 Log.d(TAG, "mCallbacks=" + mCallbacks);
1986 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1987 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1988 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1989 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001990 if (mLoaderTask != null) {
1991 mLoaderTask.dumpState();
1992 } else {
1993 Log.d(TAG, "mLoaderTask=null");
1994 }
Joe Onoratobe386092009-11-17 17:32:16 -08001995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996}