blob: f14140c16b012657d943655ad4e020cd49e81eab [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;
Winson Chungaac01e12011-08-17 10:37:13 -0700664 private HashMap<Object, byte[]> mDbIconCache;
Joe Onorato36115782010-06-17 13:28:48 -0400665
666 LoaderTask(Context context, boolean isLaunching) {
667 mContext = context;
668 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700669 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chungaac01e12011-08-17 10:37:13 -0700670 mDbIconCache = new HashMap<Object, byte[]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671 }
672
Joe Onorato36115782010-06-17 13:28:48 -0400673 boolean isLaunching() {
674 return mIsLaunching;
675 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676
Joe Onorato36115782010-06-17 13:28:48 -0400677 private void loadAndBindWorkspace() {
678 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400679 if (DEBUG_LOADERS) {
680 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700682
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700683 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400684 loadWorkspace();
685 if (mStopped) {
686 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687 }
Joe Onorato36115782010-06-17 13:28:48 -0400688 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400689 }
690
Joe Onorato36115782010-06-17 13:28:48 -0400691 // Bind the workspace
692 bindWorkspace();
693 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400694
Joe Onorato36115782010-06-17 13:28:48 -0400695 private void waitForIdle() {
696 // Wait until the either we're stopped or the other threads are done.
697 // This way we don't start loading all apps until the workspace has settled
698 // down.
699 synchronized (LoaderTask.this) {
700 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700701
Joe Onorato36115782010-06-17 13:28:48 -0400702 mHandler.postIdle(new Runnable() {
703 public void run() {
704 synchronized (LoaderTask.this) {
705 mLoadAndBindStepFinished = true;
706 if (DEBUG_LOADERS) {
707 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400708 }
Joe Onorato36115782010-06-17 13:28:48 -0400709 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400710 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400711 }
Joe Onorato36115782010-06-17 13:28:48 -0400712 });
713
714 while (!mStopped && !mLoadAndBindStepFinished) {
715 try {
716 this.wait();
717 } catch (InterruptedException ex) {
718 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400719 }
720 }
Joe Onorato36115782010-06-17 13:28:48 -0400721 if (DEBUG_LOADERS) {
722 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700723 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400724 + "ms for previous step to finish binding");
725 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400726 }
Joe Onorato36115782010-06-17 13:28:48 -0400727 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400728
Joe Onorato36115782010-06-17 13:28:48 -0400729 public void run() {
730 // Optimize for end-user experience: if the Launcher is up and // running with the
731 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
732 // workspace first (default).
733 final Callbacks cbk = mCallbacks.get();
734 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400735
Winson Chungaac01e12011-08-17 10:37:13 -0700736 // We update the icons in the database afterwards in case they have changed
737 mDbIconCache.clear();
738
Joe Onorato36115782010-06-17 13:28:48 -0400739 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400740 // Elevate priority when Home launches for the first time to avoid
741 // starving at boot time. Staring at a blank home is not cool.
742 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700743 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
744 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400745 android.os.Process.setThreadPriority(mIsLaunching
746 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
747 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400748 if (loadWorkspaceFirst) {
749 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
750 loadAndBindWorkspace();
751 } else {
752 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700753 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400754 }
755
Joe Onorato36115782010-06-17 13:28:48 -0400756 if (mStopped) {
757 break keep_running;
758 }
759
760 // Whew! Hard work done. Slow us down, and wait until the UI thread has
761 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400762 synchronized (mLock) {
763 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700764 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400765 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
766 }
767 }
Joe Onorato36115782010-06-17 13:28:48 -0400768 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400769
770 // second step
771 if (loadWorkspaceFirst) {
772 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700773 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400774 } else {
775 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
776 loadAndBindWorkspace();
777 }
Joe Onorato36115782010-06-17 13:28:48 -0400778 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779
Winson Chungaac01e12011-08-17 10:37:13 -0700780
781 // Update the saved icons if necessary
782 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
783 for (Object key : mDbIconCache.keySet()) {
784 updateSavedIcon(mContext, (ShortcutInfo) key, mDbIconCache.get(key));
785 }
786 mDbIconCache.clear();
787
Joe Onorato36115782010-06-17 13:28:48 -0400788 // Clear out this reference, otherwise we end up holding it until all of the
789 // callback runnables are done.
790 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791
Joe Onorato36115782010-06-17 13:28:48 -0400792 synchronized (mLock) {
793 // If we are still the last one to be scheduled, remove ourselves.
794 if (mLoaderTask == this) {
795 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 }
Joe Onorato36115782010-06-17 13:28:48 -0400797 }
Joe Onorato36115782010-06-17 13:28:48 -0400798 }
799
800 public void stopLocked() {
801 synchronized (LoaderTask.this) {
802 mStopped = true;
803 this.notify();
804 }
805 }
806
807 /**
808 * Gets the callbacks object. If we've been stopped, or if the launcher object
809 * has somehow been garbage collected, return null instead. Pass in the Callbacks
810 * object that was around when the deferred message was scheduled, and if there's
811 * a new Callbacks object around then also return null. This will save us from
812 * calling onto it with data that will be ignored.
813 */
814 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
815 synchronized (mLock) {
816 if (mStopped) {
817 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400818 }
Joe Onorato36115782010-06-17 13:28:48 -0400819
820 if (mCallbacks == null) {
821 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400822 }
Joe Onorato36115782010-06-17 13:28:48 -0400823
824 final Callbacks callbacks = mCallbacks.get();
825 if (callbacks != oldCallbacks) {
826 return null;
827 }
828 if (callbacks == null) {
829 Log.w(TAG, "no mCallbacks");
830 return null;
831 }
832
833 return callbacks;
834 }
835 }
836
837 // check & update map of what's occupied; used to discard overlapping/invalid items
838 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700839 int containerIndex = item.screen;
840 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
841 // We use the last index to refer to the hotseat
842 containerIndex = Launcher.SCREEN_COUNT;
843 // Return early if we detect that an item is under the hotseat button
844 if (Hotseat.isAllAppsButtonRank(item.screen)) {
845 return false;
846 }
847 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
848 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400849 return true;
850 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700851
Joe Onorato36115782010-06-17 13:28:48 -0400852 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
853 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700854 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400855 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700856 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400857 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700858 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700859 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400860 return false;
861 }
862 }
863 }
864 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
865 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700866 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400867 }
868 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700869
Joe Onorato36115782010-06-17 13:28:48 -0400870 return true;
871 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872
Joe Onorato36115782010-06-17 13:28:48 -0400873 private void loadWorkspace() {
874 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875
Joe Onorato36115782010-06-17 13:28:48 -0400876 final Context context = mContext;
877 final ContentResolver contentResolver = context.getContentResolver();
878 final PackageManager manager = context.getPackageManager();
879 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
880 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400881
Adam Cohen4eac29a2011-07-11 17:53:37 -0700882 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700883 sAppWidgets.clear();
884 sFolders.clear();
885 sItemsIdMap.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800886
Joe Onorato36115782010-06-17 13:28:48 -0400887 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888
Joe Onorato36115782010-06-17 13:28:48 -0400889 final Cursor c = contentResolver.query(
890 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400891
Winson Chungf30ad5f2011-08-08 10:55:42 -0700892 // +1 for the hotseat (it can be larger than the workspace)
Adam Cohend22015c2010-07-26 22:02:18 -0700893 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700894 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895
Joe Onorato36115782010-06-17 13:28:48 -0400896 try {
897 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
898 final int intentIndex = c.getColumnIndexOrThrow
899 (LauncherSettings.Favorites.INTENT);
900 final int titleIndex = c.getColumnIndexOrThrow
901 (LauncherSettings.Favorites.TITLE);
902 final int iconTypeIndex = c.getColumnIndexOrThrow(
903 LauncherSettings.Favorites.ICON_TYPE);
904 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
905 final int iconPackageIndex = c.getColumnIndexOrThrow(
906 LauncherSettings.Favorites.ICON_PACKAGE);
907 final int iconResourceIndex = c.getColumnIndexOrThrow(
908 LauncherSettings.Favorites.ICON_RESOURCE);
909 final int containerIndex = c.getColumnIndexOrThrow(
910 LauncherSettings.Favorites.CONTAINER);
911 final int itemTypeIndex = c.getColumnIndexOrThrow(
912 LauncherSettings.Favorites.ITEM_TYPE);
913 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
914 LauncherSettings.Favorites.APPWIDGET_ID);
915 final int screenIndex = c.getColumnIndexOrThrow(
916 LauncherSettings.Favorites.SCREEN);
917 final int cellXIndex = c.getColumnIndexOrThrow
918 (LauncherSettings.Favorites.CELLX);
919 final int cellYIndex = c.getColumnIndexOrThrow
920 (LauncherSettings.Favorites.CELLY);
921 final int spanXIndex = c.getColumnIndexOrThrow
922 (LauncherSettings.Favorites.SPANX);
923 final int spanYIndex = c.getColumnIndexOrThrow(
924 LauncherSettings.Favorites.SPANY);
925 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
926 final int displayModeIndex = c.getColumnIndexOrThrow(
927 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928
Joe Onorato36115782010-06-17 13:28:48 -0400929 ShortcutInfo info;
930 String intentDescription;
931 LauncherAppWidgetInfo appWidgetInfo;
932 int container;
933 long id;
934 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400935
Joe Onorato36115782010-06-17 13:28:48 -0400936 while (!mStopped && c.moveToNext()) {
937 try {
938 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939
Joe Onorato36115782010-06-17 13:28:48 -0400940 switch (itemType) {
941 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
942 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
943 intentDescription = c.getString(intentIndex);
944 try {
945 intent = Intent.parseUri(intentDescription, 0);
946 } catch (URISyntaxException e) {
947 continue;
948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949
Joe Onorato36115782010-06-17 13:28:48 -0400950 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
951 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -0700952 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -0400953 } else {
954 info = getShortcutInfo(c, context, iconTypeIndex,
955 iconPackageIndex, iconResourceIndex, iconIndex,
956 titleIndex);
957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958
Joe Onorato36115782010-06-17 13:28:48 -0400959 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400960 info.intent = intent;
961 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400962 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400963 info.container = container;
964 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700965 info.cellX = c.getInt(cellXIndex);
966 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967
Daniel Sandler8802e962010-05-26 16:28:16 -0400968 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400969 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400970 break;
971 }
972
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400974 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -0700975 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -0700976 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -0400977 break;
978 default:
979 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -0700980 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -0400982 folderInfo.add(info);
983 break;
984 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700985 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -0800986
987 // now that we've loaded everthing re-save it with the
988 // icon in case it disappears somehow.
Winson Chungaac01e12011-08-17 10:37:13 -0700989 queueIconToBeChecked(mDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400990 } else {
991 // Failed to load the shortcut, probably because the
992 // activity manager couldn't resolve it (maybe the app
993 // was uninstalled), or the db row was somehow screwed up.
994 // Delete it.
995 id = c.getLong(idIndex);
996 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
997 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
998 id, false), null, null);
999 }
1000 break;
1001
Adam Cohendf2cc412011-04-27 16:56:57 -07001002 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001003 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001004 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001005
Winson Chungaafa03c2010-06-11 17:34:16 -07001006 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001007 folderInfo.id = id;
1008 container = c.getInt(containerIndex);
1009 folderInfo.container = container;
1010 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001011 folderInfo.cellX = c.getInt(cellXIndex);
1012 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001013
1014 // check & update map of what's occupied
1015 if (!checkItemPlacement(occupied, folderInfo)) {
1016 break;
1017 }
Joe Onorato36115782010-06-17 13:28:48 -04001018 switch (container) {
1019 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001020 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001021 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001022 break;
1023 }
1024
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001025 sItemsIdMap.put(folderInfo.id, folderInfo);
1026 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001027 break;
1028
Joe Onorato36115782010-06-17 13:28:48 -04001029 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1030 // Read all Launcher-specific widget details
1031 int appWidgetId = c.getInt(appWidgetIdIndex);
1032 id = c.getLong(idIndex);
1033
1034 final AppWidgetProviderInfo provider =
1035 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001036
Joe Onorato36115782010-06-17 13:28:48 -04001037 if (!isSafeMode && (provider == null || provider.provider == null ||
1038 provider.provider.getPackageName() == null)) {
1039 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
1040 + id + " appWidgetId=" + appWidgetId);
1041 itemsToRemove.add(id);
1042 } else {
1043 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
1044 appWidgetInfo.id = id;
1045 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001046 appWidgetInfo.cellX = c.getInt(cellXIndex);
1047 appWidgetInfo.cellY = c.getInt(cellYIndex);
1048 appWidgetInfo.spanX = c.getInt(spanXIndex);
1049 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001050
1051 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001052 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1053 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001054 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001055 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001056 continue;
1057 }
1058 appWidgetInfo.container = c.getInt(containerIndex);
1059
1060 // check & update map of what's occupied
1061 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1062 break;
1063 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001064 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1065 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001066 }
1067 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001068 }
Joe Onorato36115782010-06-17 13:28:48 -04001069 } catch (Exception e) {
1070 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001071 }
1072 }
Joe Onorato36115782010-06-17 13:28:48 -04001073 } finally {
1074 c.close();
1075 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001076
Joe Onorato36115782010-06-17 13:28:48 -04001077 if (itemsToRemove.size() > 0) {
1078 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1079 LauncherSettings.Favorites.CONTENT_URI);
1080 // Remove dead items
1081 for (long id : itemsToRemove) {
1082 if (DEBUG_LOADERS) {
1083 Log.d(TAG, "Removed id = " + id);
1084 }
1085 // Don't notify content observers
1086 try {
1087 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1088 null, null);
1089 } catch (RemoteException e) {
1090 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001091 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001092 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 }
1094
Joe Onorato36115782010-06-17 13:28:48 -04001095 if (DEBUG_LOADERS) {
1096 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1097 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001098 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001099 String line = "";
1100 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1101 if (s > 0) {
1102 line += " | ";
1103 }
Adam Cohend22015c2010-07-26 22:02:18 -07001104 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001105 line += ((occupied[s][x][y] != null) ? "#" : ".");
1106 }
1107 }
1108 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001109 }
Joe Onorato36115782010-06-17 13:28:48 -04001110 }
1111 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112
Joe Onorato36115782010-06-17 13:28:48 -04001113 /**
1114 * Read everything out of our database.
1115 */
1116 private void bindWorkspace() {
1117 final long t = SystemClock.uptimeMillis();
1118
1119 // Don't use these two variables in any of the callback runnables.
1120 // Otherwise we hold a reference to them.
1121 final Callbacks oldCallbacks = mCallbacks.get();
1122 if (oldCallbacks == null) {
1123 // This launcher has exited and nobody bothered to tell us. Just bail.
1124 Log.w(TAG, "LoaderTask running with no launcher");
1125 return;
1126 }
1127
1128 int N;
1129 // Tell the workspace that we're about to start firing items at it
1130 mHandler.post(new Runnable() {
1131 public void run() {
1132 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1133 if (callbacks != null) {
1134 callbacks.startBinding();
1135 }
1136 }
1137 });
1138 // Add the items to the workspace.
Adam Cohen4eac29a2011-07-11 17:53:37 -07001139 N = sWorkspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001140 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1141 final int start = i;
1142 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 mHandler.post(new Runnable() {
1144 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001145 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001146 if (callbacks != null) {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001147 callbacks.bindItems(sWorkspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148 }
1149 }
1150 });
Joe Onorato36115782010-06-17 13:28:48 -04001151 }
1152 mHandler.post(new Runnable() {
1153 public void run() {
1154 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1155 if (callbacks != null) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001156 callbacks.bindFolders(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001157 }
1158 }
1159 });
1160 // Wait until the queue goes empty.
1161 mHandler.post(new Runnable() {
1162 public void run() {
1163 if (DEBUG_LOADERS) {
1164 Log.d(TAG, "Going to start binding widgets soon.");
1165 }
1166 }
1167 });
1168 // Bind the widgets, one at a time.
1169 // WARNING: this is calling into the workspace from the background thread,
1170 // but since getCurrentScreen() just returns the int, we should be okay. This
1171 // is just a hint for the order, and if it's wrong, we'll be okay.
1172 // TODO: instead, we should have that push the current screen into here.
1173 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001174 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001175 // once for the current screen
1176 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001177 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001178 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001179 mHandler.post(new Runnable() {
1180 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001181 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001183 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184 }
1185 }
1186 });
1187 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 }
Joe Onorato36115782010-06-17 13:28:48 -04001189 // once for the other screens
1190 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001191 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001192 if (widget.screen != currentScreen) {
1193 mHandler.post(new Runnable() {
1194 public void run() {
1195 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1196 if (callbacks != null) {
1197 callbacks.bindAppWidget(widget);
1198 }
1199 }
1200 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001201 }
1202 }
Joe Onorato36115782010-06-17 13:28:48 -04001203 // Tell the workspace that we're done.
1204 mHandler.post(new Runnable() {
1205 public void run() {
1206 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1207 if (callbacks != null) {
1208 callbacks.finishBindingItems();
1209 }
1210 }
1211 });
1212 // If we're profiling, this is the last thing in the queue.
1213 mHandler.post(new Runnable() {
1214 public void run() {
1215 if (DEBUG_LOADERS) {
1216 Log.d(TAG, "bound workspace in "
1217 + (SystemClock.uptimeMillis()-t) + "ms");
1218 }
1219 }
1220 });
1221 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001222
Joe Onorato36115782010-06-17 13:28:48 -04001223 private void loadAndBindAllApps() {
1224 if (DEBUG_LOADERS) {
1225 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1226 }
1227 if (!mAllAppsLoaded) {
1228 loadAllAppsByBatch();
1229 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001230 return;
1231 }
Joe Onorato36115782010-06-17 13:28:48 -04001232 mAllAppsLoaded = true;
1233 } else {
1234 onlyBindAllApps();
1235 }
1236 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001237
Joe Onorato36115782010-06-17 13:28:48 -04001238 private void onlyBindAllApps() {
1239 final Callbacks oldCallbacks = mCallbacks.get();
1240 if (oldCallbacks == null) {
1241 // This launcher has exited and nobody bothered to tell us. Just bail.
1242 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1243 return;
1244 }
1245
1246 // shallow copy
1247 final ArrayList<ApplicationInfo> list
1248 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1249 mHandler.post(new Runnable() {
1250 public void run() {
1251 final long t = SystemClock.uptimeMillis();
1252 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1253 if (callbacks != null) {
1254 callbacks.bindAllApplications(list);
1255 }
1256 if (DEBUG_LOADERS) {
1257 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1258 + (SystemClock.uptimeMillis()-t) + "ms");
1259 }
1260 }
1261 });
1262
1263 }
1264
1265 private void loadAllAppsByBatch() {
1266 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1267
1268 // Don't use these two variables in any of the callback runnables.
1269 // Otherwise we hold a reference to them.
1270 final Callbacks oldCallbacks = mCallbacks.get();
1271 if (oldCallbacks == null) {
1272 // This launcher has exited and nobody bothered to tell us. Just bail.
1273 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1274 return;
1275 }
1276
1277 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1278 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1279
1280 final PackageManager packageManager = mContext.getPackageManager();
1281 List<ResolveInfo> apps = null;
1282
1283 int N = Integer.MAX_VALUE;
1284
1285 int startIndex;
1286 int i=0;
1287 int batchSize = -1;
1288 while (i < N && !mStopped) {
1289 if (i == 0) {
1290 mAllAppsList.clear();
1291 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1292 apps = packageManager.queryIntentActivities(mainIntent, 0);
1293 if (DEBUG_LOADERS) {
1294 Log.d(TAG, "queryIntentActivities took "
1295 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1296 }
1297 if (apps == null) {
1298 return;
1299 }
1300 N = apps.size();
1301 if (DEBUG_LOADERS) {
1302 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1303 }
1304 if (N == 0) {
1305 // There are no apps?!?
1306 return;
1307 }
1308 if (mBatchSize == 0) {
1309 batchSize = N;
1310 } else {
1311 batchSize = mBatchSize;
1312 }
1313
1314 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1315 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001316 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001317 if (DEBUG_LOADERS) {
1318 Log.d(TAG, "sort took "
1319 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1320 }
1321 }
1322
1323 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1324
1325 startIndex = i;
1326 for (int j=0; i<N && j<batchSize; j++) {
1327 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001328 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
1329 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001330 i++;
1331 }
1332
1333 final boolean first = i <= batchSize;
1334 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1335 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1336 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1337
Joe Onoratocc67f472010-06-08 10:54:30 -07001338 mHandler.post(new Runnable() {
1339 public void run() {
1340 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001341 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001342 if (first) {
1343 callbacks.bindAllApplications(added);
1344 } else {
1345 callbacks.bindAppsAdded(added);
1346 }
1347 if (DEBUG_LOADERS) {
1348 Log.d(TAG, "bound " + added.size() + " apps in "
1349 + (SystemClock.uptimeMillis() - t) + "ms");
1350 }
1351 } else {
1352 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001353 }
1354 }
1355 });
1356
Daniel Sandlerdca66122010-04-13 16:23:58 -04001357 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001358 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1359 + (SystemClock.uptimeMillis()-t2) + "ms");
1360 }
1361
1362 if (mAllAppsLoadDelay > 0 && i < N) {
1363 try {
1364 if (DEBUG_LOADERS) {
1365 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1366 }
1367 Thread.sleep(mAllAppsLoadDelay);
1368 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 }
1370 }
1371
Joe Onorato36115782010-06-17 13:28:48 -04001372 if (DEBUG_LOADERS) {
1373 Log.d(TAG, "cached all " + N + " apps in "
1374 + (SystemClock.uptimeMillis()-t) + "ms"
1375 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001376 }
1377 }
1378
1379 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001380 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1381 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1382 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1383 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1384 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001385 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001386 }
1387 }
1388
1389 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001390 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001391 }
1392
1393 private class PackageUpdatedTask implements Runnable {
1394 int mOp;
1395 String[] mPackages;
1396
1397 public static final int OP_NONE = 0;
1398 public static final int OP_ADD = 1;
1399 public static final int OP_UPDATE = 2;
1400 public static final int OP_REMOVE = 3; // uninstlled
1401 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1402
1403
1404 public PackageUpdatedTask(int op, String[] packages) {
1405 mOp = op;
1406 mPackages = packages;
1407 }
1408
1409 public void run() {
1410 final Context context = mApp;
1411
1412 final String[] packages = mPackages;
1413 final int N = packages.length;
1414 switch (mOp) {
1415 case OP_ADD:
1416 for (int i=0; i<N; i++) {
1417 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1418 mAllAppsList.addPackage(context, packages[i]);
1419 }
1420 break;
1421 case OP_UPDATE:
1422 for (int i=0; i<N; i++) {
1423 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1424 mAllAppsList.updatePackage(context, packages[i]);
1425 }
1426 break;
1427 case OP_REMOVE:
1428 case OP_UNAVAILABLE:
1429 for (int i=0; i<N; i++) {
1430 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1431 mAllAppsList.removePackage(packages[i]);
1432 }
1433 break;
1434 }
1435
1436 ArrayList<ApplicationInfo> added = null;
1437 ArrayList<ApplicationInfo> removed = null;
1438 ArrayList<ApplicationInfo> modified = null;
1439
1440 if (mAllAppsList.added.size() > 0) {
1441 added = mAllAppsList.added;
1442 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1443 }
1444 if (mAllAppsList.removed.size() > 0) {
1445 removed = mAllAppsList.removed;
1446 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1447 for (ApplicationInfo info: removed) {
1448 mIconCache.remove(info.intent.getComponent());
1449 }
1450 }
1451 if (mAllAppsList.modified.size() > 0) {
1452 modified = mAllAppsList.modified;
1453 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1454 }
1455
1456 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1457 if (callbacks == null) {
1458 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1459 return;
1460 }
1461
1462 if (added != null) {
1463 final ArrayList<ApplicationInfo> addedFinal = added;
1464 mHandler.post(new Runnable() {
1465 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001466 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1467 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001468 callbacks.bindAppsAdded(addedFinal);
1469 }
1470 }
1471 });
1472 }
1473 if (modified != null) {
1474 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1475 mHandler.post(new Runnable() {
1476 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001477 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1478 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001479 callbacks.bindAppsUpdated(modifiedFinal);
1480 }
1481 }
1482 });
1483 }
1484 if (removed != null) {
1485 final boolean permanent = mOp != OP_UNAVAILABLE;
1486 final ArrayList<ApplicationInfo> removedFinal = removed;
1487 mHandler.post(new Runnable() {
1488 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001489 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1490 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001491 callbacks.bindAppsRemoved(removedFinal, permanent);
1492 }
1493 }
1494 });
Joe Onoratobe386092009-11-17 17:32:16 -08001495 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001496
1497 mHandler.post(new Runnable() {
1498 @Override
1499 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001500 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1501 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001502 callbacks.bindPackagesUpdated();
1503 }
1504 }
1505 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001506 }
1507 }
1508
1509 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001510 * This is called from the code that adds shortcuts from the intent receiver. This
1511 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 */
Joe Onorato56d82912010-03-07 14:32:10 -05001513 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001514 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001515 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001516
Joe Onorato56d82912010-03-07 14:32:10 -05001517 /**
1518 * Make an ShortcutInfo object for a shortcut that is an application.
1519 *
1520 * If c is not null, then it will be used to fill in missing data like the title and icon.
1521 */
1522 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001523 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001524 Bitmap icon = null;
1525 final ShortcutInfo info = new ShortcutInfo();
1526
1527 ComponentName componentName = intent.getComponent();
1528 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001529 return null;
1530 }
1531
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001532 // TODO: See if the PackageManager knows about this case. If it doesn't
1533 // then return null & delete this.
1534
Joe Onorato56d82912010-03-07 14:32:10 -05001535 // the resource -- This may implicitly give us back the fallback icon,
1536 // but don't worry about that. All we're doing with usingFallbackIcon is
1537 // to avoid saving lots of copies of that in the database, and most apps
1538 // have icons anyway.
1539 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1540 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001541 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001542 }
Joe Onorato56d82912010-03-07 14:32:10 -05001543 // the db
1544 if (icon == null) {
1545 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001546 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001547 }
1548 }
1549 // the fallback icon
1550 if (icon == null) {
1551 icon = getFallbackIcon();
1552 info.usingFallbackIcon = true;
1553 }
1554 info.setIcon(icon);
1555
1556 // from the resource
1557 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001558 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1559 if (labelCache != null && labelCache.containsKey(key)) {
1560 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001561 } else {
1562 info.title = resolveInfo.activityInfo.loadLabel(manager);
1563 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001564 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001565 }
1566 }
Joe Onorato56d82912010-03-07 14:32:10 -05001567 }
1568 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001570 if (c != null) {
1571 info.title = c.getString(titleIndex);
1572 }
1573 }
1574 // fall back to the class name of the activity
1575 if (info.title == null) {
1576 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001577 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001578 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1579 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001583 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001585 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001586 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1587 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588
Joe Onorato56d82912010-03-07 14:32:10 -05001589 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001590 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001591 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001593 // TODO: If there's an explicit component and we can't install that, delete it.
1594
Joe Onorato56d82912010-03-07 14:32:10 -05001595 info.title = c.getString(titleIndex);
1596
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597 int iconType = c.getInt(iconTypeIndex);
1598 switch (iconType) {
1599 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1600 String packageName = c.getString(iconPackageIndex);
1601 String resourceName = c.getString(iconResourceIndex);
1602 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001603 info.customIcon = false;
1604 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001606 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001607 if (resources != null) {
1608 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001609 icon = Utilities.createIconBitmap(
1610 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001611 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001612 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001613 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 }
Joe Onorato56d82912010-03-07 14:32:10 -05001615 // the db
1616 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001617 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001618 }
1619 // the fallback icon
1620 if (icon == null) {
1621 icon = getFallbackIcon();
1622 info.usingFallbackIcon = true;
1623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624 break;
1625 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001626 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001627 if (icon == null) {
1628 icon = getFallbackIcon();
1629 info.customIcon = false;
1630 info.usingFallbackIcon = true;
1631 } else {
1632 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001633 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001634 break;
1635 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001636 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001637 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638 info.customIcon = false;
1639 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001641 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001642 return info;
1643 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644
Michael Jurka931dc972011-08-05 15:08:15 -07001645 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001646 if (false) {
1647 Log.d(TAG, "getIconFromCursor app="
1648 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1649 }
1650 byte[] data = c.getBlob(iconIndex);
1651 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001652 return Utilities.createIconBitmap(
1653 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001654 } catch (Exception e) {
1655 return null;
1656 }
1657 }
1658
Winson Chung3d503fb2011-07-13 17:25:49 -07001659 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1660 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001661 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001662 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001663
1664 return info;
1665 }
1666
Winson Chunga9abd0e2010-10-27 17:18:37 -07001667 /**
Winson Chung55cef262010-10-28 14:14:18 -07001668 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1669 */
1670 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1671 ComponentName component) {
1672 List<AppWidgetProviderInfo> widgets =
1673 AppWidgetManager.getInstance(context).getInstalledProviders();
1674 for (AppWidgetProviderInfo info : widgets) {
1675 if (info.provider.equals(component)) {
1676 return info;
1677 }
1678 }
1679 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001680 }
1681
Winson Chung68846fd2010-10-29 11:00:27 -07001682 /**
1683 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1684 */
1685 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1686 final PackageManager packageManager = context.getPackageManager();
1687 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1688 new ArrayList<WidgetMimeTypeHandlerData>();
1689
1690 final Intent supportsIntent =
1691 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1692 supportsIntent.setType(mimeType);
1693
1694 // Create a set of widget configuration components that we can test against
1695 final List<AppWidgetProviderInfo> widgets =
1696 AppWidgetManager.getInstance(context).getInstalledProviders();
1697 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1698 new HashMap<ComponentName, AppWidgetProviderInfo>();
1699 for (AppWidgetProviderInfo info : widgets) {
1700 configurationComponentToWidget.put(info.configure, info);
1701 }
1702
1703 // Run through each of the intents that can handle this type of clip data, and cross
1704 // reference them with the components that are actual configuration components
1705 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1706 PackageManager.MATCH_DEFAULT_ONLY);
1707 for (ResolveInfo info : activities) {
1708 final ActivityInfo activityInfo = info.activityInfo;
1709 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1710 activityInfo.name);
1711 if (configurationComponentToWidget.containsKey(infoComponent)) {
1712 supportedConfigurationActivities.add(
1713 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1714 configurationComponentToWidget.get(infoComponent)));
1715 }
1716 }
1717 return supportedConfigurationActivities;
1718 }
1719
Winson Chunga9abd0e2010-10-27 17:18:37 -07001720 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001721 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1722 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1723 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1724
1725 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001726 boolean customIcon = false;
1727 ShortcutIconResource iconResource = null;
1728
1729 if (bitmap != null && bitmap instanceof Bitmap) {
1730 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001731 customIcon = true;
1732 } else {
1733 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1734 if (extra != null && extra instanceof ShortcutIconResource) {
1735 try {
1736 iconResource = (ShortcutIconResource) extra;
1737 final PackageManager packageManager = context.getPackageManager();
1738 Resources resources = packageManager.getResourcesForApplication(
1739 iconResource.packageName);
1740 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001741 icon = Utilities.createIconBitmap(
1742 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001743 } catch (Exception e) {
1744 Log.w(TAG, "Could not load shortcut icon: " + extra);
1745 }
1746 }
1747 }
1748
Joe Onorato0589f0f2010-02-08 13:44:00 -08001749 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001750
1751 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001752 if (fallbackIcon != null) {
1753 icon = fallbackIcon;
1754 } else {
1755 icon = getFallbackIcon();
1756 info.usingFallbackIcon = true;
1757 }
Joe Onorato56d82912010-03-07 14:32:10 -05001758 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001759 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001760
Joe Onorato0589f0f2010-02-08 13:44:00 -08001761 info.title = name;
1762 info.intent = intent;
1763 info.customIcon = customIcon;
1764 info.iconResource = iconResource;
1765
1766 return info;
1767 }
1768
Winson Chungaac01e12011-08-17 10:37:13 -07001769 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1770 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001771 // If apps can't be on SD, don't even bother.
1772 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001773 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001774 }
Joe Onorato56d82912010-03-07 14:32:10 -05001775 // If this icon doesn't have a custom icon, check to see
1776 // what's stored in the DB, and if it doesn't match what
1777 // we're going to show, store what we are going to show back
1778 // into the DB. We do this so when we're loading, if the
1779 // package manager can't find an icon (for example because
1780 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001781 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001782 cache.put(info, c.getBlob(iconIndex));
1783 return true;
1784 }
1785 return false;
1786 }
1787 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1788 boolean needSave = false;
1789 try {
1790 if (data != null) {
1791 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1792 Bitmap loaded = info.getIcon(mIconCache);
1793 needSave = !saved.sameAs(loaded);
1794 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001795 needSave = true;
1796 }
Winson Chungaac01e12011-08-17 10:37:13 -07001797 } catch (Exception e) {
1798 needSave = true;
1799 }
1800 if (needSave) {
1801 Log.d(TAG, "going to save icon bitmap for info=" + info);
1802 // This is slower than is ideal, but this only happens once
1803 // or when the app is updated with a new icon.
1804 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001805 }
1806 }
1807
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001809 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001810 * or make a new one.
1811 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001812 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001813 // See if a placeholder was created for us already
1814 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001815 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 // No placeholder -- create a new instance
Adam Cohendf2cc412011-04-27 16:56:57 -07001817 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 folders.put(id, folderInfo);
1819 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001820 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
1822
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001824 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001825 = new Comparator<ApplicationInfo>() {
1826 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001827 int result = sCollator.compare(a.title.toString(), b.title.toString());
1828 if (result == 0) {
1829 result = a.componentName.compareTo(b.componentName);
1830 }
1831 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833 };
Winson Chung78403fe2011-01-21 15:38:02 -08001834 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1835 = new Comparator<ApplicationInfo>() {
1836 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1837 if (a.firstInstallTime < b.firstInstallTime) return 1;
1838 if (a.firstInstallTime > b.firstInstallTime) return -1;
1839 return 0;
1840 }
1841 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001842 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1843 = new Comparator<AppWidgetProviderInfo>() {
1844 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1845 return sCollator.compare(a.label.toString(), b.label.toString());
1846 }
1847 };
Winson Chung5308f242011-08-18 12:12:41 -07001848 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1849 if (info.activityInfo != null) {
1850 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1851 } else {
1852 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1853 }
1854 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001855 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1856 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001857 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001858 ShortcutNameComparator(PackageManager pm) {
1859 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001860 mLabelCache = new HashMap<Object, CharSequence>();
1861 }
1862 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1863 mPackageManager = pm;
1864 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001865 }
1866 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001867 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001868 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1869 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1870 if (mLabelCache.containsKey(keyA)) {
1871 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001872 } else {
1873 labelA = a.loadLabel(mPackageManager).toString();
1874
Winson Chung5308f242011-08-18 12:12:41 -07001875 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001876 }
Winson Chung5308f242011-08-18 12:12:41 -07001877 if (mLabelCache.containsKey(keyB)) {
1878 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001879 } else {
1880 labelB = b.loadLabel(mPackageManager).toString();
1881
Winson Chung5308f242011-08-18 12:12:41 -07001882 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001883 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001884 return sCollator.compare(labelA, labelB);
1885 }
1886 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001887 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1888 private PackageManager mPackageManager;
1889 private HashMap<Object, String> mLabelCache;
1890 WidgetAndShortcutNameComparator(PackageManager pm) {
1891 mPackageManager = pm;
1892 mLabelCache = new HashMap<Object, String>();
1893 }
1894 public final int compare(Object a, Object b) {
1895 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001896 if (mLabelCache.containsKey(a)) {
1897 labelA = mLabelCache.get(a);
1898 } else {
1899 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001900 ((AppWidgetProviderInfo) a).label :
1901 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001902 mLabelCache.put(a, labelA);
1903 }
1904 if (mLabelCache.containsKey(b)) {
1905 labelB = mLabelCache.get(b);
1906 } else {
1907 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001908 ((AppWidgetProviderInfo) b).label :
1909 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07001910 mLabelCache.put(b, labelB);
1911 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001912 return sCollator.compare(labelA, labelB);
1913 }
1914 };
Joe Onoratobe386092009-11-17 17:32:16 -08001915
1916 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001917 Log.d(TAG, "mCallbacks=" + mCallbacks);
1918 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1919 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1920 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1921 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001922 if (mLoaderTask != null) {
1923 mLoaderTask.dumpState();
1924 } else {
1925 Log.d(TAG, "mLoaderTask=null");
1926 }
Joe Onoratobe386092009-11-17 17:32:16 -08001927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928}