blob: ea51d01694209540de540fe7be7908904a5cc705 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040022import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080024import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
26import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
37import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080038import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040039import android.os.Handler;
40import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040044import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Winson Chung68846fd2010-10-29 11:00:27 -070047import com.android.launcher.R;
48import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080049
Michael Jurkac2f801e2011-07-12 14:19:46 -070050import java.lang.ref.WeakReference;
51import java.net.URISyntaxException;
52import java.text.Collator;
53import java.util.ArrayList;
54import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
57import java.util.List;
58import java.util.Locale;
59
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060/**
61 * Maintains in-memory state of the Launcher. It is expected that there should be only one
62 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070063 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040065public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080066 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040067 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070068
Joe Onorato36115782010-06-17 13:28:48 -040069 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080070 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040071 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040072 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040073
Joe Onoratof99f8c12009-10-31 17:27:36 -040074 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075 private final Object mLock = new Object();
76 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040077 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070079 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
80 static {
81 sWorkerThread.start();
82 }
83 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
84
Joe Onoratocc67f472010-06-08 10:54:30 -070085 // We start off with everything not loaded. After that, we assume that
86 // our monitoring of the package manager provides all updates and we never
87 // need to do a requery. These are only ever touched from the loader thread.
88 private boolean mWorkspaceLoaded;
89 private boolean mAllAppsLoaded;
90
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Michael Jurkaa8c760d2011-04-28 14:59:33 -070093 // < only access in worker thread >
94 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080095
Michael Jurkaa8c760d2011-04-28 14:59:33 -070096 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
97 // LauncherModel to their ids
98 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
99
100 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
101 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
102 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700103 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700104
105 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
106 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
107 new ArrayList<LauncherAppWidgetInfo>();
108
109 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
110 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700111
112 // sDbIconCache is the set of ItemInfos that need to have their icons updated in the database
113 static final HashMap<Object, byte[]> sDbIconCache = new HashMap<Object, byte[]>();
114
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700115 // </ only access in worker thread >
116
117 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800118 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohend22015c2010-07-26 22:02:18 -0700120 private static int mCellCountX;
121 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700122
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700124 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 public int getCurrentWorkspaceScreen();
126 public void startBinding();
127 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500128 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400129 public void finishBindingItems();
130 public void bindAppWidget(LauncherAppWidgetInfo info);
131 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500132 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
133 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400134 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700135 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400136 public boolean isAllAppsVisible();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100137 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Joe Onorato0589f0f2010-02-08 13:44:00 -0800140 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800141 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400142 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143 mAllAppsList = new AllAppsList(iconCache);
144 mIconCache = iconCache;
145
146 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700147 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400148
149 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400150
151 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800152 }
153
Joe Onorato56d82912010-03-07 14:32:10 -0500154 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800155 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Adam Cohen4eac29a2011-07-11 17:53:37 -0700158 public static void unbindWorkspaceItems() {
159 for (ItemInfo item: sWorkspaceItems) {
160 item.unbind();
161 }
162 }
163
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 /**
165 * Adds an item to the DB if it was not created previously, or move it to a new
166 * <container, screen, cellX, cellY>
167 */
168 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
169 int screen, int cellX, int cellY) {
170 if (item.container == ItemInfo.NO_ID) {
171 // From all apps
172 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
173 } else {
174 // From somewhere else
175 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 }
177 }
178
179 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700181 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700182 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
183 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 item.cellX = cellX;
186 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700187 // We store hotseat items in canonical form which is this orientation invariant position
188 // in the hotseat
189 if (context instanceof Launcher && screen < 0 &&
190 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
191 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
192 } else {
193 item.screen = screen;
194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700196 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 final ContentValues values = new ContentValues();
198 final ContentResolver cr = context.getContentResolver();
199
200 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700201 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
202 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
204
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700205 sWorker.post(new Runnable() {
206 public void run() {
207 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700208 ItemInfo modelItem = sItemsIdMap.get(item.id);
209 if (item != modelItem) {
210 // the modelItem needs to match up perfectly with item if our model is to be
211 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700212 String msg = "item: " + ((item != null) ? item.toString() : "null") +
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700213 " modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
214 " creation tag of item: " + ((item != null) ? item.whereCreated : "null") +
215 " creation tag of modelItem: " + ((modelItem != null) ? modelItem.whereCreated : "null") +
216 " Error: ItemInfo passed to moveItemInDatabase doesn't match original";
Winson Chungc22a54d2011-08-25 13:51:25 -0700217 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700218 }
219
220 // Items are added/removed from the corresponding FolderInfo elsewhere, such
221 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
222 // that are on the desktop, as appropriate
Winson Chung3d503fb2011-07-13 17:25:49 -0700223 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
224 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700225 if (!sWorkspaceItems.contains(modelItem)) {
226 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700227 }
228 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700229 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700230 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700231 }
232 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700233 }
234
235 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800236 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800237 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700238 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
239 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800240 item.spanX = spanX;
241 item.spanY = spanY;
242 item.cellX = cellX;
243 item.cellY = cellY;
244
245 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
246 final ContentValues values = new ContentValues();
247 final ContentResolver cr = context.getContentResolver();
248
249 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
250 values.put(LauncherSettings.Favorites.SPANX, spanX);
251 values.put(LauncherSettings.Favorites.SPANY, spanY);
252 values.put(LauncherSettings.Favorites.CELLX, cellX);
253 values.put(LauncherSettings.Favorites.CELLY, cellY);
254
255 sWorker.post(new Runnable() {
256 public void run() {
257 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700258 ItemInfo modelItem = sItemsIdMap.get(item.id);
259 if (item != modelItem) {
260 // the modelItem needs to match up perfectly with item if our model is to be
261 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700262 String msg = "item: " + ((item != null) ? item.toString() : "null") +
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700263 " modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
264 " creation tag of item: " + ((item != null) ? item.whereCreated : "null") +
265 " creation tag of modelItem: " + ((modelItem != null) ? modelItem.whereCreated : "null") +
266 " Error: ItemInfo passed to resizeItemInDatabase doesn't match original";
Winson Chungc22a54d2011-08-25 13:51:25 -0700267 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700268 }
Adam Cohend4844c32011-02-18 19:25:06 -0800269 }
270 });
271 }
272
273 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 * Returns true if the shortcuts already exists in the database.
275 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277 static boolean shortcutExists(Context context, String title, Intent intent) {
278 final ContentResolver cr = context.getContentResolver();
279 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
280 new String[] { "title", "intent" }, "title=? and intent=?",
281 new String[] { title, intent.toUri(0) }, null);
282 boolean result = false;
283 try {
284 result = c.moveToFirst();
285 } finally {
286 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700289 }
290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700292 * Returns an ItemInfo array containing all the items in the LauncherModel.
293 * The ItemInfo.id is not set through this function.
294 */
295 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
296 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
297 final ContentResolver cr = context.getContentResolver();
298 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
299 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
300 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
301 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
302
303 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
304 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
305 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
306 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
307 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
308 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
309 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
310
311 try {
312 while (c.moveToNext()) {
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700313 ItemInfo item = new ItemInfo("17");
Winson Chungaafa03c2010-06-11 17:34:16 -0700314 item.cellX = c.getInt(cellXIndex);
315 item.cellY = c.getInt(cellYIndex);
316 item.spanX = c.getInt(spanXIndex);
317 item.spanY = c.getInt(spanYIndex);
318 item.container = c.getInt(containerIndex);
319 item.itemType = c.getInt(itemTypeIndex);
320 item.screen = c.getInt(screenIndex);
321
322 items.add(item);
323 }
324 } catch (Exception e) {
325 items.clear();
326 } finally {
327 c.close();
328 }
329
330 return items;
331 }
332
333 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400334 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
335 */
336 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
337 final ContentResolver cr = context.getContentResolver();
338 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
339 "_id=? and (itemType=? or itemType=?)",
340 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700341 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700342
Joe Onorato9c1289c2009-08-17 11:03:03 -0400343 try {
344 if (c.moveToFirst()) {
345 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
346 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
347 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
348 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
349 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
350 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 FolderInfo folderInfo = null;
353 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700354 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
355 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400356 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700357 }
358
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359 folderInfo.title = c.getString(titleIndex);
360 folderInfo.id = id;
361 folderInfo.container = c.getInt(containerIndex);
362 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700363 folderInfo.cellX = c.getInt(cellXIndex);
364 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365
366 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700367 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 } finally {
369 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700370 }
371
372 return null;
373 }
374
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 /**
376 * Add an item to the database in a specified container. Sets the container, screen, cellX and
377 * cellY fields of the item. Also assigns an ID to the item.
378 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700379 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
380 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400382 item.cellX = cellX;
383 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700384 // We store hotseat items in canonical form which is this orientation invariant position
385 // in the hotseat
386 if (context instanceof Launcher && screen < 0 &&
387 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
388 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
389 } else {
390 item.screen = screen;
391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400392
393 final ContentValues values = new ContentValues();
394 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 item.onAddToDatabase(values);
396
Michael Jurka7578ec62011-08-03 14:11:54 -0700397 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700398 item.id = app.getLauncherProvider().generateNewId();
399 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700400 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700401
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700402 sWorker.post(new Runnable() {
403 public void run() {
404 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
405 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406
Winson Chungb1094bd2011-08-24 16:14:08 -0700407 if (sItemsIdMap.containsKey(item.id)) {
408 // we should not be adding new items in the db with the same id
409 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
410 "addItemToDatabase already exists." + item.toString());
411 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700412 sItemsIdMap.put(item.id, item);
413 switch (item.itemType) {
414 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
415 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700416 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700417 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
418 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700419 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
420 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700421 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700422 }
423 break;
424 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
425 sAppWidgets.add((LauncherAppWidgetInfo) item);
426 break;
427 }
428 }
429 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700430 }
431
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700433 * Creates a new unique child id, for a given cell span across all layouts.
434 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700435 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700436 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
437 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700438 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700439 }
440
Adam Cohend22015c2010-07-26 22:02:18 -0700441 static int getCellCountX() {
442 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700443 }
444
Adam Cohend22015c2010-07-26 22:02:18 -0700445 static int getCellCountY() {
446 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700447 }
448
449 /**
450 * Updates the model orientation helper to take into account the current layout dimensions
451 * when performing local/canonical coordinate transformations.
452 */
453 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700454 mCellCountX = shortAxisCellCount;
455 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700456 }
457
458 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400459 * Update an item to the database in a specified container.
460 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700461 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400462 final ContentValues values = new ContentValues();
463 final ContentResolver cr = context.getContentResolver();
464
465 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700466 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700467
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700468 sWorker.post(new Runnable() {
469 public void run() {
470 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
471 values, null, null);
472 final ItemInfo modelItem = sItemsIdMap.get(item.id);
473 if (item != modelItem) {
474 // the modelItem needs to match up perfectly with item if our model is to be
475 // consistent with the database-- for now, just require modelItem == item
Winson Chungc22a54d2011-08-25 13:51:25 -0700476 String msg = "item: " + ((item != null) ? item.toString() : "null") +
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700477 " modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
478 " creation tag of item: " + ((item != null) ? item.whereCreated : "null") +
479 " creation tag of modelItem: " + ((modelItem != null) ? modelItem.whereCreated : "null") +
480 " Error: ItemInfo passed to updateItemInDatabase doesn't match original";
Winson Chungc22a54d2011-08-25 13:51:25 -0700481 throw new RuntimeException(msg);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700482 }
483 }
484 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400485 }
486
487 /**
488 * Removes the specified item from the database
489 * @param context
490 * @param item
491 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700492 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400493 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700494 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700495 sWorker.post(new Runnable() {
496 public void run() {
497 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700498 switch (item.itemType) {
499 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
500 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700501 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700502 break;
503 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
504 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700505 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700506 break;
507 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
508 sAppWidgets.remove((LauncherAppWidgetInfo) item);
509 break;
510 }
511 sItemsIdMap.remove(item.id);
Winson Chungb1094bd2011-08-24 16:14:08 -0700512 sDbIconCache.remove(item);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700513 }
514 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 }
516
517 /**
518 * Remove the contents of the specified folder from the database
519 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700520 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400521 final ContentResolver cr = context.getContentResolver();
522
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700523 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700524 public void run() {
525 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700526 sItemsIdMap.remove(info.id);
527 sFolders.remove(info.id);
Winson Chungc22a54d2011-08-25 13:51:25 -0700528 sDbIconCache.remove(info);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700529 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700530
Adam Cohen9932a9b2011-08-02 22:14:07 -0700531 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Adam Cohenafb01ee2011-06-23 15:38:03 -0700532 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700533 for (ItemInfo childInfo : info.contents) {
534 sItemsIdMap.remove(childInfo.id);
Winson Chungc22a54d2011-08-25 13:51:25 -0700535 sDbIconCache.remove(childInfo);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700536 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700537 }
538 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400539 }
540
541 /**
542 * Set this as the current Launcher activity object for the loader.
543 */
544 public void initialize(Callbacks callbacks) {
545 synchronized (mLock) {
546 mCallbacks = new WeakReference<Callbacks>(callbacks);
547 }
548 }
549
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700550 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
552 * ACTION_PACKAGE_CHANGED.
553 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100554 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400555 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400556 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700557
Joe Onorato36115782010-06-17 13:28:48 -0400558 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400559
Joe Onorato36115782010-06-17 13:28:48 -0400560 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
561 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
562 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
563 final String packageName = intent.getData().getSchemeSpecificPart();
564 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565
Joe Onorato36115782010-06-17 13:28:48 -0400566 int op = PackageUpdatedTask.OP_NONE;
567
568 if (packageName == null || packageName.length() == 0) {
569 // they sent us a bad intent
570 return;
571 }
572
573 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
574 op = PackageUpdatedTask.OP_UPDATE;
575 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
576 if (!replacing) {
577 op = PackageUpdatedTask.OP_REMOVE;
578 }
579 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
580 // later, we will update the package at this time
581 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
582 if (!replacing) {
583 op = PackageUpdatedTask.OP_ADD;
584 } else {
585 op = PackageUpdatedTask.OP_UPDATE;
586 }
587 }
588
589 if (op != PackageUpdatedTask.OP_NONE) {
590 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
591 }
592
593 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400594 // First, schedule to add these apps back in.
595 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
596 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
597 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700598 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400599 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
600 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
601 enqueuePackageUpdated(new PackageUpdatedTask(
602 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700603 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
604 // If we have changed locale we need to clear out the labels in all apps.
605 // Do this here because if the launcher activity is running it will be restarted.
606 // If it's not running startLoaderFromBackground will merely tell it that it needs
607 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
608 // next time.
609 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700610 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700611 startLoaderFromBackground();
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700612 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
613 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700614 if (mCallbacks != null) {
615 Callbacks callbacks = mCallbacks.get();
616 if (callbacks != null) {
617 callbacks.bindSearchablesChanged();
618 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700619 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700620 }
621 }
622
623 /**
624 * When the launcher is in the background, it's possible for it to miss paired
625 * configuration changes. So whenever we trigger the loader from the background
626 * tell the launcher that it needs to re-run the loader when it comes back instead
627 * of doing it now.
628 */
629 public void startLoaderFromBackground() {
630 boolean runLoader = false;
631 if (mCallbacks != null) {
632 Callbacks callbacks = mCallbacks.get();
633 if (callbacks != null) {
634 // Only actually run the loader if they're not paused.
635 if (!callbacks.setLoadOnResume()) {
636 runLoader = true;
637 }
638 }
639 }
640 if (runLoader) {
641 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700642 }
Joe Onorato36115782010-06-17 13:28:48 -0400643 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400644
Joe Onorato36115782010-06-17 13:28:48 -0400645 public void startLoader(Context context, boolean isLaunching) {
646 synchronized (mLock) {
647 if (DEBUG_LOADERS) {
648 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650
Joe Onorato36115782010-06-17 13:28:48 -0400651 // Don't bother to start the thread if we know it's not going to do anything
652 if (mCallbacks != null && mCallbacks.get() != null) {
653 // If there is already one running, tell it to stop.
654 LoaderTask oldTask = mLoaderTask;
655 if (oldTask != null) {
656 if (oldTask.isLaunching()) {
657 // don't downgrade isLaunching if we're already running
658 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500659 }
Joe Onorato36115782010-06-17 13:28:48 -0400660 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500661 }
Joe Onorato36115782010-06-17 13:28:48 -0400662 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700663 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665 }
666 }
667
Joe Onorato36115782010-06-17 13:28:48 -0400668 public void stopLoader() {
669 synchronized (mLock) {
670 if (mLoaderTask != null) {
671 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672 }
673 }
Joe Onorato36115782010-06-17 13:28:48 -0400674 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675
Michael Jurkac57b7a82011-08-09 22:02:20 -0700676 public boolean isAllAppsLoaded() {
677 return mAllAppsLoaded;
678 }
679
Joe Onorato36115782010-06-17 13:28:48 -0400680 /**
681 * Runnable for the thread that loads the contents of the launcher:
682 * - workspace icons
683 * - widgets
684 * - all apps icons
685 */
686 private class LoaderTask implements Runnable {
687 private Context mContext;
688 private Thread mWaitThread;
689 private boolean mIsLaunching;
690 private boolean mStopped;
691 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700692 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400693
694 LoaderTask(Context context, boolean isLaunching) {
695 mContext = context;
696 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700697 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 }
699
Joe Onorato36115782010-06-17 13:28:48 -0400700 boolean isLaunching() {
701 return mIsLaunching;
702 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703
Joe Onorato36115782010-06-17 13:28:48 -0400704 private void loadAndBindWorkspace() {
705 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400706 if (DEBUG_LOADERS) {
707 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700709
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700710 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400711 loadWorkspace();
712 if (mStopped) {
713 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714 }
Joe Onorato36115782010-06-17 13:28:48 -0400715 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 }
717
Joe Onorato36115782010-06-17 13:28:48 -0400718 // Bind the workspace
719 bindWorkspace();
720 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400721
Joe Onorato36115782010-06-17 13:28:48 -0400722 private void waitForIdle() {
723 // Wait until the either we're stopped or the other threads are done.
724 // This way we don't start loading all apps until the workspace has settled
725 // down.
726 synchronized (LoaderTask.this) {
727 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700728
Joe Onorato36115782010-06-17 13:28:48 -0400729 mHandler.postIdle(new Runnable() {
730 public void run() {
731 synchronized (LoaderTask.this) {
732 mLoadAndBindStepFinished = true;
733 if (DEBUG_LOADERS) {
734 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400735 }
Joe Onorato36115782010-06-17 13:28:48 -0400736 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400737 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400738 }
Joe Onorato36115782010-06-17 13:28:48 -0400739 });
740
741 while (!mStopped && !mLoadAndBindStepFinished) {
742 try {
743 this.wait();
744 } catch (InterruptedException ex) {
745 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400746 }
747 }
Joe Onorato36115782010-06-17 13:28:48 -0400748 if (DEBUG_LOADERS) {
749 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700750 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400751 + "ms for previous step to finish binding");
752 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400753 }
Joe Onorato36115782010-06-17 13:28:48 -0400754 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400755
Joe Onorato36115782010-06-17 13:28:48 -0400756 public void run() {
757 // Optimize for end-user experience: if the Launcher is up and // running with the
758 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
759 // workspace first (default).
760 final Callbacks cbk = mCallbacks.get();
761 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400762
Joe Onorato36115782010-06-17 13:28:48 -0400763 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400764 // Elevate priority when Home launches for the first time to avoid
765 // starving at boot time. Staring at a blank home is not cool.
766 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700767 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
768 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400769 android.os.Process.setThreadPriority(mIsLaunching
770 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
771 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400772 if (loadWorkspaceFirst) {
773 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
774 loadAndBindWorkspace();
775 } else {
776 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700777 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400778 }
779
Joe Onorato36115782010-06-17 13:28:48 -0400780 if (mStopped) {
781 break keep_running;
782 }
783
784 // Whew! Hard work done. Slow us down, and wait until the UI thread has
785 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400786 synchronized (mLock) {
787 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700788 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400789 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
790 }
791 }
Joe Onorato36115782010-06-17 13:28:48 -0400792 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400793
794 // second step
795 if (loadWorkspaceFirst) {
796 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700797 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400798 } else {
799 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
800 loadAndBindWorkspace();
801 }
Joe Onorato36115782010-06-17 13:28:48 -0400802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803
Winson Chungaac01e12011-08-17 10:37:13 -0700804
805 // Update the saved icons if necessary
806 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700807 for (Object key : sDbIconCache.keySet()) {
808 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700809 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700810 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700811
Joe Onorato36115782010-06-17 13:28:48 -0400812 // Clear out this reference, otherwise we end up holding it until all of the
813 // callback runnables are done.
814 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815
Joe Onorato36115782010-06-17 13:28:48 -0400816 synchronized (mLock) {
817 // If we are still the last one to be scheduled, remove ourselves.
818 if (mLoaderTask == this) {
819 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820 }
Joe Onorato36115782010-06-17 13:28:48 -0400821 }
Joe Onorato36115782010-06-17 13:28:48 -0400822 }
823
824 public void stopLocked() {
825 synchronized (LoaderTask.this) {
826 mStopped = true;
827 this.notify();
828 }
829 }
830
831 /**
832 * Gets the callbacks object. If we've been stopped, or if the launcher object
833 * has somehow been garbage collected, return null instead. Pass in the Callbacks
834 * object that was around when the deferred message was scheduled, and if there's
835 * a new Callbacks object around then also return null. This will save us from
836 * calling onto it with data that will be ignored.
837 */
838 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
839 synchronized (mLock) {
840 if (mStopped) {
841 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400842 }
Joe Onorato36115782010-06-17 13:28:48 -0400843
844 if (mCallbacks == null) {
845 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400846 }
Joe Onorato36115782010-06-17 13:28:48 -0400847
848 final Callbacks callbacks = mCallbacks.get();
849 if (callbacks != oldCallbacks) {
850 return null;
851 }
852 if (callbacks == null) {
853 Log.w(TAG, "no mCallbacks");
854 return null;
855 }
856
857 return callbacks;
858 }
859 }
860
861 // check & update map of what's occupied; used to discard overlapping/invalid items
862 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700863 int containerIndex = item.screen;
864 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
865 // We use the last index to refer to the hotseat
866 containerIndex = Launcher.SCREEN_COUNT;
867 // Return early if we detect that an item is under the hotseat button
868 if (Hotseat.isAllAppsButtonRank(item.screen)) {
869 return false;
870 }
871 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
872 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400873 return true;
874 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700875
Joe Onorato36115782010-06-17 13:28:48 -0400876 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
877 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700878 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400879 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700880 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400881 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700882 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700883 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400884 return false;
885 }
886 }
887 }
888 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
889 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700890 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400891 }
892 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700893
Joe Onorato36115782010-06-17 13:28:48 -0400894 return true;
895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400896
Joe Onorato36115782010-06-17 13:28:48 -0400897 private void loadWorkspace() {
898 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899
Joe Onorato36115782010-06-17 13:28:48 -0400900 final Context context = mContext;
901 final ContentResolver contentResolver = context.getContentResolver();
902 final PackageManager manager = context.getPackageManager();
903 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
904 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400905
Adam Cohen4eac29a2011-07-11 17:53:37 -0700906 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700907 sAppWidgets.clear();
908 sFolders.clear();
909 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700910 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800911
Joe Onorato36115782010-06-17 13:28:48 -0400912 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913
Joe Onorato36115782010-06-17 13:28:48 -0400914 final Cursor c = contentResolver.query(
915 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400916
Winson Chungf30ad5f2011-08-08 10:55:42 -0700917 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700918 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700919 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920
Joe Onorato36115782010-06-17 13:28:48 -0400921 try {
922 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
923 final int intentIndex = c.getColumnIndexOrThrow
924 (LauncherSettings.Favorites.INTENT);
925 final int titleIndex = c.getColumnIndexOrThrow
926 (LauncherSettings.Favorites.TITLE);
927 final int iconTypeIndex = c.getColumnIndexOrThrow(
928 LauncherSettings.Favorites.ICON_TYPE);
929 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
930 final int iconPackageIndex = c.getColumnIndexOrThrow(
931 LauncherSettings.Favorites.ICON_PACKAGE);
932 final int iconResourceIndex = c.getColumnIndexOrThrow(
933 LauncherSettings.Favorites.ICON_RESOURCE);
934 final int containerIndex = c.getColumnIndexOrThrow(
935 LauncherSettings.Favorites.CONTAINER);
936 final int itemTypeIndex = c.getColumnIndexOrThrow(
937 LauncherSettings.Favorites.ITEM_TYPE);
938 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
939 LauncherSettings.Favorites.APPWIDGET_ID);
940 final int screenIndex = c.getColumnIndexOrThrow(
941 LauncherSettings.Favorites.SCREEN);
942 final int cellXIndex = c.getColumnIndexOrThrow
943 (LauncherSettings.Favorites.CELLX);
944 final int cellYIndex = c.getColumnIndexOrThrow
945 (LauncherSettings.Favorites.CELLY);
946 final int spanXIndex = c.getColumnIndexOrThrow
947 (LauncherSettings.Favorites.SPANX);
948 final int spanYIndex = c.getColumnIndexOrThrow(
949 LauncherSettings.Favorites.SPANY);
950 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
951 final int displayModeIndex = c.getColumnIndexOrThrow(
952 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953
Joe Onorato36115782010-06-17 13:28:48 -0400954 ShortcutInfo info;
955 String intentDescription;
956 LauncherAppWidgetInfo appWidgetInfo;
957 int container;
958 long id;
959 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960
Joe Onorato36115782010-06-17 13:28:48 -0400961 while (!mStopped && c.moveToNext()) {
962 try {
963 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400964
Joe Onorato36115782010-06-17 13:28:48 -0400965 switch (itemType) {
966 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
967 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
968 intentDescription = c.getString(intentIndex);
969 try {
970 intent = Intent.parseUri(intentDescription, 0);
971 } catch (URISyntaxException e) {
972 continue;
973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974
Joe Onorato36115782010-06-17 13:28:48 -0400975 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
976 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700977 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400978 } else {
979 info = getShortcutInfo(c, context, iconTypeIndex,
980 iconPackageIndex, iconResourceIndex, iconIndex,
981 titleIndex);
982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983
Joe Onorato36115782010-06-17 13:28:48 -0400984 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400985 info.intent = intent;
986 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400988 info.container = container;
989 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700990 info.cellX = c.getInt(cellXIndex);
991 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992
Daniel Sandler8802e962010-05-26 16:28:16 -0400993 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400994 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400995 break;
996 }
997
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400999 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001001 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001002 break;
1003 default:
1004 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001005 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001007 folderInfo.add(info);
1008 break;
1009 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001011
1012 // now that we've loaded everthing re-save it with the
1013 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001014 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001015 } else {
1016 // Failed to load the shortcut, probably because the
1017 // activity manager couldn't resolve it (maybe the app
1018 // was uninstalled), or the db row was somehow screwed up.
1019 // Delete it.
1020 id = c.getLong(idIndex);
1021 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1022 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1023 id, false), null, null);
1024 }
1025 break;
1026
Adam Cohendf2cc412011-04-27 16:56:57 -07001027 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001028 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001029 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001030
Winson Chungaafa03c2010-06-11 17:34:16 -07001031 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001032 folderInfo.id = id;
1033 container = c.getInt(containerIndex);
1034 folderInfo.container = container;
1035 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001036 folderInfo.cellX = c.getInt(cellXIndex);
1037 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001038
1039 // check & update map of what's occupied
1040 if (!checkItemPlacement(occupied, folderInfo)) {
1041 break;
1042 }
Joe Onorato36115782010-06-17 13:28:48 -04001043 switch (container) {
1044 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001046 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001047 break;
1048 }
1049
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001050 sItemsIdMap.put(folderInfo.id, folderInfo);
1051 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001052 break;
1053
Joe Onorato36115782010-06-17 13:28:48 -04001054 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1055 // Read all Launcher-specific widget details
1056 int appWidgetId = c.getInt(appWidgetIdIndex);
1057 id = c.getLong(idIndex);
1058
1059 final AppWidgetProviderInfo provider =
1060 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001061
Joe Onorato36115782010-06-17 13:28:48 -04001062 if (!isSafeMode && (provider == null || provider.provider == null ||
1063 provider.provider.getPackageName() == null)) {
1064 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1065 + id + " appWidgetId=" + appWidgetId);
1066 itemsToRemove.add(id);
1067 } else {
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001068 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, "5");
Joe Onorato36115782010-06-17 13:28:48 -04001069 appWidgetInfo.id = id;
1070 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001071 appWidgetInfo.cellX = c.getInt(cellXIndex);
1072 appWidgetInfo.cellY = c.getInt(cellYIndex);
1073 appWidgetInfo.spanX = c.getInt(spanXIndex);
1074 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001075
1076 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1078 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001079 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001080 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001081 continue;
1082 }
1083 appWidgetInfo.container = c.getInt(containerIndex);
1084
1085 // check & update map of what's occupied
1086 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1087 break;
1088 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001089 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1090 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001091 }
1092 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001093 }
Joe Onorato36115782010-06-17 13:28:48 -04001094 } catch (Exception e) {
1095 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001096 }
1097 }
Joe Onorato36115782010-06-17 13:28:48 -04001098 } finally {
1099 c.close();
1100 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001101
Joe Onorato36115782010-06-17 13:28:48 -04001102 if (itemsToRemove.size() > 0) {
1103 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1104 LauncherSettings.Favorites.CONTENT_URI);
1105 // Remove dead items
1106 for (long id : itemsToRemove) {
1107 if (DEBUG_LOADERS) {
1108 Log.d(TAG, "Removed id = " + id);
1109 }
1110 // Don't notify content observers
1111 try {
1112 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1113 null, null);
1114 } catch (RemoteException e) {
1115 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001116 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118 }
1119
Joe Onorato36115782010-06-17 13:28:48 -04001120 if (DEBUG_LOADERS) {
1121 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1122 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001123 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001124 String line = "";
1125 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1126 if (s > 0) {
1127 line += " | ";
1128 }
Adam Cohend22015c2010-07-26 22:02:18 -07001129 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001130 line += ((occupied[s][x][y] != null) ? "#" : ".");
1131 }
1132 }
1133 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 }
Joe Onorato36115782010-06-17 13:28:48 -04001135 }
1136 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137
Joe Onorato36115782010-06-17 13:28:48 -04001138 /**
1139 * Read everything out of our database.
1140 */
1141 private void bindWorkspace() {
1142 final long t = SystemClock.uptimeMillis();
1143
1144 // Don't use these two variables in any of the callback runnables.
1145 // Otherwise we hold a reference to them.
1146 final Callbacks oldCallbacks = mCallbacks.get();
1147 if (oldCallbacks == null) {
1148 // This launcher has exited and nobody bothered to tell us. Just bail.
1149 Log.w(TAG, "LoaderTask running with no launcher");
1150 return;
1151 }
1152
1153 int N;
1154 // Tell the workspace that we're about to start firing items at it
1155 mHandler.post(new Runnable() {
1156 public void run() {
1157 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1158 if (callbacks != null) {
1159 callbacks.startBinding();
1160 }
1161 }
1162 });
1163 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001164 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001165 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1166 final int start = i;
1167 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 mHandler.post(new Runnable() {
1169 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001170 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001171 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001172 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 }
1174 }
1175 });
Joe Onorato36115782010-06-17 13:28:48 -04001176 }
1177 mHandler.post(new Runnable() {
1178 public void run() {
1179 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1180 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001181 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001182 }
1183 }
1184 });
1185 // Wait until the queue goes empty.
1186 mHandler.post(new Runnable() {
1187 public void run() {
1188 if (DEBUG_LOADERS) {
1189 Log.d(TAG, "Going to start binding widgets soon.");
1190 }
1191 }
1192 });
1193 // Bind the widgets, one at a time.
1194 // WARNING: this is calling into the workspace from the background thread,
1195 // but since getCurrentScreen() just returns the int, we should be okay. This
1196 // is just a hint for the order, and if it's wrong, we'll be okay.
1197 // TODO: instead, we should have that push the current screen into here.
1198 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001199 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001200 // once for the current screen
1201 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001202 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001203 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 mHandler.post(new Runnable() {
1205 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001206 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001208 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 }
1210 }
1211 });
1212 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 }
Joe Onorato36115782010-06-17 13:28:48 -04001214 // once for the other screens
1215 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001216 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001217 if (widget.screen != currentScreen) {
1218 mHandler.post(new Runnable() {
1219 public void run() {
1220 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1221 if (callbacks != null) {
1222 callbacks.bindAppWidget(widget);
1223 }
1224 }
1225 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001226 }
1227 }
Joe Onorato36115782010-06-17 13:28:48 -04001228 // Tell the workspace that we're done.
1229 mHandler.post(new Runnable() {
1230 public void run() {
1231 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1232 if (callbacks != null) {
1233 callbacks.finishBindingItems();
1234 }
1235 }
1236 });
1237 // If we're profiling, this is the last thing in the queue.
1238 mHandler.post(new Runnable() {
1239 public void run() {
1240 if (DEBUG_LOADERS) {
1241 Log.d(TAG, "bound workspace in "
1242 + (SystemClock.uptimeMillis()-t) + "ms");
1243 }
1244 }
1245 });
1246 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001247
Joe Onorato36115782010-06-17 13:28:48 -04001248 private void loadAndBindAllApps() {
1249 if (DEBUG_LOADERS) {
1250 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1251 }
1252 if (!mAllAppsLoaded) {
1253 loadAllAppsByBatch();
1254 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001255 return;
1256 }
Joe Onorato36115782010-06-17 13:28:48 -04001257 mAllAppsLoaded = true;
1258 } else {
1259 onlyBindAllApps();
1260 }
1261 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001262
Joe Onorato36115782010-06-17 13:28:48 -04001263 private void onlyBindAllApps() {
1264 final Callbacks oldCallbacks = mCallbacks.get();
1265 if (oldCallbacks == null) {
1266 // This launcher has exited and nobody bothered to tell us. Just bail.
1267 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1268 return;
1269 }
1270
1271 // shallow copy
1272 final ArrayList<ApplicationInfo> list
1273 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1274 mHandler.post(new Runnable() {
1275 public void run() {
1276 final long t = SystemClock.uptimeMillis();
1277 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1278 if (callbacks != null) {
1279 callbacks.bindAllApplications(list);
1280 }
1281 if (DEBUG_LOADERS) {
1282 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1283 + (SystemClock.uptimeMillis()-t) + "ms");
1284 }
1285 }
1286 });
1287
1288 }
1289
1290 private void loadAllAppsByBatch() {
1291 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1292
1293 // Don't use these two variables in any of the callback runnables.
1294 // Otherwise we hold a reference to them.
1295 final Callbacks oldCallbacks = mCallbacks.get();
1296 if (oldCallbacks == null) {
1297 // This launcher has exited and nobody bothered to tell us. Just bail.
1298 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1299 return;
1300 }
1301
1302 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1303 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1304
1305 final PackageManager packageManager = mContext.getPackageManager();
1306 List<ResolveInfo> apps = null;
1307
1308 int N = Integer.MAX_VALUE;
1309
1310 int startIndex;
1311 int i=0;
1312 int batchSize = -1;
1313 while (i < N && !mStopped) {
1314 if (i == 0) {
1315 mAllAppsList.clear();
1316 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1317 apps = packageManager.queryIntentActivities(mainIntent, 0);
1318 if (DEBUG_LOADERS) {
1319 Log.d(TAG, "queryIntentActivities took "
1320 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1321 }
1322 if (apps == null) {
1323 return;
1324 }
1325 N = apps.size();
1326 if (DEBUG_LOADERS) {
1327 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1328 }
1329 if (N == 0) {
1330 // There are no apps?!?
1331 return;
1332 }
1333 if (mBatchSize == 0) {
1334 batchSize = N;
1335 } else {
1336 batchSize = mBatchSize;
1337 }
1338
1339 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1340 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001341 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001342 if (DEBUG_LOADERS) {
1343 Log.d(TAG, "sort took "
1344 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1345 }
1346 }
1347
1348 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1349
1350 startIndex = i;
1351 for (int j=0; i<N && j<batchSize; j++) {
1352 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001353 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001354 mIconCache, mLabelCache, "6"));
Joe Onorato36115782010-06-17 13:28:48 -04001355 i++;
1356 }
1357
1358 final boolean first = i <= batchSize;
1359 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1360 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1361 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1362
Joe Onoratocc67f472010-06-08 10:54:30 -07001363 mHandler.post(new Runnable() {
1364 public void run() {
1365 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001366 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001367 if (first) {
1368 callbacks.bindAllApplications(added);
1369 } else {
1370 callbacks.bindAppsAdded(added);
1371 }
1372 if (DEBUG_LOADERS) {
1373 Log.d(TAG, "bound " + added.size() + " apps in "
1374 + (SystemClock.uptimeMillis() - t) + "ms");
1375 }
1376 } else {
1377 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001378 }
1379 }
1380 });
1381
Daniel Sandlerdca66122010-04-13 16:23:58 -04001382 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001383 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1384 + (SystemClock.uptimeMillis()-t2) + "ms");
1385 }
1386
1387 if (mAllAppsLoadDelay > 0 && i < N) {
1388 try {
1389 if (DEBUG_LOADERS) {
1390 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1391 }
1392 Thread.sleep(mAllAppsLoadDelay);
1393 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 }
1395 }
1396
Joe Onorato36115782010-06-17 13:28:48 -04001397 if (DEBUG_LOADERS) {
1398 Log.d(TAG, "cached all " + N + " apps in "
1399 + (SystemClock.uptimeMillis()-t) + "ms"
1400 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001401 }
1402 }
1403
1404 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001405 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1406 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1407 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1408 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1409 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001410 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001411 }
1412 }
1413
1414 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001415 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001416 }
1417
1418 private class PackageUpdatedTask implements Runnable {
1419 int mOp;
1420 String[] mPackages;
1421
1422 public static final int OP_NONE = 0;
1423 public static final int OP_ADD = 1;
1424 public static final int OP_UPDATE = 2;
1425 public static final int OP_REMOVE = 3; // uninstlled
1426 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1427
1428
1429 public PackageUpdatedTask(int op, String[] packages) {
1430 mOp = op;
1431 mPackages = packages;
1432 }
1433
1434 public void run() {
1435 final Context context = mApp;
1436
1437 final String[] packages = mPackages;
1438 final int N = packages.length;
1439 switch (mOp) {
1440 case OP_ADD:
1441 for (int i=0; i<N; i++) {
1442 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1443 mAllAppsList.addPackage(context, packages[i]);
1444 }
1445 break;
1446 case OP_UPDATE:
1447 for (int i=0; i<N; i++) {
1448 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1449 mAllAppsList.updatePackage(context, packages[i]);
1450 }
1451 break;
1452 case OP_REMOVE:
1453 case OP_UNAVAILABLE:
1454 for (int i=0; i<N; i++) {
1455 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1456 mAllAppsList.removePackage(packages[i]);
1457 }
1458 break;
1459 }
1460
1461 ArrayList<ApplicationInfo> added = null;
1462 ArrayList<ApplicationInfo> removed = null;
1463 ArrayList<ApplicationInfo> modified = null;
1464
1465 if (mAllAppsList.added.size() > 0) {
1466 added = mAllAppsList.added;
1467 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1468 }
1469 if (mAllAppsList.removed.size() > 0) {
1470 removed = mAllAppsList.removed;
1471 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1472 for (ApplicationInfo info: removed) {
1473 mIconCache.remove(info.intent.getComponent());
1474 }
1475 }
1476 if (mAllAppsList.modified.size() > 0) {
1477 modified = mAllAppsList.modified;
1478 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1479 }
1480
1481 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1482 if (callbacks == null) {
1483 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1484 return;
1485 }
1486
1487 if (added != null) {
1488 final ArrayList<ApplicationInfo> addedFinal = added;
1489 mHandler.post(new Runnable() {
1490 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001491 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1492 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001493 callbacks.bindAppsAdded(addedFinal);
1494 }
1495 }
1496 });
1497 }
1498 if (modified != null) {
1499 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1500 mHandler.post(new Runnable() {
1501 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001502 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1503 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001504 callbacks.bindAppsUpdated(modifiedFinal);
1505 }
1506 }
1507 });
1508 }
1509 if (removed != null) {
1510 final boolean permanent = mOp != OP_UNAVAILABLE;
1511 final ArrayList<ApplicationInfo> removedFinal = removed;
1512 mHandler.post(new Runnable() {
1513 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001514 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1515 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001516 callbacks.bindAppsRemoved(removedFinal, permanent);
1517 }
1518 }
1519 });
Joe Onoratobe386092009-11-17 17:32:16 -08001520 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001521
1522 mHandler.post(new Runnable() {
1523 @Override
1524 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001525 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1526 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001527 callbacks.bindPackagesUpdated();
1528 }
1529 }
1530 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001531 }
1532 }
1533
1534 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001535 * This is called from the code that adds shortcuts from the intent receiver. This
1536 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 */
Joe Onorato56d82912010-03-07 14:32:10 -05001538 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001539 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001540 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001541
Joe Onorato56d82912010-03-07 14:32:10 -05001542 /**
1543 * Make an ShortcutInfo object for a shortcut that is an application.
1544 *
1545 * If c is not null, then it will be used to fill in missing data like the title and icon.
1546 */
1547 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001548 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001549 Bitmap icon = null;
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001550 final ShortcutInfo info = new ShortcutInfo("7");
Joe Onorato56d82912010-03-07 14:32:10 -05001551
1552 ComponentName componentName = intent.getComponent();
1553 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001554 return null;
1555 }
1556
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001557 // TODO: See if the PackageManager knows about this case. If it doesn't
1558 // then return null & delete this.
1559
Joe Onorato56d82912010-03-07 14:32:10 -05001560 // the resource -- This may implicitly give us back the fallback icon,
1561 // but don't worry about that. All we're doing with usingFallbackIcon is
1562 // to avoid saving lots of copies of that in the database, and most apps
1563 // have icons anyway.
1564 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1565 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001566 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001567 }
Joe Onorato56d82912010-03-07 14:32:10 -05001568 // the db
1569 if (icon == null) {
1570 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001571 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001572 }
1573 }
1574 // the fallback icon
1575 if (icon == null) {
1576 icon = getFallbackIcon();
1577 info.usingFallbackIcon = true;
1578 }
1579 info.setIcon(icon);
1580
1581 // from the resource
1582 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001583 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1584 if (labelCache != null && labelCache.containsKey(key)) {
1585 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001586 } else {
1587 info.title = resolveInfo.activityInfo.loadLabel(manager);
1588 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001589 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001590 }
1591 }
Joe Onorato56d82912010-03-07 14:32:10 -05001592 }
1593 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001594 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001595 if (c != null) {
1596 info.title = c.getString(titleIndex);
1597 }
1598 }
1599 // fall back to the class name of the activity
1600 if (info.title == null) {
1601 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1604 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001608 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001610 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001611 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1612 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613
Joe Onorato56d82912010-03-07 14:32:10 -05001614 Bitmap icon = null;
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001615 final ShortcutInfo info = new ShortcutInfo("8");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001618 // TODO: If there's an explicit component and we can't install that, delete it.
1619
Joe Onorato56d82912010-03-07 14:32:10 -05001620 info.title = c.getString(titleIndex);
1621
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622 int iconType = c.getInt(iconTypeIndex);
1623 switch (iconType) {
1624 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1625 String packageName = c.getString(iconPackageIndex);
1626 String resourceName = c.getString(iconResourceIndex);
1627 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001628 info.customIcon = false;
1629 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001632 if (resources != null) {
1633 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001634 icon = Utilities.createIconBitmap(
1635 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001636 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001637 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001638 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
Joe Onorato56d82912010-03-07 14:32:10 -05001640 // the db
1641 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001642 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001643 }
1644 // the fallback icon
1645 if (icon == null) {
1646 icon = getFallbackIcon();
1647 info.usingFallbackIcon = true;
1648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001649 break;
1650 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001651 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001652 if (icon == null) {
1653 icon = getFallbackIcon();
1654 info.customIcon = false;
1655 info.usingFallbackIcon = true;
1656 } else {
1657 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 break;
1660 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001661 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001662 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001663 info.customIcon = false;
1664 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001666 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001667 return info;
1668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669
Michael Jurka931dc972011-08-05 15:08:15 -07001670 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001671 if (false) {
1672 Log.d(TAG, "getIconFromCursor app="
1673 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1674 }
1675 byte[] data = c.getBlob(iconIndex);
1676 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001677 return Utilities.createIconBitmap(
1678 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001679 } catch (Exception e) {
1680 return null;
1681 }
1682 }
1683
Winson Chung3d503fb2011-07-13 17:25:49 -07001684 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1685 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001686 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001687 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001688
1689 return info;
1690 }
1691
Winson Chunga9abd0e2010-10-27 17:18:37 -07001692 /**
Winson Chung55cef262010-10-28 14:14:18 -07001693 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1694 */
1695 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1696 ComponentName component) {
1697 List<AppWidgetProviderInfo> widgets =
1698 AppWidgetManager.getInstance(context).getInstalledProviders();
1699 for (AppWidgetProviderInfo info : widgets) {
1700 if (info.provider.equals(component)) {
1701 return info;
1702 }
1703 }
1704 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001705 }
1706
Winson Chung68846fd2010-10-29 11:00:27 -07001707 /**
1708 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1709 */
1710 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1711 final PackageManager packageManager = context.getPackageManager();
1712 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1713 new ArrayList<WidgetMimeTypeHandlerData>();
1714
1715 final Intent supportsIntent =
1716 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1717 supportsIntent.setType(mimeType);
1718
1719 // Create a set of widget configuration components that we can test against
1720 final List<AppWidgetProviderInfo> widgets =
1721 AppWidgetManager.getInstance(context).getInstalledProviders();
1722 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1723 new HashMap<ComponentName, AppWidgetProviderInfo>();
1724 for (AppWidgetProviderInfo info : widgets) {
1725 configurationComponentToWidget.put(info.configure, info);
1726 }
1727
1728 // Run through each of the intents that can handle this type of clip data, and cross
1729 // reference them with the components that are actual configuration components
1730 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1731 PackageManager.MATCH_DEFAULT_ONLY);
1732 for (ResolveInfo info : activities) {
1733 final ActivityInfo activityInfo = info.activityInfo;
1734 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1735 activityInfo.name);
1736 if (configurationComponentToWidget.containsKey(infoComponent)) {
1737 supportedConfigurationActivities.add(
1738 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1739 configurationComponentToWidget.get(infoComponent)));
1740 }
1741 }
1742 return supportedConfigurationActivities;
1743 }
1744
Winson Chunga9abd0e2010-10-27 17:18:37 -07001745 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001746 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1747 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1748 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1749
1750 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001751 boolean customIcon = false;
1752 ShortcutIconResource iconResource = null;
1753
1754 if (bitmap != null && bitmap instanceof Bitmap) {
1755 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001756 customIcon = true;
1757 } else {
1758 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1759 if (extra != null && extra instanceof ShortcutIconResource) {
1760 try {
1761 iconResource = (ShortcutIconResource) extra;
1762 final PackageManager packageManager = context.getPackageManager();
1763 Resources resources = packageManager.getResourcesForApplication(
1764 iconResource.packageName);
1765 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001766 icon = Utilities.createIconBitmap(
1767 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001768 } catch (Exception e) {
1769 Log.w(TAG, "Could not load shortcut icon: " + extra);
1770 }
1771 }
1772 }
1773
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001774 final ShortcutInfo info = new ShortcutInfo("9");
Joe Onorato56d82912010-03-07 14:32:10 -05001775
1776 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001777 if (fallbackIcon != null) {
1778 icon = fallbackIcon;
1779 } else {
1780 icon = getFallbackIcon();
1781 info.usingFallbackIcon = true;
1782 }
Joe Onorato56d82912010-03-07 14:32:10 -05001783 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001784 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001785
Joe Onorato0589f0f2010-02-08 13:44:00 -08001786 info.title = name;
1787 info.intent = intent;
1788 info.customIcon = customIcon;
1789 info.iconResource = iconResource;
1790
1791 return info;
1792 }
1793
Winson Chungaac01e12011-08-17 10:37:13 -07001794 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1795 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001796 // If apps can't be on SD, don't even bother.
1797 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001798 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001799 }
Joe Onorato56d82912010-03-07 14:32:10 -05001800 // If this icon doesn't have a custom icon, check to see
1801 // what's stored in the DB, and if it doesn't match what
1802 // we're going to show, store what we are going to show back
1803 // into the DB. We do this so when we're loading, if the
1804 // package manager can't find an icon (for example because
1805 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001806 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001807 cache.put(info, c.getBlob(iconIndex));
1808 return true;
1809 }
1810 return false;
1811 }
1812 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1813 boolean needSave = false;
1814 try {
1815 if (data != null) {
1816 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1817 Bitmap loaded = info.getIcon(mIconCache);
1818 needSave = !saved.sameAs(loaded);
1819 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001820 needSave = true;
1821 }
Winson Chungaac01e12011-08-17 10:37:13 -07001822 } catch (Exception e) {
1823 needSave = true;
1824 }
1825 if (needSave) {
1826 Log.d(TAG, "going to save icon bitmap for info=" + info);
1827 // This is slower than is ideal, but this only happens once
1828 // or when the app is updated with a new icon.
1829 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001830 }
1831 }
1832
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001834 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 * or make a new one.
1836 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001837 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 // See if a placeholder was created for us already
1839 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001840 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 // No placeholder -- create a new instance
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001842 folderInfo = new FolderInfo("10");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 folders.put(id, folderInfo);
1844 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001845 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
1847
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001849 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001850 = new Comparator<ApplicationInfo>() {
1851 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001852 int result = sCollator.compare(a.title.toString(), b.title.toString());
1853 if (result == 0) {
1854 result = a.componentName.compareTo(b.componentName);
1855 }
1856 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 };
Winson Chung78403fe2011-01-21 15:38:02 -08001859 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1860 = new Comparator<ApplicationInfo>() {
1861 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1862 if (a.firstInstallTime < b.firstInstallTime) return 1;
1863 if (a.firstInstallTime > b.firstInstallTime) return -1;
1864 return 0;
1865 }
1866 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001867 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1868 = new Comparator<AppWidgetProviderInfo>() {
1869 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1870 return sCollator.compare(a.label.toString(), b.label.toString());
1871 }
1872 };
Winson Chung5308f242011-08-18 12:12:41 -07001873 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1874 if (info.activityInfo != null) {
1875 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1876 } else {
1877 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1878 }
1879 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001880 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1881 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001882 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001883 ShortcutNameComparator(PackageManager pm) {
1884 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001885 mLabelCache = new HashMap<Object, CharSequence>();
1886 }
1887 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1888 mPackageManager = pm;
1889 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001890 }
1891 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001892 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001893 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1894 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1895 if (mLabelCache.containsKey(keyA)) {
1896 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001897 } else {
1898 labelA = a.loadLabel(mPackageManager).toString();
1899
Winson Chung5308f242011-08-18 12:12:41 -07001900 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001901 }
Winson Chung5308f242011-08-18 12:12:41 -07001902 if (mLabelCache.containsKey(keyB)) {
1903 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001904 } else {
1905 labelB = b.loadLabel(mPackageManager).toString();
1906
Winson Chung5308f242011-08-18 12:12:41 -07001907 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001908 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001909 return sCollator.compare(labelA, labelB);
1910 }
1911 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001912 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1913 private PackageManager mPackageManager;
1914 private HashMap<Object, String> mLabelCache;
1915 WidgetAndShortcutNameComparator(PackageManager pm) {
1916 mPackageManager = pm;
1917 mLabelCache = new HashMap<Object, String>();
1918 }
1919 public final int compare(Object a, Object b) {
1920 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001921 if (mLabelCache.containsKey(a)) {
1922 labelA = mLabelCache.get(a);
1923 } else {
1924 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001925 ((AppWidgetProviderInfo) a).label :
1926 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001927 mLabelCache.put(a, labelA);
1928 }
1929 if (mLabelCache.containsKey(b)) {
1930 labelB = mLabelCache.get(b);
1931 } else {
1932 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001933 ((AppWidgetProviderInfo) b).label :
1934 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001935 mLabelCache.put(b, labelB);
1936 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001937 return sCollator.compare(labelA, labelB);
1938 }
1939 };
Joe Onoratobe386092009-11-17 17:32:16 -08001940
1941 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001942 Log.d(TAG, "mCallbacks=" + mCallbacks);
1943 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1944 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1945 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1946 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001947 if (mLoaderTask != null) {
1948 mLoaderTask.dumpState();
1949 } else {
1950 Log.d(TAG, "mLoaderTask=null");
1951 }
Joe Onoratobe386092009-11-17 17:32:16 -08001952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953}