blob: c46e1758f0da85c2d64068e4baed992a83c7b1b3 [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>();
111 // </ only access in worker thread >
112
113 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Adam Cohend22015c2010-07-26 22:02:18 -0700116 private static int mCellCountX;
117 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700120 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 public int getCurrentWorkspaceScreen();
122 public void startBinding();
123 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500124 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 public void finishBindingItems();
126 public void bindAppWidget(LauncherAppWidgetInfo info);
127 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500128 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
129 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400130 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700131 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400132 public boolean isAllAppsVisible();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100133 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800137 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400138 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800139 mAllAppsList = new AllAppsList(iconCache);
140 mIconCache = iconCache;
141
142 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700143 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400144
145 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400146
147 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 }
149
Joe Onorato56d82912010-03-07 14:32:10 -0500150 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Adam Cohen4eac29a2011-07-11 17:53:37 -0700154 public static void unbindWorkspaceItems() {
155 for (ItemInfo item: sWorkspaceItems) {
156 item.unbind();
157 }
158 }
159
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 /**
161 * Adds an item to the DB if it was not created previously, or move it to a new
162 * <container, screen, cellX, cellY>
163 */
164 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
165 int screen, int cellX, int cellY) {
166 if (item.container == ItemInfo.NO_ID) {
167 // From all apps
168 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
169 } else {
170 // From somewhere else
171 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 }
173 }
174
175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700177 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700178 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
179 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 item.cellX = cellX;
182 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 // We store hotseat items in canonical form which is this orientation invariant position
184 // in the hotseat
185 if (context instanceof Launcher && screen < 0 &&
186 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
187 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
188 } else {
189 item.screen = screen;
190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700192 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 final ContentValues values = new ContentValues();
194 final ContentResolver cr = context.getContentResolver();
195
196 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700197 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
198 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
200
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700201 sWorker.post(new Runnable() {
202 public void run() {
203 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700204 ItemInfo modelItem = sItemsIdMap.get(item.id);
205 if (item != modelItem) {
206 // the modelItem needs to match up perfectly with item if our model is to be
207 // consistent with the database-- for now, just require modelItem == item
208 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
209 "doesn't match original");
210 }
211
212 // Items are added/removed from the corresponding FolderInfo elsewhere, such
213 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
214 // that are on the desktop, as appropriate
Winson Chung3d503fb2011-07-13 17:25:49 -0700215 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
216 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700217 if (!sWorkspaceItems.contains(modelItem)) {
218 sWorkspaceItems.add(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700219 }
220 } else {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700221 sWorkspaceItems.remove(modelItem);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700222 }
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700223 }
224 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700225 }
226
227 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800228 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800229 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700230 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
231 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800232 item.spanX = spanX;
233 item.spanY = spanY;
234 item.cellX = cellX;
235 item.cellY = cellY;
236
237 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
238 final ContentValues values = new ContentValues();
239 final ContentResolver cr = context.getContentResolver();
240
241 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
242 values.put(LauncherSettings.Favorites.SPANX, spanX);
243 values.put(LauncherSettings.Favorites.SPANY, spanY);
244 values.put(LauncherSettings.Favorites.CELLX, cellX);
245 values.put(LauncherSettings.Favorites.CELLY, cellY);
246
247 sWorker.post(new Runnable() {
248 public void run() {
249 cr.update(uri, values, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700250 ItemInfo modelItem = sItemsIdMap.get(item.id);
251 if (item != modelItem) {
252 // the modelItem needs to match up perfectly with item if our model is to be
253 // consistent with the database-- for now, just require modelItem == item
254 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
255 "doesn't match original");
256 }
Adam Cohend4844c32011-02-18 19:25:06 -0800257 }
258 });
259 }
260
261 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 * Returns true if the shortcuts already exists in the database.
263 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 static boolean shortcutExists(Context context, String title, Intent intent) {
266 final ContentResolver cr = context.getContentResolver();
267 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
268 new String[] { "title", "intent" }, "title=? and intent=?",
269 new String[] { title, intent.toUri(0) }, null);
270 boolean result = false;
271 try {
272 result = c.moveToFirst();
273 } finally {
274 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700277 }
278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700280 * Returns an ItemInfo array containing all the items in the LauncherModel.
281 * The ItemInfo.id is not set through this function.
282 */
283 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
284 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
285 final ContentResolver cr = context.getContentResolver();
286 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
287 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
288 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
289 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
290
291 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
292 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
293 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
294 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
295 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
296 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
297 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
298
299 try {
300 while (c.moveToNext()) {
301 ItemInfo item = new ItemInfo();
302 item.cellX = c.getInt(cellXIndex);
303 item.cellY = c.getInt(cellYIndex);
304 item.spanX = c.getInt(spanXIndex);
305 item.spanY = c.getInt(spanYIndex);
306 item.container = c.getInt(containerIndex);
307 item.itemType = c.getInt(itemTypeIndex);
308 item.screen = c.getInt(screenIndex);
309
310 items.add(item);
311 }
312 } catch (Exception e) {
313 items.clear();
314 } finally {
315 c.close();
316 }
317
318 return items;
319 }
320
321 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400322 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
323 */
324 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
325 final ContentResolver cr = context.getContentResolver();
326 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
327 "_id=? and (itemType=? or itemType=?)",
328 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700329 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700330
Joe Onorato9c1289c2009-08-17 11:03:03 -0400331 try {
332 if (c.moveToFirst()) {
333 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
334 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
335 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
336 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
337 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
338 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339
Joe Onorato9c1289c2009-08-17 11:03:03 -0400340 FolderInfo folderInfo = null;
341 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700342 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
343 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400344 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700345 }
346
Joe Onorato9c1289c2009-08-17 11:03:03 -0400347 folderInfo.title = c.getString(titleIndex);
348 folderInfo.id = id;
349 folderInfo.container = c.getInt(containerIndex);
350 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700351 folderInfo.cellX = c.getInt(cellXIndex);
352 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400353
354 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400356 } finally {
357 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700358 }
359
360 return null;
361 }
362
Joe Onorato9c1289c2009-08-17 11:03:03 -0400363 /**
364 * Add an item to the database in a specified container. Sets the container, screen, cellX and
365 * cellY fields of the item. Also assigns an ID to the item.
366 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700367 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
368 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 item.cellX = cellX;
371 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700372 // We store hotseat items in canonical form which is this orientation invariant position
373 // in the hotseat
374 if (context instanceof Launcher && screen < 0 &&
375 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
376 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
377 } else {
378 item.screen = screen;
379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380
381 final ContentValues values = new ContentValues();
382 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 item.onAddToDatabase(values);
384
Michael Jurka7578ec62011-08-03 14:11:54 -0700385 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700386 item.id = app.getLauncherProvider().generateNewId();
387 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700388 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700389
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700390 sWorker.post(new Runnable() {
391 public void run() {
392 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
393 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400394
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700395 sItemsIdMap.put(item.id, item);
396 switch (item.itemType) {
397 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
398 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700399 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700400 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
401 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700402 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
403 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700404 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700405 }
406 break;
407 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
408 sAppWidgets.add((LauncherAppWidgetInfo) item);
409 break;
410 }
411 }
412 });
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700413 }
414
Joe Onorato9c1289c2009-08-17 11:03:03 -0400415 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700416 * Creates a new unique child id, for a given cell span across all layouts.
417 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700418 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700419 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
420 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700421 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700422 }
423
Adam Cohend22015c2010-07-26 22:02:18 -0700424 static int getCellCountX() {
425 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700426 }
427
Adam Cohend22015c2010-07-26 22:02:18 -0700428 static int getCellCountY() {
429 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700430 }
431
432 /**
433 * Updates the model orientation helper to take into account the current layout dimensions
434 * when performing local/canonical coordinate transformations.
435 */
436 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700437 mCellCountX = shortAxisCellCount;
438 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700439 }
440
441 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442 * Update an item to the database in a specified container.
443 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700444 static void updateItemInDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400445 final ContentValues values = new ContentValues();
446 final ContentResolver cr = context.getContentResolver();
447
448 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700449 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700450
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700451 sWorker.post(new Runnable() {
452 public void run() {
453 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false),
454 values, null, null);
455 final ItemInfo modelItem = sItemsIdMap.get(item.id);
456 if (item != modelItem) {
457 // the modelItem needs to match up perfectly with item if our model is to be
458 // consistent with the database-- for now, just require modelItem == item
459 throw new RuntimeException("Error: ItemInfo passed to moveItemInDatabase " +
460 "doesn't match original");
461 }
462 }
463 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400464 }
465
466 /**
467 * Removes the specified item from the database
468 * @param context
469 * @param item
470 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700471 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400472 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700473 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700474 sWorker.post(new Runnable() {
475 public void run() {
476 cr.delete(uriToDelete, null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700477 switch (item.itemType) {
478 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
479 sFolders.remove(item.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700480 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700481 break;
482 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
483 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700484 sWorkspaceItems.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700485 break;
486 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
487 sAppWidgets.remove((LauncherAppWidgetInfo) item);
488 break;
489 }
490 sItemsIdMap.remove(item.id);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700491 }
492 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400493 }
494
495 /**
496 * Remove the contents of the specified folder from the database
497 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700498 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400499 final ContentResolver cr = context.getContentResolver();
500
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700501 sWorker.post(new Runnable() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700502 public void run() {
503 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700504 sItemsIdMap.remove(info.id);
505 sFolders.remove(info.id);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700506 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700507
Adam Cohen9932a9b2011-08-02 22:14:07 -0700508 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Adam Cohenafb01ee2011-06-23 15:38:03 -0700509 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700510 for (ItemInfo childInfo : info.contents) {
511 sItemsIdMap.remove(childInfo.id);
512 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700513 }
514 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 }
516
517 /**
518 * Set this as the current Launcher activity object for the loader.
519 */
520 public void initialize(Callbacks callbacks) {
521 synchronized (mLock) {
522 mCallbacks = new WeakReference<Callbacks>(callbacks);
523 }
524 }
525
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700526 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400527 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
528 * ACTION_PACKAGE_CHANGED.
529 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100530 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400531 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400532 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700533
Joe Onorato36115782010-06-17 13:28:48 -0400534 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400535
Joe Onorato36115782010-06-17 13:28:48 -0400536 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
537 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
538 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
539 final String packageName = intent.getData().getSchemeSpecificPart();
540 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541
Joe Onorato36115782010-06-17 13:28:48 -0400542 int op = PackageUpdatedTask.OP_NONE;
543
544 if (packageName == null || packageName.length() == 0) {
545 // they sent us a bad intent
546 return;
547 }
548
549 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
550 op = PackageUpdatedTask.OP_UPDATE;
551 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
552 if (!replacing) {
553 op = PackageUpdatedTask.OP_REMOVE;
554 }
555 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
556 // later, we will update the package at this time
557 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
558 if (!replacing) {
559 op = PackageUpdatedTask.OP_ADD;
560 } else {
561 op = PackageUpdatedTask.OP_UPDATE;
562 }
563 }
564
565 if (op != PackageUpdatedTask.OP_NONE) {
566 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
567 }
568
569 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400570 // First, schedule to add these apps back in.
571 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
572 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
573 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700574 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400575 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
576 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
577 enqueuePackageUpdated(new PackageUpdatedTask(
578 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700579 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
580 // If we have changed locale we need to clear out the labels in all apps.
581 // Do this here because if the launcher activity is running it will be restarted.
582 // If it's not running startLoaderFromBackground will merely tell it that it needs
583 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
584 // next time.
585 mAllAppsLoaded = false;
Michael Jurka288a36b2011-07-12 16:53:48 -0700586 mWorkspaceLoaded = false;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700587 startLoaderFromBackground();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100588 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) {
589 Callbacks callbacks = mCallbacks.get();
590 callbacks.bindSearchablesChanged();
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700591 }
592 }
593
594 /**
595 * When the launcher is in the background, it's possible for it to miss paired
596 * configuration changes. So whenever we trigger the loader from the background
597 * tell the launcher that it needs to re-run the loader when it comes back instead
598 * of doing it now.
599 */
600 public void startLoaderFromBackground() {
601 boolean runLoader = false;
602 if (mCallbacks != null) {
603 Callbacks callbacks = mCallbacks.get();
604 if (callbacks != null) {
605 // Only actually run the loader if they're not paused.
606 if (!callbacks.setLoadOnResume()) {
607 runLoader = true;
608 }
609 }
610 }
611 if (runLoader) {
612 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700613 }
Joe Onorato36115782010-06-17 13:28:48 -0400614 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400615
Joe Onorato36115782010-06-17 13:28:48 -0400616 public void startLoader(Context context, boolean isLaunching) {
617 synchronized (mLock) {
618 if (DEBUG_LOADERS) {
619 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
620 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621
Joe Onorato36115782010-06-17 13:28:48 -0400622 // Don't bother to start the thread if we know it's not going to do anything
623 if (mCallbacks != null && mCallbacks.get() != null) {
624 // If there is already one running, tell it to stop.
625 LoaderTask oldTask = mLoaderTask;
626 if (oldTask != null) {
627 if (oldTask.isLaunching()) {
628 // don't downgrade isLaunching if we're already running
629 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500630 }
Joe Onorato36115782010-06-17 13:28:48 -0400631 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500632 }
Joe Onorato36115782010-06-17 13:28:48 -0400633 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700634 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 }
637 }
638
Joe Onorato36115782010-06-17 13:28:48 -0400639 public void stopLoader() {
640 synchronized (mLock) {
641 if (mLoaderTask != null) {
642 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 }
644 }
Joe Onorato36115782010-06-17 13:28:48 -0400645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646
Michael Jurkac57b7a82011-08-09 22:02:20 -0700647 public boolean isAllAppsLoaded() {
648 return mAllAppsLoaded;
649 }
650
Joe Onorato36115782010-06-17 13:28:48 -0400651 /**
652 * Runnable for the thread that loads the contents of the launcher:
653 * - workspace icons
654 * - widgets
655 * - all apps icons
656 */
657 private class LoaderTask implements Runnable {
658 private Context mContext;
659 private Thread mWaitThread;
660 private boolean mIsLaunching;
661 private boolean mStopped;
662 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700663 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400664
665 LoaderTask(Context context, boolean isLaunching) {
666 mContext = context;
667 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700668 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 }
670
Joe Onorato36115782010-06-17 13:28:48 -0400671 boolean isLaunching() {
672 return mIsLaunching;
673 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674
Joe Onorato36115782010-06-17 13:28:48 -0400675 private void loadAndBindWorkspace() {
676 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400677 if (DEBUG_LOADERS) {
678 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700680
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700681 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400682 loadWorkspace();
683 if (mStopped) {
684 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 }
Joe Onorato36115782010-06-17 13:28:48 -0400686 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687 }
688
Joe Onorato36115782010-06-17 13:28:48 -0400689 // Bind the workspace
690 bindWorkspace();
691 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400692
Joe Onorato36115782010-06-17 13:28:48 -0400693 private void waitForIdle() {
694 // Wait until the either we're stopped or the other threads are done.
695 // This way we don't start loading all apps until the workspace has settled
696 // down.
697 synchronized (LoaderTask.this) {
698 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700699
Joe Onorato36115782010-06-17 13:28:48 -0400700 mHandler.postIdle(new Runnable() {
701 public void run() {
702 synchronized (LoaderTask.this) {
703 mLoadAndBindStepFinished = true;
704 if (DEBUG_LOADERS) {
705 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400706 }
Joe Onorato36115782010-06-17 13:28:48 -0400707 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400708 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400709 }
Joe Onorato36115782010-06-17 13:28:48 -0400710 });
711
712 while (!mStopped && !mLoadAndBindStepFinished) {
713 try {
714 this.wait();
715 } catch (InterruptedException ex) {
716 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400717 }
718 }
Joe Onorato36115782010-06-17 13:28:48 -0400719 if (DEBUG_LOADERS) {
720 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700721 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400722 + "ms for previous step to finish binding");
723 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400724 }
Joe Onorato36115782010-06-17 13:28:48 -0400725 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400726
Joe Onorato36115782010-06-17 13:28:48 -0400727 public void run() {
728 // Optimize for end-user experience: if the Launcher is up and // running with the
729 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
730 // workspace first (default).
731 final Callbacks cbk = mCallbacks.get();
732 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400733
Joe Onorato36115782010-06-17 13:28:48 -0400734 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400735 // Elevate priority when Home launches for the first time to avoid
736 // starving at boot time. Staring at a blank home is not cool.
737 synchronized (mLock) {
738 android.os.Process.setThreadPriority(mIsLaunching
739 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
740 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400741 if (loadWorkspaceFirst) {
742 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
743 loadAndBindWorkspace();
744 } else {
745 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700746 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400747 }
748
Joe Onorato36115782010-06-17 13:28:48 -0400749 if (mStopped) {
750 break keep_running;
751 }
752
753 // Whew! Hard work done. Slow us down, and wait until the UI thread has
754 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400755 synchronized (mLock) {
756 if (mIsLaunching) {
757 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
758 }
759 }
Joe Onorato36115782010-06-17 13:28:48 -0400760 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400761
762 // second step
763 if (loadWorkspaceFirst) {
764 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700765 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400766 } else {
767 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
768 loadAndBindWorkspace();
769 }
Joe Onorato36115782010-06-17 13:28:48 -0400770 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771
Joe Onorato36115782010-06-17 13:28:48 -0400772 // Clear out this reference, otherwise we end up holding it until all of the
773 // callback runnables are done.
774 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775
Joe Onorato36115782010-06-17 13:28:48 -0400776 synchronized (mLock) {
777 // If we are still the last one to be scheduled, remove ourselves.
778 if (mLoaderTask == this) {
779 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780 }
Joe Onorato36115782010-06-17 13:28:48 -0400781 }
Joe Onorato36115782010-06-17 13:28:48 -0400782 }
783
784 public void stopLocked() {
785 synchronized (LoaderTask.this) {
786 mStopped = true;
787 this.notify();
788 }
789 }
790
791 /**
792 * Gets the callbacks object. If we've been stopped, or if the launcher object
793 * has somehow been garbage collected, return null instead. Pass in the Callbacks
794 * object that was around when the deferred message was scheduled, and if there's
795 * a new Callbacks object around then also return null. This will save us from
796 * calling onto it with data that will be ignored.
797 */
798 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
799 synchronized (mLock) {
800 if (mStopped) {
801 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400802 }
Joe Onorato36115782010-06-17 13:28:48 -0400803
804 if (mCallbacks == null) {
805 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400806 }
Joe Onorato36115782010-06-17 13:28:48 -0400807
808 final Callbacks callbacks = mCallbacks.get();
809 if (callbacks != oldCallbacks) {
810 return null;
811 }
812 if (callbacks == null) {
813 Log.w(TAG, "no mCallbacks");
814 return null;
815 }
816
817 return callbacks;
818 }
819 }
820
821 // check & update map of what's occupied; used to discard overlapping/invalid items
822 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700823 int containerIndex = item.screen;
824 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
825 // We use the last index to refer to the hotseat
826 containerIndex = Launcher.SCREEN_COUNT;
827 // Return early if we detect that an item is under the hotseat button
828 if (Hotseat.isAllAppsButtonRank(item.screen)) {
829 return false;
830 }
831 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
832 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400833 return true;
834 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700835
Joe Onorato36115782010-06-17 13:28:48 -0400836 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
837 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700838 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400839 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700840 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400841 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700842 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700843 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400844 return false;
845 }
846 }
847 }
848 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
849 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700850 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400851 }
852 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700853
Joe Onorato36115782010-06-17 13:28:48 -0400854 return true;
855 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856
Joe Onorato36115782010-06-17 13:28:48 -0400857 private void loadWorkspace() {
858 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859
Joe Onorato36115782010-06-17 13:28:48 -0400860 final Context context = mContext;
861 final ContentResolver contentResolver = context.getContentResolver();
862 final PackageManager manager = context.getPackageManager();
863 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
864 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400865
Adam Cohen4eac29a2011-07-11 17:53:37 -0700866 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700867 sAppWidgets.clear();
868 sFolders.clear();
869 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800870
Joe Onorato36115782010-06-17 13:28:48 -0400871 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872
Joe Onorato36115782010-06-17 13:28:48 -0400873 final Cursor c = contentResolver.query(
874 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400875
Winson Chungf30ad5f2011-08-08 10:55:42 -0700876 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700877 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700878 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879
Joe Onorato36115782010-06-17 13:28:48 -0400880 try {
881 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
882 final int intentIndex = c.getColumnIndexOrThrow
883 (LauncherSettings.Favorites.INTENT);
884 final int titleIndex = c.getColumnIndexOrThrow
885 (LauncherSettings.Favorites.TITLE);
886 final int iconTypeIndex = c.getColumnIndexOrThrow(
887 LauncherSettings.Favorites.ICON_TYPE);
888 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
889 final int iconPackageIndex = c.getColumnIndexOrThrow(
890 LauncherSettings.Favorites.ICON_PACKAGE);
891 final int iconResourceIndex = c.getColumnIndexOrThrow(
892 LauncherSettings.Favorites.ICON_RESOURCE);
893 final int containerIndex = c.getColumnIndexOrThrow(
894 LauncherSettings.Favorites.CONTAINER);
895 final int itemTypeIndex = c.getColumnIndexOrThrow(
896 LauncherSettings.Favorites.ITEM_TYPE);
897 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
898 LauncherSettings.Favorites.APPWIDGET_ID);
899 final int screenIndex = c.getColumnIndexOrThrow(
900 LauncherSettings.Favorites.SCREEN);
901 final int cellXIndex = c.getColumnIndexOrThrow
902 (LauncherSettings.Favorites.CELLX);
903 final int cellYIndex = c.getColumnIndexOrThrow
904 (LauncherSettings.Favorites.CELLY);
905 final int spanXIndex = c.getColumnIndexOrThrow
906 (LauncherSettings.Favorites.SPANX);
907 final int spanYIndex = c.getColumnIndexOrThrow(
908 LauncherSettings.Favorites.SPANY);
909 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
910 final int displayModeIndex = c.getColumnIndexOrThrow(
911 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912
Joe Onorato36115782010-06-17 13:28:48 -0400913 ShortcutInfo info;
914 String intentDescription;
915 LauncherAppWidgetInfo appWidgetInfo;
916 int container;
917 long id;
918 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919
Joe Onorato36115782010-06-17 13:28:48 -0400920 while (!mStopped && c.moveToNext()) {
921 try {
922 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923
Joe Onorato36115782010-06-17 13:28:48 -0400924 switch (itemType) {
925 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
926 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
927 intentDescription = c.getString(intentIndex);
928 try {
929 intent = Intent.parseUri(intentDescription, 0);
930 } catch (URISyntaxException e) {
931 continue;
932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933
Joe Onorato36115782010-06-17 13:28:48 -0400934 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
935 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700936 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400937 } else {
938 info = getShortcutInfo(c, context, iconTypeIndex,
939 iconPackageIndex, iconResourceIndex, iconIndex,
940 titleIndex);
941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942
Joe Onorato36115782010-06-17 13:28:48 -0400943 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400944 info.intent = intent;
945 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400947 info.container = container;
948 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700949 info.cellX = c.getInt(cellXIndex);
950 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951
Daniel Sandler8802e962010-05-26 16:28:16 -0400952 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400953 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400954 break;
955 }
956
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400958 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700960 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400961 break;
962 default:
963 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700964 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700965 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400966 folderInfo.add(info);
967 break;
968 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700969 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800970
971 // now that we've loaded everthing re-save it with the
972 // icon in case it disappears somehow.
973 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400974 } else {
975 // Failed to load the shortcut, probably because the
976 // activity manager couldn't resolve it (maybe the app
977 // was uninstalled), or the db row was somehow screwed up.
978 // Delete it.
979 id = c.getLong(idIndex);
980 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
981 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
982 id, false), null, null);
983 }
984 break;
985
Adam Cohendf2cc412011-04-27 16:56:57 -0700986 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -0400987 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700988 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400989
Winson Chungaafa03c2010-06-11 17:34:16 -0700990 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400991 folderInfo.id = id;
992 container = c.getInt(containerIndex);
993 folderInfo.container = container;
994 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700995 folderInfo.cellX = c.getInt(cellXIndex);
996 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400997
998 // check & update map of what's occupied
999 if (!checkItemPlacement(occupied, folderInfo)) {
1000 break;
1001 }
Joe Onorato36115782010-06-17 13:28:48 -04001002 switch (container) {
1003 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001005 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001006 break;
1007 }
1008
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001009 sItemsIdMap.put(folderInfo.id, folderInfo);
1010 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001011 break;
1012
Joe Onorato36115782010-06-17 13:28:48 -04001013 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1014 // Read all Launcher-specific widget details
1015 int appWidgetId = c.getInt(appWidgetIdIndex);
1016 id = c.getLong(idIndex);
1017
1018 final AppWidgetProviderInfo provider =
1019 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001020
Joe Onorato36115782010-06-17 13:28:48 -04001021 if (!isSafeMode && (provider == null || provider.provider == null ||
1022 provider.provider.getPackageName() == null)) {
1023 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1024 + id + " appWidgetId=" + appWidgetId);
1025 itemsToRemove.add(id);
1026 } else {
1027 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
1028 appWidgetInfo.id = id;
1029 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001030 appWidgetInfo.cellX = c.getInt(cellXIndex);
1031 appWidgetInfo.cellY = c.getInt(cellYIndex);
1032 appWidgetInfo.spanX = c.getInt(spanXIndex);
1033 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001034
1035 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001036 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1037 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001038 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001039 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001040 continue;
1041 }
1042 appWidgetInfo.container = c.getInt(containerIndex);
1043
1044 // check & update map of what's occupied
1045 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1046 break;
1047 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001048 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1049 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001050 }
1051 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001052 }
Joe Onorato36115782010-06-17 13:28:48 -04001053 } catch (Exception e) {
1054 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001055 }
1056 }
Joe Onorato36115782010-06-17 13:28:48 -04001057 } finally {
1058 c.close();
1059 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001060
Joe Onorato36115782010-06-17 13:28:48 -04001061 if (itemsToRemove.size() > 0) {
1062 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1063 LauncherSettings.Favorites.CONTENT_URI);
1064 // Remove dead items
1065 for (long id : itemsToRemove) {
1066 if (DEBUG_LOADERS) {
1067 Log.d(TAG, "Removed id = " + id);
1068 }
1069 // Don't notify content observers
1070 try {
1071 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1072 null, null);
1073 } catch (RemoteException e) {
1074 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001075 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001076 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001077 }
1078
Joe Onorato36115782010-06-17 13:28:48 -04001079 if (DEBUG_LOADERS) {
1080 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1081 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001082 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001083 String line = "";
1084 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1085 if (s > 0) {
1086 line += " | ";
1087 }
Adam Cohend22015c2010-07-26 22:02:18 -07001088 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001089 line += ((occupied[s][x][y] != null) ? "#" : ".");
1090 }
1091 }
1092 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 }
Joe Onorato36115782010-06-17 13:28:48 -04001094 }
1095 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096
Joe Onorato36115782010-06-17 13:28:48 -04001097 /**
1098 * Read everything out of our database.
1099 */
1100 private void bindWorkspace() {
1101 final long t = SystemClock.uptimeMillis();
1102
1103 // Don't use these two variables in any of the callback runnables.
1104 // Otherwise we hold a reference to them.
1105 final Callbacks oldCallbacks = mCallbacks.get();
1106 if (oldCallbacks == null) {
1107 // This launcher has exited and nobody bothered to tell us. Just bail.
1108 Log.w(TAG, "LoaderTask running with no launcher");
1109 return;
1110 }
1111
1112 int N;
1113 // Tell the workspace that we're about to start firing items at it
1114 mHandler.post(new Runnable() {
1115 public void run() {
1116 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1117 if (callbacks != null) {
1118 callbacks.startBinding();
1119 }
1120 }
1121 });
1122 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001123 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001124 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1125 final int start = i;
1126 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 mHandler.post(new Runnable() {
1128 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001129 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001131 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
1133 }
1134 });
Joe Onorato36115782010-06-17 13:28:48 -04001135 }
1136 mHandler.post(new Runnable() {
1137 public void run() {
1138 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1139 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001140 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001141 }
1142 }
1143 });
1144 // Wait until the queue goes empty.
1145 mHandler.post(new Runnable() {
1146 public void run() {
1147 if (DEBUG_LOADERS) {
1148 Log.d(TAG, "Going to start binding widgets soon.");
1149 }
1150 }
1151 });
1152 // Bind the widgets, one at a time.
1153 // WARNING: this is calling into the workspace from the background thread,
1154 // but since getCurrentScreen() just returns the int, we should be okay. This
1155 // is just a hint for the order, and if it's wrong, we'll be okay.
1156 // TODO: instead, we should have that push the current screen into here.
1157 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001158 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001159 // once for the current screen
1160 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001161 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001162 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001163 mHandler.post(new Runnable() {
1164 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001165 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001167 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 }
1169 }
1170 });
1171 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172 }
Joe Onorato36115782010-06-17 13:28:48 -04001173 // once for the other screens
1174 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001175 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001176 if (widget.screen != currentScreen) {
1177 mHandler.post(new Runnable() {
1178 public void run() {
1179 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1180 if (callbacks != null) {
1181 callbacks.bindAppWidget(widget);
1182 }
1183 }
1184 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001185 }
1186 }
Joe Onorato36115782010-06-17 13:28:48 -04001187 // Tell the workspace that we're done.
1188 mHandler.post(new Runnable() {
1189 public void run() {
1190 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1191 if (callbacks != null) {
1192 callbacks.finishBindingItems();
1193 }
1194 }
1195 });
1196 // If we're profiling, this is the last thing in the queue.
1197 mHandler.post(new Runnable() {
1198 public void run() {
1199 if (DEBUG_LOADERS) {
1200 Log.d(TAG, "bound workspace in "
1201 + (SystemClock.uptimeMillis()-t) + "ms");
1202 }
1203 }
1204 });
1205 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001206
Joe Onorato36115782010-06-17 13:28:48 -04001207 private void loadAndBindAllApps() {
1208 if (DEBUG_LOADERS) {
1209 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1210 }
1211 if (!mAllAppsLoaded) {
1212 loadAllAppsByBatch();
1213 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001214 return;
1215 }
Joe Onorato36115782010-06-17 13:28:48 -04001216 mAllAppsLoaded = true;
1217 } else {
1218 onlyBindAllApps();
1219 }
1220 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001221
Joe Onorato36115782010-06-17 13:28:48 -04001222 private void onlyBindAllApps() {
1223 final Callbacks oldCallbacks = mCallbacks.get();
1224 if (oldCallbacks == null) {
1225 // This launcher has exited and nobody bothered to tell us. Just bail.
1226 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1227 return;
1228 }
1229
1230 // shallow copy
1231 final ArrayList<ApplicationInfo> list
1232 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1233 mHandler.post(new Runnable() {
1234 public void run() {
1235 final long t = SystemClock.uptimeMillis();
1236 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1237 if (callbacks != null) {
1238 callbacks.bindAllApplications(list);
1239 }
1240 if (DEBUG_LOADERS) {
1241 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1242 + (SystemClock.uptimeMillis()-t) + "ms");
1243 }
1244 }
1245 });
1246
1247 }
1248
1249 private void loadAllAppsByBatch() {
1250 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1251
1252 // Don't use these two variables in any of the callback runnables.
1253 // Otherwise we hold a reference to them.
1254 final Callbacks oldCallbacks = mCallbacks.get();
1255 if (oldCallbacks == null) {
1256 // This launcher has exited and nobody bothered to tell us. Just bail.
1257 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1258 return;
1259 }
1260
1261 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1262 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1263
1264 final PackageManager packageManager = mContext.getPackageManager();
1265 List<ResolveInfo> apps = null;
1266
1267 int N = Integer.MAX_VALUE;
1268
1269 int startIndex;
1270 int i=0;
1271 int batchSize = -1;
1272 while (i < N && !mStopped) {
1273 if (i == 0) {
1274 mAllAppsList.clear();
1275 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1276 apps = packageManager.queryIntentActivities(mainIntent, 0);
1277 if (DEBUG_LOADERS) {
1278 Log.d(TAG, "queryIntentActivities took "
1279 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1280 }
1281 if (apps == null) {
1282 return;
1283 }
1284 N = apps.size();
1285 if (DEBUG_LOADERS) {
1286 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1287 }
1288 if (N == 0) {
1289 // There are no apps?!?
1290 return;
1291 }
1292 if (mBatchSize == 0) {
1293 batchSize = N;
1294 } else {
1295 batchSize = mBatchSize;
1296 }
1297
1298 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1299 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001300 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001301 if (DEBUG_LOADERS) {
1302 Log.d(TAG, "sort took "
1303 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1304 }
1305 }
1306
1307 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1308
1309 startIndex = i;
1310 for (int j=0; i<N && j<batchSize; j++) {
1311 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001312 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1313 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001314 i++;
1315 }
1316
1317 final boolean first = i <= batchSize;
1318 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1319 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1320 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1321
Joe Onoratocc67f472010-06-08 10:54:30 -07001322 mHandler.post(new Runnable() {
1323 public void run() {
1324 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001325 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001326 if (first) {
1327 callbacks.bindAllApplications(added);
1328 } else {
1329 callbacks.bindAppsAdded(added);
1330 }
1331 if (DEBUG_LOADERS) {
1332 Log.d(TAG, "bound " + added.size() + " apps in "
1333 + (SystemClock.uptimeMillis() - t) + "ms");
1334 }
1335 } else {
1336 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001337 }
1338 }
1339 });
1340
Daniel Sandlerdca66122010-04-13 16:23:58 -04001341 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001342 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1343 + (SystemClock.uptimeMillis()-t2) + "ms");
1344 }
1345
1346 if (mAllAppsLoadDelay > 0 && i < N) {
1347 try {
1348 if (DEBUG_LOADERS) {
1349 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1350 }
1351 Thread.sleep(mAllAppsLoadDelay);
1352 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 }
1354 }
1355
Joe Onorato36115782010-06-17 13:28:48 -04001356 if (DEBUG_LOADERS) {
1357 Log.d(TAG, "cached all " + N + " apps in "
1358 + (SystemClock.uptimeMillis()-t) + "ms"
1359 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001360 }
1361 }
1362
1363 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001364 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1365 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1366 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1367 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1368 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001369 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001370 }
1371 }
1372
1373 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001374 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001375 }
1376
1377 private class PackageUpdatedTask implements Runnable {
1378 int mOp;
1379 String[] mPackages;
1380
1381 public static final int OP_NONE = 0;
1382 public static final int OP_ADD = 1;
1383 public static final int OP_UPDATE = 2;
1384 public static final int OP_REMOVE = 3; // uninstlled
1385 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1386
1387
1388 public PackageUpdatedTask(int op, String[] packages) {
1389 mOp = op;
1390 mPackages = packages;
1391 }
1392
1393 public void run() {
1394 final Context context = mApp;
1395
1396 final String[] packages = mPackages;
1397 final int N = packages.length;
1398 switch (mOp) {
1399 case OP_ADD:
1400 for (int i=0; i<N; i++) {
1401 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1402 mAllAppsList.addPackage(context, packages[i]);
1403 }
1404 break;
1405 case OP_UPDATE:
1406 for (int i=0; i<N; i++) {
1407 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1408 mAllAppsList.updatePackage(context, packages[i]);
1409 }
1410 break;
1411 case OP_REMOVE:
1412 case OP_UNAVAILABLE:
1413 for (int i=0; i<N; i++) {
1414 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1415 mAllAppsList.removePackage(packages[i]);
1416 }
1417 break;
1418 }
1419
1420 ArrayList<ApplicationInfo> added = null;
1421 ArrayList<ApplicationInfo> removed = null;
1422 ArrayList<ApplicationInfo> modified = null;
1423
1424 if (mAllAppsList.added.size() > 0) {
1425 added = mAllAppsList.added;
1426 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1427 }
1428 if (mAllAppsList.removed.size() > 0) {
1429 removed = mAllAppsList.removed;
1430 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1431 for (ApplicationInfo info: removed) {
1432 mIconCache.remove(info.intent.getComponent());
1433 }
1434 }
1435 if (mAllAppsList.modified.size() > 0) {
1436 modified = mAllAppsList.modified;
1437 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1438 }
1439
1440 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1441 if (callbacks == null) {
1442 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1443 return;
1444 }
1445
1446 if (added != null) {
1447 final ArrayList<ApplicationInfo> addedFinal = added;
1448 mHandler.post(new Runnable() {
1449 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001450 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1451 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001452 callbacks.bindAppsAdded(addedFinal);
1453 }
1454 }
1455 });
1456 }
1457 if (modified != null) {
1458 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1459 mHandler.post(new Runnable() {
1460 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001461 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1462 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001463 callbacks.bindAppsUpdated(modifiedFinal);
1464 }
1465 }
1466 });
1467 }
1468 if (removed != null) {
1469 final boolean permanent = mOp != OP_UNAVAILABLE;
1470 final ArrayList<ApplicationInfo> removedFinal = removed;
1471 mHandler.post(new Runnable() {
1472 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001473 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1474 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001475 callbacks.bindAppsRemoved(removedFinal, permanent);
1476 }
1477 }
1478 });
Joe Onoratobe386092009-11-17 17:32:16 -08001479 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001480
1481 mHandler.post(new Runnable() {
1482 @Override
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) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001486 callbacks.bindPackagesUpdated();
1487 }
1488 }
1489 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490 }
1491 }
1492
1493 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001494 * This is called from the code that adds shortcuts from the intent receiver. This
1495 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496 */
Joe Onorato56d82912010-03-07 14:32:10 -05001497 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001498 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500
Joe Onorato56d82912010-03-07 14:32:10 -05001501 /**
1502 * Make an ShortcutInfo object for a shortcut that is an application.
1503 *
1504 * If c is not null, then it will be used to fill in missing data like the title and icon.
1505 */
1506 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001507 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001508 Bitmap icon = null;
1509 final ShortcutInfo info = new ShortcutInfo();
1510
1511 ComponentName componentName = intent.getComponent();
1512 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001513 return null;
1514 }
1515
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001516 // TODO: See if the PackageManager knows about this case. If it doesn't
1517 // then return null & delete this.
1518
Joe Onorato56d82912010-03-07 14:32:10 -05001519 // the resource -- This may implicitly give us back the fallback icon,
1520 // but don't worry about that. All we're doing with usingFallbackIcon is
1521 // to avoid saving lots of copies of that in the database, and most apps
1522 // have icons anyway.
1523 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1524 if (resolveInfo != null) {
1525 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001526 }
Joe Onorato56d82912010-03-07 14:32:10 -05001527 // the db
1528 if (icon == null) {
1529 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001530 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001531 }
1532 }
1533 // the fallback icon
1534 if (icon == null) {
1535 icon = getFallbackIcon();
1536 info.usingFallbackIcon = true;
1537 }
1538 info.setIcon(icon);
1539
1540 // from the resource
1541 if (resolveInfo != null) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001542 if (labelCache != null && labelCache.containsKey(resolveInfo)) {
1543 info.title = labelCache.get(resolveInfo);
1544 } else {
1545 info.title = resolveInfo.activityInfo.loadLabel(manager);
1546 if (labelCache != null) {
1547 labelCache.put(resolveInfo, info.title);
1548 }
1549 }
Joe Onorato56d82912010-03-07 14:32:10 -05001550 }
1551 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001553 if (c != null) {
1554 info.title = c.getString(titleIndex);
1555 }
1556 }
1557 // fall back to the class name of the activity
1558 if (info.title == null) {
1559 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001560 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1562 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001566 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001568 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001569 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1570 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571
Joe Onorato56d82912010-03-07 14:32:10 -05001572 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001573 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001576 // TODO: If there's an explicit component and we can't install that, delete it.
1577
Joe Onorato56d82912010-03-07 14:32:10 -05001578 info.title = c.getString(titleIndex);
1579
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 int iconType = c.getInt(iconTypeIndex);
1581 switch (iconType) {
1582 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1583 String packageName = c.getString(iconPackageIndex);
1584 String resourceName = c.getString(iconResourceIndex);
1585 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001586 info.customIcon = false;
1587 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001589 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001590 if (resources != null) {
1591 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001592 icon = Utilities.createIconBitmap(
1593 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001594 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001596 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
Joe Onorato56d82912010-03-07 14:32:10 -05001598 // the db
1599 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001600 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001601 }
1602 // the fallback icon
1603 if (icon == null) {
1604 icon = getFallbackIcon();
1605 info.usingFallbackIcon = true;
1606 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607 break;
1608 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001609 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001610 if (icon == null) {
1611 icon = getFallbackIcon();
1612 info.customIcon = false;
1613 info.usingFallbackIcon = true;
1614 } else {
1615 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 break;
1618 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001619 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001620 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001621 info.customIcon = false;
1622 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001624 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001625 return info;
1626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627
Michael Jurka931dc972011-08-05 15:08:15 -07001628 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001629 if (false) {
1630 Log.d(TAG, "getIconFromCursor app="
1631 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1632 }
1633 byte[] data = c.getBlob(iconIndex);
1634 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001635 return Utilities.createIconBitmap(
1636 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001637 } catch (Exception e) {
1638 return null;
1639 }
1640 }
1641
Winson Chung3d503fb2011-07-13 17:25:49 -07001642 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1643 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001644 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001645 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001646
1647 return info;
1648 }
1649
Winson Chunga9abd0e2010-10-27 17:18:37 -07001650 /**
Winson Chung55cef262010-10-28 14:14:18 -07001651 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1652 */
1653 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1654 ComponentName component) {
1655 List<AppWidgetProviderInfo> widgets =
1656 AppWidgetManager.getInstance(context).getInstalledProviders();
1657 for (AppWidgetProviderInfo info : widgets) {
1658 if (info.provider.equals(component)) {
1659 return info;
1660 }
1661 }
1662 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001663 }
1664
Winson Chung68846fd2010-10-29 11:00:27 -07001665 /**
1666 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1667 */
1668 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1669 final PackageManager packageManager = context.getPackageManager();
1670 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1671 new ArrayList<WidgetMimeTypeHandlerData>();
1672
1673 final Intent supportsIntent =
1674 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1675 supportsIntent.setType(mimeType);
1676
1677 // Create a set of widget configuration components that we can test against
1678 final List<AppWidgetProviderInfo> widgets =
1679 AppWidgetManager.getInstance(context).getInstalledProviders();
1680 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1681 new HashMap<ComponentName, AppWidgetProviderInfo>();
1682 for (AppWidgetProviderInfo info : widgets) {
1683 configurationComponentToWidget.put(info.configure, info);
1684 }
1685
1686 // Run through each of the intents that can handle this type of clip data, and cross
1687 // reference them with the components that are actual configuration components
1688 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1689 PackageManager.MATCH_DEFAULT_ONLY);
1690 for (ResolveInfo info : activities) {
1691 final ActivityInfo activityInfo = info.activityInfo;
1692 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1693 activityInfo.name);
1694 if (configurationComponentToWidget.containsKey(infoComponent)) {
1695 supportedConfigurationActivities.add(
1696 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1697 configurationComponentToWidget.get(infoComponent)));
1698 }
1699 }
1700 return supportedConfigurationActivities;
1701 }
1702
Winson Chunga9abd0e2010-10-27 17:18:37 -07001703 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001704 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1705 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1706 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1707
1708 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001709 boolean customIcon = false;
1710 ShortcutIconResource iconResource = null;
1711
1712 if (bitmap != null && bitmap instanceof Bitmap) {
1713 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001714 customIcon = true;
1715 } else {
1716 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1717 if (extra != null && extra instanceof ShortcutIconResource) {
1718 try {
1719 iconResource = (ShortcutIconResource) extra;
1720 final PackageManager packageManager = context.getPackageManager();
1721 Resources resources = packageManager.getResourcesForApplication(
1722 iconResource.packageName);
1723 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001724 icon = Utilities.createIconBitmap(
1725 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001726 } catch (Exception e) {
1727 Log.w(TAG, "Could not load shortcut icon: " + extra);
1728 }
1729 }
1730 }
1731
Joe Onorato0589f0f2010-02-08 13:44:00 -08001732 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001733
1734 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001735 if (fallbackIcon != null) {
1736 icon = fallbackIcon;
1737 } else {
1738 icon = getFallbackIcon();
1739 info.usingFallbackIcon = true;
1740 }
Joe Onorato56d82912010-03-07 14:32:10 -05001741 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001742 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001743
Joe Onorato0589f0f2010-02-08 13:44:00 -08001744 info.title = name;
1745 info.intent = intent;
1746 info.customIcon = customIcon;
1747 info.iconResource = iconResource;
1748
1749 return info;
1750 }
1751
Joe Onorato56d82912010-03-07 14:32:10 -05001752 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001753 // If apps can't be on SD, don't even bother.
1754 if (!mAppsCanBeOnExternalStorage) {
1755 return;
1756 }
Joe Onorato56d82912010-03-07 14:32:10 -05001757 // If this icon doesn't have a custom icon, check to see
1758 // what's stored in the DB, and if it doesn't match what
1759 // we're going to show, store what we are going to show back
1760 // into the DB. We do this so when we're loading, if the
1761 // package manager can't find an icon (for example because
1762 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001763 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001764 boolean needSave;
1765 byte[] data = c.getBlob(iconIndex);
1766 try {
1767 if (data != null) {
1768 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1769 Bitmap loaded = info.getIcon(mIconCache);
1770 needSave = !saved.sameAs(loaded);
1771 } else {
1772 needSave = true;
1773 }
1774 } catch (Exception e) {
1775 needSave = true;
1776 }
1777 if (needSave) {
1778 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001779 // This is slower than is ideal, but this only happens once
1780 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001781 updateItemInDatabase(context, info);
1782 }
1783 }
1784 }
1785
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001787 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788 * or make a new one.
1789 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001790 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791 // See if a placeholder was created for us already
1792 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001793 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001794 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001795 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796 folders.put(id, folderInfo);
1797 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001798 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 }
1800
Joe Onorato9c1289c2009-08-17 11:03:03 -04001801 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001802 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001803 = new Comparator<ApplicationInfo>() {
1804 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001805 int result = sCollator.compare(a.title.toString(), b.title.toString());
1806 if (result == 0) {
1807 result = a.componentName.compareTo(b.componentName);
1808 }
1809 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001811 };
Winson Chung78403fe2011-01-21 15:38:02 -08001812 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1813 = new Comparator<ApplicationInfo>() {
1814 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1815 if (a.firstInstallTime < b.firstInstallTime) return 1;
1816 if (a.firstInstallTime > b.firstInstallTime) return -1;
1817 return 0;
1818 }
1819 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001820 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1821 = new Comparator<AppWidgetProviderInfo>() {
1822 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1823 return sCollator.compare(a.label.toString(), b.label.toString());
1824 }
1825 };
1826 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1827 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001828 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001829 ShortcutNameComparator(PackageManager pm) {
1830 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001831 mLabelCache = new HashMap<Object, CharSequence>();
1832 }
1833 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1834 mPackageManager = pm;
1835 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001836 }
1837 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001838 CharSequence labelA, labelB;
1839 if (mLabelCache.containsKey(a)) {
1840 labelA = mLabelCache.get(a);
1841 } else {
1842 labelA = a.loadLabel(mPackageManager).toString();
1843
1844 mLabelCache.put(a, labelA);
1845 }
1846 if (mLabelCache.containsKey(b)) {
1847 labelB = mLabelCache.get(b);
1848 } else {
1849 labelB = b.loadLabel(mPackageManager).toString();
1850
1851 mLabelCache.put(b, labelB);
1852 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001853 return sCollator.compare(labelA, labelB);
1854 }
1855 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001856 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1857 private PackageManager mPackageManager;
1858 private HashMap<Object, String> mLabelCache;
1859 WidgetAndShortcutNameComparator(PackageManager pm) {
1860 mPackageManager = pm;
1861 mLabelCache = new HashMap<Object, String>();
1862 }
1863 public final int compare(Object a, Object b) {
1864 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001865 if (mLabelCache.containsKey(a)) {
1866 labelA = mLabelCache.get(a);
1867 } else {
1868 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001869 ((AppWidgetProviderInfo) a).label :
1870 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001871 mLabelCache.put(a, labelA);
1872 }
1873 if (mLabelCache.containsKey(b)) {
1874 labelB = mLabelCache.get(b);
1875 } else {
1876 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001877 ((AppWidgetProviderInfo) b).label :
1878 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001879 mLabelCache.put(b, labelB);
1880 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001881 return sCollator.compare(labelA, labelB);
1882 }
1883 };
Joe Onoratobe386092009-11-17 17:32:16 -08001884
1885 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001886 Log.d(TAG, "mCallbacks=" + mCallbacks);
1887 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1888 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1889 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1890 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001891 if (mLoaderTask != null) {
1892 mLoaderTask.dumpState();
1893 } else {
1894 Log.d(TAG, "mLoaderTask=null");
1895 }
Joe Onoratobe386092009-11-17 17:32:16 -08001896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897}