blob: 1f722994ce30fd3c9c29232a4f6c2b63a279a293 [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 Chungb1094bd2011-08-24 16:14:08 -0700212 Log.e(TAG, "item: " + ((item != null) ? item.toString() : "null"));
213 Log.e(TAG, "modelItem: " + ((modelItem != null) ? modelItem.toString() :
214 "null"));
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700215 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
216 "doesn't match original");
217 }
218
219 // Items are added/removed from the corresponding FolderInfo elsewhere, such
220 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
221 // that are on the desktop, as appropriate
Winson Chung3d503fb2011-07-13 17:25:49 -0700222 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
223 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700224 if (!sWorkspaceItems.contains(modelItem)) {
225 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700226 }
227 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700228 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700229 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700230 }
231 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700232 }
233
234 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800235 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800236 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700237 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
238 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800239 item.spanX = spanX;
240 item.spanY = spanY;
241 item.cellX = cellX;
242 item.cellY = cellY;
243
244 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
245 final ContentValues values = new ContentValues();
246 final ContentResolver cr = context.getContentResolver();
247
248 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
249 values.put(LauncherSettings.Favorites.SPANX, spanX);
250 values.put(LauncherSettings.Favorites.SPANY, spanY);
251 values.put(LauncherSettings.Favorites.CELLX, cellX);
252 values.put(LauncherSettings.Favorites.CELLY, cellY);
253
254 sWorker.post(new Runnable() {
255 public void run() {
256 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700257 ItemInfo modelItem = sItemsIdMap.get(item.id);
258 if (item != modelItem) {
259 // the modelItem needs to match up perfectly with item if our model is to be
260 // consistent with the database-- for now, just require modelItem == item
Winson Chungb1094bd2011-08-24 16:14:08 -0700261 Log.e(TAG, "item: " + ((item != null) ? item.toString() : "null"));
262 Log.e(TAG, "modelItem: " + ((modelItem != null) ? modelItem.toString() :
263 "null"));
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700264 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
265 "doesn't match original");
266 }
Adam Cohend4844c32011-02-18 19:25:06 -0800267 }
268 });
269 }
270
271 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 * Returns true if the shortcuts already exists in the database.
273 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 static boolean shortcutExists(Context context, String title, Intent intent) {
276 final ContentResolver cr = context.getContentResolver();
277 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
278 new String[] { "title", "intent" }, "title=? and intent=?",
279 new String[] { title, intent.toUri(0) }, null);
280 boolean result = false;
281 try {
282 result = c.moveToFirst();
283 } finally {
284 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700287 }
288
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700290 * Returns an ItemInfo array containing all the items in the LauncherModel.
291 * The ItemInfo.id is not set through this function.
292 */
293 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
294 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
295 final ContentResolver cr = context.getContentResolver();
296 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
297 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
298 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
299 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
300
301 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
302 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
303 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
304 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
305 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
306 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
307 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
308
309 try {
310 while (c.moveToNext()) {
311 ItemInfo item = new ItemInfo();
312 item.cellX = c.getInt(cellXIndex);
313 item.cellY = c.getInt(cellYIndex);
314 item.spanX = c.getInt(spanXIndex);
315 item.spanY = c.getInt(spanYIndex);
316 item.container = c.getInt(containerIndex);
317 item.itemType = c.getInt(itemTypeIndex);
318 item.screen = c.getInt(screenIndex);
319
320 items.add(item);
321 }
322 } catch (Exception e) {
323 items.clear();
324 } finally {
325 c.close();
326 }
327
328 return items;
329 }
330
331 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400332 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
333 */
334 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
335 final ContentResolver cr = context.getContentResolver();
336 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
337 "_id=? and (itemType=? or itemType=?)",
338 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700339 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700340
Joe Onorato9c1289c2009-08-17 11:03:03 -0400341 try {
342 if (c.moveToFirst()) {
343 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
344 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
345 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
346 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
347 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
348 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349
Joe Onorato9c1289c2009-08-17 11:03:03 -0400350 FolderInfo folderInfo = null;
351 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700352 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
353 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400354 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700355 }
356
Joe Onorato9c1289c2009-08-17 11:03:03 -0400357 folderInfo.title = c.getString(titleIndex);
358 folderInfo.id = id;
359 folderInfo.container = c.getInt(containerIndex);
360 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700361 folderInfo.cellX = c.getInt(cellXIndex);
362 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400363
364 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700365 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 } finally {
367 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700368 }
369
370 return null;
371 }
372
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 /**
374 * Add an item to the database in a specified container. Sets the container, screen, cellX and
375 * cellY fields of the item. Also assigns an ID to the item.
376 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700377 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
378 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 item.cellX = cellX;
381 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700382 // We store hotseat items in canonical form which is this orientation invariant position
383 // in the hotseat
384 if (context instanceof Launcher && screen < 0 &&
385 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
386 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
387 } else {
388 item.screen = screen;
389 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390
391 final ContentValues values = new ContentValues();
392 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400393 item.onAddToDatabase(values);
394
Michael Jurka7578ec62011-08-03 14:11:54 -0700395 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700396 item.id = app.getLauncherProvider().generateNewId();
397 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700398 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700399
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700400 sWorker.post(new Runnable() {
401 public void run() {
402 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
403 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400404
Winson Chungb1094bd2011-08-24 16:14:08 -0700405 if (sItemsIdMap.containsKey(item.id)) {
406 // we should not be adding new items in the db with the same id
407 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
408 "addItemToDatabase already exists." + item.toString());
409 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700410 sItemsIdMap.put(item.id, item);
411 switch (item.itemType) {
412 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
413 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700414 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700415 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
416 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700417 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
418 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700419 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700420 }
421 break;
422 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
423 sAppWidgets.add((LauncherAppWidgetInfo) item);
424 break;
425 }
426 }
427 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700428 }
429
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700431 * Creates a new unique child id, for a given cell span across all layouts.
432 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700433 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700434 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
435 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700436 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700437 }
438
Adam Cohend22015c2010-07-26 22:02:18 -0700439 static int getCellCountX() {
440 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700441 }
442
Adam Cohend22015c2010-07-26 22:02:18 -0700443 static int getCellCountY() {
444 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700445 }
446
447 /**
448 * Updates the model orientation helper to take into account the current layout dimensions
449 * when performing local/canonical coordinate transformations.
450 */
451 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700452 mCellCountX = shortAxisCellCount;
453 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700454 }
455
456 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 * Update an item to the database in a specified container.
458 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700459 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400460 final ContentValues values = new ContentValues();
461 final ContentResolver cr = context.getContentResolver();
462
463 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700464 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700465
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700466 sWorker.post(new Runnable() {
467 public void run() {
468 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
469 values, null, null);
470 final ItemInfo modelItem = sItemsIdMap.get(item.id);
471 if (item != modelItem) {
472 // the modelItem needs to match up perfectly with item if our model is to be
473 // consistent with the database-- for now, just require modelItem == item
Winson Chungb1094bd2011-08-24 16:14:08 -0700474 Log.e(TAG, "item: " + ((item != null) ? item.toString() : "null"));
475 Log.e(TAG, "modelItem: " + ((modelItem != null) ? modelItem.toString() :
476 "null"));
477 throw new RuntimeException("Error: ItemInfo passed to updateItemInDatabase " +
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700478 "doesn't match original");
479 }
480 }
481 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400482 }
483
484 /**
485 * Removes the specified item from the database
486 * @param context
487 * @param item
488 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700489 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400490 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700491 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700492 sWorker.post(new Runnable() {
493 public void run() {
494 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700495 switch (item.itemType) {
496 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
497 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700498 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700499 break;
500 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
501 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700502 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700503 break;
504 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
505 sAppWidgets.remove((LauncherAppWidgetInfo) item);
506 break;
507 }
508 sItemsIdMap.remove(item.id);
Winson Chungb1094bd2011-08-24 16:14:08 -0700509 sDbIconCache.remove(item);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700510 }
511 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 }
513
514 /**
515 * Remove the contents of the specified folder from the database
516 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700517 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400518 final ContentResolver cr = context.getContentResolver();
519
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700520 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700521 public void run() {
522 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700523 sItemsIdMap.remove(info.id);
524 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700525 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700526
Adam Cohen9932a9b2011-08-02 22:14:07 -0700527 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Adam Cohenafb01ee2011-06-23 15:38:03 -0700528 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700529 for (ItemInfo childInfo : info.contents) {
530 sItemsIdMap.remove(childInfo.id);
531 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700532 }
533 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400534 }
535
536 /**
537 * Set this as the current Launcher activity object for the loader.
538 */
539 public void initialize(Callbacks callbacks) {
540 synchronized (mLock) {
541 mCallbacks = new WeakReference<Callbacks>(callbacks);
542 }
543 }
544
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700545 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
547 * ACTION_PACKAGE_CHANGED.
548 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100549 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400550 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400551 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700552
Joe Onorato36115782010-06-17 13:28:48 -0400553 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400554
Joe Onorato36115782010-06-17 13:28:48 -0400555 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
556 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
557 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
558 final String packageName = intent.getData().getSchemeSpecificPart();
559 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560
Joe Onorato36115782010-06-17 13:28:48 -0400561 int op = PackageUpdatedTask.OP_NONE;
562
563 if (packageName == null || packageName.length() == 0) {
564 // they sent us a bad intent
565 return;
566 }
567
568 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
569 op = PackageUpdatedTask.OP_UPDATE;
570 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
571 if (!replacing) {
572 op = PackageUpdatedTask.OP_REMOVE;
573 }
574 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
575 // later, we will update the package at this time
576 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
577 if (!replacing) {
578 op = PackageUpdatedTask.OP_ADD;
579 } else {
580 op = PackageUpdatedTask.OP_UPDATE;
581 }
582 }
583
584 if (op != PackageUpdatedTask.OP_NONE) {
585 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
586 }
587
588 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400589 // First, schedule to add these apps back in.
590 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
591 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
592 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700593 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400594 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
595 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
596 enqueuePackageUpdated(new PackageUpdatedTask(
597 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700598 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
599 // If we have changed locale we need to clear out the labels in all apps.
600 // Do this here because if the launcher activity is running it will be restarted.
601 // If it's not running startLoaderFromBackground will merely tell it that it needs
602 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
603 // next time.
604 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700605 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700606 startLoaderFromBackground();
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700607 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
608 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100609 Callbacks callbacks = mCallbacks.get();
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700610 if (callbacks != null) {
611 callbacks.bindSearchablesChanged();
612 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700613 }
614 }
615
616 /**
617 * When the launcher is in the background, it's possible for it to miss paired
618 * configuration changes. So whenever we trigger the loader from the background
619 * tell the launcher that it needs to re-run the loader when it comes back instead
620 * of doing it now.
621 */
622 public void startLoaderFromBackground() {
623 boolean runLoader = false;
624 if (mCallbacks != null) {
625 Callbacks callbacks = mCallbacks.get();
626 if (callbacks != null) {
627 // Only actually run the loader if they're not paused.
628 if (!callbacks.setLoadOnResume()) {
629 runLoader = true;
630 }
631 }
632 }
633 if (runLoader) {
634 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700635 }
Joe Onorato36115782010-06-17 13:28:48 -0400636 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400637
Joe Onorato36115782010-06-17 13:28:48 -0400638 public void startLoader(Context context, boolean isLaunching) {
639 synchronized (mLock) {
640 if (DEBUG_LOADERS) {
641 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
642 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643
Joe Onorato36115782010-06-17 13:28:48 -0400644 // Don't bother to start the thread if we know it's not going to do anything
645 if (mCallbacks != null && mCallbacks.get() != null) {
646 // If there is already one running, tell it to stop.
647 LoaderTask oldTask = mLoaderTask;
648 if (oldTask != null) {
649 if (oldTask.isLaunching()) {
650 // don't downgrade isLaunching if we're already running
651 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500652 }
Joe Onorato36115782010-06-17 13:28:48 -0400653 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500654 }
Joe Onorato36115782010-06-17 13:28:48 -0400655 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700656 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 }
659 }
660
Joe Onorato36115782010-06-17 13:28:48 -0400661 public void stopLoader() {
662 synchronized (mLock) {
663 if (mLoaderTask != null) {
664 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665 }
666 }
Joe Onorato36115782010-06-17 13:28:48 -0400667 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668
Michael Jurkac57b7a82011-08-09 22:02:20 -0700669 public boolean isAllAppsLoaded() {
670 return mAllAppsLoaded;
671 }
672
Joe Onorato36115782010-06-17 13:28:48 -0400673 /**
674 * Runnable for the thread that loads the contents of the launcher:
675 * - workspace icons
676 * - widgets
677 * - all apps icons
678 */
679 private class LoaderTask implements Runnable {
680 private Context mContext;
681 private Thread mWaitThread;
682 private boolean mIsLaunching;
683 private boolean mStopped;
684 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700685 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400686
687 LoaderTask(Context context, boolean isLaunching) {
688 mContext = context;
689 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700690 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 }
692
Joe Onorato36115782010-06-17 13:28:48 -0400693 boolean isLaunching() {
694 return mIsLaunching;
695 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696
Joe Onorato36115782010-06-17 13:28:48 -0400697 private void loadAndBindWorkspace() {
698 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400699 if (DEBUG_LOADERS) {
700 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700702
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700703 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400704 loadWorkspace();
705 if (mStopped) {
706 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 }
Joe Onorato36115782010-06-17 13:28:48 -0400708 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 }
710
Joe Onorato36115782010-06-17 13:28:48 -0400711 // Bind the workspace
712 bindWorkspace();
713 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400714
Joe Onorato36115782010-06-17 13:28:48 -0400715 private void waitForIdle() {
716 // Wait until the either we're stopped or the other threads are done.
717 // This way we don't start loading all apps until the workspace has settled
718 // down.
719 synchronized (LoaderTask.this) {
720 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700721
Joe Onorato36115782010-06-17 13:28:48 -0400722 mHandler.postIdle(new Runnable() {
723 public void run() {
724 synchronized (LoaderTask.this) {
725 mLoadAndBindStepFinished = true;
726 if (DEBUG_LOADERS) {
727 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400728 }
Joe Onorato36115782010-06-17 13:28:48 -0400729 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400730 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400731 }
Joe Onorato36115782010-06-17 13:28:48 -0400732 });
733
734 while (!mStopped && !mLoadAndBindStepFinished) {
735 try {
736 this.wait();
737 } catch (InterruptedException ex) {
738 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400739 }
740 }
Joe Onorato36115782010-06-17 13:28:48 -0400741 if (DEBUG_LOADERS) {
742 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700743 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400744 + "ms for previous step to finish binding");
745 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400746 }
Joe Onorato36115782010-06-17 13:28:48 -0400747 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400748
Joe Onorato36115782010-06-17 13:28:48 -0400749 public void run() {
750 // Optimize for end-user experience: if the Launcher is up and // running with the
751 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
752 // workspace first (default).
753 final Callbacks cbk = mCallbacks.get();
754 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400755
Joe Onorato36115782010-06-17 13:28:48 -0400756 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400757 // Elevate priority when Home launches for the first time to avoid
758 // starving at boot time. Staring at a blank home is not cool.
759 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700760 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
761 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400762 android.os.Process.setThreadPriority(mIsLaunching
763 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
764 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400765 if (loadWorkspaceFirst) {
766 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
767 loadAndBindWorkspace();
768 } else {
769 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700770 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400771 }
772
Joe Onorato36115782010-06-17 13:28:48 -0400773 if (mStopped) {
774 break keep_running;
775 }
776
777 // Whew! Hard work done. Slow us down, and wait until the UI thread has
778 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400779 synchronized (mLock) {
780 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700781 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400782 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
783 }
784 }
Joe Onorato36115782010-06-17 13:28:48 -0400785 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400786
787 // second step
788 if (loadWorkspaceFirst) {
789 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700790 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400791 } else {
792 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
793 loadAndBindWorkspace();
794 }
Joe Onorato36115782010-06-17 13:28:48 -0400795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796
Winson Chungaac01e12011-08-17 10:37:13 -0700797
798 // Update the saved icons if necessary
799 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700800 for (Object key : sDbIconCache.keySet()) {
801 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700802 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700803 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700804
Joe Onorato36115782010-06-17 13:28:48 -0400805 // Clear out this reference, otherwise we end up holding it until all of the
806 // callback runnables are done.
807 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808
Joe Onorato36115782010-06-17 13:28:48 -0400809 synchronized (mLock) {
810 // If we are still the last one to be scheduled, remove ourselves.
811 if (mLoaderTask == this) {
812 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 }
Joe Onorato36115782010-06-17 13:28:48 -0400814 }
Joe Onorato36115782010-06-17 13:28:48 -0400815 }
816
817 public void stopLocked() {
818 synchronized (LoaderTask.this) {
819 mStopped = true;
820 this.notify();
821 }
822 }
823
824 /**
825 * Gets the callbacks object. If we've been stopped, or if the launcher object
826 * has somehow been garbage collected, return null instead. Pass in the Callbacks
827 * object that was around when the deferred message was scheduled, and if there's
828 * a new Callbacks object around then also return null. This will save us from
829 * calling onto it with data that will be ignored.
830 */
831 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
832 synchronized (mLock) {
833 if (mStopped) {
834 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400835 }
Joe Onorato36115782010-06-17 13:28:48 -0400836
837 if (mCallbacks == null) {
838 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400839 }
Joe Onorato36115782010-06-17 13:28:48 -0400840
841 final Callbacks callbacks = mCallbacks.get();
842 if (callbacks != oldCallbacks) {
843 return null;
844 }
845 if (callbacks == null) {
846 Log.w(TAG, "no mCallbacks");
847 return null;
848 }
849
850 return callbacks;
851 }
852 }
853
854 // check & update map of what's occupied; used to discard overlapping/invalid items
855 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700856 int containerIndex = item.screen;
857 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
858 // We use the last index to refer to the hotseat
859 containerIndex = Launcher.SCREEN_COUNT;
860 // Return early if we detect that an item is under the hotseat button
861 if (Hotseat.isAllAppsButtonRank(item.screen)) {
862 return false;
863 }
864 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
865 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400866 return true;
867 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700868
Joe Onorato36115782010-06-17 13:28:48 -0400869 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
870 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700871 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400872 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700873 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400874 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700875 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700876 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400877 return false;
878 }
879 }
880 }
881 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 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400884 }
885 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700886
Joe Onorato36115782010-06-17 13:28:48 -0400887 return true;
888 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889
Joe Onorato36115782010-06-17 13:28:48 -0400890 private void loadWorkspace() {
891 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892
Joe Onorato36115782010-06-17 13:28:48 -0400893 final Context context = mContext;
894 final ContentResolver contentResolver = context.getContentResolver();
895 final PackageManager manager = context.getPackageManager();
896 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
897 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400898
Adam Cohen4eac29a2011-07-11 17:53:37 -0700899 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700900 sAppWidgets.clear();
901 sFolders.clear();
902 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700903 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800904
Joe Onorato36115782010-06-17 13:28:48 -0400905 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400906
Joe Onorato36115782010-06-17 13:28:48 -0400907 final Cursor c = contentResolver.query(
908 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400909
Winson Chungf30ad5f2011-08-08 10:55:42 -0700910 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700911 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700912 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913
Joe Onorato36115782010-06-17 13:28:48 -0400914 try {
915 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
916 final int intentIndex = c.getColumnIndexOrThrow
917 (LauncherSettings.Favorites.INTENT);
918 final int titleIndex = c.getColumnIndexOrThrow
919 (LauncherSettings.Favorites.TITLE);
920 final int iconTypeIndex = c.getColumnIndexOrThrow(
921 LauncherSettings.Favorites.ICON_TYPE);
922 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
923 final int iconPackageIndex = c.getColumnIndexOrThrow(
924 LauncherSettings.Favorites.ICON_PACKAGE);
925 final int iconResourceIndex = c.getColumnIndexOrThrow(
926 LauncherSettings.Favorites.ICON_RESOURCE);
927 final int containerIndex = c.getColumnIndexOrThrow(
928 LauncherSettings.Favorites.CONTAINER);
929 final int itemTypeIndex = c.getColumnIndexOrThrow(
930 LauncherSettings.Favorites.ITEM_TYPE);
931 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
932 LauncherSettings.Favorites.APPWIDGET_ID);
933 final int screenIndex = c.getColumnIndexOrThrow(
934 LauncherSettings.Favorites.SCREEN);
935 final int cellXIndex = c.getColumnIndexOrThrow
936 (LauncherSettings.Favorites.CELLX);
937 final int cellYIndex = c.getColumnIndexOrThrow
938 (LauncherSettings.Favorites.CELLY);
939 final int spanXIndex = c.getColumnIndexOrThrow
940 (LauncherSettings.Favorites.SPANX);
941 final int spanYIndex = c.getColumnIndexOrThrow(
942 LauncherSettings.Favorites.SPANY);
943 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
944 final int displayModeIndex = c.getColumnIndexOrThrow(
945 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946
Joe Onorato36115782010-06-17 13:28:48 -0400947 ShortcutInfo info;
948 String intentDescription;
949 LauncherAppWidgetInfo appWidgetInfo;
950 int container;
951 long id;
952 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953
Joe Onorato36115782010-06-17 13:28:48 -0400954 while (!mStopped && c.moveToNext()) {
955 try {
956 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957
Joe Onorato36115782010-06-17 13:28:48 -0400958 switch (itemType) {
959 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
960 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
961 intentDescription = c.getString(intentIndex);
962 try {
963 intent = Intent.parseUri(intentDescription, 0);
964 } catch (URISyntaxException e) {
965 continue;
966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967
Joe Onorato36115782010-06-17 13:28:48 -0400968 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
969 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700970 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400971 } else {
972 info = getShortcutInfo(c, context, iconTypeIndex,
973 iconPackageIndex, iconResourceIndex, iconIndex,
974 titleIndex);
975 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976
Joe Onorato36115782010-06-17 13:28:48 -0400977 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400978 info.intent = intent;
979 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400981 info.container = container;
982 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700983 info.cellX = c.getInt(cellXIndex);
984 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985
Daniel Sandler8802e962010-05-26 16:28:16 -0400986 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400987 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400988 break;
989 }
990
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400992 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700994 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400995 break;
996 default:
997 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700998 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700999 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001000 folderInfo.add(info);
1001 break;
1002 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001003 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001004
1005 // now that we've loaded everthing re-save it with the
1006 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001007 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001008 } else {
1009 // Failed to load the shortcut, probably because the
1010 // activity manager couldn't resolve it (maybe the app
1011 // was uninstalled), or the db row was somehow screwed up.
1012 // Delete it.
1013 id = c.getLong(idIndex);
1014 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1015 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1016 id, false), null, null);
1017 }
1018 break;
1019
Adam Cohendf2cc412011-04-27 16:56:57 -07001020 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001021 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001022 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001023
Winson Chungaafa03c2010-06-11 17:34:16 -07001024 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001025 folderInfo.id = id;
1026 container = c.getInt(containerIndex);
1027 folderInfo.container = container;
1028 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001029 folderInfo.cellX = c.getInt(cellXIndex);
1030 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001031
1032 // check & update map of what's occupied
1033 if (!checkItemPlacement(occupied, folderInfo)) {
1034 break;
1035 }
Joe Onorato36115782010-06-17 13:28:48 -04001036 switch (container) {
1037 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001039 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001040 break;
1041 }
1042
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001043 sItemsIdMap.put(folderInfo.id, folderInfo);
1044 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001045 break;
1046
Joe Onorato36115782010-06-17 13:28:48 -04001047 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1048 // Read all Launcher-specific widget details
1049 int appWidgetId = c.getInt(appWidgetIdIndex);
1050 id = c.getLong(idIndex);
1051
1052 final AppWidgetProviderInfo provider =
1053 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 if (!isSafeMode && (provider == null || provider.provider == null ||
1056 provider.provider.getPackageName() == null)) {
1057 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1058 + id + " appWidgetId=" + appWidgetId);
1059 itemsToRemove.add(id);
1060 } else {
1061 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
1062 appWidgetInfo.id = id;
1063 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001064 appWidgetInfo.cellX = c.getInt(cellXIndex);
1065 appWidgetInfo.cellY = c.getInt(cellYIndex);
1066 appWidgetInfo.spanX = c.getInt(spanXIndex);
1067 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001068
1069 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001070 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1071 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001072 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001073 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001074 continue;
1075 }
1076 appWidgetInfo.container = c.getInt(containerIndex);
1077
1078 // check & update map of what's occupied
1079 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1080 break;
1081 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001082 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1083 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001084 }
1085 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001086 }
Joe Onorato36115782010-06-17 13:28:48 -04001087 } catch (Exception e) {
1088 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001089 }
1090 }
Joe Onorato36115782010-06-17 13:28:48 -04001091 } finally {
1092 c.close();
1093 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001094
Joe Onorato36115782010-06-17 13:28:48 -04001095 if (itemsToRemove.size() > 0) {
1096 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1097 LauncherSettings.Favorites.CONTENT_URI);
1098 // Remove dead items
1099 for (long id : itemsToRemove) {
1100 if (DEBUG_LOADERS) {
1101 Log.d(TAG, "Removed id = " + id);
1102 }
1103 // Don't notify content observers
1104 try {
1105 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1106 null, null);
1107 } catch (RemoteException e) {
1108 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001109 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001111 }
1112
Joe Onorato36115782010-06-17 13:28:48 -04001113 if (DEBUG_LOADERS) {
1114 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1115 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001116 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001117 String line = "";
1118 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1119 if (s > 0) {
1120 line += " | ";
1121 }
Adam Cohend22015c2010-07-26 22:02:18 -07001122 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001123 line += ((occupied[s][x][y] != null) ? "#" : ".");
1124 }
1125 }
1126 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 }
Joe Onorato36115782010-06-17 13:28:48 -04001128 }
1129 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130
Joe Onorato36115782010-06-17 13:28:48 -04001131 /**
1132 * Read everything out of our database.
1133 */
1134 private void bindWorkspace() {
1135 final long t = SystemClock.uptimeMillis();
1136
1137 // Don't use these two variables in any of the callback runnables.
1138 // Otherwise we hold a reference to them.
1139 final Callbacks oldCallbacks = mCallbacks.get();
1140 if (oldCallbacks == null) {
1141 // This launcher has exited and nobody bothered to tell us. Just bail.
1142 Log.w(TAG, "LoaderTask running with no launcher");
1143 return;
1144 }
1145
1146 int N;
1147 // Tell the workspace that we're about to start firing items at it
1148 mHandler.post(new Runnable() {
1149 public void run() {
1150 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1151 if (callbacks != null) {
1152 callbacks.startBinding();
1153 }
1154 }
1155 });
1156 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001157 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001158 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1159 final int start = i;
1160 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 mHandler.post(new Runnable() {
1162 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001163 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001164 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001165 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 }
1167 }
1168 });
Joe Onorato36115782010-06-17 13:28:48 -04001169 }
1170 mHandler.post(new Runnable() {
1171 public void run() {
1172 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1173 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001174 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001175 }
1176 }
1177 });
1178 // Wait until the queue goes empty.
1179 mHandler.post(new Runnable() {
1180 public void run() {
1181 if (DEBUG_LOADERS) {
1182 Log.d(TAG, "Going to start binding widgets soon.");
1183 }
1184 }
1185 });
1186 // Bind the widgets, one at a time.
1187 // WARNING: this is calling into the workspace from the background thread,
1188 // but since getCurrentScreen() just returns the int, we should be okay. This
1189 // is just a hint for the order, and if it's wrong, we'll be okay.
1190 // TODO: instead, we should have that push the current screen into here.
1191 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001192 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001193 // once for the current screen
1194 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001195 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001196 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 mHandler.post(new Runnable() {
1198 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001199 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001201 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 }
1203 }
1204 });
1205 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 }
Joe Onorato36115782010-06-17 13:28:48 -04001207 // once for the other screens
1208 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001209 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001210 if (widget.screen != currentScreen) {
1211 mHandler.post(new Runnable() {
1212 public void run() {
1213 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1214 if (callbacks != null) {
1215 callbacks.bindAppWidget(widget);
1216 }
1217 }
1218 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001219 }
1220 }
Joe Onorato36115782010-06-17 13:28:48 -04001221 // Tell the workspace that we're done.
1222 mHandler.post(new Runnable() {
1223 public void run() {
1224 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1225 if (callbacks != null) {
1226 callbacks.finishBindingItems();
1227 }
1228 }
1229 });
1230 // If we're profiling, this is the last thing in the queue.
1231 mHandler.post(new Runnable() {
1232 public void run() {
1233 if (DEBUG_LOADERS) {
1234 Log.d(TAG, "bound workspace in "
1235 + (SystemClock.uptimeMillis()-t) + "ms");
1236 }
1237 }
1238 });
1239 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001240
Joe Onorato36115782010-06-17 13:28:48 -04001241 private void loadAndBindAllApps() {
1242 if (DEBUG_LOADERS) {
1243 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1244 }
1245 if (!mAllAppsLoaded) {
1246 loadAllAppsByBatch();
1247 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001248 return;
1249 }
Joe Onorato36115782010-06-17 13:28:48 -04001250 mAllAppsLoaded = true;
1251 } else {
1252 onlyBindAllApps();
1253 }
1254 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001255
Joe Onorato36115782010-06-17 13:28:48 -04001256 private void onlyBindAllApps() {
1257 final Callbacks oldCallbacks = mCallbacks.get();
1258 if (oldCallbacks == null) {
1259 // This launcher has exited and nobody bothered to tell us. Just bail.
1260 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1261 return;
1262 }
1263
1264 // shallow copy
1265 final ArrayList<ApplicationInfo> list
1266 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1267 mHandler.post(new Runnable() {
1268 public void run() {
1269 final long t = SystemClock.uptimeMillis();
1270 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1271 if (callbacks != null) {
1272 callbacks.bindAllApplications(list);
1273 }
1274 if (DEBUG_LOADERS) {
1275 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1276 + (SystemClock.uptimeMillis()-t) + "ms");
1277 }
1278 }
1279 });
1280
1281 }
1282
1283 private void loadAllAppsByBatch() {
1284 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1285
1286 // Don't use these two variables in any of the callback runnables.
1287 // Otherwise we hold a reference to them.
1288 final Callbacks oldCallbacks = mCallbacks.get();
1289 if (oldCallbacks == null) {
1290 // This launcher has exited and nobody bothered to tell us. Just bail.
1291 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1292 return;
1293 }
1294
1295 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1296 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1297
1298 final PackageManager packageManager = mContext.getPackageManager();
1299 List<ResolveInfo> apps = null;
1300
1301 int N = Integer.MAX_VALUE;
1302
1303 int startIndex;
1304 int i=0;
1305 int batchSize = -1;
1306 while (i < N && !mStopped) {
1307 if (i == 0) {
1308 mAllAppsList.clear();
1309 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1310 apps = packageManager.queryIntentActivities(mainIntent, 0);
1311 if (DEBUG_LOADERS) {
1312 Log.d(TAG, "queryIntentActivities took "
1313 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1314 }
1315 if (apps == null) {
1316 return;
1317 }
1318 N = apps.size();
1319 if (DEBUG_LOADERS) {
1320 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1321 }
1322 if (N == 0) {
1323 // There are no apps?!?
1324 return;
1325 }
1326 if (mBatchSize == 0) {
1327 batchSize = N;
1328 } else {
1329 batchSize = mBatchSize;
1330 }
1331
1332 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1333 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001334 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001335 if (DEBUG_LOADERS) {
1336 Log.d(TAG, "sort took "
1337 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1338 }
1339 }
1340
1341 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1342
1343 startIndex = i;
1344 for (int j=0; i<N && j<batchSize; j++) {
1345 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001346 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1347 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001348 i++;
1349 }
1350
1351 final boolean first = i <= batchSize;
1352 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1353 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1354 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1355
Joe Onoratocc67f472010-06-08 10:54:30 -07001356 mHandler.post(new Runnable() {
1357 public void run() {
1358 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001359 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001360 if (first) {
1361 callbacks.bindAllApplications(added);
1362 } else {
1363 callbacks.bindAppsAdded(added);
1364 }
1365 if (DEBUG_LOADERS) {
1366 Log.d(TAG, "bound " + added.size() + " apps in "
1367 + (SystemClock.uptimeMillis() - t) + "ms");
1368 }
1369 } else {
1370 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001371 }
1372 }
1373 });
1374
Daniel Sandlerdca66122010-04-13 16:23:58 -04001375 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001376 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1377 + (SystemClock.uptimeMillis()-t2) + "ms");
1378 }
1379
1380 if (mAllAppsLoadDelay > 0 && i < N) {
1381 try {
1382 if (DEBUG_LOADERS) {
1383 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1384 }
1385 Thread.sleep(mAllAppsLoadDelay);
1386 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 }
1388 }
1389
Joe Onorato36115782010-06-17 13:28:48 -04001390 if (DEBUG_LOADERS) {
1391 Log.d(TAG, "cached all " + N + " apps in "
1392 + (SystemClock.uptimeMillis()-t) + "ms"
1393 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001394 }
1395 }
1396
1397 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001398 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1399 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1400 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1401 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1402 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001403 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001404 }
1405 }
1406
1407 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001408 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001409 }
1410
1411 private class PackageUpdatedTask implements Runnable {
1412 int mOp;
1413 String[] mPackages;
1414
1415 public static final int OP_NONE = 0;
1416 public static final int OP_ADD = 1;
1417 public static final int OP_UPDATE = 2;
1418 public static final int OP_REMOVE = 3; // uninstlled
1419 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1420
1421
1422 public PackageUpdatedTask(int op, String[] packages) {
1423 mOp = op;
1424 mPackages = packages;
1425 }
1426
1427 public void run() {
1428 final Context context = mApp;
1429
1430 final String[] packages = mPackages;
1431 final int N = packages.length;
1432 switch (mOp) {
1433 case OP_ADD:
1434 for (int i=0; i<N; i++) {
1435 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1436 mAllAppsList.addPackage(context, packages[i]);
1437 }
1438 break;
1439 case OP_UPDATE:
1440 for (int i=0; i<N; i++) {
1441 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1442 mAllAppsList.updatePackage(context, packages[i]);
1443 }
1444 break;
1445 case OP_REMOVE:
1446 case OP_UNAVAILABLE:
1447 for (int i=0; i<N; i++) {
1448 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1449 mAllAppsList.removePackage(packages[i]);
1450 }
1451 break;
1452 }
1453
1454 ArrayList<ApplicationInfo> added = null;
1455 ArrayList<ApplicationInfo> removed = null;
1456 ArrayList<ApplicationInfo> modified = null;
1457
1458 if (mAllAppsList.added.size() > 0) {
1459 added = mAllAppsList.added;
1460 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1461 }
1462 if (mAllAppsList.removed.size() > 0) {
1463 removed = mAllAppsList.removed;
1464 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1465 for (ApplicationInfo info: removed) {
1466 mIconCache.remove(info.intent.getComponent());
1467 }
1468 }
1469 if (mAllAppsList.modified.size() > 0) {
1470 modified = mAllAppsList.modified;
1471 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1472 }
1473
1474 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1475 if (callbacks == null) {
1476 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1477 return;
1478 }
1479
1480 if (added != null) {
1481 final ArrayList<ApplicationInfo> addedFinal = added;
1482 mHandler.post(new Runnable() {
1483 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001484 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1485 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001486 callbacks.bindAppsAdded(addedFinal);
1487 }
1488 }
1489 });
1490 }
1491 if (modified != null) {
1492 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1493 mHandler.post(new Runnable() {
1494 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001495 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1496 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001497 callbacks.bindAppsUpdated(modifiedFinal);
1498 }
1499 }
1500 });
1501 }
1502 if (removed != null) {
1503 final boolean permanent = mOp != OP_UNAVAILABLE;
1504 final ArrayList<ApplicationInfo> removedFinal = removed;
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.bindAppsRemoved(removedFinal, permanent);
1510 }
1511 }
1512 });
Joe Onoratobe386092009-11-17 17:32:16 -08001513 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001514
1515 mHandler.post(new Runnable() {
1516 @Override
1517 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001518 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1519 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001520 callbacks.bindPackagesUpdated();
1521 }
1522 }
1523 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001524 }
1525 }
1526
1527 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001528 * This is called from the code that adds shortcuts from the intent receiver. This
1529 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001530 */
Joe Onorato56d82912010-03-07 14:32:10 -05001531 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001532 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001533 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001534
Joe Onorato56d82912010-03-07 14:32:10 -05001535 /**
1536 * Make an ShortcutInfo object for a shortcut that is an application.
1537 *
1538 * If c is not null, then it will be used to fill in missing data like the title and icon.
1539 */
1540 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001541 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001542 Bitmap icon = null;
1543 final ShortcutInfo info = new ShortcutInfo();
1544
1545 ComponentName componentName = intent.getComponent();
1546 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001547 return null;
1548 }
1549
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001550 // TODO: See if the PackageManager knows about this case. If it doesn't
1551 // then return null & delete this.
1552
Joe Onorato56d82912010-03-07 14:32:10 -05001553 // the resource -- This may implicitly give us back the fallback icon,
1554 // but don't worry about that. All we're doing with usingFallbackIcon is
1555 // to avoid saving lots of copies of that in the database, and most apps
1556 // have icons anyway.
1557 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1558 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001559 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001560 }
Joe Onorato56d82912010-03-07 14:32:10 -05001561 // the db
1562 if (icon == null) {
1563 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001564 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001565 }
1566 }
1567 // the fallback icon
1568 if (icon == null) {
1569 icon = getFallbackIcon();
1570 info.usingFallbackIcon = true;
1571 }
1572 info.setIcon(icon);
1573
1574 // from the resource
1575 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001576 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1577 if (labelCache != null && labelCache.containsKey(key)) {
1578 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001579 } else {
1580 info.title = resolveInfo.activityInfo.loadLabel(manager);
1581 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001582 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001583 }
1584 }
Joe Onorato56d82912010-03-07 14:32:10 -05001585 }
1586 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001588 if (c != null) {
1589 info.title = c.getString(titleIndex);
1590 }
1591 }
1592 // fall back to the class name of the activity
1593 if (info.title == null) {
1594 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001595 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001596 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1597 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001599
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001601 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001603 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001604 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1605 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606
Joe Onorato56d82912010-03-07 14:32:10 -05001607 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001608 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001609 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001611 // TODO: If there's an explicit component and we can't install that, delete it.
1612
Joe Onorato56d82912010-03-07 14:32:10 -05001613 info.title = c.getString(titleIndex);
1614
Joe Onorato9c1289c2009-08-17 11:03:03 -04001615 int iconType = c.getInt(iconTypeIndex);
1616 switch (iconType) {
1617 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1618 String packageName = c.getString(iconPackageIndex);
1619 String resourceName = c.getString(iconResourceIndex);
1620 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001621 info.customIcon = false;
1622 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001625 if (resources != null) {
1626 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001627 icon = Utilities.createIconBitmap(
1628 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001631 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 }
Joe Onorato56d82912010-03-07 14:32:10 -05001633 // the db
1634 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001635 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001636 }
1637 // the fallback icon
1638 if (icon == null) {
1639 icon = getFallbackIcon();
1640 info.usingFallbackIcon = true;
1641 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001642 break;
1643 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001644 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001645 if (icon == null) {
1646 icon = getFallbackIcon();
1647 info.customIcon = false;
1648 info.usingFallbackIcon = true;
1649 } else {
1650 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001651 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001652 break;
1653 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001654 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001655 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001656 info.customIcon = false;
1657 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001659 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 return info;
1661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662
Michael Jurka931dc972011-08-05 15:08:15 -07001663 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001664 if (false) {
1665 Log.d(TAG, "getIconFromCursor app="
1666 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1667 }
1668 byte[] data = c.getBlob(iconIndex);
1669 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001670 return Utilities.createIconBitmap(
1671 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001672 } catch (Exception e) {
1673 return null;
1674 }
1675 }
1676
Winson Chung3d503fb2011-07-13 17:25:49 -07001677 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1678 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001679 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001681
1682 return info;
1683 }
1684
Winson Chunga9abd0e2010-10-27 17:18:37 -07001685 /**
Winson Chung55cef262010-10-28 14:14:18 -07001686 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1687 */
1688 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1689 ComponentName component) {
1690 List<AppWidgetProviderInfo> widgets =
1691 AppWidgetManager.getInstance(context).getInstalledProviders();
1692 for (AppWidgetProviderInfo info : widgets) {
1693 if (info.provider.equals(component)) {
1694 return info;
1695 }
1696 }
1697 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001698 }
1699
Winson Chung68846fd2010-10-29 11:00:27 -07001700 /**
1701 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1702 */
1703 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1704 final PackageManager packageManager = context.getPackageManager();
1705 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1706 new ArrayList<WidgetMimeTypeHandlerData>();
1707
1708 final Intent supportsIntent =
1709 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1710 supportsIntent.setType(mimeType);
1711
1712 // Create a set of widget configuration components that we can test against
1713 final List<AppWidgetProviderInfo> widgets =
1714 AppWidgetManager.getInstance(context).getInstalledProviders();
1715 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1716 new HashMap<ComponentName, AppWidgetProviderInfo>();
1717 for (AppWidgetProviderInfo info : widgets) {
1718 configurationComponentToWidget.put(info.configure, info);
1719 }
1720
1721 // Run through each of the intents that can handle this type of clip data, and cross
1722 // reference them with the components that are actual configuration components
1723 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1724 PackageManager.MATCH_DEFAULT_ONLY);
1725 for (ResolveInfo info : activities) {
1726 final ActivityInfo activityInfo = info.activityInfo;
1727 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1728 activityInfo.name);
1729 if (configurationComponentToWidget.containsKey(infoComponent)) {
1730 supportedConfigurationActivities.add(
1731 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1732 configurationComponentToWidget.get(infoComponent)));
1733 }
1734 }
1735 return supportedConfigurationActivities;
1736 }
1737
Winson Chunga9abd0e2010-10-27 17:18:37 -07001738 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001739 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1740 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1741 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1742
1743 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001744 boolean customIcon = false;
1745 ShortcutIconResource iconResource = null;
1746
1747 if (bitmap != null && bitmap instanceof Bitmap) {
1748 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001749 customIcon = true;
1750 } else {
1751 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1752 if (extra != null && extra instanceof ShortcutIconResource) {
1753 try {
1754 iconResource = (ShortcutIconResource) extra;
1755 final PackageManager packageManager = context.getPackageManager();
1756 Resources resources = packageManager.getResourcesForApplication(
1757 iconResource.packageName);
1758 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001759 icon = Utilities.createIconBitmap(
1760 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001761 } catch (Exception e) {
1762 Log.w(TAG, "Could not load shortcut icon: " + extra);
1763 }
1764 }
1765 }
1766
Joe Onorato0589f0f2010-02-08 13:44:00 -08001767 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001768
1769 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001770 if (fallbackIcon != null) {
1771 icon = fallbackIcon;
1772 } else {
1773 icon = getFallbackIcon();
1774 info.usingFallbackIcon = true;
1775 }
Joe Onorato56d82912010-03-07 14:32:10 -05001776 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001777 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001778
Joe Onorato0589f0f2010-02-08 13:44:00 -08001779 info.title = name;
1780 info.intent = intent;
1781 info.customIcon = customIcon;
1782 info.iconResource = iconResource;
1783
1784 return info;
1785 }
1786
Winson Chungaac01e12011-08-17 10:37:13 -07001787 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1788 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001789 // If apps can't be on SD, don't even bother.
1790 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001791 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001792 }
Joe Onorato56d82912010-03-07 14:32:10 -05001793 // If this icon doesn't have a custom icon, check to see
1794 // what's stored in the DB, and if it doesn't match what
1795 // we're going to show, store what we are going to show back
1796 // into the DB. We do this so when we're loading, if the
1797 // package manager can't find an icon (for example because
1798 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001799 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001800 cache.put(info, c.getBlob(iconIndex));
1801 return true;
1802 }
1803 return false;
1804 }
1805 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1806 boolean needSave = false;
1807 try {
1808 if (data != null) {
1809 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1810 Bitmap loaded = info.getIcon(mIconCache);
1811 needSave = !saved.sameAs(loaded);
1812 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001813 needSave = true;
1814 }
Winson Chungaac01e12011-08-17 10:37:13 -07001815 } catch (Exception e) {
1816 needSave = true;
1817 }
1818 if (needSave) {
1819 Log.d(TAG, "going to save icon bitmap for info=" + info);
1820 // This is slower than is ideal, but this only happens once
1821 // or when the app is updated with a new icon.
1822 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001823 }
1824 }
1825
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001827 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001828 * or make a new one.
1829 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001830 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001831 // See if a placeholder was created for us already
1832 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001833 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001835 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 folders.put(id, folderInfo);
1837 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001838 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
1840
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001842 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 = new Comparator<ApplicationInfo>() {
1844 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001845 int result = sCollator.compare(a.title.toString(), b.title.toString());
1846 if (result == 0) {
1847 result = a.componentName.compareTo(b.componentName);
1848 }
1849 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001851 };
Winson Chung78403fe2011-01-21 15:38:02 -08001852 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1853 = new Comparator<ApplicationInfo>() {
1854 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1855 if (a.firstInstallTime < b.firstInstallTime) return 1;
1856 if (a.firstInstallTime > b.firstInstallTime) return -1;
1857 return 0;
1858 }
1859 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001860 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1861 = new Comparator<AppWidgetProviderInfo>() {
1862 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1863 return sCollator.compare(a.label.toString(), b.label.toString());
1864 }
1865 };
Winson Chung5308f242011-08-18 12:12:41 -07001866 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1867 if (info.activityInfo != null) {
1868 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1869 } else {
1870 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1871 }
1872 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001873 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1874 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001875 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001876 ShortcutNameComparator(PackageManager pm) {
1877 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001878 mLabelCache = new HashMap<Object, CharSequence>();
1879 }
1880 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1881 mPackageManager = pm;
1882 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001883 }
1884 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001885 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001886 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1887 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1888 if (mLabelCache.containsKey(keyA)) {
1889 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001890 } else {
1891 labelA = a.loadLabel(mPackageManager).toString();
1892
Winson Chung5308f242011-08-18 12:12:41 -07001893 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001894 }
Winson Chung5308f242011-08-18 12:12:41 -07001895 if (mLabelCache.containsKey(keyB)) {
1896 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001897 } else {
1898 labelB = b.loadLabel(mPackageManager).toString();
1899
Winson Chung5308f242011-08-18 12:12:41 -07001900 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001901 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001902 return sCollator.compare(labelA, labelB);
1903 }
1904 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001905 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1906 private PackageManager mPackageManager;
1907 private HashMap<Object, String> mLabelCache;
1908 WidgetAndShortcutNameComparator(PackageManager pm) {
1909 mPackageManager = pm;
1910 mLabelCache = new HashMap<Object, String>();
1911 }
1912 public final int compare(Object a, Object b) {
1913 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001914 if (mLabelCache.containsKey(a)) {
1915 labelA = mLabelCache.get(a);
1916 } else {
1917 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001918 ((AppWidgetProviderInfo) a).label :
1919 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001920 mLabelCache.put(a, labelA);
1921 }
1922 if (mLabelCache.containsKey(b)) {
1923 labelB = mLabelCache.get(b);
1924 } else {
1925 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001926 ((AppWidgetProviderInfo) b).label :
1927 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001928 mLabelCache.put(b, labelB);
1929 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001930 return sCollator.compare(labelA, labelB);
1931 }
1932 };
Joe Onoratobe386092009-11-17 17:32:16 -08001933
1934 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001935 Log.d(TAG, "mCallbacks=" + mCallbacks);
1936 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1937 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1938 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1939 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001940 if (mLoaderTask != null) {
1941 mLoaderTask.dumpState();
1942 } else {
1943 Log.d(TAG, "mLoaderTask=null");
1944 }
Joe Onoratobe386092009-11-17 17:32:16 -08001945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946}