blob: ba5370dfa6fd968b8a18fe2950ca44fb6a4ab631 [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;
Adam Cohen00fcb492011-11-02 21:53:47 -070031import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070035import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Resources;
37import android.database.Cursor;
38import android.graphics.Bitmap;
39import android.graphics.BitmapFactory;
40import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080041import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040042import android.os.Handler;
43import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040047import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Winson Chung68846fd2010-10-29 11:00:27 -070050import com.android.launcher.R;
51import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080052
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.lang.ref.WeakReference;
54import java.net.URISyntaxException;
55import java.text.Collator;
56import java.util.ArrayList;
57import java.util.Collections;
58import java.util.Comparator;
59import java.util.HashMap;
60import java.util.List;
Michael Jurkac2f801e2011-07-12 14:19:46 -070061
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062/**
63 * Maintains in-memory state of the Launcher. It is expected that there should be only one
64 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070065 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040067public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080068 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040069 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070070
Joe Onorato36115782010-06-17 13:28:48 -040071 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080072 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040073 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040074 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040075
Joe Onoratof99f8c12009-10-31 17:27:36 -040076 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040077 private final Object mLock = new Object();
78 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040079 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070081 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
82 static {
83 sWorkerThread.start();
84 }
85 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
86
Joe Onoratocc67f472010-06-08 10:54:30 -070087 // We start off with everything not loaded. After that, we assume that
88 // our monitoring of the package manager provides all updates and we never
89 // need to do a requery. These are only ever touched from the loader thread.
90 private boolean mWorkspaceLoaded;
91 private boolean mAllAppsLoaded;
92
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Michael Jurkaa8c760d2011-04-28 14:59:33 -070095 // < only access in worker thread >
96 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080097
Michael Jurkaa8c760d2011-04-28 14:59:33 -070098 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
99 // LauncherModel to their ids
100 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
101
102 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
103 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
104 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700105 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700106
107 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
108 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
109 new ArrayList<LauncherAppWidgetInfo>();
110
111 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
112 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700113
114 // sDbIconCache is the set of ItemInfos that need to have their icons updated in the database
115 static final HashMap<Object, byte[]> sDbIconCache = new HashMap<Object, byte[]>();
116
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700117 // </ only access in worker thread >
118
119 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800120 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Adam Cohend22015c2010-07-26 22:02:18 -0700122 private static int mCellCountX;
123 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700124
Reena Lee99a73f32011-10-24 17:27:37 -0700125 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700126
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700128 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400129 public int getCurrentWorkspaceScreen();
130 public void startBinding();
131 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500132 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400133 public void finishBindingItems();
134 public void bindAppWidget(LauncherAppWidgetInfo info);
135 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500136 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
137 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400138 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700139 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400140 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100142 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800146 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400147 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 mAllAppsList = new AllAppsList(iconCache);
149 mIconCache = iconCache;
150
151 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700152 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400153
Reena Lee93f824a2011-09-23 17:20:28 -0700154 final Resources res = app.getResources();
155 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
156 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700157 Configuration config = res.getConfiguration();
158 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800159 }
160
Joe Onorato56d82912010-03-07 14:32:10 -0500161 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800162 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chung603bcb92011-09-02 11:45:39 -0700165 public void unbindWorkspaceItems() {
166 sWorker.post(new Runnable() {
167 @Override
168 public void run() {
169 unbindWorkspaceItemsOnMainThread();
170 }
171 });
172 }
173
174 /** Unbinds all the sWorkspaceItems on the main thread, and return a copy of sWorkspaceItems
175 * that is save to reference from the main thread. */
176 private ArrayList<ItemInfo> unbindWorkspaceItemsOnMainThread() {
177 // Ensure that we don't use the same workspace items data structure on the main thread
178 // by making a copy of workspace items first.
179 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(sWorkspaceItems);
Michael Jurka05bf644e2011-11-30 20:28:53 -0800180 final ArrayList<ItemInfo> appWidgets = new ArrayList<ItemInfo>(sAppWidgets);
Winson Chung603bcb92011-09-02 11:45:39 -0700181 mHandler.post(new Runnable() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700182 @Override
Winson Chung603bcb92011-09-02 11:45:39 -0700183 public void run() {
184 for (ItemInfo item : workspaceItems) {
185 item.unbind();
186 }
Michael Jurka05bf644e2011-11-30 20:28:53 -0800187 for (ItemInfo item : appWidgets) {
188 item.unbind();
189 }
Winson Chung603bcb92011-09-02 11:45:39 -0700190 }
191 });
192
193 return workspaceItems;
Adam Cohen4eac29a2011-07-11 17:53:37 -0700194 }
195
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 /**
197 * Adds an item to the DB if it was not created previously, or move it to a new
198 * <container, screen, cellX, cellY>
199 */
200 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
201 int screen, int cellX, int cellY) {
202 if (item.container == ItemInfo.NO_ID) {
203 // From all apps
204 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
205 } else {
206 // From somewhere else
207 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 }
209 }
210
Michael Jurkac9d95c52011-08-29 14:03:34 -0700211 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
212 final ItemInfo item, final String callingFunction) {
213 final long itemId = item.id;
214 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
215 final ContentResolver cr = context.getContentResolver();
216
217 Runnable r = new Runnable() {
218 public void run() {
219 cr.update(uri, values, null, null);
220
221 ItemInfo modelItem = sItemsIdMap.get(itemId);
222 if (item != modelItem) {
223 // the modelItem needs to match up perfectly with item if our model is to be
224 // consistent with the database-- for now, just require modelItem == item
225 String msg = "item: " + ((item != null) ? item.toString() : "null") +
226 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
227 "Error: ItemInfo passed to " + callingFunction + " doesn't match original";
228 throw new RuntimeException(msg);
229 }
230
231 // Items are added/removed from the corresponding FolderInfo elsewhere, such
232 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
233 // that are on the desktop, as appropriate
234 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
235 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
236 if (!sWorkspaceItems.contains(modelItem)) {
237 sWorkspaceItems.add(modelItem);
238 }
239 } else {
240 sWorkspaceItems.remove(modelItem);
241 }
242 }
243 };
244
245 if (sWorkerThread.getThreadId() == Process.myTid()) {
246 r.run();
247 } else {
248 sWorker.post(r);
249 }
250 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700254 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700255 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
256 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258 item.cellX = cellX;
259 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700260
Winson Chung3d503fb2011-07-13 17:25:49 -0700261 // We store hotseat items in canonical form which is this orientation invariant position
262 // in the hotseat
263 if (context instanceof Launcher && screen < 0 &&
264 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
265 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
266 } else {
267 item.screen = screen;
268 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
270 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700272 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
273 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
275
Michael Jurkac9d95c52011-08-29 14:03:34 -0700276 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700277 }
278
279 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700280 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800281 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700282 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
283 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) {
284 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800285 item.cellX = cellX;
286 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700287 item.spanX = spanX;
288 item.spanY = spanY;
289
290 // We store hotseat items in canonical form which is this orientation invariant position
291 // in the hotseat
292 if (context instanceof Launcher && screen < 0 &&
293 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
294 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
295 } else {
296 item.screen = screen;
297 }
Adam Cohend4844c32011-02-18 19:25:06 -0800298
Adam Cohend4844c32011-02-18 19:25:06 -0800299 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800300 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700301 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
302 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
303 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
304 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
305 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
Adam Cohend4844c32011-02-18 19:25:06 -0800306
Adam Cohenbebf0422012-04-11 18:06:28 -0700307 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
308 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700309
310 /**
311 * Update an item to the database in a specified container.
312 */
313 static void updateItemInDatabase(Context context, final ItemInfo item) {
314 final ContentValues values = new ContentValues();
315 item.onAddToDatabase(values);
316 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
317 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800318 }
319
320 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 * Returns true if the shortcuts already exists in the database.
322 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400324 static boolean shortcutExists(Context context, String title, Intent intent) {
325 final ContentResolver cr = context.getContentResolver();
326 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
327 new String[] { "title", "intent" }, "title=? and intent=?",
328 new String[] { title, intent.toUri(0) }, null);
329 boolean result = false;
330 try {
331 result = c.moveToFirst();
332 } finally {
333 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400335 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700336 }
337
Joe Onorato9c1289c2009-08-17 11:03:03 -0400338 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700339 * Returns an ItemInfo array containing all the items in the LauncherModel.
340 * The ItemInfo.id is not set through this function.
341 */
342 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
343 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
344 final ContentResolver cr = context.getContentResolver();
345 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
346 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
347 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
348 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
349
350 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
351 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
352 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
353 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
354 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
355 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
356 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
357
358 try {
359 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700360 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700361 item.cellX = c.getInt(cellXIndex);
362 item.cellY = c.getInt(cellYIndex);
363 item.spanX = c.getInt(spanXIndex);
364 item.spanY = c.getInt(spanYIndex);
365 item.container = c.getInt(containerIndex);
366 item.itemType = c.getInt(itemTypeIndex);
367 item.screen = c.getInt(screenIndex);
368
369 items.add(item);
370 }
371 } catch (Exception e) {
372 items.clear();
373 } finally {
374 c.close();
375 }
376
377 return items;
378 }
379
380 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
382 */
383 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
384 final ContentResolver cr = context.getContentResolver();
385 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
386 "_id=? and (itemType=? or itemType=?)",
387 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700388 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700389
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 try {
391 if (c.moveToFirst()) {
392 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
393 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
394 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
395 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
396 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
397 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 FolderInfo folderInfo = null;
400 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700401 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
402 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700404 }
405
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 folderInfo.title = c.getString(titleIndex);
407 folderInfo.id = id;
408 folderInfo.container = c.getInt(containerIndex);
409 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700410 folderInfo.cellX = c.getInt(cellXIndex);
411 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400412
413 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400415 } finally {
416 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700417 }
418
419 return null;
420 }
421
Joe Onorato9c1289c2009-08-17 11:03:03 -0400422 /**
423 * Add an item to the database in a specified container. Sets the container, screen, cellX and
424 * cellY fields of the item. Also assigns an ID to the item.
425 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700426 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
427 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400428 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 item.cellX = cellX;
430 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700431 // We store hotseat items in canonical form which is this orientation invariant position
432 // in the hotseat
433 if (context instanceof Launcher && screen < 0 &&
434 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
435 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
436 } else {
437 item.screen = screen;
438 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439
440 final ContentValues values = new ContentValues();
441 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442 item.onAddToDatabase(values);
443
Michael Jurka7578ec62011-08-03 14:11:54 -0700444 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700445 item.id = app.getLauncherProvider().generateNewId();
446 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700447 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700448
Michael Jurkac9d95c52011-08-29 14:03:34 -0700449 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700450 public void run() {
451 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
452 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400453
Winson Chungb1094bd2011-08-24 16:14:08 -0700454 if (sItemsIdMap.containsKey(item.id)) {
455 // we should not be adding new items in the db with the same id
456 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
457 "addItemToDatabase already exists." + item.toString());
458 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700459 sItemsIdMap.put(item.id, item);
460 switch (item.itemType) {
461 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
462 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700463 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700464 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
465 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700466 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
467 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700468 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700469 }
470 break;
471 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
472 sAppWidgets.add((LauncherAppWidgetInfo) item);
473 break;
474 }
475 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700476 };
477
478 if (sWorkerThread.getThreadId() == Process.myTid()) {
479 r.run();
480 } else {
481 sWorker.post(r);
482 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700483 }
484
Joe Onorato9c1289c2009-08-17 11:03:03 -0400485 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700486 * Creates a new unique child id, for a given cell span across all layouts.
487 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700488 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700489 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
490 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700491 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700492 }
493
Adam Cohend22015c2010-07-26 22:02:18 -0700494 static int getCellCountX() {
495 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700496 }
497
Adam Cohend22015c2010-07-26 22:02:18 -0700498 static int getCellCountY() {
499 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700500 }
501
502 /**
503 * Updates the model orientation helper to take into account the current layout dimensions
504 * when performing local/canonical coordinate transformations.
505 */
506 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700507 mCellCountX = shortAxisCellCount;
508 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700509 }
510
511 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700512 * Removes the specified item from the database
513 * @param context
514 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700516 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700518 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Michael Jurka83df1882011-08-31 20:59:26 -0700519 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700520 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700521 cr.delete(uriToDelete, null, null);
522 switch (item.itemType) {
523 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
524 sFolders.remove(item.id);
525 sWorkspaceItems.remove(item);
526 break;
527 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
528 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
529 sWorkspaceItems.remove(item);
530 break;
531 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
532 sAppWidgets.remove((LauncherAppWidgetInfo) item);
533 break;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700534 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700535 sItemsIdMap.remove(item.id);
536 sDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700537 }
Michael Jurka83df1882011-08-31 20:59:26 -0700538 };
539 if (sWorkerThread.getThreadId() == Process.myTid()) {
540 r.run();
541 } else {
542 sWorker.post(r);
543 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 }
545
546 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 * Remove the contents of the specified folder from the database
548 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700549 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550 final ContentResolver cr = context.getContentResolver();
551
Michael Jurkac9d95c52011-08-29 14:03:34 -0700552 Runnable r = new Runnable() {
553 public void run() {
554 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
555 sItemsIdMap.remove(info.id);
556 sFolders.remove(info.id);
557 sDbIconCache.remove(info);
558 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700559
Michael Jurkac9d95c52011-08-29 14:03:34 -0700560 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
561 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
562 for (ItemInfo childInfo : info.contents) {
563 sItemsIdMap.remove(childInfo.id);
564 sDbIconCache.remove(childInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700565 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700566 }
567 };
568 if (sWorkerThread.getThreadId() == Process.myTid()) {
569 r.run();
570 } else {
571 sWorker.post(r);
572 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 }
574
575 /**
576 * Set this as the current Launcher activity object for the loader.
577 */
578 public void initialize(Callbacks callbacks) {
579 synchronized (mLock) {
580 mCallbacks = new WeakReference<Callbacks>(callbacks);
581 }
582 }
583
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700584 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400585 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
586 * ACTION_PACKAGE_CHANGED.
587 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100588 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400589 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400590 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700591
Joe Onorato36115782010-06-17 13:28:48 -0400592 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400593
Joe Onorato36115782010-06-17 13:28:48 -0400594 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
595 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
596 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
597 final String packageName = intent.getData().getSchemeSpecificPart();
598 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400599
Joe Onorato36115782010-06-17 13:28:48 -0400600 int op = PackageUpdatedTask.OP_NONE;
601
602 if (packageName == null || packageName.length() == 0) {
603 // they sent us a bad intent
604 return;
605 }
606
607 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
608 op = PackageUpdatedTask.OP_UPDATE;
609 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
610 if (!replacing) {
611 op = PackageUpdatedTask.OP_REMOVE;
612 }
613 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
614 // later, we will update the package at this time
615 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
616 if (!replacing) {
617 op = PackageUpdatedTask.OP_ADD;
618 } else {
619 op = PackageUpdatedTask.OP_UPDATE;
620 }
621 }
622
623 if (op != PackageUpdatedTask.OP_NONE) {
624 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
625 }
626
627 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400628 // First, schedule to add these apps back in.
629 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
630 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
631 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700632 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400633 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
634 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
635 enqueuePackageUpdated(new PackageUpdatedTask(
636 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700637 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700638 // If we have changed locale we need to clear out the labels in all apps/workspace.
639 forceReload();
640 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
641 // Check if configuration change was an mcc/mnc change which would affect app resources
642 // and we would need to clear out the labels in all apps/workspace. Same handling as
643 // above for ACTION_LOCALE_CHANGED
644 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -0700645 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -0700646 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -0700647 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -0700648 forceReload();
649 }
650 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -0700651 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700652 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
653 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700654 if (mCallbacks != null) {
655 Callbacks callbacks = mCallbacks.get();
656 if (callbacks != null) {
657 callbacks.bindSearchablesChanged();
658 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700659 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700660 }
661 }
662
Reena Lee93f824a2011-09-23 17:20:28 -0700663 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700664 resetLoadedState(true, true);
665
Reena Lee93f824a2011-09-23 17:20:28 -0700666 // Do this here because if the launcher activity is running it will be restarted.
667 // If it's not running startLoaderFromBackground will merely tell it that it needs
668 // to reload.
669 startLoaderFromBackground();
670 }
671
Winson Chungf0c6ae02012-03-21 16:10:31 -0700672 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
673 synchronized (mLock) {
674 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
675 // mWorkspaceLoaded to true later
676 stopLoaderLocked();
677 if (resetAllAppsLoaded) mAllAppsLoaded = false;
678 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
679 }
680 }
681
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700682 /**
683 * When the launcher is in the background, it's possible for it to miss paired
684 * configuration changes. So whenever we trigger the loader from the background
685 * tell the launcher that it needs to re-run the loader when it comes back instead
686 * of doing it now.
687 */
688 public void startLoaderFromBackground() {
689 boolean runLoader = false;
690 if (mCallbacks != null) {
691 Callbacks callbacks = mCallbacks.get();
692 if (callbacks != null) {
693 // Only actually run the loader if they're not paused.
694 if (!callbacks.setLoadOnResume()) {
695 runLoader = true;
696 }
697 }
698 }
699 if (runLoader) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700700 startLoader(false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700701 }
Joe Onorato36115782010-06-17 13:28:48 -0400702 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400703
Reena Lee93f824a2011-09-23 17:20:28 -0700704 // If there is already a loader task running, tell it to stop.
705 // returns true if isLaunching() was true on the old task
706 private boolean stopLoaderLocked() {
707 boolean isLaunching = false;
708 LoaderTask oldTask = mLoaderTask;
709 if (oldTask != null) {
710 if (oldTask.isLaunching()) {
711 isLaunching = true;
712 }
713 oldTask.stopLocked();
714 }
715 return isLaunching;
716 }
717
Winson Chungf0c6ae02012-03-21 16:10:31 -0700718 public void startLoader(boolean isLaunching) {
Joe Onorato36115782010-06-17 13:28:48 -0400719 synchronized (mLock) {
720 if (DEBUG_LOADERS) {
721 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
722 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723
Joe Onorato36115782010-06-17 13:28:48 -0400724 // Don't bother to start the thread if we know it's not going to do anything
725 if (mCallbacks != null && mCallbacks.get() != null) {
726 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700727 // also, don't downgrade isLaunching if we're already running
728 isLaunching = isLaunching || stopLoaderLocked();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700729 mLoaderTask = new LoaderTask(mApp, isLaunching);
Winson Chung7ed37742011-09-08 15:45:51 -0700730 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700731 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 }
734 }
735
Joe Onorato36115782010-06-17 13:28:48 -0400736 public void stopLoader() {
737 synchronized (mLock) {
738 if (mLoaderTask != null) {
739 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 }
741 }
Joe Onorato36115782010-06-17 13:28:48 -0400742 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743
Michael Jurkac57b7a82011-08-09 22:02:20 -0700744 public boolean isAllAppsLoaded() {
745 return mAllAppsLoaded;
746 }
747
Winson Chung36a62fe2012-05-06 18:04:42 -0700748 boolean isLoadingWorkspace() {
749 synchronized (mLock) {
750 if (mLoaderTask != null) {
751 return mLoaderTask.isLoadingWorkspace();
752 }
753 }
754 return false;
755 }
756
Joe Onorato36115782010-06-17 13:28:48 -0400757 /**
758 * Runnable for the thread that loads the contents of the launcher:
759 * - workspace icons
760 * - widgets
761 * - all apps icons
762 */
763 private class LoaderTask implements Runnable {
764 private Context mContext;
765 private Thread mWaitThread;
766 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -0700767 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400768 private boolean mStopped;
769 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700770 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400771
772 LoaderTask(Context context, boolean isLaunching) {
773 mContext = context;
774 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700775 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 }
777
Joe Onorato36115782010-06-17 13:28:48 -0400778 boolean isLaunching() {
779 return mIsLaunching;
780 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400781
Winson Chung36a62fe2012-05-06 18:04:42 -0700782 boolean isLoadingWorkspace() {
783 return mIsLoadingAndBindingWorkspace;
784 }
785
Joe Onorato36115782010-06-17 13:28:48 -0400786 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700787 mIsLoadingAndBindingWorkspace = true;
788
Joe Onorato36115782010-06-17 13:28:48 -0400789 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400790 if (DEBUG_LOADERS) {
791 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700793
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700794 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400795 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700796 synchronized (LoaderTask.this) {
797 if (mStopped) {
798 return;
799 }
800 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 }
802 }
803
Joe Onorato36115782010-06-17 13:28:48 -0400804 // Bind the workspace
805 bindWorkspace();
806 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400807
Joe Onorato36115782010-06-17 13:28:48 -0400808 private void waitForIdle() {
809 // Wait until the either we're stopped or the other threads are done.
810 // This way we don't start loading all apps until the workspace has settled
811 // down.
812 synchronized (LoaderTask.this) {
813 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700814
Joe Onorato36115782010-06-17 13:28:48 -0400815 mHandler.postIdle(new Runnable() {
816 public void run() {
817 synchronized (LoaderTask.this) {
818 mLoadAndBindStepFinished = true;
819 if (DEBUG_LOADERS) {
820 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400821 }
Joe Onorato36115782010-06-17 13:28:48 -0400822 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400823 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400824 }
Joe Onorato36115782010-06-17 13:28:48 -0400825 });
826
827 while (!mStopped && !mLoadAndBindStepFinished) {
828 try {
829 this.wait();
830 } catch (InterruptedException ex) {
831 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400832 }
833 }
Joe Onorato36115782010-06-17 13:28:48 -0400834 if (DEBUG_LOADERS) {
835 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700836 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400837 + "ms for previous step to finish binding");
838 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400839 }
Joe Onorato36115782010-06-17 13:28:48 -0400840 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400841
Joe Onorato36115782010-06-17 13:28:48 -0400842 public void run() {
843 // Optimize for end-user experience: if the Launcher is up and // running with the
844 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
845 // workspace first (default).
846 final Callbacks cbk = mCallbacks.get();
847 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400848
Joe Onorato36115782010-06-17 13:28:48 -0400849 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400850 // Elevate priority when Home launches for the first time to avoid
851 // starving at boot time. Staring at a blank home is not cool.
852 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700853 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
854 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400855 android.os.Process.setThreadPriority(mIsLaunching
856 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
857 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400858 if (loadWorkspaceFirst) {
859 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
860 loadAndBindWorkspace();
861 } else {
862 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700863 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400864 }
865
Joe Onorato36115782010-06-17 13:28:48 -0400866 if (mStopped) {
867 break keep_running;
868 }
869
870 // Whew! Hard work done. Slow us down, and wait until the UI thread has
871 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400872 synchronized (mLock) {
873 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700874 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400875 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
876 }
877 }
Joe Onorato36115782010-06-17 13:28:48 -0400878 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400879
880 // second step
881 if (loadWorkspaceFirst) {
882 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700883 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400884 } else {
885 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
886 loadAndBindWorkspace();
887 }
Winson Chung7ed37742011-09-08 15:45:51 -0700888
889 // Restore the default thread priority after we are done loading items
890 synchronized (mLock) {
891 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
892 }
Joe Onorato36115782010-06-17 13:28:48 -0400893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894
Winson Chungaac01e12011-08-17 10:37:13 -0700895
896 // Update the saved icons if necessary
897 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700898 for (Object key : sDbIconCache.keySet()) {
899 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700900 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700901 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700902
Joe Onorato36115782010-06-17 13:28:48 -0400903 // Clear out this reference, otherwise we end up holding it until all of the
904 // callback runnables are done.
905 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400906
Joe Onorato36115782010-06-17 13:28:48 -0400907 synchronized (mLock) {
908 // If we are still the last one to be scheduled, remove ourselves.
909 if (mLoaderTask == this) {
910 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911 }
Joe Onorato36115782010-06-17 13:28:48 -0400912 }
Joe Onorato36115782010-06-17 13:28:48 -0400913 }
914
915 public void stopLocked() {
916 synchronized (LoaderTask.this) {
917 mStopped = true;
918 this.notify();
919 }
920 }
921
922 /**
923 * Gets the callbacks object. If we've been stopped, or if the launcher object
924 * has somehow been garbage collected, return null instead. Pass in the Callbacks
925 * object that was around when the deferred message was scheduled, and if there's
926 * a new Callbacks object around then also return null. This will save us from
927 * calling onto it with data that will be ignored.
928 */
929 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
930 synchronized (mLock) {
931 if (mStopped) {
932 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400933 }
Joe Onorato36115782010-06-17 13:28:48 -0400934
935 if (mCallbacks == null) {
936 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400937 }
Joe Onorato36115782010-06-17 13:28:48 -0400938
939 final Callbacks callbacks = mCallbacks.get();
940 if (callbacks != oldCallbacks) {
941 return null;
942 }
943 if (callbacks == null) {
944 Log.w(TAG, "no mCallbacks");
945 return null;
946 }
947
948 return callbacks;
949 }
950 }
951
952 // check & update map of what's occupied; used to discard overlapping/invalid items
953 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700954 int containerIndex = item.screen;
955 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700956 // Return early if we detect that an item is under the hotseat button
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800957 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700958 return false;
959 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700960
961 // We use the last index to refer to the hotseat and the screen as the rank, so
962 // test and update the occupied state accordingly
963 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
964 Log.e(TAG, "Error loading shortcut into hotseat " + item
965 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
966 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
967 return false;
968 } else {
969 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
970 return true;
971 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700972 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
973 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400974 return true;
975 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700976
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700977 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -0400978 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
979 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700980 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400981 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700982 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400983 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700984 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700985 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400986 return false;
987 }
988 }
989 }
990 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
991 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700992 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400993 }
994 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700995
Joe Onorato36115782010-06-17 13:28:48 -0400996 return true;
997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998
Joe Onorato36115782010-06-17 13:28:48 -0400999 private void loadWorkspace() {
1000 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001
Joe Onorato36115782010-06-17 13:28:48 -04001002 final Context context = mContext;
1003 final ContentResolver contentResolver = context.getContentResolver();
1004 final PackageManager manager = context.getPackageManager();
1005 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1006 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001007
Michael Jurkab85f8a42012-04-25 15:48:32 -07001008 // Make sure the default workspace is loaded, if needed
1009 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary();
1010
Adam Cohen4eac29a2011-07-11 17:53:37 -07001011 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001012 sAppWidgets.clear();
1013 sFolders.clear();
1014 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -07001015 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001016
Joe Onorato36115782010-06-17 13:28:48 -04001017 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018
Joe Onorato36115782010-06-17 13:28:48 -04001019 final Cursor c = contentResolver.query(
Winson Chunge61e93e2011-09-12 10:59:49 -07001020 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001021
Winson Chungf30ad5f2011-08-08 10:55:42 -07001022 // +1 for the hotseat (it can be larger than the workspace)
Winson Chung36f97362011-09-07 11:20:10 -07001023 // Load workspace in reverse order to ensure that latest items are loaded first (and
1024 // before any earlier duplicates)
Adam Cohend22015c2010-07-26 22:02:18 -07001025 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -07001026 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027
Joe Onorato36115782010-06-17 13:28:48 -04001028 try {
1029 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1030 final int intentIndex = c.getColumnIndexOrThrow
1031 (LauncherSettings.Favorites.INTENT);
1032 final int titleIndex = c.getColumnIndexOrThrow
1033 (LauncherSettings.Favorites.TITLE);
1034 final int iconTypeIndex = c.getColumnIndexOrThrow(
1035 LauncherSettings.Favorites.ICON_TYPE);
1036 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1037 final int iconPackageIndex = c.getColumnIndexOrThrow(
1038 LauncherSettings.Favorites.ICON_PACKAGE);
1039 final int iconResourceIndex = c.getColumnIndexOrThrow(
1040 LauncherSettings.Favorites.ICON_RESOURCE);
1041 final int containerIndex = c.getColumnIndexOrThrow(
1042 LauncherSettings.Favorites.CONTAINER);
1043 final int itemTypeIndex = c.getColumnIndexOrThrow(
1044 LauncherSettings.Favorites.ITEM_TYPE);
1045 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1046 LauncherSettings.Favorites.APPWIDGET_ID);
1047 final int screenIndex = c.getColumnIndexOrThrow(
1048 LauncherSettings.Favorites.SCREEN);
1049 final int cellXIndex = c.getColumnIndexOrThrow
1050 (LauncherSettings.Favorites.CELLX);
1051 final int cellYIndex = c.getColumnIndexOrThrow
1052 (LauncherSettings.Favorites.CELLY);
1053 final int spanXIndex = c.getColumnIndexOrThrow
1054 (LauncherSettings.Favorites.SPANX);
1055 final int spanYIndex = c.getColumnIndexOrThrow(
1056 LauncherSettings.Favorites.SPANY);
Michael Jurka3a9fced2012-04-13 14:44:29 -07001057 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1058 //final int displayModeIndex = c.getColumnIndexOrThrow(
1059 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060
Joe Onorato36115782010-06-17 13:28:48 -04001061 ShortcutInfo info;
1062 String intentDescription;
1063 LauncherAppWidgetInfo appWidgetInfo;
1064 int container;
1065 long id;
1066 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001067
Joe Onorato36115782010-06-17 13:28:48 -04001068 while (!mStopped && c.moveToNext()) {
1069 try {
1070 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001071
Joe Onorato36115782010-06-17 13:28:48 -04001072 switch (itemType) {
1073 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1074 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1075 intentDescription = c.getString(intentIndex);
1076 try {
1077 intent = Intent.parseUri(intentDescription, 0);
1078 } catch (URISyntaxException e) {
1079 continue;
1080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081
Joe Onorato36115782010-06-17 13:28:48 -04001082 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1083 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -07001084 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001085 } else {
1086 info = getShortcutInfo(c, context, iconTypeIndex,
1087 iconPackageIndex, iconResourceIndex, iconIndex,
1088 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001089
1090 // App shortcuts that used to be automatically added to Launcher
1091 // didn't always have the correct intent flags set, so do that here
1092 if (intent.getAction().equals(Intent.ACTION_MAIN) &&
1093 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
1094 intent.addFlags(
1095 Intent.FLAG_ACTIVITY_NEW_TASK |
1096 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1097 }
Joe Onorato36115782010-06-17 13:28:48 -04001098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099
Joe Onorato36115782010-06-17 13:28:48 -04001100 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001101 info.intent = intent;
1102 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001103 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001104 info.container = container;
1105 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001106 info.cellX = c.getInt(cellXIndex);
1107 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108
Daniel Sandler8802e962010-05-26 16:28:16 -04001109 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001110 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001111 break;
1112 }
1113
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001115 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001116 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001117 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001118 break;
1119 default:
1120 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001121 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001122 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001123 folderInfo.add(info);
1124 break;
1125 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001126 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001127
1128 // now that we've loaded everthing re-save it with the
1129 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001130 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001131 } else {
1132 // Failed to load the shortcut, probably because the
1133 // activity manager couldn't resolve it (maybe the app
1134 // was uninstalled), or the db row was somehow screwed up.
1135 // Delete it.
1136 id = c.getLong(idIndex);
1137 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1138 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1139 id, false), null, null);
1140 }
1141 break;
1142
Adam Cohendf2cc412011-04-27 16:56:57 -07001143 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001144 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001145 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001146
Winson Chungaafa03c2010-06-11 17:34:16 -07001147 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001148 folderInfo.id = id;
1149 container = c.getInt(containerIndex);
1150 folderInfo.container = container;
1151 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001152 folderInfo.cellX = c.getInt(cellXIndex);
1153 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001154
1155 // check & update map of what's occupied
1156 if (!checkItemPlacement(occupied, folderInfo)) {
1157 break;
1158 }
Joe Onorato36115782010-06-17 13:28:48 -04001159 switch (container) {
1160 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001161 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001162 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001163 break;
1164 }
1165
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001166 sItemsIdMap.put(folderInfo.id, folderInfo);
1167 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001168 break;
1169
Joe Onorato36115782010-06-17 13:28:48 -04001170 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1171 // Read all Launcher-specific widget details
1172 int appWidgetId = c.getInt(appWidgetIdIndex);
1173 id = c.getLong(idIndex);
1174
1175 final AppWidgetProviderInfo provider =
1176 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001177
Joe Onorato36115782010-06-17 13:28:48 -04001178 if (!isSafeMode && (provider == null || provider.provider == null ||
1179 provider.provider.getPackageName() == null)) {
Adam Cohen16d7ffc2011-10-05 17:49:14 -07001180 String log = "Deleting widget that isn't installed anymore: id="
1181 + id + " appWidgetId=" + appWidgetId;
1182 Log.e(TAG, log);
1183 Launcher.sDumpLogs.add(log);
Joe Onorato36115782010-06-17 13:28:48 -04001184 itemsToRemove.add(id);
1185 } else {
Winson Chung11a49372012-04-27 15:12:38 -07001186 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1187 provider.provider);
Joe Onorato36115782010-06-17 13:28:48 -04001188 appWidgetInfo.id = id;
1189 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001190 appWidgetInfo.cellX = c.getInt(cellXIndex);
1191 appWidgetInfo.cellY = c.getInt(cellYIndex);
1192 appWidgetInfo.spanX = c.getInt(spanXIndex);
1193 appWidgetInfo.spanY = c.getInt(spanYIndex);
Adam Cohen2f093b62012-04-30 18:59:53 -07001194 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1195 appWidgetInfo.minSpanX = minSpan[0];
1196 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001197
1198 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001199 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1200 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001201 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001202 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001203 continue;
1204 }
1205 appWidgetInfo.container = c.getInt(containerIndex);
1206
1207 // check & update map of what's occupied
1208 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1209 break;
1210 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001211 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1212 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001213 }
1214 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001215 }
Joe Onorato36115782010-06-17 13:28:48 -04001216 } catch (Exception e) {
1217 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001218 }
1219 }
Joe Onorato36115782010-06-17 13:28:48 -04001220 } finally {
1221 c.close();
1222 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001223
Joe Onorato36115782010-06-17 13:28:48 -04001224 if (itemsToRemove.size() > 0) {
1225 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1226 LauncherSettings.Favorites.CONTENT_URI);
1227 // Remove dead items
1228 for (long id : itemsToRemove) {
1229 if (DEBUG_LOADERS) {
1230 Log.d(TAG, "Removed id = " + id);
1231 }
1232 // Don't notify content observers
1233 try {
1234 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1235 null, null);
1236 } catch (RemoteException e) {
1237 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001238 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001239 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001240 }
1241
Joe Onorato36115782010-06-17 13:28:48 -04001242 if (DEBUG_LOADERS) {
1243 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1244 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001245 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001246 String line = "";
1247 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1248 if (s > 0) {
1249 line += " | ";
1250 }
Adam Cohend22015c2010-07-26 22:02:18 -07001251 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001252 line += ((occupied[s][x][y] != null) ? "#" : ".");
1253 }
1254 }
1255 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 }
Joe Onorato36115782010-06-17 13:28:48 -04001257 }
1258 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001259
Joe Onorato36115782010-06-17 13:28:48 -04001260 /**
1261 * Read everything out of our database.
1262 */
1263 private void bindWorkspace() {
1264 final long t = SystemClock.uptimeMillis();
1265
1266 // Don't use these two variables in any of the callback runnables.
1267 // Otherwise we hold a reference to them.
1268 final Callbacks oldCallbacks = mCallbacks.get();
1269 if (oldCallbacks == null) {
1270 // This launcher has exited and nobody bothered to tell us. Just bail.
1271 Log.w(TAG, "LoaderTask running with no launcher");
1272 return;
1273 }
1274
Winson Chungdb8a8942012-04-03 14:08:41 -07001275 // Get the list of workspace items to load and unbind the existing ShortcutInfos
1276 // before we call startBinding() below.
1277 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1278 final ArrayList<ItemInfo> tmpWorkspaceItems = unbindWorkspaceItemsOnMainThread();
1279 // Order the items for loading as follows: current workspace, hotseat, everything else
1280 Collections.sort(tmpWorkspaceItems, new Comparator<ItemInfo>() {
1281 @Override
1282 public int compare(ItemInfo lhs, ItemInfo rhs) {
1283 int cellCountX = LauncherModel.getCellCountX();
1284 int cellCountY = LauncherModel.getCellCountY();
1285 int screenOffset = cellCountX * cellCountY;
1286 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
1287 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset +
1288 lhs.cellY * cellCountX + lhs.cellX);
1289 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset +
1290 rhs.cellY * cellCountX + rhs.cellX);
1291 return (int) (lr - rr);
1292 }
1293 });
1294 // Precondition: the items are ordered by page, screen
1295 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
1296 for (ItemInfo ii : tmpWorkspaceItems) {
1297 // Prepend the current items, hotseat items, append everything else
1298 if (ii.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1299 ii.screen == currentScreen) {
1300 workspaceItems.add(0, ii);
1301 } else if (ii.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1302 workspaceItems.add(0, ii);
1303 } else {
1304 workspaceItems.add(ii);
1305 }
1306 }
1307
Joe Onorato36115782010-06-17 13:28:48 -04001308 // Tell the workspace that we're about to start firing items at it
1309 mHandler.post(new Runnable() {
1310 public void run() {
1311 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1312 if (callbacks != null) {
1313 callbacks.startBinding();
1314 }
1315 }
1316 });
Winson Chung603bcb92011-09-02 11:45:39 -07001317
Joe Onorato36115782010-06-17 13:28:48 -04001318 // Add the items to the workspace.
Winson Chungdb8a8942012-04-03 14:08:41 -07001319 int N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001320 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1321 final int start = i;
1322 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323 mHandler.post(new Runnable() {
1324 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001325 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001327 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 }
1329 }
1330 });
Joe Onorato36115782010-06-17 13:28:48 -04001331 }
Winson Chung603bcb92011-09-02 11:45:39 -07001332 // Ensure that we don't use the same folders data structure on the main thread
1333 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001334 mHandler.post(new Runnable() {
1335 public void run() {
1336 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1337 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001338 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001339 }
1340 }
1341 });
1342 // Wait until the queue goes empty.
1343 mHandler.post(new Runnable() {
1344 public void run() {
1345 if (DEBUG_LOADERS) {
1346 Log.d(TAG, "Going to start binding widgets soon.");
1347 }
1348 }
1349 });
1350 // Bind the widgets, one at a time.
1351 // WARNING: this is calling into the workspace from the background thread,
1352 // but since getCurrentScreen() just returns the int, we should be okay. This
1353 // is just a hint for the order, and if it's wrong, we'll be okay.
1354 // TODO: instead, we should have that push the current screen into here.
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001355 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001356 // once for the current screen
1357 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001358 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001359 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 mHandler.post(new Runnable() {
1361 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001362 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001364 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 }
1366 }
1367 });
1368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 }
Joe Onorato36115782010-06-17 13:28:48 -04001370 // once for the other screens
1371 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001372 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001373 if (widget.screen != currentScreen) {
1374 mHandler.post(new Runnable() {
1375 public void run() {
1376 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1377 if (callbacks != null) {
1378 callbacks.bindAppWidget(widget);
1379 }
1380 }
1381 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001382 }
1383 }
Joe Onorato36115782010-06-17 13:28:48 -04001384 // Tell the workspace that we're done.
1385 mHandler.post(new Runnable() {
1386 public void run() {
1387 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1388 if (callbacks != null) {
1389 callbacks.finishBindingItems();
1390 }
1391 }
1392 });
1393 // If we're profiling, this is the last thing in the queue.
1394 mHandler.post(new Runnable() {
1395 public void run() {
1396 if (DEBUG_LOADERS) {
1397 Log.d(TAG, "bound workspace in "
1398 + (SystemClock.uptimeMillis()-t) + "ms");
1399 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001400
1401 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04001402 }
1403 });
1404 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001405
Joe Onorato36115782010-06-17 13:28:48 -04001406 private void loadAndBindAllApps() {
1407 if (DEBUG_LOADERS) {
1408 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1409 }
1410 if (!mAllAppsLoaded) {
1411 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001412 synchronized (LoaderTask.this) {
1413 if (mStopped) {
1414 return;
1415 }
1416 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001417 }
Joe Onorato36115782010-06-17 13:28:48 -04001418 } else {
1419 onlyBindAllApps();
1420 }
1421 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001422
Joe Onorato36115782010-06-17 13:28:48 -04001423 private void onlyBindAllApps() {
1424 final Callbacks oldCallbacks = mCallbacks.get();
1425 if (oldCallbacks == null) {
1426 // This launcher has exited and nobody bothered to tell us. Just bail.
1427 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1428 return;
1429 }
1430
1431 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001432 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04001433 final ArrayList<ApplicationInfo> list
Michael Jurka92f3d462011-11-22 21:02:29 -08001434 = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone();
Joe Onorato36115782010-06-17 13:28:48 -04001435 mHandler.post(new Runnable() {
1436 public void run() {
1437 final long t = SystemClock.uptimeMillis();
1438 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1439 if (callbacks != null) {
1440 callbacks.bindAllApplications(list);
1441 }
1442 if (DEBUG_LOADERS) {
1443 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1444 + (SystemClock.uptimeMillis()-t) + "ms");
1445 }
1446 }
1447 });
1448
1449 }
1450
1451 private void loadAllAppsByBatch() {
1452 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1453
1454 // Don't use these two variables in any of the callback runnables.
1455 // Otherwise we hold a reference to them.
1456 final Callbacks oldCallbacks = mCallbacks.get();
1457 if (oldCallbacks == null) {
1458 // This launcher has exited and nobody bothered to tell us. Just bail.
1459 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1460 return;
1461 }
1462
1463 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1464 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1465
1466 final PackageManager packageManager = mContext.getPackageManager();
1467 List<ResolveInfo> apps = null;
1468
1469 int N = Integer.MAX_VALUE;
1470
1471 int startIndex;
1472 int i=0;
1473 int batchSize = -1;
1474 while (i < N && !mStopped) {
1475 if (i == 0) {
1476 mAllAppsList.clear();
1477 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1478 apps = packageManager.queryIntentActivities(mainIntent, 0);
1479 if (DEBUG_LOADERS) {
1480 Log.d(TAG, "queryIntentActivities took "
1481 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1482 }
1483 if (apps == null) {
1484 return;
1485 }
1486 N = apps.size();
1487 if (DEBUG_LOADERS) {
1488 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1489 }
1490 if (N == 0) {
1491 // There are no apps?!?
1492 return;
1493 }
1494 if (mBatchSize == 0) {
1495 batchSize = N;
1496 } else {
1497 batchSize = mBatchSize;
1498 }
1499
1500 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1501 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001502 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001503 if (DEBUG_LOADERS) {
1504 Log.d(TAG, "sort took "
1505 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1506 }
1507 }
1508
1509 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1510
1511 startIndex = i;
1512 for (int j=0; i<N && j<batchSize; j++) {
1513 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001514 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001515 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001516 i++;
1517 }
1518
1519 final boolean first = i <= batchSize;
1520 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1521 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1522 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1523
Joe Onoratocc67f472010-06-08 10:54:30 -07001524 mHandler.post(new Runnable() {
1525 public void run() {
1526 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001527 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001528 if (first) {
1529 callbacks.bindAllApplications(added);
1530 } else {
1531 callbacks.bindAppsAdded(added);
1532 }
1533 if (DEBUG_LOADERS) {
1534 Log.d(TAG, "bound " + added.size() + " apps in "
1535 + (SystemClock.uptimeMillis() - t) + "ms");
1536 }
1537 } else {
1538 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001539 }
1540 }
1541 });
1542
Daniel Sandlerdca66122010-04-13 16:23:58 -04001543 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001544 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1545 + (SystemClock.uptimeMillis()-t2) + "ms");
1546 }
1547
1548 if (mAllAppsLoadDelay > 0 && i < N) {
1549 try {
1550 if (DEBUG_LOADERS) {
1551 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1552 }
1553 Thread.sleep(mAllAppsLoadDelay);
1554 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001555 }
1556 }
1557
Joe Onorato36115782010-06-17 13:28:48 -04001558 if (DEBUG_LOADERS) {
1559 Log.d(TAG, "cached all " + N + " apps in "
1560 + (SystemClock.uptimeMillis()-t) + "ms"
1561 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001562 }
1563 }
1564
1565 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001566 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1567 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1568 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1569 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1570 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001571 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001572 }
1573 }
1574
1575 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001576 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001577 }
1578
1579 private class PackageUpdatedTask implements Runnable {
1580 int mOp;
1581 String[] mPackages;
1582
1583 public static final int OP_NONE = 0;
1584 public static final int OP_ADD = 1;
1585 public static final int OP_UPDATE = 2;
1586 public static final int OP_REMOVE = 3; // uninstlled
1587 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1588
1589
1590 public PackageUpdatedTask(int op, String[] packages) {
1591 mOp = op;
1592 mPackages = packages;
1593 }
1594
1595 public void run() {
1596 final Context context = mApp;
1597
1598 final String[] packages = mPackages;
1599 final int N = packages.length;
1600 switch (mOp) {
1601 case OP_ADD:
1602 for (int i=0; i<N; i++) {
1603 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1604 mAllAppsList.addPackage(context, packages[i]);
1605 }
1606 break;
1607 case OP_UPDATE:
1608 for (int i=0; i<N; i++) {
1609 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1610 mAllAppsList.updatePackage(context, packages[i]);
1611 }
1612 break;
1613 case OP_REMOVE:
1614 case OP_UNAVAILABLE:
1615 for (int i=0; i<N; i++) {
1616 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1617 mAllAppsList.removePackage(packages[i]);
1618 }
1619 break;
1620 }
1621
1622 ArrayList<ApplicationInfo> added = null;
1623 ArrayList<ApplicationInfo> removed = null;
1624 ArrayList<ApplicationInfo> modified = null;
1625
1626 if (mAllAppsList.added.size() > 0) {
1627 added = mAllAppsList.added;
1628 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1629 }
1630 if (mAllAppsList.removed.size() > 0) {
1631 removed = mAllAppsList.removed;
1632 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1633 for (ApplicationInfo info: removed) {
1634 mIconCache.remove(info.intent.getComponent());
1635 }
1636 }
1637 if (mAllAppsList.modified.size() > 0) {
1638 modified = mAllAppsList.modified;
1639 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1640 }
1641
1642 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1643 if (callbacks == null) {
1644 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1645 return;
1646 }
1647
1648 if (added != null) {
1649 final ArrayList<ApplicationInfo> addedFinal = added;
1650 mHandler.post(new Runnable() {
1651 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001652 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1653 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001654 callbacks.bindAppsAdded(addedFinal);
1655 }
1656 }
1657 });
1658 }
1659 if (modified != null) {
1660 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1661 mHandler.post(new Runnable() {
1662 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001663 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1664 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001665 callbacks.bindAppsUpdated(modifiedFinal);
1666 }
1667 }
1668 });
1669 }
1670 if (removed != null) {
1671 final boolean permanent = mOp != OP_UNAVAILABLE;
1672 final ArrayList<ApplicationInfo> removedFinal = removed;
1673 mHandler.post(new Runnable() {
1674 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001675 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1676 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001677 callbacks.bindAppsRemoved(removedFinal, permanent);
1678 }
1679 }
1680 });
Joe Onoratobe386092009-11-17 17:32:16 -08001681 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001682
1683 mHandler.post(new Runnable() {
1684 @Override
1685 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001686 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1687 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001688 callbacks.bindPackagesUpdated();
1689 }
1690 }
1691 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001692 }
1693 }
1694
1695 /**
Winson Chung2efec4e2012-05-03 12:31:48 -07001696 * Returns all the Workspace ShortcutInfos associated with a particular package.
1697 * @param intent
1698 * @return
1699 */
1700 ArrayList<ShortcutInfo> getShortcutInfosForPackage(String packageName) {
1701 ArrayList<ShortcutInfo> infos = new ArrayList<ShortcutInfo>();
1702 for (ItemInfo i : sWorkspaceItems) {
1703 if (i instanceof ShortcutInfo) {
1704 ShortcutInfo info = (ShortcutInfo) i;
Winson Chung452b4db2012-05-06 16:50:16 -07001705 if (info.intent.getComponent().getPackageName().equals(packageName)) {
Winson Chung2efec4e2012-05-03 12:31:48 -07001706 infos.add(info);
1707 }
1708 }
1709 }
1710 return infos;
1711 }
1712
1713 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001714 * This is called from the code that adds shortcuts from the intent receiver. This
1715 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001716 */
Joe Onorato56d82912010-03-07 14:32:10 -05001717 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001718 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001719 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001720
Joe Onorato56d82912010-03-07 14:32:10 -05001721 /**
1722 * Make an ShortcutInfo object for a shortcut that is an application.
1723 *
1724 * If c is not null, then it will be used to fill in missing data like the title and icon.
1725 */
1726 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001727 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001728 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001729 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001730
1731 ComponentName componentName = intent.getComponent();
1732 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001733 return null;
1734 }
1735
Adam Cohen00fcb492011-11-02 21:53:47 -07001736 try {
1737 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
1738 if (!pi.applicationInfo.enabled) {
1739 // If we return null here, the corresponding item will be removed from the launcher
1740 // db and will not appear in the workspace.
1741 return null;
1742 }
1743 } catch (NameNotFoundException e) {
1744 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
1745 }
1746
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001747 // TODO: See if the PackageManager knows about this case. If it doesn't
1748 // then return null & delete this.
1749
Joe Onorato56d82912010-03-07 14:32:10 -05001750 // the resource -- This may implicitly give us back the fallback icon,
1751 // but don't worry about that. All we're doing with usingFallbackIcon is
1752 // to avoid saving lots of copies of that in the database, and most apps
1753 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07001754
1755 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
1756 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
1757 // via resolveActivity().
1758 ResolveInfo resolveInfo = null;
1759 ComponentName oldComponent = intent.getComponent();
1760 Intent newIntent = new Intent(intent.getAction(), null);
1761 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1762 newIntent.setPackage(oldComponent.getPackageName());
1763 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
1764 for (ResolveInfo i : infos) {
1765 ComponentName cn = new ComponentName(i.activityInfo.packageName,
1766 i.activityInfo.name);
1767 if (cn.equals(oldComponent)) {
1768 resolveInfo = i;
1769 }
1770 }
1771 if (resolveInfo == null) {
1772 resolveInfo = manager.resolveActivity(intent, 0);
1773 }
Joe Onorato56d82912010-03-07 14:32:10 -05001774 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001775 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001776 }
Joe Onorato56d82912010-03-07 14:32:10 -05001777 // the db
1778 if (icon == null) {
1779 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001780 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001781 }
1782 }
1783 // the fallback icon
1784 if (icon == null) {
1785 icon = getFallbackIcon();
1786 info.usingFallbackIcon = true;
1787 }
1788 info.setIcon(icon);
1789
1790 // from the resource
1791 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001792 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1793 if (labelCache != null && labelCache.containsKey(key)) {
1794 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001795 } else {
1796 info.title = resolveInfo.activityInfo.loadLabel(manager);
1797 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001798 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001799 }
1800 }
Joe Onorato56d82912010-03-07 14:32:10 -05001801 }
1802 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001803 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001804 if (c != null) {
1805 info.title = c.getString(titleIndex);
1806 }
1807 }
1808 // fall back to the class name of the activity
1809 if (info.title == null) {
1810 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001811 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1813 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001815
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001817 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001819 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001820 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1821 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822
Joe Onorato56d82912010-03-07 14:32:10 -05001823 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001824 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001825 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001827 // TODO: If there's an explicit component and we can't install that, delete it.
1828
Joe Onorato56d82912010-03-07 14:32:10 -05001829 info.title = c.getString(titleIndex);
1830
Joe Onorato9c1289c2009-08-17 11:03:03 -04001831 int iconType = c.getInt(iconTypeIndex);
1832 switch (iconType) {
1833 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1834 String packageName = c.getString(iconPackageIndex);
1835 String resourceName = c.getString(iconResourceIndex);
1836 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001837 info.customIcon = false;
1838 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001841 if (resources != null) {
1842 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001843 icon = Utilities.createIconBitmap(
1844 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001845 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001847 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
Joe Onorato56d82912010-03-07 14:32:10 -05001849 // the db
1850 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001851 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001852 }
1853 // the fallback icon
1854 if (icon == null) {
1855 icon = getFallbackIcon();
1856 info.usingFallbackIcon = true;
1857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 break;
1859 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001860 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001861 if (icon == null) {
1862 icon = getFallbackIcon();
1863 info.customIcon = false;
1864 info.usingFallbackIcon = true;
1865 } else {
1866 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001867 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001868 break;
1869 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001870 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001871 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 info.customIcon = false;
1873 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001875 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 return info;
1877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878
Michael Jurka931dc972011-08-05 15:08:15 -07001879 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07001880 @SuppressWarnings("all") // suppress dead code warning
1881 final boolean debug = false;
1882 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05001883 Log.d(TAG, "getIconFromCursor app="
1884 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1885 }
1886 byte[] data = c.getBlob(iconIndex);
1887 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001888 return Utilities.createIconBitmap(
1889 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001890 } catch (Exception e) {
1891 return null;
1892 }
1893 }
1894
Winson Chung3d503fb2011-07-13 17:25:49 -07001895 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1896 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001897 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001898 if (info == null) {
1899 return null;
1900 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001901 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001902
1903 return info;
1904 }
1905
Winson Chunga9abd0e2010-10-27 17:18:37 -07001906 /**
Winson Chung55cef262010-10-28 14:14:18 -07001907 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1908 */
1909 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1910 ComponentName component) {
1911 List<AppWidgetProviderInfo> widgets =
1912 AppWidgetManager.getInstance(context).getInstalledProviders();
1913 for (AppWidgetProviderInfo info : widgets) {
1914 if (info.provider.equals(component)) {
1915 return info;
1916 }
1917 }
1918 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001919 }
1920
Winson Chung68846fd2010-10-29 11:00:27 -07001921 /**
1922 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1923 */
1924 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1925 final PackageManager packageManager = context.getPackageManager();
1926 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1927 new ArrayList<WidgetMimeTypeHandlerData>();
1928
1929 final Intent supportsIntent =
1930 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1931 supportsIntent.setType(mimeType);
1932
1933 // Create a set of widget configuration components that we can test against
1934 final List<AppWidgetProviderInfo> widgets =
1935 AppWidgetManager.getInstance(context).getInstalledProviders();
1936 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1937 new HashMap<ComponentName, AppWidgetProviderInfo>();
1938 for (AppWidgetProviderInfo info : widgets) {
1939 configurationComponentToWidget.put(info.configure, info);
1940 }
1941
1942 // Run through each of the intents that can handle this type of clip data, and cross
1943 // reference them with the components that are actual configuration components
1944 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1945 PackageManager.MATCH_DEFAULT_ONLY);
1946 for (ResolveInfo info : activities) {
1947 final ActivityInfo activityInfo = info.activityInfo;
1948 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1949 activityInfo.name);
1950 if (configurationComponentToWidget.containsKey(infoComponent)) {
1951 supportedConfigurationActivities.add(
1952 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1953 configurationComponentToWidget.get(infoComponent)));
1954 }
1955 }
1956 return supportedConfigurationActivities;
1957 }
1958
Winson Chunga9abd0e2010-10-27 17:18:37 -07001959 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001960 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1961 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1962 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1963
Adam Cohend9198822011-11-22 16:42:47 -08001964 if (intent == null) {
1965 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
1966 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
1967 return null;
1968 }
1969
Joe Onorato0589f0f2010-02-08 13:44:00 -08001970 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001971 boolean customIcon = false;
1972 ShortcutIconResource iconResource = null;
1973
1974 if (bitmap != null && bitmap instanceof Bitmap) {
1975 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001976 customIcon = true;
1977 } else {
1978 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1979 if (extra != null && extra instanceof ShortcutIconResource) {
1980 try {
1981 iconResource = (ShortcutIconResource) extra;
1982 final PackageManager packageManager = context.getPackageManager();
1983 Resources resources = packageManager.getResourcesForApplication(
1984 iconResource.packageName);
1985 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001986 icon = Utilities.createIconBitmap(
1987 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001988 } catch (Exception e) {
1989 Log.w(TAG, "Could not load shortcut icon: " + extra);
1990 }
1991 }
1992 }
1993
Michael Jurkac9d95c52011-08-29 14:03:34 -07001994 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001995
1996 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001997 if (fallbackIcon != null) {
1998 icon = fallbackIcon;
1999 } else {
2000 icon = getFallbackIcon();
2001 info.usingFallbackIcon = true;
2002 }
Joe Onorato56d82912010-03-07 14:32:10 -05002003 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002004 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002005
Joe Onorato0589f0f2010-02-08 13:44:00 -08002006 info.title = name;
2007 info.intent = intent;
2008 info.customIcon = customIcon;
2009 info.iconResource = iconResource;
2010
2011 return info;
2012 }
2013
Winson Chungaac01e12011-08-17 10:37:13 -07002014 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2015 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002016 // If apps can't be on SD, don't even bother.
2017 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002018 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002019 }
Joe Onorato56d82912010-03-07 14:32:10 -05002020 // If this icon doesn't have a custom icon, check to see
2021 // what's stored in the DB, and if it doesn't match what
2022 // we're going to show, store what we are going to show back
2023 // into the DB. We do this so when we're loading, if the
2024 // package manager can't find an icon (for example because
2025 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002026 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002027 cache.put(info, c.getBlob(iconIndex));
2028 return true;
2029 }
2030 return false;
2031 }
2032 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2033 boolean needSave = false;
2034 try {
2035 if (data != null) {
2036 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2037 Bitmap loaded = info.getIcon(mIconCache);
2038 needSave = !saved.sameAs(loaded);
2039 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002040 needSave = true;
2041 }
Winson Chungaac01e12011-08-17 10:37:13 -07002042 } catch (Exception e) {
2043 needSave = true;
2044 }
2045 if (needSave) {
2046 Log.d(TAG, "going to save icon bitmap for info=" + info);
2047 // This is slower than is ideal, but this only happens once
2048 // or when the app is updated with a new icon.
2049 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002050 }
2051 }
2052
Joe Onorato9c1289c2009-08-17 11:03:03 -04002053 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002054 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002055 * or make a new one.
2056 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002057 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058 // See if a placeholder was created for us already
2059 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002060 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002061 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002062 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002063 folders.put(id, folderInfo);
2064 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002065 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
2067
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08002069 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070 = new Comparator<ApplicationInfo>() {
2071 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07002072 int result = sCollator.compare(a.title.toString(), b.title.toString());
2073 if (result == 0) {
2074 result = a.componentName.compareTo(b.componentName);
2075 }
2076 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002078 };
Winson Chung78403fe2011-01-21 15:38:02 -08002079 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2080 = new Comparator<ApplicationInfo>() {
2081 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2082 if (a.firstInstallTime < b.firstInstallTime) return 1;
2083 if (a.firstInstallTime > b.firstInstallTime) return -1;
2084 return 0;
2085 }
2086 };
Winson Chung785d2eb2011-04-14 16:08:02 -07002087 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
2088 = new Comparator<AppWidgetProviderInfo>() {
2089 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2090 return sCollator.compare(a.label.toString(), b.label.toString());
2091 }
2092 };
Winson Chung5308f242011-08-18 12:12:41 -07002093 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2094 if (info.activityInfo != null) {
2095 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2096 } else {
2097 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2098 }
2099 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002100 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
2101 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002102 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002103 ShortcutNameComparator(PackageManager pm) {
2104 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002105 mLabelCache = new HashMap<Object, CharSequence>();
2106 }
2107 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2108 mPackageManager = pm;
2109 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002110 }
2111 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002112 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002113 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2114 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2115 if (mLabelCache.containsKey(keyA)) {
2116 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002117 } else {
2118 labelA = a.loadLabel(mPackageManager).toString();
2119
Winson Chung5308f242011-08-18 12:12:41 -07002120 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002121 }
Winson Chung5308f242011-08-18 12:12:41 -07002122 if (mLabelCache.containsKey(keyB)) {
2123 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002124 } else {
2125 labelB = b.loadLabel(mPackageManager).toString();
2126
Winson Chung5308f242011-08-18 12:12:41 -07002127 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002128 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002129 return sCollator.compare(labelA, labelB);
2130 }
2131 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002132 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
2133 private PackageManager mPackageManager;
2134 private HashMap<Object, String> mLabelCache;
2135 WidgetAndShortcutNameComparator(PackageManager pm) {
2136 mPackageManager = pm;
2137 mLabelCache = new HashMap<Object, String>();
2138 }
2139 public final int compare(Object a, Object b) {
2140 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002141 if (mLabelCache.containsKey(a)) {
2142 labelA = mLabelCache.get(a);
2143 } else {
2144 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002145 ((AppWidgetProviderInfo) a).label :
2146 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002147 mLabelCache.put(a, labelA);
2148 }
2149 if (mLabelCache.containsKey(b)) {
2150 labelB = mLabelCache.get(b);
2151 } else {
2152 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002153 ((AppWidgetProviderInfo) b).label :
2154 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002155 mLabelCache.put(b, labelB);
2156 }
Winson Chung1ed747a2011-05-03 16:18:34 -07002157 return sCollator.compare(labelA, labelB);
2158 }
2159 };
Joe Onoratobe386092009-11-17 17:32:16 -08002160
2161 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002162 Log.d(TAG, "mCallbacks=" + mCallbacks);
2163 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
2164 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
2165 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
2166 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002167 if (mLoaderTask != null) {
2168 mLoaderTask.dumpState();
2169 } else {
2170 Log.d(TAG, "mLoaderTask=null");
2171 }
Joe Onoratobe386092009-11-17 17:32:16 -08002172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173}