blob: 1e58ca0e420a16ee3c10f1c7afb734f0f223ccda [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
Winson Chungaafa03c2010-06-11 17:34:16 -070019import java.lang.ref.WeakReference;
20import java.net.URISyntaxException;
21import java.text.Collator;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.List;
27
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040030import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080032import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ContentResolver;
34import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.ActivityInfo;
39import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080040import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.pm.ResolveInfo;
42import android.content.res.Resources;
43import android.database.Cursor;
44import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040047import android.os.Handler;
48import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040052import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import java.lang.ref.WeakReference;
56import java.net.URISyntaxException;
57import java.text.Collator;
58import java.util.ArrayList;
59import java.util.Arrays;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import java.util.Collections;
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070061import java.util.Comparator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import java.util.HashMap;
63import java.util.List;
64
Romain Guyedcce092010-03-04 13:03:17 -080065import com.android.launcher.R;
66
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067/**
68 * Maintains in-memory state of the Launcher. It is expected that there should be only one
69 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070070 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040072public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080073 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070075
Joe Onorato36115782010-06-17 13:28:48 -040076 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onoratod65d08e2010-04-20 15:43:37 -040077 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040078 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040079
Joe Onoratof99f8c12009-10-31 17:27:36 -040080 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 private final Object mLock = new Object();
82 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040083 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070085 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
86 static {
87 sWorkerThread.start();
88 }
89 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
90
Joe Onoratocc67f472010-06-08 10:54:30 -070091 // We start off with everything not loaded. After that, we assume that
92 // our monitoring of the package manager provides all updates and we never
93 // need to do a requery. These are only ever touched from the loader thread.
94 private boolean mWorkspaceLoaded;
95 private boolean mAllAppsLoaded;
96
Joe Onorato9c1289c2009-08-17 11:03:03 -040097 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098
Joe Onorato36115782010-06-17 13:28:48 -040099 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -0800100 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -0400101 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
102 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
103 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800104
105 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
Adam Cohend22015c2010-07-26 22:02:18 -0700107 private static int mCellCountX;
108 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700111 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 public int getCurrentWorkspaceScreen();
113 public void startBinding();
114 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500115 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 public void finishBindingItems();
117 public void bindAppWidget(LauncherAppWidgetInfo info);
118 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500119 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
120 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400121 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700122 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400123 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato0589f0f2010-02-08 13:44:00 -0800126 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400127 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800128 mAllAppsList = new AllAppsList(iconCache);
129 mIconCache = iconCache;
130
131 mDefaultIcon = Utilities.createIconBitmap(
132 app.getPackageManager().getDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400133
134 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400135
136 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800137 }
138
Joe Onorato56d82912010-03-07 14:32:10 -0500139 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800140 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Joe Onorato9c1289c2009-08-17 11:03:03 -0400143 /**
144 * Adds an item to the DB if it was not created previously, or move it to a new
145 * <container, screen, cellX, cellY>
146 */
147 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
148 int screen, int cellX, int cellY) {
149 if (item.container == ItemInfo.NO_ID) {
150 // From all apps
151 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
152 } else {
153 // From somewhere else
154 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 }
156 }
157
158 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700160 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
162 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700163
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 item.container = container;
165 item.screen = screen;
166 item.cellX = cellX;
167 item.cellY = cellY;
168
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700169 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 final ContentValues values = new ContentValues();
171 final ContentResolver cr = context.getContentResolver();
172
173 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700174 values.put(LauncherSettings.Favorites.CELLX, cellX);
175 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
177
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700178 sWorker.post(new Runnable() {
179 public void run() {
180 cr.update(uri, values, null, null);
181 }
182 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700183 }
184
185 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 * Returns true if the shortcuts already exists in the database.
187 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static boolean shortcutExists(Context context, String title, Intent intent) {
190 final ContentResolver cr = context.getContentResolver();
191 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
192 new String[] { "title", "intent" }, "title=? and intent=?",
193 new String[] { title, intent.toUri(0) }, null);
194 boolean result = false;
195 try {
196 result = c.moveToFirst();
197 } finally {
198 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700201 }
202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700204 * Returns an ItemInfo array containing all the items in the LauncherModel.
205 * The ItemInfo.id is not set through this function.
206 */
207 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
208 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
209 final ContentResolver cr = context.getContentResolver();
210 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
211 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
212 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
213 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
214
215 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
216 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
217 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
218 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
219 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
220 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
221 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
222
223 try {
224 while (c.moveToNext()) {
225 ItemInfo item = new ItemInfo();
226 item.cellX = c.getInt(cellXIndex);
227 item.cellY = c.getInt(cellYIndex);
228 item.spanX = c.getInt(spanXIndex);
229 item.spanY = c.getInt(spanYIndex);
230 item.container = c.getInt(containerIndex);
231 item.itemType = c.getInt(itemTypeIndex);
232 item.screen = c.getInt(screenIndex);
233
234 items.add(item);
235 }
236 } catch (Exception e) {
237 items.clear();
238 } finally {
239 c.close();
240 }
241
242 return items;
243 }
244
245 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
247 */
248 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
249 final ContentResolver cr = context.getContentResolver();
250 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
251 "_id=? and (itemType=? or itemType=?)",
252 new String[] { String.valueOf(id),
253 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
254 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700255
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 try {
257 if (c.moveToFirst()) {
258 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
259 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
260 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
261 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
262 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
263 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 FolderInfo folderInfo = null;
266 switch (c.getInt(itemTypeIndex)) {
267 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
268 folderInfo = findOrMakeUserFolder(folderList, id);
269 break;
270 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
271 folderInfo = findOrMakeLiveFolder(folderList, id);
272 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700273 }
274
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 folderInfo.title = c.getString(titleIndex);
276 folderInfo.id = id;
277 folderInfo.container = c.getInt(containerIndex);
278 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700279 folderInfo.cellX = c.getInt(cellXIndex);
280 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281
282 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700283 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 } finally {
285 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700286 }
287
288 return null;
289 }
290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 /**
292 * Add an item to the database in a specified container. Sets the container, screen, cellX and
293 * cellY fields of the item. Also assigns an ID to the item.
294 */
295 static void addItemToDatabase(Context context, ItemInfo item, long container,
296 int screen, int cellX, int cellY, boolean notify) {
297 item.container = container;
298 item.screen = screen;
299 item.cellX = cellX;
300 item.cellY = cellY;
301
302 final ContentValues values = new ContentValues();
303 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304 item.onAddToDatabase(values);
305
Adam Cohend22015c2010-07-26 22:02:18 -0700306 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700307
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
309 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
310
311 if (result != null) {
312 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700313 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700314 }
315
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700317 * Creates a new unique child id, for a given cell span across all layouts.
318 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700319 static int getCellLayoutChildId(
320 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
321 return ((cellId & 0xFF) << 24)
322 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700323 }
324
Adam Cohend22015c2010-07-26 22:02:18 -0700325 static int getCellCountX() {
326 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700327 }
328
Adam Cohend22015c2010-07-26 22:02:18 -0700329 static int getCellCountY() {
330 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700331 }
332
333 /**
334 * Updates the model orientation helper to take into account the current layout dimensions
335 * when performing local/canonical coordinate transformations.
336 */
337 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700338 mCellCountX = shortAxisCellCount;
339 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700340 }
341
342 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400343 * Update an item to the database in a specified container.
344 */
345 static void updateItemInDatabase(Context context, ItemInfo item) {
346 final ContentValues values = new ContentValues();
347 final ContentResolver cr = context.getContentResolver();
348
349 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700350 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700351
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
353 }
354
355 /**
356 * Removes the specified item from the database
357 * @param context
358 * @param item
359 */
360 static void deleteItemFromDatabase(Context context, ItemInfo item) {
361 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700362 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700363 sWorker.post(new Runnable() {
364 public void run() {
365 cr.delete(uriToDelete, null, null);
366 }
367 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 }
369
370 /**
371 * Remove the contents of the specified folder from the database
372 */
373 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
374 final ContentResolver cr = context.getContentResolver();
375
376 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
377 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
378 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
379 }
380
381 /**
382 * Set this as the current Launcher activity object for the loader.
383 */
384 public void initialize(Callbacks callbacks) {
385 synchronized (mLock) {
386 mCallbacks = new WeakReference<Callbacks>(callbacks);
387 }
388 }
389
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700390 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400391 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
392 * ACTION_PACKAGE_CHANGED.
393 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400394 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400395 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700396
Joe Onorato36115782010-06-17 13:28:48 -0400397 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400398
Joe Onorato36115782010-06-17 13:28:48 -0400399 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
400 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
401 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
402 final String packageName = intent.getData().getSchemeSpecificPart();
403 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400404
Joe Onorato36115782010-06-17 13:28:48 -0400405 int op = PackageUpdatedTask.OP_NONE;
406
407 if (packageName == null || packageName.length() == 0) {
408 // they sent us a bad intent
409 return;
410 }
411
412 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
413 op = PackageUpdatedTask.OP_UPDATE;
414 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
415 if (!replacing) {
416 op = PackageUpdatedTask.OP_REMOVE;
417 }
418 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
419 // later, we will update the package at this time
420 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
421 if (!replacing) {
422 op = PackageUpdatedTask.OP_ADD;
423 } else {
424 op = PackageUpdatedTask.OP_UPDATE;
425 }
426 }
427
428 if (op != PackageUpdatedTask.OP_NONE) {
429 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
430 }
431
432 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400433 // First, schedule to add these apps back in.
434 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
435 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
436 // Then, rebind everything.
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700437 boolean runLoader = true;
438 if (mCallbacks != null) {
439 Callbacks callbacks = mCallbacks.get();
440 if (callbacks != null) {
441 // If they're paused, we can skip loading, because they'll do it again anyway
442 if (callbacks.setLoadOnResume()) {
443 runLoader = false;
444 }
445 }
446 }
447 if (runLoader) {
448 startLoader(mApp, false);
449 }
Joe Onorato36115782010-06-17 13:28:48 -0400450
451 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
452 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
453 enqueuePackageUpdated(new PackageUpdatedTask(
454 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onorato790c2d92010-06-11 00:14:11 -0700455 }
Joe Onorato36115782010-06-17 13:28:48 -0400456 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400457
Joe Onorato36115782010-06-17 13:28:48 -0400458 public void startLoader(Context context, boolean isLaunching) {
459 synchronized (mLock) {
460 if (DEBUG_LOADERS) {
461 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400463
Joe Onorato36115782010-06-17 13:28:48 -0400464 // Don't bother to start the thread if we know it's not going to do anything
465 if (mCallbacks != null && mCallbacks.get() != null) {
466 // If there is already one running, tell it to stop.
467 LoaderTask oldTask = mLoaderTask;
468 if (oldTask != null) {
469 if (oldTask.isLaunching()) {
470 // don't downgrade isLaunching if we're already running
471 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500472 }
Joe Onorato36115782010-06-17 13:28:48 -0400473 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500474 }
Joe Onorato36115782010-06-17 13:28:48 -0400475 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700476 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400477 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400478 }
479 }
480
Joe Onorato36115782010-06-17 13:28:48 -0400481 public void stopLoader() {
482 synchronized (mLock) {
483 if (mLoaderTask != null) {
484 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400485 }
486 }
Joe Onorato36115782010-06-17 13:28:48 -0400487 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400488
Joe Onorato36115782010-06-17 13:28:48 -0400489 /**
490 * Runnable for the thread that loads the contents of the launcher:
491 * - workspace icons
492 * - widgets
493 * - all apps icons
494 */
495 private class LoaderTask implements Runnable {
496 private Context mContext;
497 private Thread mWaitThread;
498 private boolean mIsLaunching;
499 private boolean mStopped;
500 private boolean mLoadAndBindStepFinished;
501
502 LoaderTask(Context context, boolean isLaunching) {
503 mContext = context;
504 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400505 }
506
Joe Onorato36115782010-06-17 13:28:48 -0400507 boolean isLaunching() {
508 return mIsLaunching;
509 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400510
Joe Onorato36115782010-06-17 13:28:48 -0400511 private void loadAndBindWorkspace() {
512 // Load the workspace
513
514 // For now, just always reload the workspace. It's ~100 ms vs. the
515 // binding which takes many hundreds of ms.
516 // We can reconsider.
517 if (DEBUG_LOADERS) {
518 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519 }
Joe Onorato36115782010-06-17 13:28:48 -0400520 if (true || !mWorkspaceLoaded) {
521 loadWorkspace();
522 if (mStopped) {
523 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 }
Joe Onorato36115782010-06-17 13:28:48 -0400525 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526 }
527
Joe Onorato36115782010-06-17 13:28:48 -0400528 // Bind the workspace
529 bindWorkspace();
530 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400531
Joe Onorato36115782010-06-17 13:28:48 -0400532 private void waitForIdle() {
533 // Wait until the either we're stopped or the other threads are done.
534 // This way we don't start loading all apps until the workspace has settled
535 // down.
536 synchronized (LoaderTask.this) {
537 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700538
Joe Onorato36115782010-06-17 13:28:48 -0400539 mHandler.postIdle(new Runnable() {
540 public void run() {
541 synchronized (LoaderTask.this) {
542 mLoadAndBindStepFinished = true;
543 if (DEBUG_LOADERS) {
544 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400545 }
Joe Onorato36115782010-06-17 13:28:48 -0400546 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400547 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400548 }
Joe Onorato36115782010-06-17 13:28:48 -0400549 });
550
551 while (!mStopped && !mLoadAndBindStepFinished) {
552 try {
553 this.wait();
554 } catch (InterruptedException ex) {
555 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400556 }
557 }
Joe Onorato36115782010-06-17 13:28:48 -0400558 if (DEBUG_LOADERS) {
559 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700560 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400561 + "ms for previous step to finish binding");
562 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400563 }
Joe Onorato36115782010-06-17 13:28:48 -0400564 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400565
Joe Onorato36115782010-06-17 13:28:48 -0400566 public void run() {
567 // Optimize for end-user experience: if the Launcher is up and // running with the
568 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
569 // workspace first (default).
570 final Callbacks cbk = mCallbacks.get();
571 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400572
Joe Onorato36115782010-06-17 13:28:48 -0400573 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400574 // Elevate priority when Home launches for the first time to avoid
575 // starving at boot time. Staring at a blank home is not cool.
576 synchronized (mLock) {
577 android.os.Process.setThreadPriority(mIsLaunching
578 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
579 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400580 if (loadWorkspaceFirst) {
581 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
582 loadAndBindWorkspace();
583 } else {
584 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700585 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400586 }
587
Joe Onorato36115782010-06-17 13:28:48 -0400588 if (mStopped) {
589 break keep_running;
590 }
591
592 // Whew! Hard work done. Slow us down, and wait until the UI thread has
593 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400594 synchronized (mLock) {
595 if (mIsLaunching) {
596 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
597 }
598 }
Joe Onorato36115782010-06-17 13:28:48 -0400599 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400600
601 // second step
602 if (loadWorkspaceFirst) {
603 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700604 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400605 } else {
606 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
607 loadAndBindWorkspace();
608 }
Joe Onorato36115782010-06-17 13:28:48 -0400609 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400610
Joe Onorato36115782010-06-17 13:28:48 -0400611 // Clear out this reference, otherwise we end up holding it until all of the
612 // callback runnables are done.
613 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400614
Joe Onorato36115782010-06-17 13:28:48 -0400615 synchronized (mLock) {
616 // If we are still the last one to be scheduled, remove ourselves.
617 if (mLoaderTask == this) {
618 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 }
Joe Onorato36115782010-06-17 13:28:48 -0400620 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700621
Joe Onorato36115782010-06-17 13:28:48 -0400622 // Trigger a gc to try to clean up after the stuff is done, since the
623 // renderscript allocations aren't charged to the java heap.
624 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700625 mHandler.post(new Runnable() {
626 public void run() {
627 System.gc();
628 }
629 });
Joe Onorato36115782010-06-17 13:28:48 -0400630 } else {
631 mHandler.postIdle(new Runnable() {
632 public void run() {
633 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400634 }
Joe Onorato36115782010-06-17 13:28:48 -0400635 });
636 }
637 }
638
639 public void stopLocked() {
640 synchronized (LoaderTask.this) {
641 mStopped = true;
642 this.notify();
643 }
644 }
645
646 /**
647 * Gets the callbacks object. If we've been stopped, or if the launcher object
648 * has somehow been garbage collected, return null instead. Pass in the Callbacks
649 * object that was around when the deferred message was scheduled, and if there's
650 * a new Callbacks object around then also return null. This will save us from
651 * calling onto it with data that will be ignored.
652 */
653 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
654 synchronized (mLock) {
655 if (mStopped) {
656 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400657 }
Joe Onorato36115782010-06-17 13:28:48 -0400658
659 if (mCallbacks == null) {
660 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400661 }
Joe Onorato36115782010-06-17 13:28:48 -0400662
663 final Callbacks callbacks = mCallbacks.get();
664 if (callbacks != oldCallbacks) {
665 return null;
666 }
667 if (callbacks == null) {
668 Log.w(TAG, "no mCallbacks");
669 return null;
670 }
671
672 return callbacks;
673 }
674 }
675
676 // check & update map of what's occupied; used to discard overlapping/invalid items
677 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
678 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400679 return true;
680 }
Joe Onorato36115782010-06-17 13:28:48 -0400681 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
682 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
683 if (occupied[item.screen][x][y] != null) {
684 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700685 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400686 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700687 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400688 + occupied[item.screen][x][y]);
689 return false;
690 }
691 }
692 }
693 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
694 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
695 occupied[item.screen][x][y] = item;
696 }
697 }
698 return true;
699 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700
Joe Onorato36115782010-06-17 13:28:48 -0400701 private void loadWorkspace() {
702 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703
Joe Onorato36115782010-06-17 13:28:48 -0400704 final Context context = mContext;
705 final ContentResolver contentResolver = context.getContentResolver();
706 final PackageManager manager = context.getPackageManager();
707 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
708 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400709
Joe Onorato36115782010-06-17 13:28:48 -0400710 mItems.clear();
711 mAppWidgets.clear();
712 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800713
Joe Onorato36115782010-06-17 13:28:48 -0400714 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715
Joe Onorato36115782010-06-17 13:28:48 -0400716 final Cursor c = contentResolver.query(
717 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400718
Adam Cohend22015c2010-07-26 22:02:18 -0700719 final ItemInfo occupied[][][] =
720 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721
Joe Onorato36115782010-06-17 13:28:48 -0400722 try {
723 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
724 final int intentIndex = c.getColumnIndexOrThrow
725 (LauncherSettings.Favorites.INTENT);
726 final int titleIndex = c.getColumnIndexOrThrow
727 (LauncherSettings.Favorites.TITLE);
728 final int iconTypeIndex = c.getColumnIndexOrThrow(
729 LauncherSettings.Favorites.ICON_TYPE);
730 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
731 final int iconPackageIndex = c.getColumnIndexOrThrow(
732 LauncherSettings.Favorites.ICON_PACKAGE);
733 final int iconResourceIndex = c.getColumnIndexOrThrow(
734 LauncherSettings.Favorites.ICON_RESOURCE);
735 final int containerIndex = c.getColumnIndexOrThrow(
736 LauncherSettings.Favorites.CONTAINER);
737 final int itemTypeIndex = c.getColumnIndexOrThrow(
738 LauncherSettings.Favorites.ITEM_TYPE);
739 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
740 LauncherSettings.Favorites.APPWIDGET_ID);
741 final int screenIndex = c.getColumnIndexOrThrow(
742 LauncherSettings.Favorites.SCREEN);
743 final int cellXIndex = c.getColumnIndexOrThrow
744 (LauncherSettings.Favorites.CELLX);
745 final int cellYIndex = c.getColumnIndexOrThrow
746 (LauncherSettings.Favorites.CELLY);
747 final int spanXIndex = c.getColumnIndexOrThrow
748 (LauncherSettings.Favorites.SPANX);
749 final int spanYIndex = c.getColumnIndexOrThrow(
750 LauncherSettings.Favorites.SPANY);
751 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
752 final int displayModeIndex = c.getColumnIndexOrThrow(
753 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754
Joe Onorato36115782010-06-17 13:28:48 -0400755 ShortcutInfo info;
756 String intentDescription;
757 LauncherAppWidgetInfo appWidgetInfo;
758 int container;
759 long id;
760 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761
Joe Onorato36115782010-06-17 13:28:48 -0400762 while (!mStopped && c.moveToNext()) {
763 try {
764 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400765
Joe Onorato36115782010-06-17 13:28:48 -0400766 switch (itemType) {
767 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
768 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
769 intentDescription = c.getString(intentIndex);
770 try {
771 intent = Intent.parseUri(intentDescription, 0);
772 } catch (URISyntaxException e) {
773 continue;
774 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775
Joe Onorato36115782010-06-17 13:28:48 -0400776 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
777 info = getShortcutInfo(manager, intent, context, c, iconIndex,
778 titleIndex);
779 } else {
780 info = getShortcutInfo(c, context, iconTypeIndex,
781 iconPackageIndex, iconResourceIndex, iconIndex,
782 titleIndex);
783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784
Joe Onorato36115782010-06-17 13:28:48 -0400785 if (info != null) {
786 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787
Joe Onorato36115782010-06-17 13:28:48 -0400788 info.intent = intent;
789 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400791 info.container = container;
792 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700793 info.cellX = c.getInt(cellXIndex);
794 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795
Daniel Sandler8802e962010-05-26 16:28:16 -0400796 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400797 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400798 break;
799 }
800
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400802 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
803 mItems.add(info);
804 break;
805 default:
806 // Item is in a user folder
807 UserFolderInfo folderInfo =
808 findOrMakeUserFolder(mFolders, container);
809 folderInfo.add(info);
810 break;
811 }
812 } else {
813 // Failed to load the shortcut, probably because the
814 // activity manager couldn't resolve it (maybe the app
815 // was uninstalled), or the db row was somehow screwed up.
816 // Delete it.
817 id = c.getLong(idIndex);
818 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
819 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
820 id, false), null, null);
821 }
822 break;
823
824 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
825 id = c.getLong(idIndex);
826 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
827
Winson Chungaafa03c2010-06-11 17:34:16 -0700828 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400829 folderInfo.id = id;
830 container = c.getInt(containerIndex);
831 folderInfo.container = container;
832 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700833 folderInfo.cellX = c.getInt(cellXIndex);
834 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400835
836 // check & update map of what's occupied
837 if (!checkItemPlacement(occupied, folderInfo)) {
838 break;
839 }
Joe Onorato36115782010-06-17 13:28:48 -0400840 switch (container) {
841 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
842 mItems.add(folderInfo);
843 break;
844 }
845
846 mFolders.put(folderInfo.id, folderInfo);
847 break;
848
849 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
850 id = c.getLong(idIndex);
851 Uri uri = Uri.parse(c.getString(uriIndex));
852
853 // Make sure the live folder exists
854 final ProviderInfo providerInfo =
855 context.getPackageManager().resolveContentProvider(
856 uri.getAuthority(), 0);
857
858 if (providerInfo == null && !isSafeMode) {
859 itemsToRemove.add(id);
860 } else {
861 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400862 intentDescription = c.getString(intentIndex);
863 intent = null;
864 if (intentDescription != null) {
865 try {
866 intent = Intent.parseUri(intentDescription, 0);
867 } catch (URISyntaxException e) {
868 // Ignore, a live folder might not have a base intent
869 }
870 }
871
872 liveFolderInfo.title = c.getString(titleIndex);
873 liveFolderInfo.id = id;
874 liveFolderInfo.uri = uri;
875 container = c.getInt(containerIndex);
876 liveFolderInfo.container = container;
877 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700878 liveFolderInfo.cellX = c.getInt(cellXIndex);
879 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400880 liveFolderInfo.baseIntent = intent;
881 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
882
883 // check & update map of what's occupied
884 if (!checkItemPlacement(occupied, liveFolderInfo)) {
885 break;
886 }
887
888 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
889 iconResourceIndex, liveFolderInfo);
890
891 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400893 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894 break;
895 }
Joe Onorato36115782010-06-17 13:28:48 -0400896 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897 }
Joe Onorato36115782010-06-17 13:28:48 -0400898 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800899
Joe Onorato36115782010-06-17 13:28:48 -0400900 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
901 // Read all Launcher-specific widget details
902 int appWidgetId = c.getInt(appWidgetIdIndex);
903 id = c.getLong(idIndex);
904
905 final AppWidgetProviderInfo provider =
906 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700907
Joe Onorato36115782010-06-17 13:28:48 -0400908 if (!isSafeMode && (provider == null || provider.provider == null ||
909 provider.provider.getPackageName() == null)) {
910 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
911 + id + " appWidgetId=" + appWidgetId);
912 itemsToRemove.add(id);
913 } else {
914 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
915 appWidgetInfo.id = id;
916 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700917 appWidgetInfo.cellX = c.getInt(cellXIndex);
918 appWidgetInfo.cellY = c.getInt(cellYIndex);
919 appWidgetInfo.spanX = c.getInt(spanXIndex);
920 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400921
922 container = c.getInt(containerIndex);
923 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
924 Log.e(TAG, "Widget found where container "
925 + "!= CONTAINER_DESKTOP -- ignoring!");
926 continue;
927 }
928 appWidgetInfo.container = c.getInt(containerIndex);
929
930 // check & update map of what's occupied
931 if (!checkItemPlacement(occupied, appWidgetInfo)) {
932 break;
933 }
934
935 mAppWidgets.add(appWidgetInfo);
936 }
937 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800938 }
Joe Onorato36115782010-06-17 13:28:48 -0400939 } catch (Exception e) {
940 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800941 }
942 }
Joe Onorato36115782010-06-17 13:28:48 -0400943 } finally {
944 c.close();
945 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800946
Joe Onorato36115782010-06-17 13:28:48 -0400947 if (itemsToRemove.size() > 0) {
948 ContentProviderClient client = contentResolver.acquireContentProviderClient(
949 LauncherSettings.Favorites.CONTENT_URI);
950 // Remove dead items
951 for (long id : itemsToRemove) {
952 if (DEBUG_LOADERS) {
953 Log.d(TAG, "Removed id = " + id);
954 }
955 // Don't notify content observers
956 try {
957 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
958 null, null);
959 } catch (RemoteException e) {
960 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400961 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800962 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 }
964
Joe Onorato36115782010-06-17 13:28:48 -0400965 if (DEBUG_LOADERS) {
966 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
967 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700968 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400969 String line = "";
970 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
971 if (s > 0) {
972 line += " | ";
973 }
Adam Cohend22015c2010-07-26 22:02:18 -0700974 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400975 line += ((occupied[s][x][y] != null) ? "#" : ".");
976 }
977 }
978 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 }
Joe Onorato36115782010-06-17 13:28:48 -0400980 }
981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982
Joe Onorato36115782010-06-17 13:28:48 -0400983 /**
984 * Read everything out of our database.
985 */
986 private void bindWorkspace() {
987 final long t = SystemClock.uptimeMillis();
988
989 // Don't use these two variables in any of the callback runnables.
990 // Otherwise we hold a reference to them.
991 final Callbacks oldCallbacks = mCallbacks.get();
992 if (oldCallbacks == null) {
993 // This launcher has exited and nobody bothered to tell us. Just bail.
994 Log.w(TAG, "LoaderTask running with no launcher");
995 return;
996 }
997
998 int N;
999 // Tell the workspace that we're about to start firing items at it
1000 mHandler.post(new Runnable() {
1001 public void run() {
1002 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1003 if (callbacks != null) {
1004 callbacks.startBinding();
1005 }
1006 }
1007 });
1008 // Add the items to the workspace.
1009 N = mItems.size();
1010 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1011 final int start = i;
1012 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013 mHandler.post(new Runnable() {
1014 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001015 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001017 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 }
1019 }
1020 });
Joe Onorato36115782010-06-17 13:28:48 -04001021 }
1022 mHandler.post(new Runnable() {
1023 public void run() {
1024 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1025 if (callbacks != null) {
1026 callbacks.bindFolders(mFolders);
1027 }
1028 }
1029 });
1030 // Wait until the queue goes empty.
1031 mHandler.post(new Runnable() {
1032 public void run() {
1033 if (DEBUG_LOADERS) {
1034 Log.d(TAG, "Going to start binding widgets soon.");
1035 }
1036 }
1037 });
1038 // Bind the widgets, one at a time.
1039 // WARNING: this is calling into the workspace from the background thread,
1040 // but since getCurrentScreen() just returns the int, we should be okay. This
1041 // is just a hint for the order, and if it's wrong, we'll be okay.
1042 // TODO: instead, we should have that push the current screen into here.
1043 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1044 N = mAppWidgets.size();
1045 // once for the current screen
1046 for (int i=0; i<N; i++) {
1047 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1048 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 mHandler.post(new Runnable() {
1050 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001051 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001053 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054 }
1055 }
1056 });
1057 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058 }
Joe Onorato36115782010-06-17 13:28:48 -04001059 // once for the other screens
1060 for (int i=0; i<N; i++) {
1061 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1062 if (widget.screen != currentScreen) {
1063 mHandler.post(new Runnable() {
1064 public void run() {
1065 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1066 if (callbacks != null) {
1067 callbacks.bindAppWidget(widget);
1068 }
1069 }
1070 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001071 }
1072 }
Joe Onorato36115782010-06-17 13:28:48 -04001073 // Tell the workspace that we're done.
1074 mHandler.post(new Runnable() {
1075 public void run() {
1076 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1077 if (callbacks != null) {
1078 callbacks.finishBindingItems();
1079 }
1080 }
1081 });
1082 // If we're profiling, this is the last thing in the queue.
1083 mHandler.post(new Runnable() {
1084 public void run() {
1085 if (DEBUG_LOADERS) {
1086 Log.d(TAG, "bound workspace in "
1087 + (SystemClock.uptimeMillis()-t) + "ms");
1088 }
1089 }
1090 });
1091 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001092
Joe Onorato36115782010-06-17 13:28:48 -04001093 private void loadAndBindAllApps() {
1094 if (DEBUG_LOADERS) {
1095 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1096 }
1097 if (!mAllAppsLoaded) {
1098 loadAllAppsByBatch();
1099 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001100 return;
1101 }
Joe Onorato36115782010-06-17 13:28:48 -04001102 mAllAppsLoaded = true;
1103 } else {
1104 onlyBindAllApps();
1105 }
1106 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001107
Joe Onorato36115782010-06-17 13:28:48 -04001108 private void onlyBindAllApps() {
1109 final Callbacks oldCallbacks = mCallbacks.get();
1110 if (oldCallbacks == null) {
1111 // This launcher has exited and nobody bothered to tell us. Just bail.
1112 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1113 return;
1114 }
1115
1116 // shallow copy
1117 final ArrayList<ApplicationInfo> list
1118 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1119 mHandler.post(new Runnable() {
1120 public void run() {
1121 final long t = SystemClock.uptimeMillis();
1122 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1123 if (callbacks != null) {
1124 callbacks.bindAllApplications(list);
1125 }
1126 if (DEBUG_LOADERS) {
1127 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1128 + (SystemClock.uptimeMillis()-t) + "ms");
1129 }
1130 }
1131 });
1132
1133 }
1134
1135 private void loadAllAppsByBatch() {
1136 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1137
1138 // Don't use these two variables in any of the callback runnables.
1139 // Otherwise we hold a reference to them.
1140 final Callbacks oldCallbacks = mCallbacks.get();
1141 if (oldCallbacks == null) {
1142 // This launcher has exited and nobody bothered to tell us. Just bail.
1143 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1144 return;
1145 }
1146
1147 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1148 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1149
1150 final PackageManager packageManager = mContext.getPackageManager();
1151 List<ResolveInfo> apps = null;
1152
1153 int N = Integer.MAX_VALUE;
1154
1155 int startIndex;
1156 int i=0;
1157 int batchSize = -1;
1158 while (i < N && !mStopped) {
1159 if (i == 0) {
1160 mAllAppsList.clear();
1161 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1162 apps = packageManager.queryIntentActivities(mainIntent, 0);
1163 if (DEBUG_LOADERS) {
1164 Log.d(TAG, "queryIntentActivities took "
1165 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1166 }
1167 if (apps == null) {
1168 return;
1169 }
1170 N = apps.size();
1171 if (DEBUG_LOADERS) {
1172 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1173 }
1174 if (N == 0) {
1175 // There are no apps?!?
1176 return;
1177 }
1178 if (mBatchSize == 0) {
1179 batchSize = N;
1180 } else {
1181 batchSize = mBatchSize;
1182 }
1183
1184 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1185 Collections.sort(apps,
1186 new ResolveInfo.DisplayNameComparator(packageManager));
1187 if (DEBUG_LOADERS) {
1188 Log.d(TAG, "sort took "
1189 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1190 }
1191 }
1192
1193 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1194
1195 startIndex = i;
1196 for (int j=0; i<N && j<batchSize; j++) {
1197 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001198 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001199 i++;
1200 }
1201
1202 final boolean first = i <= batchSize;
1203 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1204 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1205 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1206
Joe Onoratocc67f472010-06-08 10:54:30 -07001207 mHandler.post(new Runnable() {
1208 public void run() {
1209 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001210 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001211 if (first) {
1212 callbacks.bindAllApplications(added);
1213 } else {
1214 callbacks.bindAppsAdded(added);
1215 }
1216 if (DEBUG_LOADERS) {
1217 Log.d(TAG, "bound " + added.size() + " apps in "
1218 + (SystemClock.uptimeMillis() - t) + "ms");
1219 }
1220 } else {
1221 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001222 }
1223 }
1224 });
1225
Daniel Sandlerdca66122010-04-13 16:23:58 -04001226 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001227 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1228 + (SystemClock.uptimeMillis()-t2) + "ms");
1229 }
1230
1231 if (mAllAppsLoadDelay > 0 && i < N) {
1232 try {
1233 if (DEBUG_LOADERS) {
1234 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1235 }
1236 Thread.sleep(mAllAppsLoadDelay);
1237 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 }
1239 }
1240
Joe Onorato36115782010-06-17 13:28:48 -04001241 if (DEBUG_LOADERS) {
1242 Log.d(TAG, "cached all " + N + " apps in "
1243 + (SystemClock.uptimeMillis()-t) + "ms"
1244 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001245 }
1246 }
1247
1248 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001249 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1250 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1251 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1252 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1253 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1254 }
1255 }
1256
1257 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001258 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001259 }
1260
1261 private class PackageUpdatedTask implements Runnable {
1262 int mOp;
1263 String[] mPackages;
1264
1265 public static final int OP_NONE = 0;
1266 public static final int OP_ADD = 1;
1267 public static final int OP_UPDATE = 2;
1268 public static final int OP_REMOVE = 3; // uninstlled
1269 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1270
1271
1272 public PackageUpdatedTask(int op, String[] packages) {
1273 mOp = op;
1274 mPackages = packages;
1275 }
1276
1277 public void run() {
1278 final Context context = mApp;
1279
1280 final String[] packages = mPackages;
1281 final int N = packages.length;
1282 switch (mOp) {
1283 case OP_ADD:
1284 for (int i=0; i<N; i++) {
1285 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1286 mAllAppsList.addPackage(context, packages[i]);
1287 }
1288 break;
1289 case OP_UPDATE:
1290 for (int i=0; i<N; i++) {
1291 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1292 mAllAppsList.updatePackage(context, packages[i]);
1293 }
1294 break;
1295 case OP_REMOVE:
1296 case OP_UNAVAILABLE:
1297 for (int i=0; i<N; i++) {
1298 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1299 mAllAppsList.removePackage(packages[i]);
1300 }
1301 break;
1302 }
1303
1304 ArrayList<ApplicationInfo> added = null;
1305 ArrayList<ApplicationInfo> removed = null;
1306 ArrayList<ApplicationInfo> modified = null;
1307
1308 if (mAllAppsList.added.size() > 0) {
1309 added = mAllAppsList.added;
1310 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1311 }
1312 if (mAllAppsList.removed.size() > 0) {
1313 removed = mAllAppsList.removed;
1314 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1315 for (ApplicationInfo info: removed) {
1316 mIconCache.remove(info.intent.getComponent());
1317 }
1318 }
1319 if (mAllAppsList.modified.size() > 0) {
1320 modified = mAllAppsList.modified;
1321 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1322 }
1323
1324 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1325 if (callbacks == null) {
1326 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1327 return;
1328 }
1329
1330 if (added != null) {
1331 final ArrayList<ApplicationInfo> addedFinal = added;
1332 mHandler.post(new Runnable() {
1333 public void run() {
1334 if (callbacks == mCallbacks.get()) {
1335 callbacks.bindAppsAdded(addedFinal);
1336 }
1337 }
1338 });
1339 }
1340 if (modified != null) {
1341 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1342 mHandler.post(new Runnable() {
1343 public void run() {
1344 if (callbacks == mCallbacks.get()) {
1345 callbacks.bindAppsUpdated(modifiedFinal);
1346 }
1347 }
1348 });
1349 }
1350 if (removed != null) {
1351 final boolean permanent = mOp != OP_UNAVAILABLE;
1352 final ArrayList<ApplicationInfo> removedFinal = removed;
1353 mHandler.post(new Runnable() {
1354 public void run() {
1355 if (callbacks == mCallbacks.get()) {
1356 callbacks.bindAppsRemoved(removedFinal, permanent);
1357 }
1358 }
1359 });
Joe Onoratobe386092009-11-17 17:32:16 -08001360 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001361
1362 mHandler.post(new Runnable() {
1363 @Override
1364 public void run() {
1365 if (callbacks == mCallbacks.get()) {
1366 callbacks.bindPackagesUpdated();
1367 }
1368 }
1369 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 }
1371 }
1372
1373 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001374 * This is called from the code that adds shortcuts from the intent receiver. This
1375 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 */
Joe Onorato56d82912010-03-07 14:32:10 -05001377 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001378 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380
Joe Onorato56d82912010-03-07 14:32:10 -05001381 /**
1382 * Make an ShortcutInfo object for a shortcut that is an application.
1383 *
1384 * If c is not null, then it will be used to fill in missing data like the title and icon.
1385 */
1386 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1387 Cursor c, int iconIndex, int titleIndex) {
1388 Bitmap icon = null;
1389 final ShortcutInfo info = new ShortcutInfo();
1390
1391 ComponentName componentName = intent.getComponent();
1392 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001393 return null;
1394 }
1395
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001396 // TODO: See if the PackageManager knows about this case. If it doesn't
1397 // then return null & delete this.
1398
Joe Onorato56d82912010-03-07 14:32:10 -05001399 // the resource -- This may implicitly give us back the fallback icon,
1400 // but don't worry about that. All we're doing with usingFallbackIcon is
1401 // to avoid saving lots of copies of that in the database, and most apps
1402 // have icons anyway.
1403 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1404 if (resolveInfo != null) {
1405 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001406 }
Joe Onorato56d82912010-03-07 14:32:10 -05001407 // the db
1408 if (icon == null) {
1409 if (c != null) {
1410 icon = getIconFromCursor(c, iconIndex);
1411 }
1412 }
1413 // the fallback icon
1414 if (icon == null) {
1415 icon = getFallbackIcon();
1416 info.usingFallbackIcon = true;
1417 }
1418 info.setIcon(icon);
1419
1420 // from the resource
1421 if (resolveInfo != null) {
1422 info.title = resolveInfo.activityInfo.loadLabel(manager);
1423 }
1424 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001426 if (c != null) {
1427 info.title = c.getString(titleIndex);
1428 }
1429 }
1430 // fall back to the class name of the activity
1431 if (info.title == null) {
1432 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001433 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1435 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001437
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001439 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001441 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001442 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1443 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444
Joe Onorato56d82912010-03-07 14:32:10 -05001445 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001446 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001447 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001449 // TODO: If there's an explicit component and we can't install that, delete it.
1450
Joe Onorato56d82912010-03-07 14:32:10 -05001451 info.title = c.getString(titleIndex);
1452
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 int iconType = c.getInt(iconTypeIndex);
1454 switch (iconType) {
1455 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1456 String packageName = c.getString(iconPackageIndex);
1457 String resourceName = c.getString(iconResourceIndex);
1458 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001459 info.customIcon = false;
1460 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001463 if (resources != null) {
1464 final int id = resources.getIdentifier(resourceName, null, null);
1465 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001468 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
Joe Onorato56d82912010-03-07 14:32:10 -05001470 // the db
1471 if (icon == null) {
1472 icon = getIconFromCursor(c, iconIndex);
1473 }
1474 // the fallback icon
1475 if (icon == null) {
1476 icon = getFallbackIcon();
1477 info.usingFallbackIcon = true;
1478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 break;
1480 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001481 icon = getIconFromCursor(c, iconIndex);
1482 if (icon == null) {
1483 icon = getFallbackIcon();
1484 info.customIcon = false;
1485 info.usingFallbackIcon = true;
1486 } else {
1487 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 break;
1490 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001491 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001492 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 info.customIcon = false;
1494 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001496 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 return info;
1498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499
Joe Onorato56d82912010-03-07 14:32:10 -05001500 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1501 if (false) {
1502 Log.d(TAG, "getIconFromCursor app="
1503 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1504 }
1505 byte[] data = c.getBlob(iconIndex);
1506 try {
1507 return BitmapFactory.decodeByteArray(data, 0, data.length);
1508 } catch (Exception e) {
1509 return null;
1510 }
1511 }
1512
Joe Onorato0589f0f2010-02-08 13:44:00 -08001513 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001514 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001515
1516 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1517 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001518 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001519
1520 return info;
1521 }
1522
1523 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1524 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1525 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1526 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1527
1528 Bitmap icon = null;
1529 boolean filtered = false;
1530 boolean customIcon = false;
1531 ShortcutIconResource iconResource = null;
1532
1533 if (bitmap != null && bitmap instanceof Bitmap) {
1534 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1535 filtered = true;
1536 customIcon = true;
1537 } else {
1538 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1539 if (extra != null && extra instanceof ShortcutIconResource) {
1540 try {
1541 iconResource = (ShortcutIconResource) extra;
1542 final PackageManager packageManager = context.getPackageManager();
1543 Resources resources = packageManager.getResourcesForApplication(
1544 iconResource.packageName);
1545 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1546 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1547 } catch (Exception e) {
1548 Log.w(TAG, "Could not load shortcut icon: " + extra);
1549 }
1550 }
1551 }
1552
Joe Onorato0589f0f2010-02-08 13:44:00 -08001553 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001554
1555 if (icon == null) {
1556 icon = getFallbackIcon();
1557 info.usingFallbackIcon = true;
1558 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001559 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001560
Joe Onorato0589f0f2010-02-08 13:44:00 -08001561 info.title = name;
1562 info.intent = intent;
1563 info.customIcon = customIcon;
1564 info.iconResource = iconResource;
1565
1566 return info;
1567 }
1568
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1570 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1571
1572 int iconType = c.getInt(iconTypeIndex);
1573 switch (iconType) {
1574 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1575 String packageName = c.getString(iconPackageIndex);
1576 String resourceName = c.getString(iconResourceIndex);
1577 PackageManager packageManager = context.getPackageManager();
1578 try {
1579 Resources resources = packageManager.getResourcesForApplication(packageName);
1580 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001581 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1582 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001584 liveFolderInfo.icon = Utilities.createIconBitmap(
1585 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1586 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587 }
1588 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1589 liveFolderInfo.iconResource.packageName = packageName;
1590 liveFolderInfo.iconResource.resourceName = resourceName;
1591 break;
1592 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001593 liveFolderInfo.icon = Utilities.createIconBitmap(
1594 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1595 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001596 }
1597 }
1598
Joe Onorato56d82912010-03-07 14:32:10 -05001599 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1600 // If this icon doesn't have a custom icon, check to see
1601 // what's stored in the DB, and if it doesn't match what
1602 // we're going to show, store what we are going to show back
1603 // into the DB. We do this so when we're loading, if the
1604 // package manager can't find an icon (for example because
1605 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001606 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001607 boolean needSave;
1608 byte[] data = c.getBlob(iconIndex);
1609 try {
1610 if (data != null) {
1611 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1612 Bitmap loaded = info.getIcon(mIconCache);
1613 needSave = !saved.sameAs(loaded);
1614 } else {
1615 needSave = true;
1616 }
1617 } catch (Exception e) {
1618 needSave = true;
1619 }
1620 if (needSave) {
1621 Log.d(TAG, "going to save icon bitmap for info=" + info);
1622 // This is slower than is ideal, but this only happens either
1623 // after the froyo OTA or when the app is updated with a new
1624 // icon.
1625 updateItemInDatabase(context, info);
1626 }
1627 }
1628 }
1629
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 /**
1631 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1632 * or make a new one.
1633 */
1634 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1635 // See if a placeholder was created for us already
1636 FolderInfo folderInfo = folders.get(id);
1637 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1638 // No placeholder -- create a new instance
1639 folderInfo = new UserFolderInfo();
1640 folders.put(id, folderInfo);
1641 }
1642 return (UserFolderInfo) folderInfo;
1643 }
1644
1645 /**
1646 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1647 * new one.
1648 */
1649 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1650 // See if a placeholder was created for us already
1651 FolderInfo folderInfo = folders.get(id);
1652 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1653 // No placeholder -- create a new instance
1654 folderInfo = new LiveFolderInfo();
1655 folders.put(id, folderInfo);
1656 }
1657 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 }
1659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1661 String label = activityInfo.loadLabel(manager).toString();
1662 if (label == null) {
1663 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1664 if (label == null) {
1665 label = activityInfo.name;
1666 }
1667 }
1668 return label;
1669 }
1670
Joe Onorato9c1289c2009-08-17 11:03:03 -04001671 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001672 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001673 = new Comparator<ApplicationInfo>() {
1674 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1675 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677 };
Joe Onoratobe386092009-11-17 17:32:16 -08001678
1679 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001680 Log.d(TAG, "mCallbacks=" + mCallbacks);
1681 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1682 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1683 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1684 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001685 Log.d(TAG, "mItems size=" + mItems.size());
1686 if (mLoaderTask != null) {
1687 mLoaderTask.dumpState();
1688 } else {
1689 Log.d(TAG, "mLoaderTask=null");
1690 }
Joe Onoratobe386092009-11-17 17:32:16 -08001691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692}