blob: 239970e84d132756c56fcd9763404cd3ac8be624 [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 Jurka83df1882011-08-31 20:59:26 -0700468 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700469 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 }
Michael Jurka83df1882011-08-31 20:59:26 -0700484 };
485 if (sWorkerThread.getThreadId() == Process.myTid()) {
486 r.run();
487 } else {
488 sWorker.post(r);
489 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400490 }
491
492 /**
493 * Removes the specified item from the database
494 * @param context
495 * @param item
496 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700497 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400498 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700499 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700500 sWorker.post(new Runnable() {
501 public void run() {
502 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700503 switch (item.itemType) {
504 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
505 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700506 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700507 break;
508 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
509 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700510 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700511 break;
512 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
513 sAppWidgets.remove((LauncherAppWidgetInfo) item);
514 break;
515 }
516 sItemsIdMap.remove(item.id);
Winson Chungb1094bd2011-08-24 16:14:08 -0700517 sDbIconCache.remove(item);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700518 }
519 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400520 }
521
522 /**
523 * Remove the contents of the specified folder from the database
524 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700525 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526 final ContentResolver cr = context.getContentResolver();
527
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700528 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700529 public void run() {
530 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700531 sItemsIdMap.remove(info.id);
532 sFolders.remove(info.id);
Winson Chungc22a54d2011-08-25 13:51:25 -0700533 sDbIconCache.remove(info);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700534 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700535
Adam Cohen9932a9b2011-08-02 22:14:07 -0700536 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Adam Cohenafb01ee2011-06-23 15:38:03 -0700537 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700538 for (ItemInfo childInfo : info.contents) {
539 sItemsIdMap.remove(childInfo.id);
Winson Chungc22a54d2011-08-25 13:51:25 -0700540 sDbIconCache.remove(childInfo);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700541 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700542 }
543 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 }
545
546 /**
547 * Set this as the current Launcher activity object for the loader.
548 */
549 public void initialize(Callbacks callbacks) {
550 synchronized (mLock) {
551 mCallbacks = new WeakReference<Callbacks>(callbacks);
552 }
553 }
554
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700555 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
557 * ACTION_PACKAGE_CHANGED.
558 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100559 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400560 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400561 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700562
Joe Onorato36115782010-06-17 13:28:48 -0400563 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400564
Joe Onorato36115782010-06-17 13:28:48 -0400565 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
566 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
567 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
568 final String packageName = intent.getData().getSchemeSpecificPart();
569 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400570
Joe Onorato36115782010-06-17 13:28:48 -0400571 int op = PackageUpdatedTask.OP_NONE;
572
573 if (packageName == null || packageName.length() == 0) {
574 // they sent us a bad intent
575 return;
576 }
577
578 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
579 op = PackageUpdatedTask.OP_UPDATE;
580 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
581 if (!replacing) {
582 op = PackageUpdatedTask.OP_REMOVE;
583 }
584 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
585 // later, we will update the package at this time
586 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
587 if (!replacing) {
588 op = PackageUpdatedTask.OP_ADD;
589 } else {
590 op = PackageUpdatedTask.OP_UPDATE;
591 }
592 }
593
594 if (op != PackageUpdatedTask.OP_NONE) {
595 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
596 }
597
598 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400599 // First, schedule to add these apps back in.
600 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
601 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
602 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700603 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400604 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
605 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
606 enqueuePackageUpdated(new PackageUpdatedTask(
607 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700608 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
609 // If we have changed locale we need to clear out the labels in all apps.
610 // Do this here because if the launcher activity is running it will be restarted.
611 // If it's not running startLoaderFromBackground will merely tell it that it needs
612 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
613 // next time.
614 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700615 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700616 startLoaderFromBackground();
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700617 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
618 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700619 if (mCallbacks != null) {
620 Callbacks callbacks = mCallbacks.get();
621 if (callbacks != null) {
622 callbacks.bindSearchablesChanged();
623 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700624 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700625 }
626 }
627
628 /**
629 * When the launcher is in the background, it's possible for it to miss paired
630 * configuration changes. So whenever we trigger the loader from the background
631 * tell the launcher that it needs to re-run the loader when it comes back instead
632 * of doing it now.
633 */
634 public void startLoaderFromBackground() {
635 boolean runLoader = false;
636 if (mCallbacks != null) {
637 Callbacks callbacks = mCallbacks.get();
638 if (callbacks != null) {
639 // Only actually run the loader if they're not paused.
640 if (!callbacks.setLoadOnResume()) {
641 runLoader = true;
642 }
643 }
644 }
645 if (runLoader) {
646 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700647 }
Joe Onorato36115782010-06-17 13:28:48 -0400648 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400649
Joe Onorato36115782010-06-17 13:28:48 -0400650 public void startLoader(Context context, boolean isLaunching) {
651 synchronized (mLock) {
652 if (DEBUG_LOADERS) {
653 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
654 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655
Joe Onorato36115782010-06-17 13:28:48 -0400656 // Don't bother to start the thread if we know it's not going to do anything
657 if (mCallbacks != null && mCallbacks.get() != null) {
658 // If there is already one running, tell it to stop.
659 LoaderTask oldTask = mLoaderTask;
660 if (oldTask != null) {
661 if (oldTask.isLaunching()) {
662 // don't downgrade isLaunching if we're already running
663 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500664 }
Joe Onorato36115782010-06-17 13:28:48 -0400665 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500666 }
Joe Onorato36115782010-06-17 13:28:48 -0400667 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700668 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 }
671 }
672
Joe Onorato36115782010-06-17 13:28:48 -0400673 public void stopLoader() {
674 synchronized (mLock) {
675 if (mLoaderTask != null) {
676 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 }
678 }
Joe Onorato36115782010-06-17 13:28:48 -0400679 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680
Michael Jurkac57b7a82011-08-09 22:02:20 -0700681 public boolean isAllAppsLoaded() {
682 return mAllAppsLoaded;
683 }
684
Joe Onorato36115782010-06-17 13:28:48 -0400685 /**
686 * Runnable for the thread that loads the contents of the launcher:
687 * - workspace icons
688 * - widgets
689 * - all apps icons
690 */
691 private class LoaderTask implements Runnable {
692 private Context mContext;
693 private Thread mWaitThread;
694 private boolean mIsLaunching;
695 private boolean mStopped;
696 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700697 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400698
699 LoaderTask(Context context, boolean isLaunching) {
700 mContext = context;
701 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700702 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 }
704
Joe Onorato36115782010-06-17 13:28:48 -0400705 boolean isLaunching() {
706 return mIsLaunching;
707 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708
Joe Onorato36115782010-06-17 13:28:48 -0400709 private void loadAndBindWorkspace() {
710 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400711 if (DEBUG_LOADERS) {
712 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700714
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700715 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400716 loadWorkspace();
717 if (mStopped) {
718 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 }
Joe Onorato36115782010-06-17 13:28:48 -0400720 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 }
722
Joe Onorato36115782010-06-17 13:28:48 -0400723 // Bind the workspace
724 bindWorkspace();
725 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400726
Joe Onorato36115782010-06-17 13:28:48 -0400727 private void waitForIdle() {
728 // Wait until the either we're stopped or the other threads are done.
729 // This way we don't start loading all apps until the workspace has settled
730 // down.
731 synchronized (LoaderTask.this) {
732 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700733
Joe Onorato36115782010-06-17 13:28:48 -0400734 mHandler.postIdle(new Runnable() {
735 public void run() {
736 synchronized (LoaderTask.this) {
737 mLoadAndBindStepFinished = true;
738 if (DEBUG_LOADERS) {
739 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400740 }
Joe Onorato36115782010-06-17 13:28:48 -0400741 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400742 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400743 }
Joe Onorato36115782010-06-17 13:28:48 -0400744 });
745
746 while (!mStopped && !mLoadAndBindStepFinished) {
747 try {
748 this.wait();
749 } catch (InterruptedException ex) {
750 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400751 }
752 }
Joe Onorato36115782010-06-17 13:28:48 -0400753 if (DEBUG_LOADERS) {
754 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700755 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400756 + "ms for previous step to finish binding");
757 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400758 }
Joe Onorato36115782010-06-17 13:28:48 -0400759 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400760
Joe Onorato36115782010-06-17 13:28:48 -0400761 public void run() {
762 // Optimize for end-user experience: if the Launcher is up and // running with the
763 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
764 // workspace first (default).
765 final Callbacks cbk = mCallbacks.get();
766 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400767
Joe Onorato36115782010-06-17 13:28:48 -0400768 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400769 // Elevate priority when Home launches for the first time to avoid
770 // starving at boot time. Staring at a blank home is not cool.
771 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700772 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
773 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400774 android.os.Process.setThreadPriority(mIsLaunching
775 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
776 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400777 if (loadWorkspaceFirst) {
778 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
779 loadAndBindWorkspace();
780 } else {
781 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700782 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400783 }
784
Joe Onorato36115782010-06-17 13:28:48 -0400785 if (mStopped) {
786 break keep_running;
787 }
788
789 // Whew! Hard work done. Slow us down, and wait until the UI thread has
790 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400791 synchronized (mLock) {
792 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700793 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400794 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
795 }
796 }
Joe Onorato36115782010-06-17 13:28:48 -0400797 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400798
799 // second step
800 if (loadWorkspaceFirst) {
801 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700802 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400803 } else {
804 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
805 loadAndBindWorkspace();
806 }
Joe Onorato36115782010-06-17 13:28:48 -0400807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808
Winson Chungaac01e12011-08-17 10:37:13 -0700809
810 // Update the saved icons if necessary
811 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700812 for (Object key : sDbIconCache.keySet()) {
813 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700814 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700815 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700816
Joe Onorato36115782010-06-17 13:28:48 -0400817 // Clear out this reference, otherwise we end up holding it until all of the
818 // callback runnables are done.
819 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820
Joe Onorato36115782010-06-17 13:28:48 -0400821 synchronized (mLock) {
822 // If we are still the last one to be scheduled, remove ourselves.
823 if (mLoaderTask == this) {
824 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400825 }
Joe Onorato36115782010-06-17 13:28:48 -0400826 }
Joe Onorato36115782010-06-17 13:28:48 -0400827 }
828
829 public void stopLocked() {
830 synchronized (LoaderTask.this) {
831 mStopped = true;
832 this.notify();
833 }
834 }
835
836 /**
837 * Gets the callbacks object. If we've been stopped, or if the launcher object
838 * has somehow been garbage collected, return null instead. Pass in the Callbacks
839 * object that was around when the deferred message was scheduled, and if there's
840 * a new Callbacks object around then also return null. This will save us from
841 * calling onto it with data that will be ignored.
842 */
843 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
844 synchronized (mLock) {
845 if (mStopped) {
846 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400847 }
Joe Onorato36115782010-06-17 13:28:48 -0400848
849 if (mCallbacks == null) {
850 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400851 }
Joe Onorato36115782010-06-17 13:28:48 -0400852
853 final Callbacks callbacks = mCallbacks.get();
854 if (callbacks != oldCallbacks) {
855 return null;
856 }
857 if (callbacks == null) {
858 Log.w(TAG, "no mCallbacks");
859 return null;
860 }
861
862 return callbacks;
863 }
864 }
865
866 // check & update map of what's occupied; used to discard overlapping/invalid items
867 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700868 int containerIndex = item.screen;
869 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
870 // We use the last index to refer to the hotseat
871 containerIndex = Launcher.SCREEN_COUNT;
872 // Return early if we detect that an item is under the hotseat button
873 if (Hotseat.isAllAppsButtonRank(item.screen)) {
874 return false;
875 }
876 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
877 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400878 return true;
879 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700880
Joe Onorato36115782010-06-17 13:28:48 -0400881 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
882 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700883 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400884 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700885 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400886 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700887 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700888 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400889 return false;
890 }
891 }
892 }
893 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
894 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700895 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400896 }
897 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700898
Joe Onorato36115782010-06-17 13:28:48 -0400899 return true;
900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901
Joe Onorato36115782010-06-17 13:28:48 -0400902 private void loadWorkspace() {
903 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904
Joe Onorato36115782010-06-17 13:28:48 -0400905 final Context context = mContext;
906 final ContentResolver contentResolver = context.getContentResolver();
907 final PackageManager manager = context.getPackageManager();
908 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
909 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400910
Adam Cohen4eac29a2011-07-11 17:53:37 -0700911 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700912 sAppWidgets.clear();
913 sFolders.clear();
914 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700915 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800916
Joe Onorato36115782010-06-17 13:28:48 -0400917 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918
Joe Onorato36115782010-06-17 13:28:48 -0400919 final Cursor c = contentResolver.query(
920 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400921
Winson Chungf30ad5f2011-08-08 10:55:42 -0700922 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700923 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700924 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925
Joe Onorato36115782010-06-17 13:28:48 -0400926 try {
927 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
928 final int intentIndex = c.getColumnIndexOrThrow
929 (LauncherSettings.Favorites.INTENT);
930 final int titleIndex = c.getColumnIndexOrThrow
931 (LauncherSettings.Favorites.TITLE);
932 final int iconTypeIndex = c.getColumnIndexOrThrow(
933 LauncherSettings.Favorites.ICON_TYPE);
934 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
935 final int iconPackageIndex = c.getColumnIndexOrThrow(
936 LauncherSettings.Favorites.ICON_PACKAGE);
937 final int iconResourceIndex = c.getColumnIndexOrThrow(
938 LauncherSettings.Favorites.ICON_RESOURCE);
939 final int containerIndex = c.getColumnIndexOrThrow(
940 LauncherSettings.Favorites.CONTAINER);
941 final int itemTypeIndex = c.getColumnIndexOrThrow(
942 LauncherSettings.Favorites.ITEM_TYPE);
943 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
944 LauncherSettings.Favorites.APPWIDGET_ID);
945 final int screenIndex = c.getColumnIndexOrThrow(
946 LauncherSettings.Favorites.SCREEN);
947 final int cellXIndex = c.getColumnIndexOrThrow
948 (LauncherSettings.Favorites.CELLX);
949 final int cellYIndex = c.getColumnIndexOrThrow
950 (LauncherSettings.Favorites.CELLY);
951 final int spanXIndex = c.getColumnIndexOrThrow
952 (LauncherSettings.Favorites.SPANX);
953 final int spanYIndex = c.getColumnIndexOrThrow(
954 LauncherSettings.Favorites.SPANY);
955 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
956 final int displayModeIndex = c.getColumnIndexOrThrow(
957 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958
Joe Onorato36115782010-06-17 13:28:48 -0400959 ShortcutInfo info;
960 String intentDescription;
961 LauncherAppWidgetInfo appWidgetInfo;
962 int container;
963 long id;
964 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965
Joe Onorato36115782010-06-17 13:28:48 -0400966 while (!mStopped && c.moveToNext()) {
967 try {
968 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969
Joe Onorato36115782010-06-17 13:28:48 -0400970 switch (itemType) {
971 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
972 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
973 intentDescription = c.getString(intentIndex);
974 try {
975 intent = Intent.parseUri(intentDescription, 0);
976 } catch (URISyntaxException e) {
977 continue;
978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979
Joe Onorato36115782010-06-17 13:28:48 -0400980 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
981 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700982 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400983 } else {
984 info = getShortcutInfo(c, context, iconTypeIndex,
985 iconPackageIndex, iconResourceIndex, iconIndex,
986 titleIndex);
987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988
Joe Onorato36115782010-06-17 13:28:48 -0400989 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400990 info.intent = intent;
991 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400993 info.container = container;
994 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700995 info.cellX = c.getInt(cellXIndex);
996 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997
Daniel Sandler8802e962010-05-26 16:28:16 -0400998 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400999 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001000 break;
1001 }
1002
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001004 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001006 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001007 break;
1008 default:
1009 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001010 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001011 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001012 folderInfo.add(info);
1013 break;
1014 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001015 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001016
1017 // now that we've loaded everthing re-save it with the
1018 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001019 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001020 } else {
1021 // Failed to load the shortcut, probably because the
1022 // activity manager couldn't resolve it (maybe the app
1023 // was uninstalled), or the db row was somehow screwed up.
1024 // Delete it.
1025 id = c.getLong(idIndex);
1026 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1027 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1028 id, false), null, null);
1029 }
1030 break;
1031
Adam Cohendf2cc412011-04-27 16:56:57 -07001032 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001033 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001034 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001035
Winson Chungaafa03c2010-06-11 17:34:16 -07001036 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001037 folderInfo.id = id;
1038 container = c.getInt(containerIndex);
1039 folderInfo.container = container;
1040 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001041 folderInfo.cellX = c.getInt(cellXIndex);
1042 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001043
1044 // check & update map of what's occupied
1045 if (!checkItemPlacement(occupied, folderInfo)) {
1046 break;
1047 }
Joe Onorato36115782010-06-17 13:28:48 -04001048 switch (container) {
1049 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001051 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001052 break;
1053 }
1054
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001055 sItemsIdMap.put(folderInfo.id, folderInfo);
1056 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001057 break;
1058
Joe Onorato36115782010-06-17 13:28:48 -04001059 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1060 // Read all Launcher-specific widget details
1061 int appWidgetId = c.getInt(appWidgetIdIndex);
1062 id = c.getLong(idIndex);
1063
1064 final AppWidgetProviderInfo provider =
1065 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001066
Joe Onorato36115782010-06-17 13:28:48 -04001067 if (!isSafeMode && (provider == null || provider.provider == null ||
1068 provider.provider.getPackageName() == null)) {
1069 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1070 + id + " appWidgetId=" + appWidgetId);
1071 itemsToRemove.add(id);
1072 } else {
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001073 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, "5");
Joe Onorato36115782010-06-17 13:28:48 -04001074 appWidgetInfo.id = id;
1075 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001076 appWidgetInfo.cellX = c.getInt(cellXIndex);
1077 appWidgetInfo.cellY = c.getInt(cellYIndex);
1078 appWidgetInfo.spanX = c.getInt(spanXIndex);
1079 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001080
1081 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001082 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1083 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001084 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001085 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001086 continue;
1087 }
1088 appWidgetInfo.container = c.getInt(containerIndex);
1089
1090 // check & update map of what's occupied
1091 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1092 break;
1093 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001094 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1095 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001096 }
1097 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001098 }
Joe Onorato36115782010-06-17 13:28:48 -04001099 } catch (Exception e) {
1100 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001101 }
1102 }
Joe Onorato36115782010-06-17 13:28:48 -04001103 } finally {
1104 c.close();
1105 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001106
Joe Onorato36115782010-06-17 13:28:48 -04001107 if (itemsToRemove.size() > 0) {
1108 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1109 LauncherSettings.Favorites.CONTENT_URI);
1110 // Remove dead items
1111 for (long id : itemsToRemove) {
1112 if (DEBUG_LOADERS) {
1113 Log.d(TAG, "Removed id = " + id);
1114 }
1115 // Don't notify content observers
1116 try {
1117 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1118 null, null);
1119 } catch (RemoteException e) {
1120 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001121 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 }
1124
Joe Onorato36115782010-06-17 13:28:48 -04001125 if (DEBUG_LOADERS) {
1126 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1127 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001128 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001129 String line = "";
1130 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1131 if (s > 0) {
1132 line += " | ";
1133 }
Adam Cohend22015c2010-07-26 22:02:18 -07001134 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001135 line += ((occupied[s][x][y] != null) ? "#" : ".");
1136 }
1137 }
1138 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 }
Joe Onorato36115782010-06-17 13:28:48 -04001140 }
1141 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142
Joe Onorato36115782010-06-17 13:28:48 -04001143 /**
1144 * Read everything out of our database.
1145 */
1146 private void bindWorkspace() {
1147 final long t = SystemClock.uptimeMillis();
1148
1149 // Don't use these two variables in any of the callback runnables.
1150 // Otherwise we hold a reference to them.
1151 final Callbacks oldCallbacks = mCallbacks.get();
1152 if (oldCallbacks == null) {
1153 // This launcher has exited and nobody bothered to tell us. Just bail.
1154 Log.w(TAG, "LoaderTask running with no launcher");
1155 return;
1156 }
1157
1158 int N;
1159 // Tell the workspace that we're about to start firing items at it
1160 mHandler.post(new Runnable() {
1161 public void run() {
1162 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1163 if (callbacks != null) {
1164 callbacks.startBinding();
1165 }
1166 }
1167 });
1168 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001169 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001170 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1171 final int start = i;
1172 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 mHandler.post(new Runnable() {
1174 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001175 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001177 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001178 }
1179 }
1180 });
Joe Onorato36115782010-06-17 13:28:48 -04001181 }
1182 mHandler.post(new Runnable() {
1183 public void run() {
1184 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1185 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001186 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001187 }
1188 }
1189 });
1190 // Wait until the queue goes empty.
1191 mHandler.post(new Runnable() {
1192 public void run() {
1193 if (DEBUG_LOADERS) {
1194 Log.d(TAG, "Going to start binding widgets soon.");
1195 }
1196 }
1197 });
1198 // Bind the widgets, one at a time.
1199 // WARNING: this is calling into the workspace from the background thread,
1200 // but since getCurrentScreen() just returns the int, we should be okay. This
1201 // is just a hint for the order, and if it's wrong, we'll be okay.
1202 // TODO: instead, we should have that push the current screen into here.
1203 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001204 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001205 // once for the current screen
1206 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001207 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001208 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 mHandler.post(new Runnable() {
1210 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001211 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001213 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 }
1215 }
1216 });
1217 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001218 }
Joe Onorato36115782010-06-17 13:28:48 -04001219 // once for the other screens
1220 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001221 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001222 if (widget.screen != currentScreen) {
1223 mHandler.post(new Runnable() {
1224 public void run() {
1225 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1226 if (callbacks != null) {
1227 callbacks.bindAppWidget(widget);
1228 }
1229 }
1230 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001231 }
1232 }
Joe Onorato36115782010-06-17 13:28:48 -04001233 // Tell the workspace that we're done.
1234 mHandler.post(new Runnable() {
1235 public void run() {
1236 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1237 if (callbacks != null) {
1238 callbacks.finishBindingItems();
1239 }
1240 }
1241 });
1242 // If we're profiling, this is the last thing in the queue.
1243 mHandler.post(new Runnable() {
1244 public void run() {
1245 if (DEBUG_LOADERS) {
1246 Log.d(TAG, "bound workspace in "
1247 + (SystemClock.uptimeMillis()-t) + "ms");
1248 }
1249 }
1250 });
1251 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001252
Joe Onorato36115782010-06-17 13:28:48 -04001253 private void loadAndBindAllApps() {
1254 if (DEBUG_LOADERS) {
1255 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1256 }
1257 if (!mAllAppsLoaded) {
1258 loadAllAppsByBatch();
1259 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001260 return;
1261 }
Joe Onorato36115782010-06-17 13:28:48 -04001262 mAllAppsLoaded = true;
1263 } else {
1264 onlyBindAllApps();
1265 }
1266 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001267
Joe Onorato36115782010-06-17 13:28:48 -04001268 private void onlyBindAllApps() {
1269 final Callbacks oldCallbacks = mCallbacks.get();
1270 if (oldCallbacks == null) {
1271 // This launcher has exited and nobody bothered to tell us. Just bail.
1272 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1273 return;
1274 }
1275
1276 // shallow copy
1277 final ArrayList<ApplicationInfo> list
1278 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1279 mHandler.post(new Runnable() {
1280 public void run() {
1281 final long t = SystemClock.uptimeMillis();
1282 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1283 if (callbacks != null) {
1284 callbacks.bindAllApplications(list);
1285 }
1286 if (DEBUG_LOADERS) {
1287 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1288 + (SystemClock.uptimeMillis()-t) + "ms");
1289 }
1290 }
1291 });
1292
1293 }
1294
1295 private void loadAllAppsByBatch() {
1296 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1297
1298 // Don't use these two variables in any of the callback runnables.
1299 // Otherwise we hold a reference to them.
1300 final Callbacks oldCallbacks = mCallbacks.get();
1301 if (oldCallbacks == null) {
1302 // This launcher has exited and nobody bothered to tell us. Just bail.
1303 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1304 return;
1305 }
1306
1307 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1308 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1309
1310 final PackageManager packageManager = mContext.getPackageManager();
1311 List<ResolveInfo> apps = null;
1312
1313 int N = Integer.MAX_VALUE;
1314
1315 int startIndex;
1316 int i=0;
1317 int batchSize = -1;
1318 while (i < N && !mStopped) {
1319 if (i == 0) {
1320 mAllAppsList.clear();
1321 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1322 apps = packageManager.queryIntentActivities(mainIntent, 0);
1323 if (DEBUG_LOADERS) {
1324 Log.d(TAG, "queryIntentActivities took "
1325 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1326 }
1327 if (apps == null) {
1328 return;
1329 }
1330 N = apps.size();
1331 if (DEBUG_LOADERS) {
1332 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1333 }
1334 if (N == 0) {
1335 // There are no apps?!?
1336 return;
1337 }
1338 if (mBatchSize == 0) {
1339 batchSize = N;
1340 } else {
1341 batchSize = mBatchSize;
1342 }
1343
1344 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1345 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001346 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001347 if (DEBUG_LOADERS) {
1348 Log.d(TAG, "sort took "
1349 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1350 }
1351 }
1352
1353 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1354
1355 startIndex = i;
1356 for (int j=0; i<N && j<batchSize; j++) {
1357 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001358 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001359 mIconCache, mLabelCache, "6"));
Joe Onorato36115782010-06-17 13:28:48 -04001360 i++;
1361 }
1362
1363 final boolean first = i <= batchSize;
1364 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1365 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1366 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1367
Joe Onoratocc67f472010-06-08 10:54:30 -07001368 mHandler.post(new Runnable() {
1369 public void run() {
1370 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001371 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001372 if (first) {
1373 callbacks.bindAllApplications(added);
1374 } else {
1375 callbacks.bindAppsAdded(added);
1376 }
1377 if (DEBUG_LOADERS) {
1378 Log.d(TAG, "bound " + added.size() + " apps in "
1379 + (SystemClock.uptimeMillis() - t) + "ms");
1380 }
1381 } else {
1382 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001383 }
1384 }
1385 });
1386
Daniel Sandlerdca66122010-04-13 16:23:58 -04001387 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001388 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1389 + (SystemClock.uptimeMillis()-t2) + "ms");
1390 }
1391
1392 if (mAllAppsLoadDelay > 0 && i < N) {
1393 try {
1394 if (DEBUG_LOADERS) {
1395 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1396 }
1397 Thread.sleep(mAllAppsLoadDelay);
1398 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001399 }
1400 }
1401
Joe Onorato36115782010-06-17 13:28:48 -04001402 if (DEBUG_LOADERS) {
1403 Log.d(TAG, "cached all " + N + " apps in "
1404 + (SystemClock.uptimeMillis()-t) + "ms"
1405 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001406 }
1407 }
1408
1409 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001410 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1411 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1412 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1413 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1414 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001415 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001416 }
1417 }
1418
1419 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001420 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001421 }
1422
1423 private class PackageUpdatedTask implements Runnable {
1424 int mOp;
1425 String[] mPackages;
1426
1427 public static final int OP_NONE = 0;
1428 public static final int OP_ADD = 1;
1429 public static final int OP_UPDATE = 2;
1430 public static final int OP_REMOVE = 3; // uninstlled
1431 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1432
1433
1434 public PackageUpdatedTask(int op, String[] packages) {
1435 mOp = op;
1436 mPackages = packages;
1437 }
1438
1439 public void run() {
1440 final Context context = mApp;
1441
1442 final String[] packages = mPackages;
1443 final int N = packages.length;
1444 switch (mOp) {
1445 case OP_ADD:
1446 for (int i=0; i<N; i++) {
1447 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1448 mAllAppsList.addPackage(context, packages[i]);
1449 }
1450 break;
1451 case OP_UPDATE:
1452 for (int i=0; i<N; i++) {
1453 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1454 mAllAppsList.updatePackage(context, packages[i]);
1455 }
1456 break;
1457 case OP_REMOVE:
1458 case OP_UNAVAILABLE:
1459 for (int i=0; i<N; i++) {
1460 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1461 mAllAppsList.removePackage(packages[i]);
1462 }
1463 break;
1464 }
1465
1466 ArrayList<ApplicationInfo> added = null;
1467 ArrayList<ApplicationInfo> removed = null;
1468 ArrayList<ApplicationInfo> modified = null;
1469
1470 if (mAllAppsList.added.size() > 0) {
1471 added = mAllAppsList.added;
1472 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1473 }
1474 if (mAllAppsList.removed.size() > 0) {
1475 removed = mAllAppsList.removed;
1476 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1477 for (ApplicationInfo info: removed) {
1478 mIconCache.remove(info.intent.getComponent());
1479 }
1480 }
1481 if (mAllAppsList.modified.size() > 0) {
1482 modified = mAllAppsList.modified;
1483 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1484 }
1485
1486 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1487 if (callbacks == null) {
1488 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1489 return;
1490 }
1491
1492 if (added != null) {
1493 final ArrayList<ApplicationInfo> addedFinal = added;
1494 mHandler.post(new Runnable() {
1495 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001496 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1497 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001498 callbacks.bindAppsAdded(addedFinal);
1499 }
1500 }
1501 });
1502 }
1503 if (modified != null) {
1504 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1505 mHandler.post(new Runnable() {
1506 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001507 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1508 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001509 callbacks.bindAppsUpdated(modifiedFinal);
1510 }
1511 }
1512 });
1513 }
1514 if (removed != null) {
1515 final boolean permanent = mOp != OP_UNAVAILABLE;
1516 final ArrayList<ApplicationInfo> removedFinal = removed;
1517 mHandler.post(new Runnable() {
1518 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001519 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1520 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001521 callbacks.bindAppsRemoved(removedFinal, permanent);
1522 }
1523 }
1524 });
Joe Onoratobe386092009-11-17 17:32:16 -08001525 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001526
1527 mHandler.post(new Runnable() {
1528 @Override
1529 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001530 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1531 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001532 callbacks.bindPackagesUpdated();
1533 }
1534 }
1535 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001536 }
1537 }
1538
1539 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001540 * This is called from the code that adds shortcuts from the intent receiver. This
1541 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001542 */
Joe Onorato56d82912010-03-07 14:32:10 -05001543 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001544 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001546
Joe Onorato56d82912010-03-07 14:32:10 -05001547 /**
1548 * Make an ShortcutInfo object for a shortcut that is an application.
1549 *
1550 * If c is not null, then it will be used to fill in missing data like the title and icon.
1551 */
1552 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001553 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001554 Bitmap icon = null;
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001555 final ShortcutInfo info = new ShortcutInfo("7");
Joe Onorato56d82912010-03-07 14:32:10 -05001556
1557 ComponentName componentName = intent.getComponent();
1558 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001559 return null;
1560 }
1561
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001562 // TODO: See if the PackageManager knows about this case. If it doesn't
1563 // then return null & delete this.
1564
Joe Onorato56d82912010-03-07 14:32:10 -05001565 // the resource -- This may implicitly give us back the fallback icon,
1566 // but don't worry about that. All we're doing with usingFallbackIcon is
1567 // to avoid saving lots of copies of that in the database, and most apps
1568 // have icons anyway.
1569 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1570 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001571 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001572 }
Joe Onorato56d82912010-03-07 14:32:10 -05001573 // the db
1574 if (icon == null) {
1575 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001576 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001577 }
1578 }
1579 // the fallback icon
1580 if (icon == null) {
1581 icon = getFallbackIcon();
1582 info.usingFallbackIcon = true;
1583 }
1584 info.setIcon(icon);
1585
1586 // from the resource
1587 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001588 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1589 if (labelCache != null && labelCache.containsKey(key)) {
1590 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001591 } else {
1592 info.title = resolveInfo.activityInfo.loadLabel(manager);
1593 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001594 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001595 }
1596 }
Joe Onorato56d82912010-03-07 14:32:10 -05001597 }
1598 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001599 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001600 if (c != null) {
1601 info.title = c.getString(titleIndex);
1602 }
1603 }
1604 // fall back to the class name of the activity
1605 if (info.title == null) {
1606 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001607 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001608 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1609 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001613 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001615 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001616 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1617 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618
Joe Onorato56d82912010-03-07 14:32:10 -05001619 Bitmap icon = null;
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001620 final ShortcutInfo info = new ShortcutInfo("8");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001621 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001623 // TODO: If there's an explicit component and we can't install that, delete it.
1624
Joe Onorato56d82912010-03-07 14:32:10 -05001625 info.title = c.getString(titleIndex);
1626
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627 int iconType = c.getInt(iconTypeIndex);
1628 switch (iconType) {
1629 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1630 String packageName = c.getString(iconPackageIndex);
1631 String resourceName = c.getString(iconResourceIndex);
1632 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001633 info.customIcon = false;
1634 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001636 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001637 if (resources != null) {
1638 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001639 icon = Utilities.createIconBitmap(
1640 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001641 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001642 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001643 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 }
Joe Onorato56d82912010-03-07 14:32:10 -05001645 // the db
1646 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001647 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001648 }
1649 // the fallback icon
1650 if (icon == null) {
1651 icon = getFallbackIcon();
1652 info.usingFallbackIcon = true;
1653 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001654 break;
1655 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001656 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001657 if (icon == null) {
1658 icon = getFallbackIcon();
1659 info.customIcon = false;
1660 info.usingFallbackIcon = true;
1661 } else {
1662 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001663 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001664 break;
1665 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001666 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001667 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668 info.customIcon = false;
1669 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001671 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001672 return info;
1673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674
Michael Jurka931dc972011-08-05 15:08:15 -07001675 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001676 if (false) {
1677 Log.d(TAG, "getIconFromCursor app="
1678 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1679 }
1680 byte[] data = c.getBlob(iconIndex);
1681 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001682 return Utilities.createIconBitmap(
1683 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001684 } catch (Exception e) {
1685 return null;
1686 }
1687 }
1688
Winson Chung3d503fb2011-07-13 17:25:49 -07001689 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1690 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001691 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001692 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001693
1694 return info;
1695 }
1696
Winson Chunga9abd0e2010-10-27 17:18:37 -07001697 /**
Winson Chung55cef262010-10-28 14:14:18 -07001698 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1699 */
1700 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1701 ComponentName component) {
1702 List<AppWidgetProviderInfo> widgets =
1703 AppWidgetManager.getInstance(context).getInstalledProviders();
1704 for (AppWidgetProviderInfo info : widgets) {
1705 if (info.provider.equals(component)) {
1706 return info;
1707 }
1708 }
1709 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001710 }
1711
Winson Chung68846fd2010-10-29 11:00:27 -07001712 /**
1713 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1714 */
1715 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1716 final PackageManager packageManager = context.getPackageManager();
1717 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1718 new ArrayList<WidgetMimeTypeHandlerData>();
1719
1720 final Intent supportsIntent =
1721 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1722 supportsIntent.setType(mimeType);
1723
1724 // Create a set of widget configuration components that we can test against
1725 final List<AppWidgetProviderInfo> widgets =
1726 AppWidgetManager.getInstance(context).getInstalledProviders();
1727 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1728 new HashMap<ComponentName, AppWidgetProviderInfo>();
1729 for (AppWidgetProviderInfo info : widgets) {
1730 configurationComponentToWidget.put(info.configure, info);
1731 }
1732
1733 // Run through each of the intents that can handle this type of clip data, and cross
1734 // reference them with the components that are actual configuration components
1735 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1736 PackageManager.MATCH_DEFAULT_ONLY);
1737 for (ResolveInfo info : activities) {
1738 final ActivityInfo activityInfo = info.activityInfo;
1739 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1740 activityInfo.name);
1741 if (configurationComponentToWidget.containsKey(infoComponent)) {
1742 supportedConfigurationActivities.add(
1743 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1744 configurationComponentToWidget.get(infoComponent)));
1745 }
1746 }
1747 return supportedConfigurationActivities;
1748 }
1749
Winson Chunga9abd0e2010-10-27 17:18:37 -07001750 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001751 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1752 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1753 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1754
1755 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001756 boolean customIcon = false;
1757 ShortcutIconResource iconResource = null;
1758
1759 if (bitmap != null && bitmap instanceof Bitmap) {
1760 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001761 customIcon = true;
1762 } else {
1763 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1764 if (extra != null && extra instanceof ShortcutIconResource) {
1765 try {
1766 iconResource = (ShortcutIconResource) extra;
1767 final PackageManager packageManager = context.getPackageManager();
1768 Resources resources = packageManager.getResourcesForApplication(
1769 iconResource.packageName);
1770 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001771 icon = Utilities.createIconBitmap(
1772 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001773 } catch (Exception e) {
1774 Log.w(TAG, "Could not load shortcut icon: " + extra);
1775 }
1776 }
1777 }
1778
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001779 final ShortcutInfo info = new ShortcutInfo("9");
Joe Onorato56d82912010-03-07 14:32:10 -05001780
1781 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001782 if (fallbackIcon != null) {
1783 icon = fallbackIcon;
1784 } else {
1785 icon = getFallbackIcon();
1786 info.usingFallbackIcon = true;
1787 }
Joe Onorato56d82912010-03-07 14:32:10 -05001788 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001789 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001790
Joe Onorato0589f0f2010-02-08 13:44:00 -08001791 info.title = name;
1792 info.intent = intent;
1793 info.customIcon = customIcon;
1794 info.iconResource = iconResource;
1795
1796 return info;
1797 }
1798
Winson Chungaac01e12011-08-17 10:37:13 -07001799 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1800 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001801 // If apps can't be on SD, don't even bother.
1802 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001803 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001804 }
Joe Onorato56d82912010-03-07 14:32:10 -05001805 // If this icon doesn't have a custom icon, check to see
1806 // what's stored in the DB, and if it doesn't match what
1807 // we're going to show, store what we are going to show back
1808 // into the DB. We do this so when we're loading, if the
1809 // package manager can't find an icon (for example because
1810 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001811 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001812 cache.put(info, c.getBlob(iconIndex));
1813 return true;
1814 }
1815 return false;
1816 }
1817 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1818 boolean needSave = false;
1819 try {
1820 if (data != null) {
1821 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1822 Bitmap loaded = info.getIcon(mIconCache);
1823 needSave = !saved.sameAs(loaded);
1824 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001825 needSave = true;
1826 }
Winson Chungaac01e12011-08-17 10:37:13 -07001827 } catch (Exception e) {
1828 needSave = true;
1829 }
1830 if (needSave) {
1831 Log.d(TAG, "going to save icon bitmap for info=" + info);
1832 // This is slower than is ideal, but this only happens once
1833 // or when the app is updated with a new icon.
1834 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001835 }
1836 }
1837
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001839 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 * or make a new one.
1841 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001842 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 // See if a placeholder was created for us already
1844 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001845 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 // No placeholder -- create a new instance
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001847 folderInfo = new FolderInfo("10");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 folders.put(id, folderInfo);
1849 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001850 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 }
1852
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001854 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001855 = new Comparator<ApplicationInfo>() {
1856 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001857 int result = sCollator.compare(a.title.toString(), b.title.toString());
1858 if (result == 0) {
1859 result = a.componentName.compareTo(b.componentName);
1860 }
1861 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 };
Winson Chung78403fe2011-01-21 15:38:02 -08001864 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1865 = new Comparator<ApplicationInfo>() {
1866 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1867 if (a.firstInstallTime < b.firstInstallTime) return 1;
1868 if (a.firstInstallTime > b.firstInstallTime) return -1;
1869 return 0;
1870 }
1871 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001872 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1873 = new Comparator<AppWidgetProviderInfo>() {
1874 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1875 return sCollator.compare(a.label.toString(), b.label.toString());
1876 }
1877 };
Winson Chung5308f242011-08-18 12:12:41 -07001878 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1879 if (info.activityInfo != null) {
1880 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1881 } else {
1882 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1883 }
1884 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001885 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1886 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001887 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001888 ShortcutNameComparator(PackageManager pm) {
1889 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001890 mLabelCache = new HashMap<Object, CharSequence>();
1891 }
1892 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1893 mPackageManager = pm;
1894 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001895 }
1896 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001897 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001898 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1899 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1900 if (mLabelCache.containsKey(keyA)) {
1901 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001902 } else {
1903 labelA = a.loadLabel(mPackageManager).toString();
1904
Winson Chung5308f242011-08-18 12:12:41 -07001905 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001906 }
Winson Chung5308f242011-08-18 12:12:41 -07001907 if (mLabelCache.containsKey(keyB)) {
1908 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001909 } else {
1910 labelB = b.loadLabel(mPackageManager).toString();
1911
Winson Chung5308f242011-08-18 12:12:41 -07001912 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001913 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001914 return sCollator.compare(labelA, labelB);
1915 }
1916 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001917 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1918 private PackageManager mPackageManager;
1919 private HashMap<Object, String> mLabelCache;
1920 WidgetAndShortcutNameComparator(PackageManager pm) {
1921 mPackageManager = pm;
1922 mLabelCache = new HashMap<Object, String>();
1923 }
1924 public final int compare(Object a, Object b) {
1925 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001926 if (mLabelCache.containsKey(a)) {
1927 labelA = mLabelCache.get(a);
1928 } else {
1929 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001930 ((AppWidgetProviderInfo) a).label :
1931 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001932 mLabelCache.put(a, labelA);
1933 }
1934 if (mLabelCache.containsKey(b)) {
1935 labelB = mLabelCache.get(b);
1936 } else {
1937 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001938 ((AppWidgetProviderInfo) b).label :
1939 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001940 mLabelCache.put(b, labelB);
1941 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001942 return sCollator.compare(labelA, labelB);
1943 }
1944 };
Joe Onoratobe386092009-11-17 17:32:16 -08001945
1946 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001947 Log.d(TAG, "mCallbacks=" + mCallbacks);
1948 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1949 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1950 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1951 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001952 if (mLoaderTask != null) {
1953 mLoaderTask.dumpState();
1954 } else {
1955 Log.d(TAG, "mLoaderTask=null");
1956 }
Joe Onoratobe386092009-11-17 17:32:16 -08001957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958}