blob: 080d9976c00c0d565bdcc823f04c6cd73087c91e [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);
Winson Chung7ed37742011-09-08 15:45:51 -0700689 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700690 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 }
693 }
694
Joe Onorato36115782010-06-17 13:28:48 -0400695 public void stopLoader() {
696 synchronized (mLock) {
697 if (mLoaderTask != null) {
698 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400699 }
700 }
Joe Onorato36115782010-06-17 13:28:48 -0400701 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702
Michael Jurkac57b7a82011-08-09 22:02:20 -0700703 public boolean isAllAppsLoaded() {
704 return mAllAppsLoaded;
705 }
706
Joe Onorato36115782010-06-17 13:28:48 -0400707 /**
708 * Runnable for the thread that loads the contents of the launcher:
709 * - workspace icons
710 * - widgets
711 * - all apps icons
712 */
713 private class LoaderTask implements Runnable {
714 private Context mContext;
715 private Thread mWaitThread;
716 private boolean mIsLaunching;
717 private boolean mStopped;
718 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700719 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400720
721 LoaderTask(Context context, boolean isLaunching) {
722 mContext = context;
723 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700724 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 }
726
Joe Onorato36115782010-06-17 13:28:48 -0400727 boolean isLaunching() {
728 return mIsLaunching;
729 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400730
Joe Onorato36115782010-06-17 13:28:48 -0400731 private void loadAndBindWorkspace() {
732 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400733 if (DEBUG_LOADERS) {
734 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700736
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700737 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400738 loadWorkspace();
739 if (mStopped) {
740 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 }
Joe Onorato36115782010-06-17 13:28:48 -0400742 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743 }
744
Joe Onorato36115782010-06-17 13:28:48 -0400745 // Bind the workspace
746 bindWorkspace();
747 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400748
Joe Onorato36115782010-06-17 13:28:48 -0400749 private void waitForIdle() {
750 // Wait until the either we're stopped or the other threads are done.
751 // This way we don't start loading all apps until the workspace has settled
752 // down.
753 synchronized (LoaderTask.this) {
754 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700755
Joe Onorato36115782010-06-17 13:28:48 -0400756 mHandler.postIdle(new Runnable() {
757 public void run() {
758 synchronized (LoaderTask.this) {
759 mLoadAndBindStepFinished = true;
760 if (DEBUG_LOADERS) {
761 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400762 }
Joe Onorato36115782010-06-17 13:28:48 -0400763 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400764 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400765 }
Joe Onorato36115782010-06-17 13:28:48 -0400766 });
767
768 while (!mStopped && !mLoadAndBindStepFinished) {
769 try {
770 this.wait();
771 } catch (InterruptedException ex) {
772 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400773 }
774 }
Joe Onorato36115782010-06-17 13:28:48 -0400775 if (DEBUG_LOADERS) {
776 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700777 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400778 + "ms for previous step to finish binding");
779 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400780 }
Joe Onorato36115782010-06-17 13:28:48 -0400781 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400782
Joe Onorato36115782010-06-17 13:28:48 -0400783 public void run() {
784 // Optimize for end-user experience: if the Launcher is up and // running with the
785 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
786 // workspace first (default).
787 final Callbacks cbk = mCallbacks.get();
788 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400789
Joe Onorato36115782010-06-17 13:28:48 -0400790 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400791 // Elevate priority when Home launches for the first time to avoid
792 // starving at boot time. Staring at a blank home is not cool.
793 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700794 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
795 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400796 android.os.Process.setThreadPriority(mIsLaunching
797 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
798 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400799 if (loadWorkspaceFirst) {
800 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
801 loadAndBindWorkspace();
802 } else {
803 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700804 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400805 }
806
Joe Onorato36115782010-06-17 13:28:48 -0400807 if (mStopped) {
808 break keep_running;
809 }
810
811 // Whew! Hard work done. Slow us down, and wait until the UI thread has
812 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400813 synchronized (mLock) {
814 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700815 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400816 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
817 }
818 }
Joe Onorato36115782010-06-17 13:28:48 -0400819 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400820
821 // second step
822 if (loadWorkspaceFirst) {
823 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700824 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400825 } else {
826 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
827 loadAndBindWorkspace();
828 }
Winson Chung7ed37742011-09-08 15:45:51 -0700829
830 // Restore the default thread priority after we are done loading items
831 synchronized (mLock) {
832 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
833 }
Joe Onorato36115782010-06-17 13:28:48 -0400834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400835
Winson Chungaac01e12011-08-17 10:37:13 -0700836
837 // Update the saved icons if necessary
838 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700839 for (Object key : sDbIconCache.keySet()) {
840 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700841 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700842 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700843
Joe Onorato36115782010-06-17 13:28:48 -0400844 // Clear out this reference, otherwise we end up holding it until all of the
845 // callback runnables are done.
846 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400847
Joe Onorato36115782010-06-17 13:28:48 -0400848 synchronized (mLock) {
849 // If we are still the last one to be scheduled, remove ourselves.
850 if (mLoaderTask == this) {
851 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852 }
Joe Onorato36115782010-06-17 13:28:48 -0400853 }
Joe Onorato36115782010-06-17 13:28:48 -0400854 }
855
856 public void stopLocked() {
857 synchronized (LoaderTask.this) {
858 mStopped = true;
859 this.notify();
860 }
861 }
862
863 /**
864 * Gets the callbacks object. If we've been stopped, or if the launcher object
865 * has somehow been garbage collected, return null instead. Pass in the Callbacks
866 * object that was around when the deferred message was scheduled, and if there's
867 * a new Callbacks object around then also return null. This will save us from
868 * calling onto it with data that will be ignored.
869 */
870 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
871 synchronized (mLock) {
872 if (mStopped) {
873 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400874 }
Joe Onorato36115782010-06-17 13:28:48 -0400875
876 if (mCallbacks == null) {
877 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400878 }
Joe Onorato36115782010-06-17 13:28:48 -0400879
880 final Callbacks callbacks = mCallbacks.get();
881 if (callbacks != oldCallbacks) {
882 return null;
883 }
884 if (callbacks == null) {
885 Log.w(TAG, "no mCallbacks");
886 return null;
887 }
888
889 return callbacks;
890 }
891 }
892
893 // check & update map of what's occupied; used to discard overlapping/invalid items
894 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700895 int containerIndex = item.screen;
896 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700897 // Return early if we detect that an item is under the hotseat button
898 if (Hotseat.isAllAppsButtonRank(item.screen)) {
899 return false;
900 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700901
902 // We use the last index to refer to the hotseat and the screen as the rank, so
903 // test and update the occupied state accordingly
904 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
905 Log.e(TAG, "Error loading shortcut into hotseat " + item
906 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
907 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
908 return false;
909 } else {
910 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
911 return true;
912 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700913 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
914 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400915 return true;
916 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700917
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700918 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -0400919 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
920 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700921 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400922 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700923 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400924 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700925 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700926 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400927 return false;
928 }
929 }
930 }
931 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
932 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700933 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400934 }
935 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700936
Joe Onorato36115782010-06-17 13:28:48 -0400937 return true;
938 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939
Joe Onorato36115782010-06-17 13:28:48 -0400940 private void loadWorkspace() {
941 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942
Joe Onorato36115782010-06-17 13:28:48 -0400943 final Context context = mContext;
944 final ContentResolver contentResolver = context.getContentResolver();
945 final PackageManager manager = context.getPackageManager();
946 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
947 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400948
Adam Cohen4eac29a2011-07-11 17:53:37 -0700949 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700950 sAppWidgets.clear();
951 sFolders.clear();
952 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700953 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800954
Joe Onorato36115782010-06-17 13:28:48 -0400955 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956
Joe Onorato36115782010-06-17 13:28:48 -0400957 final Cursor c = contentResolver.query(
958 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400959
Winson Chungf30ad5f2011-08-08 10:55:42 -0700960 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700961 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700962 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963
Joe Onorato36115782010-06-17 13:28:48 -0400964 try {
965 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
966 final int intentIndex = c.getColumnIndexOrThrow
967 (LauncherSettings.Favorites.INTENT);
968 final int titleIndex = c.getColumnIndexOrThrow
969 (LauncherSettings.Favorites.TITLE);
970 final int iconTypeIndex = c.getColumnIndexOrThrow(
971 LauncherSettings.Favorites.ICON_TYPE);
972 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
973 final int iconPackageIndex = c.getColumnIndexOrThrow(
974 LauncherSettings.Favorites.ICON_PACKAGE);
975 final int iconResourceIndex = c.getColumnIndexOrThrow(
976 LauncherSettings.Favorites.ICON_RESOURCE);
977 final int containerIndex = c.getColumnIndexOrThrow(
978 LauncherSettings.Favorites.CONTAINER);
979 final int itemTypeIndex = c.getColumnIndexOrThrow(
980 LauncherSettings.Favorites.ITEM_TYPE);
981 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
982 LauncherSettings.Favorites.APPWIDGET_ID);
983 final int screenIndex = c.getColumnIndexOrThrow(
984 LauncherSettings.Favorites.SCREEN);
985 final int cellXIndex = c.getColumnIndexOrThrow
986 (LauncherSettings.Favorites.CELLX);
987 final int cellYIndex = c.getColumnIndexOrThrow
988 (LauncherSettings.Favorites.CELLY);
989 final int spanXIndex = c.getColumnIndexOrThrow
990 (LauncherSettings.Favorites.SPANX);
991 final int spanYIndex = c.getColumnIndexOrThrow(
992 LauncherSettings.Favorites.SPANY);
993 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
994 final int displayModeIndex = c.getColumnIndexOrThrow(
995 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996
Joe Onorato36115782010-06-17 13:28:48 -0400997 ShortcutInfo info;
998 String intentDescription;
999 LauncherAppWidgetInfo appWidgetInfo;
1000 int container;
1001 long id;
1002 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003
Joe Onorato36115782010-06-17 13:28:48 -04001004 while (!mStopped && c.moveToNext()) {
1005 try {
1006 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007
Joe Onorato36115782010-06-17 13:28:48 -04001008 switch (itemType) {
1009 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1010 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1011 intentDescription = c.getString(intentIndex);
1012 try {
1013 intent = Intent.parseUri(intentDescription, 0);
1014 } catch (URISyntaxException e) {
1015 continue;
1016 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017
Joe Onorato36115782010-06-17 13:28:48 -04001018 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1019 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -07001020 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001021 } else {
1022 info = getShortcutInfo(c, context, iconTypeIndex,
1023 iconPackageIndex, iconResourceIndex, iconIndex,
1024 titleIndex);
1025 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026
Joe Onorato36115782010-06-17 13:28:48 -04001027 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001028 info.intent = intent;
1029 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001030 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001031 info.container = container;
1032 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001033 info.cellX = c.getInt(cellXIndex);
1034 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035
Daniel Sandler8802e962010-05-26 16:28:16 -04001036 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001037 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001038 break;
1039 }
1040
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001042 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001043 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001044 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001045 break;
1046 default:
1047 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001048 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001049 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001050 folderInfo.add(info);
1051 break;
1052 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001053 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001054
1055 // now that we've loaded everthing re-save it with the
1056 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001057 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001058 } else {
1059 // Failed to load the shortcut, probably because the
1060 // activity manager couldn't resolve it (maybe the app
1061 // was uninstalled), or the db row was somehow screwed up.
1062 // Delete it.
1063 id = c.getLong(idIndex);
1064 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1065 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1066 id, false), null, null);
1067 }
1068 break;
1069
Adam Cohendf2cc412011-04-27 16:56:57 -07001070 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001071 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001072 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001073
Winson Chungaafa03c2010-06-11 17:34:16 -07001074 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001075 folderInfo.id = id;
1076 container = c.getInt(containerIndex);
1077 folderInfo.container = container;
1078 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001079 folderInfo.cellX = c.getInt(cellXIndex);
1080 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001081
1082 // check & update map of what's occupied
1083 if (!checkItemPlacement(occupied, folderInfo)) {
1084 break;
1085 }
Joe Onorato36115782010-06-17 13:28:48 -04001086 switch (container) {
1087 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001088 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001089 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001090 break;
1091 }
1092
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001093 sItemsIdMap.put(folderInfo.id, folderInfo);
1094 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001095 break;
1096
Joe Onorato36115782010-06-17 13:28:48 -04001097 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1098 // Read all Launcher-specific widget details
1099 int appWidgetId = c.getInt(appWidgetIdIndex);
1100 id = c.getLong(idIndex);
1101
1102 final AppWidgetProviderInfo provider =
1103 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001104
Joe Onorato36115782010-06-17 13:28:48 -04001105 if (!isSafeMode && (provider == null || provider.provider == null ||
1106 provider.provider.getPackageName() == null)) {
1107 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1108 + id + " appWidgetId=" + appWidgetId);
1109 itemsToRemove.add(id);
1110 } else {
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001111 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, "5");
Joe Onorato36115782010-06-17 13:28:48 -04001112 appWidgetInfo.id = id;
1113 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001114 appWidgetInfo.cellX = c.getInt(cellXIndex);
1115 appWidgetInfo.cellY = c.getInt(cellYIndex);
1116 appWidgetInfo.spanX = c.getInt(spanXIndex);
1117 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001118
1119 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001120 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1121 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001122 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001123 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001124 continue;
1125 }
1126 appWidgetInfo.container = c.getInt(containerIndex);
1127
1128 // check & update map of what's occupied
1129 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1130 break;
1131 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001132 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1133 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001134 }
1135 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001136 }
Joe Onorato36115782010-06-17 13:28:48 -04001137 } catch (Exception e) {
1138 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001139 }
1140 }
Joe Onorato36115782010-06-17 13:28:48 -04001141 } finally {
1142 c.close();
1143 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001144
Joe Onorato36115782010-06-17 13:28:48 -04001145 if (itemsToRemove.size() > 0) {
1146 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1147 LauncherSettings.Favorites.CONTENT_URI);
1148 // Remove dead items
1149 for (long id : itemsToRemove) {
1150 if (DEBUG_LOADERS) {
1151 Log.d(TAG, "Removed id = " + id);
1152 }
1153 // Don't notify content observers
1154 try {
1155 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1156 null, null);
1157 } catch (RemoteException e) {
1158 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001159 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001160 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 }
1162
Joe Onorato36115782010-06-17 13:28:48 -04001163 if (DEBUG_LOADERS) {
1164 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1165 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001166 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001167 String line = "";
1168 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1169 if (s > 0) {
1170 line += " | ";
1171 }
Adam Cohend22015c2010-07-26 22:02:18 -07001172 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001173 line += ((occupied[s][x][y] != null) ? "#" : ".");
1174 }
1175 }
1176 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001177 }
Joe Onorato36115782010-06-17 13:28:48 -04001178 }
1179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180
Joe Onorato36115782010-06-17 13:28:48 -04001181 /**
1182 * Read everything out of our database.
1183 */
1184 private void bindWorkspace() {
1185 final long t = SystemClock.uptimeMillis();
1186
1187 // Don't use these two variables in any of the callback runnables.
1188 // Otherwise we hold a reference to them.
1189 final Callbacks oldCallbacks = mCallbacks.get();
1190 if (oldCallbacks == null) {
1191 // This launcher has exited and nobody bothered to tell us. Just bail.
1192 Log.w(TAG, "LoaderTask running with no launcher");
1193 return;
1194 }
1195
1196 int N;
1197 // Tell the workspace that we're about to start firing items at it
1198 mHandler.post(new Runnable() {
1199 public void run() {
1200 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1201 if (callbacks != null) {
1202 callbacks.startBinding();
1203 }
1204 }
1205 });
Winson Chung603bcb92011-09-02 11:45:39 -07001206
1207 // Unbind previously bound workspace items to prevent a leak of AppWidgetHostViews.
1208 final ArrayList<ItemInfo> workspaceItems = unbindWorkspaceItemsOnMainThread();
1209
Joe Onorato36115782010-06-17 13:28:48 -04001210 // Add the items to the workspace.
Winson Chung603bcb92011-09-02 11:45:39 -07001211 N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001212 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1213 final int start = i;
1214 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215 mHandler.post(new Runnable() {
1216 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001217 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001218 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001219 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 }
1221 }
1222 });
Joe Onorato36115782010-06-17 13:28:48 -04001223 }
Winson Chung603bcb92011-09-02 11:45:39 -07001224 // Ensure that we don't use the same folders data structure on the main thread
1225 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001226 mHandler.post(new Runnable() {
1227 public void run() {
1228 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1229 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001230 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001231 }
1232 }
1233 });
1234 // Wait until the queue goes empty.
1235 mHandler.post(new Runnable() {
1236 public void run() {
1237 if (DEBUG_LOADERS) {
1238 Log.d(TAG, "Going to start binding widgets soon.");
1239 }
1240 }
1241 });
1242 // Bind the widgets, one at a time.
1243 // WARNING: this is calling into the workspace from the background thread,
1244 // but since getCurrentScreen() just returns the int, we should be okay. This
1245 // is just a hint for the order, and if it's wrong, we'll be okay.
1246 // TODO: instead, we should have that push the current screen into here.
1247 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001248 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001249 // once for the current screen
1250 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001251 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001252 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 mHandler.post(new Runnable() {
1254 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001255 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001257 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258 }
1259 }
1260 });
1261 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 }
Joe Onorato36115782010-06-17 13:28:48 -04001263 // once for the other screens
1264 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001265 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001266 if (widget.screen != currentScreen) {
1267 mHandler.post(new Runnable() {
1268 public void run() {
1269 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1270 if (callbacks != null) {
1271 callbacks.bindAppWidget(widget);
1272 }
1273 }
1274 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001275 }
1276 }
Joe Onorato36115782010-06-17 13:28:48 -04001277 // Tell the workspace that we're done.
1278 mHandler.post(new Runnable() {
1279 public void run() {
1280 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1281 if (callbacks != null) {
1282 callbacks.finishBindingItems();
1283 }
1284 }
1285 });
1286 // If we're profiling, this is the last thing in the queue.
1287 mHandler.post(new Runnable() {
1288 public void run() {
1289 if (DEBUG_LOADERS) {
1290 Log.d(TAG, "bound workspace in "
1291 + (SystemClock.uptimeMillis()-t) + "ms");
1292 }
1293 }
1294 });
1295 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001296
Joe Onorato36115782010-06-17 13:28:48 -04001297 private void loadAndBindAllApps() {
1298 if (DEBUG_LOADERS) {
1299 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1300 }
1301 if (!mAllAppsLoaded) {
1302 loadAllAppsByBatch();
1303 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001304 return;
1305 }
Joe Onorato36115782010-06-17 13:28:48 -04001306 mAllAppsLoaded = true;
1307 } else {
1308 onlyBindAllApps();
1309 }
1310 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001311
Joe Onorato36115782010-06-17 13:28:48 -04001312 private void onlyBindAllApps() {
1313 final Callbacks oldCallbacks = mCallbacks.get();
1314 if (oldCallbacks == null) {
1315 // This launcher has exited and nobody bothered to tell us. Just bail.
1316 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1317 return;
1318 }
1319
1320 // shallow copy
1321 final ArrayList<ApplicationInfo> list
1322 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1323 mHandler.post(new Runnable() {
1324 public void run() {
1325 final long t = SystemClock.uptimeMillis();
1326 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1327 if (callbacks != null) {
1328 callbacks.bindAllApplications(list);
1329 }
1330 if (DEBUG_LOADERS) {
1331 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1332 + (SystemClock.uptimeMillis()-t) + "ms");
1333 }
1334 }
1335 });
1336
1337 }
1338
1339 private void loadAllAppsByBatch() {
1340 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1341
1342 // Don't use these two variables in any of the callback runnables.
1343 // Otherwise we hold a reference to them.
1344 final Callbacks oldCallbacks = mCallbacks.get();
1345 if (oldCallbacks == null) {
1346 // This launcher has exited and nobody bothered to tell us. Just bail.
1347 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1348 return;
1349 }
1350
1351 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1352 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1353
1354 final PackageManager packageManager = mContext.getPackageManager();
1355 List<ResolveInfo> apps = null;
1356
1357 int N = Integer.MAX_VALUE;
1358
1359 int startIndex;
1360 int i=0;
1361 int batchSize = -1;
1362 while (i < N && !mStopped) {
1363 if (i == 0) {
1364 mAllAppsList.clear();
1365 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1366 apps = packageManager.queryIntentActivities(mainIntent, 0);
1367 if (DEBUG_LOADERS) {
1368 Log.d(TAG, "queryIntentActivities took "
1369 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1370 }
1371 if (apps == null) {
1372 return;
1373 }
1374 N = apps.size();
1375 if (DEBUG_LOADERS) {
1376 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1377 }
1378 if (N == 0) {
1379 // There are no apps?!?
1380 return;
1381 }
1382 if (mBatchSize == 0) {
1383 batchSize = N;
1384 } else {
1385 batchSize = mBatchSize;
1386 }
1387
1388 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1389 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001390 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001391 if (DEBUG_LOADERS) {
1392 Log.d(TAG, "sort took "
1393 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1394 }
1395 }
1396
1397 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1398
1399 startIndex = i;
1400 for (int j=0; i<N && j<batchSize; j++) {
1401 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001402 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001403 mIconCache, mLabelCache, "6"));
Joe Onorato36115782010-06-17 13:28:48 -04001404 i++;
1405 }
1406
1407 final boolean first = i <= batchSize;
1408 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1409 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1410 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1411
Joe Onoratocc67f472010-06-08 10:54:30 -07001412 mHandler.post(new Runnable() {
1413 public void run() {
1414 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001415 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001416 if (first) {
1417 callbacks.bindAllApplications(added);
1418 } else {
1419 callbacks.bindAppsAdded(added);
1420 }
1421 if (DEBUG_LOADERS) {
1422 Log.d(TAG, "bound " + added.size() + " apps in "
1423 + (SystemClock.uptimeMillis() - t) + "ms");
1424 }
1425 } else {
1426 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001427 }
1428 }
1429 });
1430
Daniel Sandlerdca66122010-04-13 16:23:58 -04001431 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001432 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1433 + (SystemClock.uptimeMillis()-t2) + "ms");
1434 }
1435
1436 if (mAllAppsLoadDelay > 0 && i < N) {
1437 try {
1438 if (DEBUG_LOADERS) {
1439 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1440 }
1441 Thread.sleep(mAllAppsLoadDelay);
1442 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 }
1444 }
1445
Joe Onorato36115782010-06-17 13:28:48 -04001446 if (DEBUG_LOADERS) {
1447 Log.d(TAG, "cached all " + N + " apps in "
1448 + (SystemClock.uptimeMillis()-t) + "ms"
1449 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001450 }
1451 }
1452
1453 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001454 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1455 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1456 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1457 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1458 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001459 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001460 }
1461 }
1462
1463 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001464 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001465 }
1466
1467 private class PackageUpdatedTask implements Runnable {
1468 int mOp;
1469 String[] mPackages;
1470
1471 public static final int OP_NONE = 0;
1472 public static final int OP_ADD = 1;
1473 public static final int OP_UPDATE = 2;
1474 public static final int OP_REMOVE = 3; // uninstlled
1475 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1476
1477
1478 public PackageUpdatedTask(int op, String[] packages) {
1479 mOp = op;
1480 mPackages = packages;
1481 }
1482
1483 public void run() {
1484 final Context context = mApp;
1485
1486 final String[] packages = mPackages;
1487 final int N = packages.length;
1488 switch (mOp) {
1489 case OP_ADD:
1490 for (int i=0; i<N; i++) {
1491 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1492 mAllAppsList.addPackage(context, packages[i]);
1493 }
1494 break;
1495 case OP_UPDATE:
1496 for (int i=0; i<N; i++) {
1497 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1498 mAllAppsList.updatePackage(context, packages[i]);
1499 }
1500 break;
1501 case OP_REMOVE:
1502 case OP_UNAVAILABLE:
1503 for (int i=0; i<N; i++) {
1504 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1505 mAllAppsList.removePackage(packages[i]);
1506 }
1507 break;
1508 }
1509
1510 ArrayList<ApplicationInfo> added = null;
1511 ArrayList<ApplicationInfo> removed = null;
1512 ArrayList<ApplicationInfo> modified = null;
1513
1514 if (mAllAppsList.added.size() > 0) {
1515 added = mAllAppsList.added;
1516 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1517 }
1518 if (mAllAppsList.removed.size() > 0) {
1519 removed = mAllAppsList.removed;
1520 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1521 for (ApplicationInfo info: removed) {
1522 mIconCache.remove(info.intent.getComponent());
1523 }
1524 }
1525 if (mAllAppsList.modified.size() > 0) {
1526 modified = mAllAppsList.modified;
1527 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1528 }
1529
1530 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1531 if (callbacks == null) {
1532 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1533 return;
1534 }
1535
1536 if (added != null) {
1537 final ArrayList<ApplicationInfo> addedFinal = added;
1538 mHandler.post(new Runnable() {
1539 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001540 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1541 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001542 callbacks.bindAppsAdded(addedFinal);
1543 }
1544 }
1545 });
1546 }
1547 if (modified != null) {
1548 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1549 mHandler.post(new Runnable() {
1550 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001551 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1552 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001553 callbacks.bindAppsUpdated(modifiedFinal);
1554 }
1555 }
1556 });
1557 }
1558 if (removed != null) {
1559 final boolean permanent = mOp != OP_UNAVAILABLE;
1560 final ArrayList<ApplicationInfo> removedFinal = removed;
1561 mHandler.post(new Runnable() {
1562 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001563 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1564 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001565 callbacks.bindAppsRemoved(removedFinal, permanent);
1566 }
1567 }
1568 });
Joe Onoratobe386092009-11-17 17:32:16 -08001569 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001570
1571 mHandler.post(new Runnable() {
1572 @Override
1573 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001574 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1575 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001576 callbacks.bindPackagesUpdated();
1577 }
1578 }
1579 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 }
1581 }
1582
1583 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001584 * This is called from the code that adds shortcuts from the intent receiver. This
1585 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586 */
Joe Onorato56d82912010-03-07 14:32:10 -05001587 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001588 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001589 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001590
Joe Onorato56d82912010-03-07 14:32:10 -05001591 /**
1592 * Make an ShortcutInfo object for a shortcut that is an application.
1593 *
1594 * If c is not null, then it will be used to fill in missing data like the title and icon.
1595 */
1596 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001597 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001598 Bitmap icon = null;
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001599 final ShortcutInfo info = new ShortcutInfo("7");
Joe Onorato56d82912010-03-07 14:32:10 -05001600
1601 ComponentName componentName = intent.getComponent();
1602 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001603 return null;
1604 }
1605
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001606 // TODO: See if the PackageManager knows about this case. If it doesn't
1607 // then return null & delete this.
1608
Joe Onorato56d82912010-03-07 14:32:10 -05001609 // the resource -- This may implicitly give us back the fallback icon,
1610 // but don't worry about that. All we're doing with usingFallbackIcon is
1611 // to avoid saving lots of copies of that in the database, and most apps
1612 // have icons anyway.
1613 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1614 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001615 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001616 }
Joe Onorato56d82912010-03-07 14:32:10 -05001617 // the db
1618 if (icon == null) {
1619 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001620 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001621 }
1622 }
1623 // the fallback icon
1624 if (icon == null) {
1625 icon = getFallbackIcon();
1626 info.usingFallbackIcon = true;
1627 }
1628 info.setIcon(icon);
1629
1630 // from the resource
1631 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001632 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1633 if (labelCache != null && labelCache.containsKey(key)) {
1634 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001635 } else {
1636 info.title = resolveInfo.activityInfo.loadLabel(manager);
1637 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001638 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001639 }
1640 }
Joe Onorato56d82912010-03-07 14:32:10 -05001641 }
1642 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001643 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001644 if (c != null) {
1645 info.title = c.getString(titleIndex);
1646 }
1647 }
1648 // fall back to the class name of the activity
1649 if (info.title == null) {
1650 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001651 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001652 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1653 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001657 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001659 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001660 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1661 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662
Joe Onorato56d82912010-03-07 14:32:10 -05001663 Bitmap icon = null;
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001664 final ShortcutInfo info = new ShortcutInfo("8");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001665 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001667 // TODO: If there's an explicit component and we can't install that, delete it.
1668
Joe Onorato56d82912010-03-07 14:32:10 -05001669 info.title = c.getString(titleIndex);
1670
Joe Onorato9c1289c2009-08-17 11:03:03 -04001671 int iconType = c.getInt(iconTypeIndex);
1672 switch (iconType) {
1673 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1674 String packageName = c.getString(iconPackageIndex);
1675 String resourceName = c.getString(iconResourceIndex);
1676 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001677 info.customIcon = false;
1678 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001680 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001681 if (resources != null) {
1682 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001683 icon = Utilities.createIconBitmap(
1684 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001686 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001687 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 }
Joe Onorato56d82912010-03-07 14:32:10 -05001689 // the db
1690 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001691 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001692 }
1693 // the fallback icon
1694 if (icon == null) {
1695 icon = getFallbackIcon();
1696 info.usingFallbackIcon = true;
1697 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001698 break;
1699 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001700 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001701 if (icon == null) {
1702 icon = getFallbackIcon();
1703 info.customIcon = false;
1704 info.usingFallbackIcon = true;
1705 } else {
1706 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001708 break;
1709 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001710 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001711 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712 info.customIcon = false;
1713 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001715 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001716 return info;
1717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718
Michael Jurka931dc972011-08-05 15:08:15 -07001719 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001720 if (false) {
1721 Log.d(TAG, "getIconFromCursor app="
1722 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1723 }
1724 byte[] data = c.getBlob(iconIndex);
1725 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001726 return Utilities.createIconBitmap(
1727 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001728 } catch (Exception e) {
1729 return null;
1730 }
1731 }
1732
Winson Chung3d503fb2011-07-13 17:25:49 -07001733 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1734 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001735 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001736 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001737
1738 return info;
1739 }
1740
Winson Chunga9abd0e2010-10-27 17:18:37 -07001741 /**
Winson Chung55cef262010-10-28 14:14:18 -07001742 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1743 */
1744 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1745 ComponentName component) {
1746 List<AppWidgetProviderInfo> widgets =
1747 AppWidgetManager.getInstance(context).getInstalledProviders();
1748 for (AppWidgetProviderInfo info : widgets) {
1749 if (info.provider.equals(component)) {
1750 return info;
1751 }
1752 }
1753 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001754 }
1755
Winson Chung68846fd2010-10-29 11:00:27 -07001756 /**
1757 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1758 */
1759 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1760 final PackageManager packageManager = context.getPackageManager();
1761 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1762 new ArrayList<WidgetMimeTypeHandlerData>();
1763
1764 final Intent supportsIntent =
1765 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1766 supportsIntent.setType(mimeType);
1767
1768 // Create a set of widget configuration components that we can test against
1769 final List<AppWidgetProviderInfo> widgets =
1770 AppWidgetManager.getInstance(context).getInstalledProviders();
1771 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1772 new HashMap<ComponentName, AppWidgetProviderInfo>();
1773 for (AppWidgetProviderInfo info : widgets) {
1774 configurationComponentToWidget.put(info.configure, info);
1775 }
1776
1777 // Run through each of the intents that can handle this type of clip data, and cross
1778 // reference them with the components that are actual configuration components
1779 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1780 PackageManager.MATCH_DEFAULT_ONLY);
1781 for (ResolveInfo info : activities) {
1782 final ActivityInfo activityInfo = info.activityInfo;
1783 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1784 activityInfo.name);
1785 if (configurationComponentToWidget.containsKey(infoComponent)) {
1786 supportedConfigurationActivities.add(
1787 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1788 configurationComponentToWidget.get(infoComponent)));
1789 }
1790 }
1791 return supportedConfigurationActivities;
1792 }
1793
Winson Chunga9abd0e2010-10-27 17:18:37 -07001794 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001795 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1796 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1797 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1798
1799 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001800 boolean customIcon = false;
1801 ShortcutIconResource iconResource = null;
1802
1803 if (bitmap != null && bitmap instanceof Bitmap) {
1804 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001805 customIcon = true;
1806 } else {
1807 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1808 if (extra != null && extra instanceof ShortcutIconResource) {
1809 try {
1810 iconResource = (ShortcutIconResource) extra;
1811 final PackageManager packageManager = context.getPackageManager();
1812 Resources resources = packageManager.getResourcesForApplication(
1813 iconResource.packageName);
1814 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001815 icon = Utilities.createIconBitmap(
1816 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001817 } catch (Exception e) {
1818 Log.w(TAG, "Could not load shortcut icon: " + extra);
1819 }
1820 }
1821 }
1822
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001823 final ShortcutInfo info = new ShortcutInfo("9");
Joe Onorato56d82912010-03-07 14:32:10 -05001824
1825 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001826 if (fallbackIcon != null) {
1827 icon = fallbackIcon;
1828 } else {
1829 icon = getFallbackIcon();
1830 info.usingFallbackIcon = true;
1831 }
Joe Onorato56d82912010-03-07 14:32:10 -05001832 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001833 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001834
Joe Onorato0589f0f2010-02-08 13:44:00 -08001835 info.title = name;
1836 info.intent = intent;
1837 info.customIcon = customIcon;
1838 info.iconResource = iconResource;
1839
1840 return info;
1841 }
1842
Winson Chungaac01e12011-08-17 10:37:13 -07001843 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1844 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001845 // If apps can't be on SD, don't even bother.
1846 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001847 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001848 }
Joe Onorato56d82912010-03-07 14:32:10 -05001849 // If this icon doesn't have a custom icon, check to see
1850 // what's stored in the DB, and if it doesn't match what
1851 // we're going to show, store what we are going to show back
1852 // into the DB. We do this so when we're loading, if the
1853 // package manager can't find an icon (for example because
1854 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001855 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001856 cache.put(info, c.getBlob(iconIndex));
1857 return true;
1858 }
1859 return false;
1860 }
1861 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1862 boolean needSave = false;
1863 try {
1864 if (data != null) {
1865 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1866 Bitmap loaded = info.getIcon(mIconCache);
1867 needSave = !saved.sameAs(loaded);
1868 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001869 needSave = true;
1870 }
Winson Chungaac01e12011-08-17 10:37:13 -07001871 } catch (Exception e) {
1872 needSave = true;
1873 }
1874 if (needSave) {
1875 Log.d(TAG, "going to save icon bitmap for info=" + info);
1876 // This is slower than is ideal, but this only happens once
1877 // or when the app is updated with a new icon.
1878 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001879 }
1880 }
1881
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001883 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001884 * or make a new one.
1885 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001886 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 // See if a placeholder was created for us already
1888 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001889 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001890 // No placeholder -- create a new instance
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001891 folderInfo = new FolderInfo("10");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001892 folders.put(id, folderInfo);
1893 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001894 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
1896
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001898 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899 = new Comparator<ApplicationInfo>() {
1900 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001901 int result = sCollator.compare(a.title.toString(), b.title.toString());
1902 if (result == 0) {
1903 result = a.componentName.compareTo(b.componentName);
1904 }
1905 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001907 };
Winson Chung78403fe2011-01-21 15:38:02 -08001908 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1909 = new Comparator<ApplicationInfo>() {
1910 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1911 if (a.firstInstallTime < b.firstInstallTime) return 1;
1912 if (a.firstInstallTime > b.firstInstallTime) return -1;
1913 return 0;
1914 }
1915 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001916 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1917 = new Comparator<AppWidgetProviderInfo>() {
1918 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1919 return sCollator.compare(a.label.toString(), b.label.toString());
1920 }
1921 };
Winson Chung5308f242011-08-18 12:12:41 -07001922 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1923 if (info.activityInfo != null) {
1924 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1925 } else {
1926 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1927 }
1928 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001929 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1930 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001931 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001932 ShortcutNameComparator(PackageManager pm) {
1933 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001934 mLabelCache = new HashMap<Object, CharSequence>();
1935 }
1936 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1937 mPackageManager = pm;
1938 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001939 }
1940 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001941 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001942 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1943 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1944 if (mLabelCache.containsKey(keyA)) {
1945 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001946 } else {
1947 labelA = a.loadLabel(mPackageManager).toString();
1948
Winson Chung5308f242011-08-18 12:12:41 -07001949 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001950 }
Winson Chung5308f242011-08-18 12:12:41 -07001951 if (mLabelCache.containsKey(keyB)) {
1952 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001953 } else {
1954 labelB = b.loadLabel(mPackageManager).toString();
1955
Winson Chung5308f242011-08-18 12:12:41 -07001956 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001957 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001958 return sCollator.compare(labelA, labelB);
1959 }
1960 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001961 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1962 private PackageManager mPackageManager;
1963 private HashMap<Object, String> mLabelCache;
1964 WidgetAndShortcutNameComparator(PackageManager pm) {
1965 mPackageManager = pm;
1966 mLabelCache = new HashMap<Object, String>();
1967 }
1968 public final int compare(Object a, Object b) {
1969 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001970 if (mLabelCache.containsKey(a)) {
1971 labelA = mLabelCache.get(a);
1972 } else {
1973 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001974 ((AppWidgetProviderInfo) a).label :
1975 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001976 mLabelCache.put(a, labelA);
1977 }
1978 if (mLabelCache.containsKey(b)) {
1979 labelB = mLabelCache.get(b);
1980 } else {
1981 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001982 ((AppWidgetProviderInfo) b).label :
1983 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001984 mLabelCache.put(b, labelB);
1985 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001986 return sCollator.compare(labelA, labelB);
1987 }
1988 };
Joe Onoratobe386092009-11-17 17:32:16 -08001989
1990 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001991 Log.d(TAG, "mCallbacks=" + mCallbacks);
1992 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1993 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1994 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1995 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001996 if (mLoaderTask != null) {
1997 mLoaderTask.dumpState();
1998 } else {
1999 Log.d(TAG, "mLoaderTask=null");
2000 }
Joe Onoratobe386092009-11-17 17:32:16 -08002001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002}