blob: aa259aa862258e7185e8310ee8b80b464b287048 [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
Romain Guyedcce092010-03-04 13:03:17 -080055import com.android.launcher.R;
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * Maintains in-memory state of the Launcher. It is expected that there should be only one
59 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070060 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040062public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080063 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040064 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070065
Joe Onorato36115782010-06-17 13:28:48 -040066 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onoratod65d08e2010-04-20 15:43:37 -040067 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040068 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040069
Joe Onoratof99f8c12009-10-31 17:27:36 -040070 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 private final Object mLock = new Object();
72 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040073 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070075 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
76 static {
77 sWorkerThread.start();
78 }
79 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
80
Joe Onoratocc67f472010-06-08 10:54:30 -070081 // We start off with everything not loaded. After that, we assume that
82 // our monitoring of the package manager provides all updates and we never
83 // need to do a requery. These are only ever touched from the loader thread.
84 private boolean mWorkspaceLoaded;
85 private boolean mAllAppsLoaded;
86
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Joe Onorato36115782010-06-17 13:28:48 -040089 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -080090 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -040091 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
92 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
93 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -080094
95 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Adam Cohend22015c2010-07-26 22:02:18 -070097 private static int mCellCountX;
98 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -070099
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700101 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400102 public int getCurrentWorkspaceScreen();
103 public void startBinding();
104 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500105 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 public void finishBindingItems();
107 public void bindAppWidget(LauncherAppWidgetInfo info);
108 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500109 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
110 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400111 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400113 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato0589f0f2010-02-08 13:44:00 -0800116 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400117 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800118 mAllAppsList = new AllAppsList(iconCache);
119 mIconCache = iconCache;
120
121 mDefaultIcon = Utilities.createIconBitmap(
122 app.getPackageManager().getDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400123
124 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400125
126 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800127 }
128
Joe Onorato56d82912010-03-07 14:32:10 -0500129 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800130 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato9c1289c2009-08-17 11:03:03 -0400133 /**
134 * Adds an item to the DB if it was not created previously, or move it to a new
135 * <container, screen, cellX, cellY>
136 */
137 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
138 int screen, int cellX, int cellY) {
139 if (item.container == ItemInfo.NO_ID) {
140 // From all apps
141 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
142 } else {
143 // From somewhere else
144 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
146 }
147
148 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700150 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
152 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700153
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 item.container = container;
155 item.screen = screen;
156 item.cellX = cellX;
157 item.cellY = cellY;
158
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700159 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 final ContentValues values = new ContentValues();
161 final ContentResolver cr = context.getContentResolver();
162
163 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700164 values.put(LauncherSettings.Favorites.CELLX, cellX);
165 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
167
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700168 sWorker.post(new Runnable() {
169 public void run() {
170 cr.update(uri, values, null, null);
171 }
172 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700173 }
174
175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 * Returns true if the shortcuts already exists in the database.
177 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 static boolean shortcutExists(Context context, String title, Intent intent) {
180 final ContentResolver cr = context.getContentResolver();
181 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
182 new String[] { "title", "intent" }, "title=? and intent=?",
183 new String[] { title, intent.toUri(0) }, null);
184 boolean result = false;
185 try {
186 result = c.moveToFirst();
187 } finally {
188 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700191 }
192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700194 * Returns an ItemInfo array containing all the items in the LauncherModel.
195 * The ItemInfo.id is not set through this function.
196 */
197 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
198 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
199 final ContentResolver cr = context.getContentResolver();
200 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
201 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
202 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
203 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
204
205 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
206 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
207 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
208 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
209 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
210 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
211 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
212
213 try {
214 while (c.moveToNext()) {
215 ItemInfo item = new ItemInfo();
216 item.cellX = c.getInt(cellXIndex);
217 item.cellY = c.getInt(cellYIndex);
218 item.spanX = c.getInt(spanXIndex);
219 item.spanY = c.getInt(spanYIndex);
220 item.container = c.getInt(containerIndex);
221 item.itemType = c.getInt(itemTypeIndex);
222 item.screen = c.getInt(screenIndex);
223
224 items.add(item);
225 }
226 } catch (Exception e) {
227 items.clear();
228 } finally {
229 c.close();
230 }
231
232 return items;
233 }
234
235 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
237 */
238 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
239 final ContentResolver cr = context.getContentResolver();
240 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
241 "_id=? and (itemType=? or itemType=?)",
242 new String[] { String.valueOf(id),
243 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
244 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700245
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246 try {
247 if (c.moveToFirst()) {
248 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
249 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
250 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
251 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
252 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
253 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 FolderInfo folderInfo = null;
256 switch (c.getInt(itemTypeIndex)) {
257 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
258 folderInfo = findOrMakeUserFolder(folderList, id);
259 break;
260 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
261 folderInfo = findOrMakeLiveFolder(folderList, id);
262 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700263 }
264
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 folderInfo.title = c.getString(titleIndex);
266 folderInfo.id = id;
267 folderInfo.container = c.getInt(containerIndex);
268 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700269 folderInfo.cellX = c.getInt(cellXIndex);
270 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
272 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700273 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 } finally {
275 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700276 }
277
278 return null;
279 }
280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 /**
282 * Add an item to the database in a specified container. Sets the container, screen, cellX and
283 * cellY fields of the item. Also assigns an ID to the item.
284 */
285 static void addItemToDatabase(Context context, ItemInfo item, long container,
286 int screen, int cellX, int cellY, boolean notify) {
287 item.container = container;
288 item.screen = screen;
289 item.cellX = cellX;
290 item.cellY = cellY;
291
292 final ContentValues values = new ContentValues();
293 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 item.onAddToDatabase(values);
295
Adam Cohend22015c2010-07-26 22:02:18 -0700296 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700297
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
299 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
300
301 if (result != null) {
302 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700303 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700304 }
305
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700307 * Creates a new unique child id, for a given cell span across all layouts.
308 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700309 static int getCellLayoutChildId(
310 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
311 return ((cellId & 0xFF) << 24)
312 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700313 }
314
Adam Cohend22015c2010-07-26 22:02:18 -0700315 static int getCellCountX() {
316 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700317 }
318
Adam Cohend22015c2010-07-26 22:02:18 -0700319 static int getCellCountY() {
320 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700321 }
322
323 /**
324 * Updates the model orientation helper to take into account the current layout dimensions
325 * when performing local/canonical coordinate transformations.
326 */
327 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700328 mCellCountX = shortAxisCellCount;
329 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700330 }
331
332 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400333 * Update an item to the database in a specified container.
334 */
335 static void updateItemInDatabase(Context context, ItemInfo item) {
336 final ContentValues values = new ContentValues();
337 final ContentResolver cr = context.getContentResolver();
338
339 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700340 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700341
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
343 }
344
345 /**
346 * Removes the specified item from the database
347 * @param context
348 * @param item
349 */
350 static void deleteItemFromDatabase(Context context, ItemInfo item) {
351 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700352 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700353 sWorker.post(new Runnable() {
354 public void run() {
355 cr.delete(uriToDelete, null, null);
356 }
357 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 }
359
360 /**
361 * Remove the contents of the specified folder from the database
362 */
363 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
364 final ContentResolver cr = context.getContentResolver();
365
366 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
367 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
368 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
369 }
370
371 /**
372 * Set this as the current Launcher activity object for the loader.
373 */
374 public void initialize(Callbacks callbacks) {
375 synchronized (mLock) {
376 mCallbacks = new WeakReference<Callbacks>(callbacks);
377 }
378 }
379
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700380 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
382 * ACTION_PACKAGE_CHANGED.
383 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400384 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400385 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700386
Joe Onorato36115782010-06-17 13:28:48 -0400387 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400388
Joe Onorato36115782010-06-17 13:28:48 -0400389 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
390 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
391 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
392 final String packageName = intent.getData().getSchemeSpecificPart();
393 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400394
Joe Onorato36115782010-06-17 13:28:48 -0400395 int op = PackageUpdatedTask.OP_NONE;
396
397 if (packageName == null || packageName.length() == 0) {
398 // they sent us a bad intent
399 return;
400 }
401
402 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
403 op = PackageUpdatedTask.OP_UPDATE;
404 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
405 if (!replacing) {
406 op = PackageUpdatedTask.OP_REMOVE;
407 }
408 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
409 // later, we will update the package at this time
410 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
411 if (!replacing) {
412 op = PackageUpdatedTask.OP_ADD;
413 } else {
414 op = PackageUpdatedTask.OP_UPDATE;
415 }
416 }
417
418 if (op != PackageUpdatedTask.OP_NONE) {
419 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
420 }
421
422 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400423 // First, schedule to add these apps back in.
424 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
425 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
426 // Then, rebind everything.
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700427 boolean runLoader = true;
428 if (mCallbacks != null) {
429 Callbacks callbacks = mCallbacks.get();
430 if (callbacks != null) {
431 // If they're paused, we can skip loading, because they'll do it again anyway
432 if (callbacks.setLoadOnResume()) {
433 runLoader = false;
434 }
435 }
436 }
437 if (runLoader) {
438 startLoader(mApp, false);
439 }
Joe Onorato36115782010-06-17 13:28:48 -0400440
441 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
442 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
443 enqueuePackageUpdated(new PackageUpdatedTask(
444 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onorato790c2d92010-06-11 00:14:11 -0700445 }
Joe Onorato36115782010-06-17 13:28:48 -0400446 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400447
Joe Onorato36115782010-06-17 13:28:48 -0400448 public void startLoader(Context context, boolean isLaunching) {
449 synchronized (mLock) {
450 if (DEBUG_LOADERS) {
451 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
452 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400453
Joe Onorato36115782010-06-17 13:28:48 -0400454 // Don't bother to start the thread if we know it's not going to do anything
455 if (mCallbacks != null && mCallbacks.get() != null) {
456 // If there is already one running, tell it to stop.
457 LoaderTask oldTask = mLoaderTask;
458 if (oldTask != null) {
459 if (oldTask.isLaunching()) {
460 // don't downgrade isLaunching if we're already running
461 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500462 }
Joe Onorato36115782010-06-17 13:28:48 -0400463 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500464 }
Joe Onorato36115782010-06-17 13:28:48 -0400465 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700466 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400467 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400468 }
469 }
470
Joe Onorato36115782010-06-17 13:28:48 -0400471 public void stopLoader() {
472 synchronized (mLock) {
473 if (mLoaderTask != null) {
474 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400475 }
476 }
Joe Onorato36115782010-06-17 13:28:48 -0400477 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400478
Joe Onorato36115782010-06-17 13:28:48 -0400479 /**
480 * Runnable for the thread that loads the contents of the launcher:
481 * - workspace icons
482 * - widgets
483 * - all apps icons
484 */
485 private class LoaderTask implements Runnable {
486 private Context mContext;
487 private Thread mWaitThread;
488 private boolean mIsLaunching;
489 private boolean mStopped;
490 private boolean mLoadAndBindStepFinished;
491
492 LoaderTask(Context context, boolean isLaunching) {
493 mContext = context;
494 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400495 }
496
Joe Onorato36115782010-06-17 13:28:48 -0400497 boolean isLaunching() {
498 return mIsLaunching;
499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400500
Joe Onorato36115782010-06-17 13:28:48 -0400501 private void loadAndBindWorkspace() {
502 // Load the workspace
503
504 // For now, just always reload the workspace. It's ~100 ms vs. the
505 // binding which takes many hundreds of ms.
506 // We can reconsider.
507 if (DEBUG_LOADERS) {
508 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400509 }
Joe Onorato36115782010-06-17 13:28:48 -0400510 if (true || !mWorkspaceLoaded) {
511 loadWorkspace();
512 if (mStopped) {
513 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 }
Joe Onorato36115782010-06-17 13:28:48 -0400515 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 }
517
Joe Onorato36115782010-06-17 13:28:48 -0400518 // Bind the workspace
519 bindWorkspace();
520 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400521
Joe Onorato36115782010-06-17 13:28:48 -0400522 private void waitForIdle() {
523 // Wait until the either we're stopped or the other threads are done.
524 // This way we don't start loading all apps until the workspace has settled
525 // down.
526 synchronized (LoaderTask.this) {
527 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700528
Joe Onorato36115782010-06-17 13:28:48 -0400529 mHandler.postIdle(new Runnable() {
530 public void run() {
531 synchronized (LoaderTask.this) {
532 mLoadAndBindStepFinished = true;
533 if (DEBUG_LOADERS) {
534 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400535 }
Joe Onorato36115782010-06-17 13:28:48 -0400536 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400537 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400538 }
Joe Onorato36115782010-06-17 13:28:48 -0400539 });
540
541 while (!mStopped && !mLoadAndBindStepFinished) {
542 try {
543 this.wait();
544 } catch (InterruptedException ex) {
545 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400546 }
547 }
Joe Onorato36115782010-06-17 13:28:48 -0400548 if (DEBUG_LOADERS) {
549 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700550 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400551 + "ms for previous step to finish binding");
552 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400553 }
Joe Onorato36115782010-06-17 13:28:48 -0400554 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400555
Joe Onorato36115782010-06-17 13:28:48 -0400556 public void run() {
557 // Optimize for end-user experience: if the Launcher is up and // running with the
558 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
559 // workspace first (default).
560 final Callbacks cbk = mCallbacks.get();
561 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400562
Joe Onorato36115782010-06-17 13:28:48 -0400563 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400564 // Elevate priority when Home launches for the first time to avoid
565 // starving at boot time. Staring at a blank home is not cool.
566 synchronized (mLock) {
567 android.os.Process.setThreadPriority(mIsLaunching
568 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
569 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400570 if (loadWorkspaceFirst) {
571 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
572 loadAndBindWorkspace();
573 } else {
574 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700575 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400576 }
577
Joe Onorato36115782010-06-17 13:28:48 -0400578 if (mStopped) {
579 break keep_running;
580 }
581
582 // Whew! Hard work done. Slow us down, and wait until the UI thread has
583 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400584 synchronized (mLock) {
585 if (mIsLaunching) {
586 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
587 }
588 }
Joe Onorato36115782010-06-17 13:28:48 -0400589 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400590
591 // second step
592 if (loadWorkspaceFirst) {
593 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700594 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400595 } else {
596 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
597 loadAndBindWorkspace();
598 }
Joe Onorato36115782010-06-17 13:28:48 -0400599 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600
Joe Onorato36115782010-06-17 13:28:48 -0400601 // Clear out this reference, otherwise we end up holding it until all of the
602 // callback runnables are done.
603 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400604
Joe Onorato36115782010-06-17 13:28:48 -0400605 synchronized (mLock) {
606 // If we are still the last one to be scheduled, remove ourselves.
607 if (mLoaderTask == this) {
608 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400609 }
Joe Onorato36115782010-06-17 13:28:48 -0400610 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700611
Joe Onorato36115782010-06-17 13:28:48 -0400612 // Trigger a gc to try to clean up after the stuff is done, since the
613 // renderscript allocations aren't charged to the java heap.
614 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700615 mHandler.post(new Runnable() {
616 public void run() {
617 System.gc();
618 }
619 });
Joe Onorato36115782010-06-17 13:28:48 -0400620 } else {
621 mHandler.postIdle(new Runnable() {
622 public void run() {
623 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400624 }
Joe Onorato36115782010-06-17 13:28:48 -0400625 });
626 }
627 }
628
629 public void stopLocked() {
630 synchronized (LoaderTask.this) {
631 mStopped = true;
632 this.notify();
633 }
634 }
635
636 /**
637 * Gets the callbacks object. If we've been stopped, or if the launcher object
638 * has somehow been garbage collected, return null instead. Pass in the Callbacks
639 * object that was around when the deferred message was scheduled, and if there's
640 * a new Callbacks object around then also return null. This will save us from
641 * calling onto it with data that will be ignored.
642 */
643 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
644 synchronized (mLock) {
645 if (mStopped) {
646 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400647 }
Joe Onorato36115782010-06-17 13:28:48 -0400648
649 if (mCallbacks == null) {
650 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400651 }
Joe Onorato36115782010-06-17 13:28:48 -0400652
653 final Callbacks callbacks = mCallbacks.get();
654 if (callbacks != oldCallbacks) {
655 return null;
656 }
657 if (callbacks == null) {
658 Log.w(TAG, "no mCallbacks");
659 return null;
660 }
661
662 return callbacks;
663 }
664 }
665
666 // check & update map of what's occupied; used to discard overlapping/invalid items
667 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
668 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400669 return true;
670 }
Joe Onorato36115782010-06-17 13:28:48 -0400671 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
672 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
673 if (occupied[item.screen][x][y] != null) {
674 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700675 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400676 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700677 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400678 + occupied[item.screen][x][y]);
679 return false;
680 }
681 }
682 }
683 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
684 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
685 occupied[item.screen][x][y] = item;
686 }
687 }
688 return true;
689 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690
Joe Onorato36115782010-06-17 13:28:48 -0400691 private void loadWorkspace() {
692 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400693
Joe Onorato36115782010-06-17 13:28:48 -0400694 final Context context = mContext;
695 final ContentResolver contentResolver = context.getContentResolver();
696 final PackageManager manager = context.getPackageManager();
697 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
698 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400699
Joe Onorato36115782010-06-17 13:28:48 -0400700 mItems.clear();
701 mAppWidgets.clear();
702 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800703
Joe Onorato36115782010-06-17 13:28:48 -0400704 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705
Joe Onorato36115782010-06-17 13:28:48 -0400706 final Cursor c = contentResolver.query(
707 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400708
Adam Cohend22015c2010-07-26 22:02:18 -0700709 final ItemInfo occupied[][][] =
710 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711
Joe Onorato36115782010-06-17 13:28:48 -0400712 try {
713 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
714 final int intentIndex = c.getColumnIndexOrThrow
715 (LauncherSettings.Favorites.INTENT);
716 final int titleIndex = c.getColumnIndexOrThrow
717 (LauncherSettings.Favorites.TITLE);
718 final int iconTypeIndex = c.getColumnIndexOrThrow(
719 LauncherSettings.Favorites.ICON_TYPE);
720 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
721 final int iconPackageIndex = c.getColumnIndexOrThrow(
722 LauncherSettings.Favorites.ICON_PACKAGE);
723 final int iconResourceIndex = c.getColumnIndexOrThrow(
724 LauncherSettings.Favorites.ICON_RESOURCE);
725 final int containerIndex = c.getColumnIndexOrThrow(
726 LauncherSettings.Favorites.CONTAINER);
727 final int itemTypeIndex = c.getColumnIndexOrThrow(
728 LauncherSettings.Favorites.ITEM_TYPE);
729 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
730 LauncherSettings.Favorites.APPWIDGET_ID);
731 final int screenIndex = c.getColumnIndexOrThrow(
732 LauncherSettings.Favorites.SCREEN);
733 final int cellXIndex = c.getColumnIndexOrThrow
734 (LauncherSettings.Favorites.CELLX);
735 final int cellYIndex = c.getColumnIndexOrThrow
736 (LauncherSettings.Favorites.CELLY);
737 final int spanXIndex = c.getColumnIndexOrThrow
738 (LauncherSettings.Favorites.SPANX);
739 final int spanYIndex = c.getColumnIndexOrThrow(
740 LauncherSettings.Favorites.SPANY);
741 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
742 final int displayModeIndex = c.getColumnIndexOrThrow(
743 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744
Joe Onorato36115782010-06-17 13:28:48 -0400745 ShortcutInfo info;
746 String intentDescription;
747 LauncherAppWidgetInfo appWidgetInfo;
748 int container;
749 long id;
750 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751
Joe Onorato36115782010-06-17 13:28:48 -0400752 while (!mStopped && c.moveToNext()) {
753 try {
754 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755
Joe Onorato36115782010-06-17 13:28:48 -0400756 switch (itemType) {
757 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
758 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
759 intentDescription = c.getString(intentIndex);
760 try {
761 intent = Intent.parseUri(intentDescription, 0);
762 } catch (URISyntaxException e) {
763 continue;
764 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400765
Joe Onorato36115782010-06-17 13:28:48 -0400766 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
767 info = getShortcutInfo(manager, intent, context, c, iconIndex,
768 titleIndex);
769 } else {
770 info = getShortcutInfo(c, context, iconTypeIndex,
771 iconPackageIndex, iconResourceIndex, iconIndex,
772 titleIndex);
773 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774
Joe Onorato36115782010-06-17 13:28:48 -0400775 if (info != null) {
776 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400777
Joe Onorato36115782010-06-17 13:28:48 -0400778 info.intent = intent;
779 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400781 info.container = container;
782 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700783 info.cellX = c.getInt(cellXIndex);
784 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785
Daniel Sandler8802e962010-05-26 16:28:16 -0400786 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400787 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400788 break;
789 }
790
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400792 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
793 mItems.add(info);
794 break;
795 default:
796 // Item is in a user folder
797 UserFolderInfo folderInfo =
798 findOrMakeUserFolder(mFolders, container);
799 folderInfo.add(info);
800 break;
801 }
802 } else {
803 // Failed to load the shortcut, probably because the
804 // activity manager couldn't resolve it (maybe the app
805 // was uninstalled), or the db row was somehow screwed up.
806 // Delete it.
807 id = c.getLong(idIndex);
808 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
809 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
810 id, false), null, null);
811 }
812 break;
813
814 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
815 id = c.getLong(idIndex);
816 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
817
Winson Chungaafa03c2010-06-11 17:34:16 -0700818 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400819 folderInfo.id = id;
820 container = c.getInt(containerIndex);
821 folderInfo.container = container;
822 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700823 folderInfo.cellX = c.getInt(cellXIndex);
824 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400825
826 // check & update map of what's occupied
827 if (!checkItemPlacement(occupied, folderInfo)) {
828 break;
829 }
Joe Onorato36115782010-06-17 13:28:48 -0400830 switch (container) {
831 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
832 mItems.add(folderInfo);
833 break;
834 }
835
836 mFolders.put(folderInfo.id, folderInfo);
837 break;
838
839 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
840 id = c.getLong(idIndex);
841 Uri uri = Uri.parse(c.getString(uriIndex));
842
843 // Make sure the live folder exists
844 final ProviderInfo providerInfo =
845 context.getPackageManager().resolveContentProvider(
846 uri.getAuthority(), 0);
847
848 if (providerInfo == null && !isSafeMode) {
849 itemsToRemove.add(id);
850 } else {
851 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400852 intentDescription = c.getString(intentIndex);
853 intent = null;
854 if (intentDescription != null) {
855 try {
856 intent = Intent.parseUri(intentDescription, 0);
857 } catch (URISyntaxException e) {
858 // Ignore, a live folder might not have a base intent
859 }
860 }
861
862 liveFolderInfo.title = c.getString(titleIndex);
863 liveFolderInfo.id = id;
864 liveFolderInfo.uri = uri;
865 container = c.getInt(containerIndex);
866 liveFolderInfo.container = container;
867 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700868 liveFolderInfo.cellX = c.getInt(cellXIndex);
869 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400870 liveFolderInfo.baseIntent = intent;
871 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
872
873 // check & update map of what's occupied
874 if (!checkItemPlacement(occupied, liveFolderInfo)) {
875 break;
876 }
877
878 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
879 iconResourceIndex, liveFolderInfo);
880
881 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400883 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 break;
885 }
Joe Onorato36115782010-06-17 13:28:48 -0400886 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887 }
Joe Onorato36115782010-06-17 13:28:48 -0400888 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800889
Joe Onorato36115782010-06-17 13:28:48 -0400890 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
891 // Read all Launcher-specific widget details
892 int appWidgetId = c.getInt(appWidgetIdIndex);
893 id = c.getLong(idIndex);
894
895 final AppWidgetProviderInfo provider =
896 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700897
Joe Onorato36115782010-06-17 13:28:48 -0400898 if (!isSafeMode && (provider == null || provider.provider == null ||
899 provider.provider.getPackageName() == null)) {
900 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
901 + id + " appWidgetId=" + appWidgetId);
902 itemsToRemove.add(id);
903 } else {
904 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
905 appWidgetInfo.id = id;
906 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700907 appWidgetInfo.cellX = c.getInt(cellXIndex);
908 appWidgetInfo.cellY = c.getInt(cellYIndex);
909 appWidgetInfo.spanX = c.getInt(spanXIndex);
910 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400911
912 container = c.getInt(containerIndex);
913 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
914 Log.e(TAG, "Widget found where container "
915 + "!= CONTAINER_DESKTOP -- ignoring!");
916 continue;
917 }
918 appWidgetInfo.container = c.getInt(containerIndex);
919
920 // check & update map of what's occupied
921 if (!checkItemPlacement(occupied, appWidgetInfo)) {
922 break;
923 }
924
925 mAppWidgets.add(appWidgetInfo);
926 }
927 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800928 }
Joe Onorato36115782010-06-17 13:28:48 -0400929 } catch (Exception e) {
930 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800931 }
932 }
Joe Onorato36115782010-06-17 13:28:48 -0400933 } finally {
934 c.close();
935 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800936
Joe Onorato36115782010-06-17 13:28:48 -0400937 if (itemsToRemove.size() > 0) {
938 ContentProviderClient client = contentResolver.acquireContentProviderClient(
939 LauncherSettings.Favorites.CONTENT_URI);
940 // Remove dead items
941 for (long id : itemsToRemove) {
942 if (DEBUG_LOADERS) {
943 Log.d(TAG, "Removed id = " + id);
944 }
945 // Don't notify content observers
946 try {
947 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
948 null, null);
949 } catch (RemoteException e) {
950 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400951 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953 }
954
Joe Onorato36115782010-06-17 13:28:48 -0400955 if (DEBUG_LOADERS) {
956 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
957 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700958 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400959 String line = "";
960 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
961 if (s > 0) {
962 line += " | ";
963 }
Adam Cohend22015c2010-07-26 22:02:18 -0700964 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400965 line += ((occupied[s][x][y] != null) ? "#" : ".");
966 }
967 }
968 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 }
Joe Onorato36115782010-06-17 13:28:48 -0400970 }
971 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972
Joe Onorato36115782010-06-17 13:28:48 -0400973 /**
974 * Read everything out of our database.
975 */
976 private void bindWorkspace() {
977 final long t = SystemClock.uptimeMillis();
978
979 // Don't use these two variables in any of the callback runnables.
980 // Otherwise we hold a reference to them.
981 final Callbacks oldCallbacks = mCallbacks.get();
982 if (oldCallbacks == null) {
983 // This launcher has exited and nobody bothered to tell us. Just bail.
984 Log.w(TAG, "LoaderTask running with no launcher");
985 return;
986 }
987
988 int N;
989 // Tell the workspace that we're about to start firing items at it
990 mHandler.post(new Runnable() {
991 public void run() {
992 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
993 if (callbacks != null) {
994 callbacks.startBinding();
995 }
996 }
997 });
998 // Add the items to the workspace.
999 N = mItems.size();
1000 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1001 final int start = i;
1002 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 mHandler.post(new Runnable() {
1004 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001005 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001007 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008 }
1009 }
1010 });
Joe Onorato36115782010-06-17 13:28:48 -04001011 }
1012 mHandler.post(new Runnable() {
1013 public void run() {
1014 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1015 if (callbacks != null) {
1016 callbacks.bindFolders(mFolders);
1017 }
1018 }
1019 });
1020 // Wait until the queue goes empty.
1021 mHandler.post(new Runnable() {
1022 public void run() {
1023 if (DEBUG_LOADERS) {
1024 Log.d(TAG, "Going to start binding widgets soon.");
1025 }
1026 }
1027 });
1028 // Bind the widgets, one at a time.
1029 // WARNING: this is calling into the workspace from the background thread,
1030 // but since getCurrentScreen() just returns the int, we should be okay. This
1031 // is just a hint for the order, and if it's wrong, we'll be okay.
1032 // TODO: instead, we should have that push the current screen into here.
1033 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1034 N = mAppWidgets.size();
1035 // once for the current screen
1036 for (int i=0; i<N; i++) {
1037 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1038 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 mHandler.post(new Runnable() {
1040 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001041 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001043 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044 }
1045 }
1046 });
1047 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 }
Joe Onorato36115782010-06-17 13:28:48 -04001049 // once for the other screens
1050 for (int i=0; i<N; i++) {
1051 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1052 if (widget.screen != currentScreen) {
1053 mHandler.post(new Runnable() {
1054 public void run() {
1055 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1056 if (callbacks != null) {
1057 callbacks.bindAppWidget(widget);
1058 }
1059 }
1060 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001061 }
1062 }
Joe Onorato36115782010-06-17 13:28:48 -04001063 // Tell the workspace that we're done.
1064 mHandler.post(new Runnable() {
1065 public void run() {
1066 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1067 if (callbacks != null) {
1068 callbacks.finishBindingItems();
1069 }
1070 }
1071 });
1072 // If we're profiling, this is the last thing in the queue.
1073 mHandler.post(new Runnable() {
1074 public void run() {
1075 if (DEBUG_LOADERS) {
1076 Log.d(TAG, "bound workspace in "
1077 + (SystemClock.uptimeMillis()-t) + "ms");
1078 }
1079 }
1080 });
1081 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001082
Joe Onorato36115782010-06-17 13:28:48 -04001083 private void loadAndBindAllApps() {
1084 if (DEBUG_LOADERS) {
1085 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1086 }
1087 if (!mAllAppsLoaded) {
1088 loadAllAppsByBatch();
1089 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001090 return;
1091 }
Joe Onorato36115782010-06-17 13:28:48 -04001092 mAllAppsLoaded = true;
1093 } else {
1094 onlyBindAllApps();
1095 }
1096 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001097
Joe Onorato36115782010-06-17 13:28:48 -04001098 private void onlyBindAllApps() {
1099 final Callbacks oldCallbacks = mCallbacks.get();
1100 if (oldCallbacks == null) {
1101 // This launcher has exited and nobody bothered to tell us. Just bail.
1102 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1103 return;
1104 }
1105
1106 // shallow copy
1107 final ArrayList<ApplicationInfo> list
1108 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1109 mHandler.post(new Runnable() {
1110 public void run() {
1111 final long t = SystemClock.uptimeMillis();
1112 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1113 if (callbacks != null) {
1114 callbacks.bindAllApplications(list);
1115 }
1116 if (DEBUG_LOADERS) {
1117 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1118 + (SystemClock.uptimeMillis()-t) + "ms");
1119 }
1120 }
1121 });
1122
1123 }
1124
1125 private void loadAllAppsByBatch() {
1126 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1127
1128 // Don't use these two variables in any of the callback runnables.
1129 // Otherwise we hold a reference to them.
1130 final Callbacks oldCallbacks = mCallbacks.get();
1131 if (oldCallbacks == null) {
1132 // This launcher has exited and nobody bothered to tell us. Just bail.
1133 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1134 return;
1135 }
1136
1137 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1138 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1139
1140 final PackageManager packageManager = mContext.getPackageManager();
1141 List<ResolveInfo> apps = null;
1142
1143 int N = Integer.MAX_VALUE;
1144
1145 int startIndex;
1146 int i=0;
1147 int batchSize = -1;
1148 while (i < N && !mStopped) {
1149 if (i == 0) {
1150 mAllAppsList.clear();
1151 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1152 apps = packageManager.queryIntentActivities(mainIntent, 0);
1153 if (DEBUG_LOADERS) {
1154 Log.d(TAG, "queryIntentActivities took "
1155 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1156 }
1157 if (apps == null) {
1158 return;
1159 }
1160 N = apps.size();
1161 if (DEBUG_LOADERS) {
1162 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1163 }
1164 if (N == 0) {
1165 // There are no apps?!?
1166 return;
1167 }
1168 if (mBatchSize == 0) {
1169 batchSize = N;
1170 } else {
1171 batchSize = mBatchSize;
1172 }
1173
1174 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1175 Collections.sort(apps,
1176 new ResolveInfo.DisplayNameComparator(packageManager));
1177 if (DEBUG_LOADERS) {
1178 Log.d(TAG, "sort took "
1179 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1180 }
1181 }
1182
1183 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1184
1185 startIndex = i;
1186 for (int j=0; i<N && j<batchSize; j++) {
1187 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001188 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001189 i++;
1190 }
1191
1192 final boolean first = i <= batchSize;
1193 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1194 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1195 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1196
Joe Onoratocc67f472010-06-08 10:54:30 -07001197 mHandler.post(new Runnable() {
1198 public void run() {
1199 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001200 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001201 if (first) {
1202 callbacks.bindAllApplications(added);
1203 } else {
1204 callbacks.bindAppsAdded(added);
1205 }
1206 if (DEBUG_LOADERS) {
1207 Log.d(TAG, "bound " + added.size() + " apps in "
1208 + (SystemClock.uptimeMillis() - t) + "ms");
1209 }
1210 } else {
1211 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001212 }
1213 }
1214 });
1215
Daniel Sandlerdca66122010-04-13 16:23:58 -04001216 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001217 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1218 + (SystemClock.uptimeMillis()-t2) + "ms");
1219 }
1220
1221 if (mAllAppsLoadDelay > 0 && i < N) {
1222 try {
1223 if (DEBUG_LOADERS) {
1224 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1225 }
1226 Thread.sleep(mAllAppsLoadDelay);
1227 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228 }
1229 }
1230
Joe Onorato36115782010-06-17 13:28:48 -04001231 if (DEBUG_LOADERS) {
1232 Log.d(TAG, "cached all " + N + " apps in "
1233 + (SystemClock.uptimeMillis()-t) + "ms"
1234 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001235 }
1236 }
1237
1238 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001239 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1240 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1241 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1242 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1243 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1244 }
1245 }
1246
1247 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001248 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001249 }
1250
1251 private class PackageUpdatedTask implements Runnable {
1252 int mOp;
1253 String[] mPackages;
1254
1255 public static final int OP_NONE = 0;
1256 public static final int OP_ADD = 1;
1257 public static final int OP_UPDATE = 2;
1258 public static final int OP_REMOVE = 3; // uninstlled
1259 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1260
1261
1262 public PackageUpdatedTask(int op, String[] packages) {
1263 mOp = op;
1264 mPackages = packages;
1265 }
1266
1267 public void run() {
1268 final Context context = mApp;
1269
1270 final String[] packages = mPackages;
1271 final int N = packages.length;
1272 switch (mOp) {
1273 case OP_ADD:
1274 for (int i=0; i<N; i++) {
1275 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1276 mAllAppsList.addPackage(context, packages[i]);
1277 }
1278 break;
1279 case OP_UPDATE:
1280 for (int i=0; i<N; i++) {
1281 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1282 mAllAppsList.updatePackage(context, packages[i]);
1283 }
1284 break;
1285 case OP_REMOVE:
1286 case OP_UNAVAILABLE:
1287 for (int i=0; i<N; i++) {
1288 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1289 mAllAppsList.removePackage(packages[i]);
1290 }
1291 break;
1292 }
1293
1294 ArrayList<ApplicationInfo> added = null;
1295 ArrayList<ApplicationInfo> removed = null;
1296 ArrayList<ApplicationInfo> modified = null;
1297
1298 if (mAllAppsList.added.size() > 0) {
1299 added = mAllAppsList.added;
1300 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1301 }
1302 if (mAllAppsList.removed.size() > 0) {
1303 removed = mAllAppsList.removed;
1304 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1305 for (ApplicationInfo info: removed) {
1306 mIconCache.remove(info.intent.getComponent());
1307 }
1308 }
1309 if (mAllAppsList.modified.size() > 0) {
1310 modified = mAllAppsList.modified;
1311 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1312 }
1313
1314 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1315 if (callbacks == null) {
1316 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1317 return;
1318 }
1319
1320 if (added != null) {
1321 final ArrayList<ApplicationInfo> addedFinal = added;
1322 mHandler.post(new Runnable() {
1323 public void run() {
1324 if (callbacks == mCallbacks.get()) {
1325 callbacks.bindAppsAdded(addedFinal);
1326 }
1327 }
1328 });
1329 }
1330 if (modified != null) {
1331 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1332 mHandler.post(new Runnable() {
1333 public void run() {
1334 if (callbacks == mCallbacks.get()) {
1335 callbacks.bindAppsUpdated(modifiedFinal);
1336 }
1337 }
1338 });
1339 }
1340 if (removed != null) {
1341 final boolean permanent = mOp != OP_UNAVAILABLE;
1342 final ArrayList<ApplicationInfo> removedFinal = removed;
1343 mHandler.post(new Runnable() {
1344 public void run() {
1345 if (callbacks == mCallbacks.get()) {
1346 callbacks.bindAppsRemoved(removedFinal, permanent);
1347 }
1348 }
1349 });
Joe Onoratobe386092009-11-17 17:32:16 -08001350 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001351
1352 mHandler.post(new Runnable() {
1353 @Override
1354 public void run() {
1355 if (callbacks == mCallbacks.get()) {
1356 callbacks.bindPackagesUpdated();
1357 }
1358 }
1359 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 }
1361 }
1362
1363 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001364 * This is called from the code that adds shortcuts from the intent receiver. This
1365 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 */
Joe Onorato56d82912010-03-07 14:32:10 -05001367 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001368 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370
Joe Onorato56d82912010-03-07 14:32:10 -05001371 /**
1372 * Make an ShortcutInfo object for a shortcut that is an application.
1373 *
1374 * If c is not null, then it will be used to fill in missing data like the title and icon.
1375 */
1376 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1377 Cursor c, int iconIndex, int titleIndex) {
1378 Bitmap icon = null;
1379 final ShortcutInfo info = new ShortcutInfo();
1380
1381 ComponentName componentName = intent.getComponent();
1382 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001383 return null;
1384 }
1385
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001386 // TODO: See if the PackageManager knows about this case. If it doesn't
1387 // then return null & delete this.
1388
Joe Onorato56d82912010-03-07 14:32:10 -05001389 // the resource -- This may implicitly give us back the fallback icon,
1390 // but don't worry about that. All we're doing with usingFallbackIcon is
1391 // to avoid saving lots of copies of that in the database, and most apps
1392 // have icons anyway.
1393 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1394 if (resolveInfo != null) {
1395 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001396 }
Joe Onorato56d82912010-03-07 14:32:10 -05001397 // the db
1398 if (icon == null) {
1399 if (c != null) {
1400 icon = getIconFromCursor(c, iconIndex);
1401 }
1402 }
1403 // the fallback icon
1404 if (icon == null) {
1405 icon = getFallbackIcon();
1406 info.usingFallbackIcon = true;
1407 }
1408 info.setIcon(icon);
1409
1410 // from the resource
1411 if (resolveInfo != null) {
1412 info.title = resolveInfo.activityInfo.loadLabel(manager);
1413 }
1414 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001415 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001416 if (c != null) {
1417 info.title = c.getString(titleIndex);
1418 }
1419 }
1420 // fall back to the class name of the activity
1421 if (info.title == null) {
1422 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1425 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001429 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001431 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001432 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1433 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434
Joe Onorato56d82912010-03-07 14:32:10 -05001435 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001436 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001439 // TODO: If there's an explicit component and we can't install that, delete it.
1440
Joe Onorato56d82912010-03-07 14:32:10 -05001441 info.title = c.getString(titleIndex);
1442
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 int iconType = c.getInt(iconTypeIndex);
1444 switch (iconType) {
1445 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1446 String packageName = c.getString(iconPackageIndex);
1447 String resourceName = c.getString(iconResourceIndex);
1448 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001449 info.customIcon = false;
1450 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001453 if (resources != null) {
1454 final int id = resources.getIdentifier(resourceName, null, null);
1455 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1456 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001457 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001458 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
Joe Onorato56d82912010-03-07 14:32:10 -05001460 // the db
1461 if (icon == null) {
1462 icon = getIconFromCursor(c, iconIndex);
1463 }
1464 // the fallback icon
1465 if (icon == null) {
1466 icon = getFallbackIcon();
1467 info.usingFallbackIcon = true;
1468 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469 break;
1470 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001471 icon = getIconFromCursor(c, iconIndex);
1472 if (icon == null) {
1473 icon = getFallbackIcon();
1474 info.customIcon = false;
1475 info.usingFallbackIcon = true;
1476 } else {
1477 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 break;
1480 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001481 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001482 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483 info.customIcon = false;
1484 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001486 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001487 return info;
1488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489
Joe Onorato56d82912010-03-07 14:32:10 -05001490 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1491 if (false) {
1492 Log.d(TAG, "getIconFromCursor app="
1493 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1494 }
1495 byte[] data = c.getBlob(iconIndex);
1496 try {
1497 return BitmapFactory.decodeByteArray(data, 0, data.length);
1498 } catch (Exception e) {
1499 return null;
1500 }
1501 }
1502
Joe Onorato0589f0f2010-02-08 13:44:00 -08001503 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001505
Winson Chunga9abd0e2010-10-27 17:18:37 -07001506 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001507 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001508 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001509
1510 return info;
1511 }
1512
Winson Chunga9abd0e2010-10-27 17:18:37 -07001513 /**
1514 * Ensures that a given shortcut intent actually has all the fields that we need to create a
1515 * proper ShortcutInfo.
1516 */
1517 boolean validateShortcutIntent(Intent data) {
1518 // We don't require Intent.EXTRA_SHORTCUT_ICON, since we can pull a default fallback icon
1519 return InstallShortcutReceiver.ACTION_INSTALL_SHORTCUT.equals(data.getAction()) &&
Winson Chung55cef262010-10-28 14:14:18 -07001520 (data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME) != null) &&
1521 (data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT) != null);
1522 }
1523
1524 /**
1525 * Ensures that a given widget intent actually has all the fields that we need to create a
1526 * proper widget.
1527 */
1528 boolean validateWidgetIntent(Intent data) {
1529 // We don't require Intent.EXTRA_APPWIDGET_CONFIGURATION_DATA, since that will just be
1530 // forwarded onto the widget's configuration activity if it exists
1531 return InstallWidgetReceiver.ACTION_INSTALL_WIDGET.equals(data.getAction()) &&
1532 (data.getStringExtra(InstallWidgetReceiver.EXTRA_APPWIDGET_COMPONENT) != null);
1533 }
1534
1535 /**
1536 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1537 */
1538 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1539 ComponentName component) {
1540 List<AppWidgetProviderInfo> widgets =
1541 AppWidgetManager.getInstance(context).getInstalledProviders();
1542 for (AppWidgetProviderInfo info : widgets) {
1543 if (info.provider.equals(component)) {
1544 return info;
1545 }
1546 }
1547 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001548 }
1549
1550 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001551 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1552 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1553 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1554
1555 Bitmap icon = null;
1556 boolean filtered = false;
1557 boolean customIcon = false;
1558 ShortcutIconResource iconResource = null;
1559
1560 if (bitmap != null && bitmap instanceof Bitmap) {
1561 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1562 filtered = true;
1563 customIcon = true;
1564 } else {
1565 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1566 if (extra != null && extra instanceof ShortcutIconResource) {
1567 try {
1568 iconResource = (ShortcutIconResource) extra;
1569 final PackageManager packageManager = context.getPackageManager();
1570 Resources resources = packageManager.getResourcesForApplication(
1571 iconResource.packageName);
1572 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1573 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1574 } catch (Exception e) {
1575 Log.w(TAG, "Could not load shortcut icon: " + extra);
1576 }
1577 }
1578 }
1579
Joe Onorato0589f0f2010-02-08 13:44:00 -08001580 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001581
1582 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001583 if (fallbackIcon != null) {
1584 icon = fallbackIcon;
1585 } else {
1586 icon = getFallbackIcon();
1587 info.usingFallbackIcon = true;
1588 }
Joe Onorato56d82912010-03-07 14:32:10 -05001589 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001590 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001591
Joe Onorato0589f0f2010-02-08 13:44:00 -08001592 info.title = name;
1593 info.intent = intent;
1594 info.customIcon = customIcon;
1595 info.iconResource = iconResource;
1596
1597 return info;
1598 }
1599
Joe Onorato9c1289c2009-08-17 11:03:03 -04001600 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1601 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1602
1603 int iconType = c.getInt(iconTypeIndex);
1604 switch (iconType) {
1605 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1606 String packageName = c.getString(iconPackageIndex);
1607 String resourceName = c.getString(iconResourceIndex);
1608 PackageManager packageManager = context.getPackageManager();
1609 try {
1610 Resources resources = packageManager.getResourcesForApplication(packageName);
1611 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001612 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1613 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001614 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001615 liveFolderInfo.icon = Utilities.createIconBitmap(
1616 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1617 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001618 }
1619 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1620 liveFolderInfo.iconResource.packageName = packageName;
1621 liveFolderInfo.iconResource.resourceName = resourceName;
1622 break;
1623 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001624 liveFolderInfo.icon = Utilities.createIconBitmap(
1625 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1626 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627 }
1628 }
1629
Joe Onorato56d82912010-03-07 14:32:10 -05001630 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1631 // If this icon doesn't have a custom icon, check to see
1632 // what's stored in the DB, and if it doesn't match what
1633 // we're going to show, store what we are going to show back
1634 // into the DB. We do this so when we're loading, if the
1635 // package manager can't find an icon (for example because
1636 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001637 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001638 boolean needSave;
1639 byte[] data = c.getBlob(iconIndex);
1640 try {
1641 if (data != null) {
1642 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1643 Bitmap loaded = info.getIcon(mIconCache);
1644 needSave = !saved.sameAs(loaded);
1645 } else {
1646 needSave = true;
1647 }
1648 } catch (Exception e) {
1649 needSave = true;
1650 }
1651 if (needSave) {
1652 Log.d(TAG, "going to save icon bitmap for info=" + info);
1653 // This is slower than is ideal, but this only happens either
1654 // after the froyo OTA or when the app is updated with a new
1655 // icon.
1656 updateItemInDatabase(context, info);
1657 }
1658 }
1659 }
1660
Joe Onorato9c1289c2009-08-17 11:03:03 -04001661 /**
1662 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1663 * or make a new one.
1664 */
1665 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1666 // See if a placeholder was created for us already
1667 FolderInfo folderInfo = folders.get(id);
1668 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1669 // No placeholder -- create a new instance
1670 folderInfo = new UserFolderInfo();
1671 folders.put(id, folderInfo);
1672 }
1673 return (UserFolderInfo) folderInfo;
1674 }
1675
1676 /**
1677 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1678 * new one.
1679 */
1680 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1681 // See if a placeholder was created for us already
1682 FolderInfo folderInfo = folders.get(id);
1683 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1684 // No placeholder -- create a new instance
1685 folderInfo = new LiveFolderInfo();
1686 folders.put(id, folderInfo);
1687 }
1688 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 }
1690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1692 String label = activityInfo.loadLabel(manager).toString();
1693 if (label == null) {
1694 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1695 if (label == null) {
1696 label = activityInfo.name;
1697 }
1698 }
1699 return label;
1700 }
1701
Joe Onorato9c1289c2009-08-17 11:03:03 -04001702 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001703 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704 = new Comparator<ApplicationInfo>() {
1705 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1706 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001708 };
Joe Onoratobe386092009-11-17 17:32:16 -08001709
1710 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001711 Log.d(TAG, "mCallbacks=" + mCallbacks);
1712 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1713 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1714 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1715 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001716 Log.d(TAG, "mItems size=" + mItems.size());
1717 if (mLoaderTask != null) {
1718 mLoaderTask.dumpState();
1719 } else {
1720 Log.d(TAG, "mLoaderTask=null");
1721 }
Joe Onoratobe386092009-11-17 17:32:16 -08001722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723}