blob: 5b4ff5a2bf9d5df8fe1e055d6688a70d6eae8e53 [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 HandlerThread mWorkerThread;
74 private Handler mWorker;
75 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Joe Onoratocc67f472010-06-08 10:54:30 -070077 // We start off with everything not loaded. After that, we assume that
78 // our monitoring of the package manager provides all updates and we never
79 // need to do a requery. These are only ever touched from the loader thread.
80 private boolean mWorkspaceLoaded;
81 private boolean mAllAppsLoaded;
82
Joe Onorato9c1289c2009-08-17 11:03:03 -040083 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Joe Onorato36115782010-06-17 13:28:48 -040085 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -080086 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -040087 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
88 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
89 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -080090
91 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Adam Cohend22015c2010-07-26 22:02:18 -070093 private static int mCellCountX;
94 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -070095
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 public interface Callbacks {
97 public int getCurrentWorkspaceScreen();
98 public void startBinding();
99 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500100 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400101 public void finishBindingItems();
102 public void bindAppWidget(LauncherAppWidgetInfo info);
103 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500104 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
105 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400106 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700107 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400108 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Joe Onorato0589f0f2010-02-08 13:44:00 -0800111 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400112 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800113 mAllAppsList = new AllAppsList(iconCache);
114 mIconCache = iconCache;
115
116 mDefaultIcon = Utilities.createIconBitmap(
117 app.getPackageManager().getDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400118
119 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400120
121 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato36115782010-06-17 13:28:48 -0400122
123 mWorkerThread = new HandlerThread("launcher-loader");
124 mWorkerThread.start();
125 mWorker = new Handler(mWorkerThread.getLooper());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800126 }
127
Joe Onorato56d82912010-03-07 14:32:10 -0500128 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800129 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Joe Onorato9c1289c2009-08-17 11:03:03 -0400132 /**
133 * Adds an item to the DB if it was not created previously, or move it to a new
134 * <container, screen, cellX, cellY>
135 */
136 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
137 int screen, int cellX, int cellY) {
138 if (item.container == ItemInfo.NO_ID) {
139 // From all apps
140 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
141 } else {
142 // From somewhere else
143 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 }
145 }
146
147 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700149 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
151 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 item.container = container;
154 item.screen = screen;
155 item.cellX = cellX;
156 item.cellY = cellY;
157
158 final ContentValues values = new ContentValues();
159 final ContentResolver cr = context.getContentResolver();
160
161 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700162 values.put(LauncherSettings.Favorites.CELLX, cellX);
163 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
165
166 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700167 }
168
169 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 * Returns true if the shortcuts already exists in the database.
171 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 static boolean shortcutExists(Context context, String title, Intent intent) {
174 final ContentResolver cr = context.getContentResolver();
175 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
176 new String[] { "title", "intent" }, "title=? and intent=?",
177 new String[] { title, intent.toUri(0) }, null);
178 boolean result = false;
179 try {
180 result = c.moveToFirst();
181 } finally {
182 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700185 }
186
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700188 * Returns an ItemInfo array containing all the items in the LauncherModel.
189 * The ItemInfo.id is not set through this function.
190 */
191 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
192 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
193 final ContentResolver cr = context.getContentResolver();
194 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
195 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
196 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
197 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
198
199 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
200 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
201 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
202 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
203 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
204 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
205 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
206
207 try {
208 while (c.moveToNext()) {
209 ItemInfo item = new ItemInfo();
210 item.cellX = c.getInt(cellXIndex);
211 item.cellY = c.getInt(cellYIndex);
212 item.spanX = c.getInt(spanXIndex);
213 item.spanY = c.getInt(spanYIndex);
214 item.container = c.getInt(containerIndex);
215 item.itemType = c.getInt(itemTypeIndex);
216 item.screen = c.getInt(screenIndex);
217
218 items.add(item);
219 }
220 } catch (Exception e) {
221 items.clear();
222 } finally {
223 c.close();
224 }
225
226 return items;
227 }
228
229 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
231 */
232 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
233 final ContentResolver cr = context.getContentResolver();
234 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
235 "_id=? and (itemType=? or itemType=?)",
236 new String[] { String.valueOf(id),
237 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
238 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700239
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240 try {
241 if (c.moveToFirst()) {
242 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
243 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
244 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
245 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
246 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
247 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248
Joe Onorato9c1289c2009-08-17 11:03:03 -0400249 FolderInfo folderInfo = null;
250 switch (c.getInt(itemTypeIndex)) {
251 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
252 folderInfo = findOrMakeUserFolder(folderList, id);
253 break;
254 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
255 folderInfo = findOrMakeLiveFolder(folderList, id);
256 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700257 }
258
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259 folderInfo.title = c.getString(titleIndex);
260 folderInfo.id = id;
261 folderInfo.container = c.getInt(containerIndex);
262 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700263 folderInfo.cellX = c.getInt(cellXIndex);
264 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
266 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700267 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 } finally {
269 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700270 }
271
272 return null;
273 }
274
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 /**
276 * Add an item to the database in a specified container. Sets the container, screen, cellX and
277 * cellY fields of the item. Also assigns an ID to the item.
278 */
279 static void addItemToDatabase(Context context, ItemInfo item, long container,
280 int screen, int cellX, int cellY, boolean notify) {
281 item.container = container;
282 item.screen = screen;
283 item.cellX = cellX;
284 item.cellY = cellY;
285
286 final ContentValues values = new ContentValues();
287 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288 item.onAddToDatabase(values);
289
Adam Cohend22015c2010-07-26 22:02:18 -0700290 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700291
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
293 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
294
295 if (result != null) {
296 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700297 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700298 }
299
Joe Onorato9c1289c2009-08-17 11:03:03 -0400300 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700301 * Creates a new unique child id, for a given cell span across all layouts.
302 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700303 static int getCellLayoutChildId(
304 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
305 return ((cellId & 0xFF) << 24)
306 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700307 }
308
Adam Cohend22015c2010-07-26 22:02:18 -0700309 static int getCellCountX() {
310 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700311 }
312
Adam Cohend22015c2010-07-26 22:02:18 -0700313 static int getCellCountY() {
314 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700315 }
316
317 /**
318 * Updates the model orientation helper to take into account the current layout dimensions
319 * when performing local/canonical coordinate transformations.
320 */
321 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700322 mCellCountX = shortAxisCellCount;
323 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700324 }
325
326 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400327 * Update an item to the database in a specified container.
328 */
329 static void updateItemInDatabase(Context context, ItemInfo item) {
330 final ContentValues values = new ContentValues();
331 final ContentResolver cr = context.getContentResolver();
332
333 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700334 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700335
Joe Onorato9c1289c2009-08-17 11:03:03 -0400336 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
337 }
338
339 /**
340 * Removes the specified item from the database
341 * @param context
342 * @param item
343 */
344 static void deleteItemFromDatabase(Context context, ItemInfo item) {
345 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700346 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
347 new Thread("deleteItemFromDatabase") {
348 public void run() {
349 cr.delete(uriToDelete, null, null);
350 }
351 }.start();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 }
353
354 /**
355 * Remove the contents of the specified folder from the database
356 */
357 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
358 final ContentResolver cr = context.getContentResolver();
359
360 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
361 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
362 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
363 }
364
365 /**
366 * Set this as the current Launcher activity object for the loader.
367 */
368 public void initialize(Callbacks callbacks) {
369 synchronized (mLock) {
370 mCallbacks = new WeakReference<Callbacks>(callbacks);
371 }
372 }
373
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700374 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
376 * ACTION_PACKAGE_CHANGED.
377 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400378 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400379 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700380
Joe Onorato36115782010-06-17 13:28:48 -0400381 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400382
Joe Onorato36115782010-06-17 13:28:48 -0400383 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
384 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
385 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
386 final String packageName = intent.getData().getSchemeSpecificPart();
387 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400388
Joe Onorato36115782010-06-17 13:28:48 -0400389 int op = PackageUpdatedTask.OP_NONE;
390
391 if (packageName == null || packageName.length() == 0) {
392 // they sent us a bad intent
393 return;
394 }
395
396 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
397 op = PackageUpdatedTask.OP_UPDATE;
398 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
399 if (!replacing) {
400 op = PackageUpdatedTask.OP_REMOVE;
401 }
402 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
403 // later, we will update the package at this time
404 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
405 if (!replacing) {
406 op = PackageUpdatedTask.OP_ADD;
407 } else {
408 op = PackageUpdatedTask.OP_UPDATE;
409 }
410 }
411
412 if (op != PackageUpdatedTask.OP_NONE) {
413 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
414 }
415
416 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
417 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
418 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
419
420 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
421 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
422 enqueuePackageUpdated(new PackageUpdatedTask(
423 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onorato790c2d92010-06-11 00:14:11 -0700424 }
Joe Onorato36115782010-06-17 13:28:48 -0400425 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400426
Joe Onorato36115782010-06-17 13:28:48 -0400427 public void startLoader(Context context, boolean isLaunching) {
428 synchronized (mLock) {
429 if (DEBUG_LOADERS) {
430 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
431 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400432
Joe Onorato36115782010-06-17 13:28:48 -0400433 // Don't bother to start the thread if we know it's not going to do anything
434 if (mCallbacks != null && mCallbacks.get() != null) {
435 // If there is already one running, tell it to stop.
436 LoaderTask oldTask = mLoaderTask;
437 if (oldTask != null) {
438 if (oldTask.isLaunching()) {
439 // don't downgrade isLaunching if we're already running
440 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500441 }
Joe Onorato36115782010-06-17 13:28:48 -0400442 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500443 }
Joe Onorato36115782010-06-17 13:28:48 -0400444 mLoaderTask = new LoaderTask(context, isLaunching);
445 mWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447 }
448 }
449
Joe Onorato36115782010-06-17 13:28:48 -0400450 public void stopLoader() {
451 synchronized (mLock) {
452 if (mLoaderTask != null) {
453 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400454 }
455 }
Joe Onorato36115782010-06-17 13:28:48 -0400456 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457
Joe Onorato36115782010-06-17 13:28:48 -0400458 /**
459 * Runnable for the thread that loads the contents of the launcher:
460 * - workspace icons
461 * - widgets
462 * - all apps icons
463 */
464 private class LoaderTask implements Runnable {
465 private Context mContext;
466 private Thread mWaitThread;
467 private boolean mIsLaunching;
468 private boolean mStopped;
469 private boolean mLoadAndBindStepFinished;
470
471 LoaderTask(Context context, boolean isLaunching) {
472 mContext = context;
473 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400474 }
475
Joe Onorato36115782010-06-17 13:28:48 -0400476 boolean isLaunching() {
477 return mIsLaunching;
478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400479
Joe Onorato36115782010-06-17 13:28:48 -0400480 private void loadAndBindWorkspace() {
481 // Load the workspace
482
483 // For now, just always reload the workspace. It's ~100 ms vs. the
484 // binding which takes many hundreds of ms.
485 // We can reconsider.
486 if (DEBUG_LOADERS) {
487 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400488 }
Joe Onorato36115782010-06-17 13:28:48 -0400489 if (true || !mWorkspaceLoaded) {
490 loadWorkspace();
491 if (mStopped) {
492 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400493 }
Joe Onorato36115782010-06-17 13:28:48 -0400494 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400495 }
496
Joe Onorato36115782010-06-17 13:28:48 -0400497 // Bind the workspace
498 bindWorkspace();
499 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400500
Joe Onorato36115782010-06-17 13:28:48 -0400501 private void waitForIdle() {
502 // Wait until the either we're stopped or the other threads are done.
503 // This way we don't start loading all apps until the workspace has settled
504 // down.
505 synchronized (LoaderTask.this) {
506 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700507
Joe Onorato36115782010-06-17 13:28:48 -0400508 mHandler.postIdle(new Runnable() {
509 public void run() {
510 synchronized (LoaderTask.this) {
511 mLoadAndBindStepFinished = true;
512 if (DEBUG_LOADERS) {
513 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400514 }
Joe Onorato36115782010-06-17 13:28:48 -0400515 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400516 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400517 }
Joe Onorato36115782010-06-17 13:28:48 -0400518 });
519
520 while (!mStopped && !mLoadAndBindStepFinished) {
521 try {
522 this.wait();
523 } catch (InterruptedException ex) {
524 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400525 }
526 }
Joe Onorato36115782010-06-17 13:28:48 -0400527 if (DEBUG_LOADERS) {
528 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700529 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400530 + "ms for previous step to finish binding");
531 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400532 }
Joe Onorato36115782010-06-17 13:28:48 -0400533 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400534
Joe Onorato36115782010-06-17 13:28:48 -0400535 public void run() {
536 // Optimize for end-user experience: if the Launcher is up and // running with the
537 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
538 // workspace first (default).
539 final Callbacks cbk = mCallbacks.get();
540 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400541
Joe Onorato36115782010-06-17 13:28:48 -0400542 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400543 // Elevate priority when Home launches for the first time to avoid
544 // starving at boot time. Staring at a blank home is not cool.
545 synchronized (mLock) {
546 android.os.Process.setThreadPriority(mIsLaunching
547 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
548 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400549 if (loadWorkspaceFirst) {
550 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
551 loadAndBindWorkspace();
552 } else {
553 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700554 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400555 }
556
Joe Onorato36115782010-06-17 13:28:48 -0400557 if (mStopped) {
558 break keep_running;
559 }
560
561 // Whew! Hard work done. Slow us down, and wait until the UI thread has
562 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400563 synchronized (mLock) {
564 if (mIsLaunching) {
565 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
566 }
567 }
Joe Onorato36115782010-06-17 13:28:48 -0400568 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400569
570 // second step
571 if (loadWorkspaceFirst) {
572 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700573 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400574 } else {
575 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
576 loadAndBindWorkspace();
577 }
Joe Onorato36115782010-06-17 13:28:48 -0400578 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400579
Joe Onorato36115782010-06-17 13:28:48 -0400580 // Clear out this reference, otherwise we end up holding it until all of the
581 // callback runnables are done.
582 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400583
Joe Onorato36115782010-06-17 13:28:48 -0400584 synchronized (mLock) {
585 // If we are still the last one to be scheduled, remove ourselves.
586 if (mLoaderTask == this) {
587 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400588 }
Joe Onorato36115782010-06-17 13:28:48 -0400589 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700590
Joe Onorato36115782010-06-17 13:28:48 -0400591 // Trigger a gc to try to clean up after the stuff is done, since the
592 // renderscript allocations aren't charged to the java heap.
593 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700594 mHandler.post(new Runnable() {
595 public void run() {
596 System.gc();
597 }
598 });
Joe Onorato36115782010-06-17 13:28:48 -0400599 } else {
600 mHandler.postIdle(new Runnable() {
601 public void run() {
602 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400603 }
Joe Onorato36115782010-06-17 13:28:48 -0400604 });
605 }
606 }
607
608 public void stopLocked() {
609 synchronized (LoaderTask.this) {
610 mStopped = true;
611 this.notify();
612 }
613 }
614
615 /**
616 * Gets the callbacks object. If we've been stopped, or if the launcher object
617 * has somehow been garbage collected, return null instead. Pass in the Callbacks
618 * object that was around when the deferred message was scheduled, and if there's
619 * a new Callbacks object around then also return null. This will save us from
620 * calling onto it with data that will be ignored.
621 */
622 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
623 synchronized (mLock) {
624 if (mStopped) {
625 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400626 }
Joe Onorato36115782010-06-17 13:28:48 -0400627
628 if (mCallbacks == null) {
629 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400630 }
Joe Onorato36115782010-06-17 13:28:48 -0400631
632 final Callbacks callbacks = mCallbacks.get();
633 if (callbacks != oldCallbacks) {
634 return null;
635 }
636 if (callbacks == null) {
637 Log.w(TAG, "no mCallbacks");
638 return null;
639 }
640
641 return callbacks;
642 }
643 }
644
645 // check & update map of what's occupied; used to discard overlapping/invalid items
646 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
647 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400648 return true;
649 }
Joe Onorato36115782010-06-17 13:28:48 -0400650 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
651 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
652 if (occupied[item.screen][x][y] != null) {
653 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700654 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400655 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700656 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400657 + occupied[item.screen][x][y]);
658 return false;
659 }
660 }
661 }
662 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
663 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
664 occupied[item.screen][x][y] = item;
665 }
666 }
667 return true;
668 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669
Joe Onorato36115782010-06-17 13:28:48 -0400670 private void loadWorkspace() {
671 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672
Joe Onorato36115782010-06-17 13:28:48 -0400673 final Context context = mContext;
674 final ContentResolver contentResolver = context.getContentResolver();
675 final PackageManager manager = context.getPackageManager();
676 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
677 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400678
Joe Onorato36115782010-06-17 13:28:48 -0400679 mItems.clear();
680 mAppWidgets.clear();
681 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800682
Joe Onorato36115782010-06-17 13:28:48 -0400683 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684
Joe Onorato36115782010-06-17 13:28:48 -0400685 final Cursor c = contentResolver.query(
686 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400687
Adam Cohend22015c2010-07-26 22:02:18 -0700688 final ItemInfo occupied[][][] =
689 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690
Joe Onorato36115782010-06-17 13:28:48 -0400691 try {
692 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
693 final int intentIndex = c.getColumnIndexOrThrow
694 (LauncherSettings.Favorites.INTENT);
695 final int titleIndex = c.getColumnIndexOrThrow
696 (LauncherSettings.Favorites.TITLE);
697 final int iconTypeIndex = c.getColumnIndexOrThrow(
698 LauncherSettings.Favorites.ICON_TYPE);
699 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
700 final int iconPackageIndex = c.getColumnIndexOrThrow(
701 LauncherSettings.Favorites.ICON_PACKAGE);
702 final int iconResourceIndex = c.getColumnIndexOrThrow(
703 LauncherSettings.Favorites.ICON_RESOURCE);
704 final int containerIndex = c.getColumnIndexOrThrow(
705 LauncherSettings.Favorites.CONTAINER);
706 final int itemTypeIndex = c.getColumnIndexOrThrow(
707 LauncherSettings.Favorites.ITEM_TYPE);
708 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
709 LauncherSettings.Favorites.APPWIDGET_ID);
710 final int screenIndex = c.getColumnIndexOrThrow(
711 LauncherSettings.Favorites.SCREEN);
712 final int cellXIndex = c.getColumnIndexOrThrow
713 (LauncherSettings.Favorites.CELLX);
714 final int cellYIndex = c.getColumnIndexOrThrow
715 (LauncherSettings.Favorites.CELLY);
716 final int spanXIndex = c.getColumnIndexOrThrow
717 (LauncherSettings.Favorites.SPANX);
718 final int spanYIndex = c.getColumnIndexOrThrow(
719 LauncherSettings.Favorites.SPANY);
720 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
721 final int displayModeIndex = c.getColumnIndexOrThrow(
722 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723
Joe Onorato36115782010-06-17 13:28:48 -0400724 ShortcutInfo info;
725 String intentDescription;
726 LauncherAppWidgetInfo appWidgetInfo;
727 int container;
728 long id;
729 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400730
Joe Onorato36115782010-06-17 13:28:48 -0400731 while (!mStopped && c.moveToNext()) {
732 try {
733 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734
Joe Onorato36115782010-06-17 13:28:48 -0400735 switch (itemType) {
736 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
737 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
738 intentDescription = c.getString(intentIndex);
739 try {
740 intent = Intent.parseUri(intentDescription, 0);
741 } catch (URISyntaxException e) {
742 continue;
743 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744
Joe Onorato36115782010-06-17 13:28:48 -0400745 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
746 info = getShortcutInfo(manager, intent, context, c, iconIndex,
747 titleIndex);
748 } else {
749 info = getShortcutInfo(c, context, iconTypeIndex,
750 iconPackageIndex, iconResourceIndex, iconIndex,
751 titleIndex);
752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753
Joe Onorato36115782010-06-17 13:28:48 -0400754 if (info != null) {
755 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756
Joe Onorato36115782010-06-17 13:28:48 -0400757 info.intent = intent;
758 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400760 info.container = container;
761 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700762 info.cellX = c.getInt(cellXIndex);
763 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764
Daniel Sandler8802e962010-05-26 16:28:16 -0400765 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400766 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400767 break;
768 }
769
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400771 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
772 mItems.add(info);
773 break;
774 default:
775 // Item is in a user folder
776 UserFolderInfo folderInfo =
777 findOrMakeUserFolder(mFolders, container);
778 folderInfo.add(info);
779 break;
780 }
781 } else {
782 // Failed to load the shortcut, probably because the
783 // activity manager couldn't resolve it (maybe the app
784 // was uninstalled), or the db row was somehow screwed up.
785 // Delete it.
786 id = c.getLong(idIndex);
787 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
788 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
789 id, false), null, null);
790 }
791 break;
792
793 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
794 id = c.getLong(idIndex);
795 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
796
Winson Chungaafa03c2010-06-11 17:34:16 -0700797 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400798 folderInfo.id = id;
799 container = c.getInt(containerIndex);
800 folderInfo.container = container;
801 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700802 folderInfo.cellX = c.getInt(cellXIndex);
803 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400804
805 // check & update map of what's occupied
806 if (!checkItemPlacement(occupied, folderInfo)) {
807 break;
808 }
Joe Onorato36115782010-06-17 13:28:48 -0400809 switch (container) {
810 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
811 mItems.add(folderInfo);
812 break;
813 }
814
815 mFolders.put(folderInfo.id, folderInfo);
816 break;
817
818 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
819 id = c.getLong(idIndex);
820 Uri uri = Uri.parse(c.getString(uriIndex));
821
822 // Make sure the live folder exists
823 final ProviderInfo providerInfo =
824 context.getPackageManager().resolveContentProvider(
825 uri.getAuthority(), 0);
826
827 if (providerInfo == null && !isSafeMode) {
828 itemsToRemove.add(id);
829 } else {
830 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400831 intentDescription = c.getString(intentIndex);
832 intent = null;
833 if (intentDescription != null) {
834 try {
835 intent = Intent.parseUri(intentDescription, 0);
836 } catch (URISyntaxException e) {
837 // Ignore, a live folder might not have a base intent
838 }
839 }
840
841 liveFolderInfo.title = c.getString(titleIndex);
842 liveFolderInfo.id = id;
843 liveFolderInfo.uri = uri;
844 container = c.getInt(containerIndex);
845 liveFolderInfo.container = container;
846 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700847 liveFolderInfo.cellX = c.getInt(cellXIndex);
848 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400849 liveFolderInfo.baseIntent = intent;
850 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
851
852 // check & update map of what's occupied
853 if (!checkItemPlacement(occupied, liveFolderInfo)) {
854 break;
855 }
856
857 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
858 iconResourceIndex, liveFolderInfo);
859
860 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400862 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863 break;
864 }
Joe Onorato36115782010-06-17 13:28:48 -0400865 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866 }
Joe Onorato36115782010-06-17 13:28:48 -0400867 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800868
Joe Onorato36115782010-06-17 13:28:48 -0400869 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
870 // Read all Launcher-specific widget details
871 int appWidgetId = c.getInt(appWidgetIdIndex);
872 id = c.getLong(idIndex);
873
874 final AppWidgetProviderInfo provider =
875 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700876
Joe Onorato36115782010-06-17 13:28:48 -0400877 if (!isSafeMode && (provider == null || provider.provider == null ||
878 provider.provider.getPackageName() == null)) {
879 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
880 + id + " appWidgetId=" + appWidgetId);
881 itemsToRemove.add(id);
882 } else {
883 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
884 appWidgetInfo.id = id;
885 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700886 appWidgetInfo.cellX = c.getInt(cellXIndex);
887 appWidgetInfo.cellY = c.getInt(cellYIndex);
888 appWidgetInfo.spanX = c.getInt(spanXIndex);
889 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400890
891 container = c.getInt(containerIndex);
892 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
893 Log.e(TAG, "Widget found where container "
894 + "!= CONTAINER_DESKTOP -- ignoring!");
895 continue;
896 }
897 appWidgetInfo.container = c.getInt(containerIndex);
898
899 // check & update map of what's occupied
900 if (!checkItemPlacement(occupied, appWidgetInfo)) {
901 break;
902 }
903
904 mAppWidgets.add(appWidgetInfo);
905 }
906 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800907 }
Joe Onorato36115782010-06-17 13:28:48 -0400908 } catch (Exception e) {
909 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800910 }
911 }
Joe Onorato36115782010-06-17 13:28:48 -0400912 } finally {
913 c.close();
914 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800915
Joe Onorato36115782010-06-17 13:28:48 -0400916 if (itemsToRemove.size() > 0) {
917 ContentProviderClient client = contentResolver.acquireContentProviderClient(
918 LauncherSettings.Favorites.CONTENT_URI);
919 // Remove dead items
920 for (long id : itemsToRemove) {
921 if (DEBUG_LOADERS) {
922 Log.d(TAG, "Removed id = " + id);
923 }
924 // Don't notify content observers
925 try {
926 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
927 null, null);
928 } catch (RemoteException e) {
929 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400930 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 }
933
Joe Onorato36115782010-06-17 13:28:48 -0400934 if (DEBUG_LOADERS) {
935 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
936 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700937 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400938 String line = "";
939 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
940 if (s > 0) {
941 line += " | ";
942 }
Adam Cohend22015c2010-07-26 22:02:18 -0700943 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400944 line += ((occupied[s][x][y] != null) ? "#" : ".");
945 }
946 }
947 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 }
Joe Onorato36115782010-06-17 13:28:48 -0400949 }
950 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951
Joe Onorato36115782010-06-17 13:28:48 -0400952 /**
953 * Read everything out of our database.
954 */
955 private void bindWorkspace() {
956 final long t = SystemClock.uptimeMillis();
957
958 // Don't use these two variables in any of the callback runnables.
959 // Otherwise we hold a reference to them.
960 final Callbacks oldCallbacks = mCallbacks.get();
961 if (oldCallbacks == null) {
962 // This launcher has exited and nobody bothered to tell us. Just bail.
963 Log.w(TAG, "LoaderTask running with no launcher");
964 return;
965 }
966
967 int N;
968 // Tell the workspace that we're about to start firing items at it
969 mHandler.post(new Runnable() {
970 public void run() {
971 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
972 if (callbacks != null) {
973 callbacks.startBinding();
974 }
975 }
976 });
977 // Add the items to the workspace.
978 N = mItems.size();
979 for (int i=0; i<N; i+=ITEMS_CHUNK) {
980 final int start = i;
981 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 mHandler.post(new Runnable() {
983 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800984 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400986 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 }
988 }
989 });
Joe Onorato36115782010-06-17 13:28:48 -0400990 }
991 mHandler.post(new Runnable() {
992 public void run() {
993 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
994 if (callbacks != null) {
995 callbacks.bindFolders(mFolders);
996 }
997 }
998 });
999 // Wait until the queue goes empty.
1000 mHandler.post(new Runnable() {
1001 public void run() {
1002 if (DEBUG_LOADERS) {
1003 Log.d(TAG, "Going to start binding widgets soon.");
1004 }
1005 }
1006 });
1007 // Bind the widgets, one at a time.
1008 // WARNING: this is calling into the workspace from the background thread,
1009 // but since getCurrentScreen() just returns the int, we should be okay. This
1010 // is just a hint for the order, and if it's wrong, we'll be okay.
1011 // TODO: instead, we should have that push the current screen into here.
1012 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1013 N = mAppWidgets.size();
1014 // once for the current screen
1015 for (int i=0; i<N; i++) {
1016 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1017 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 mHandler.post(new Runnable() {
1019 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001020 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001022 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 }
1024 }
1025 });
1026 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 }
Joe Onorato36115782010-06-17 13:28:48 -04001028 // once for the other screens
1029 for (int i=0; i<N; i++) {
1030 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1031 if (widget.screen != currentScreen) {
1032 mHandler.post(new Runnable() {
1033 public void run() {
1034 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1035 if (callbacks != null) {
1036 callbacks.bindAppWidget(widget);
1037 }
1038 }
1039 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001040 }
1041 }
Joe Onorato36115782010-06-17 13:28:48 -04001042 // Tell the workspace that we're done.
1043 mHandler.post(new Runnable() {
1044 public void run() {
1045 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1046 if (callbacks != null) {
1047 callbacks.finishBindingItems();
1048 }
1049 }
1050 });
1051 // If we're profiling, this is the last thing in the queue.
1052 mHandler.post(new Runnable() {
1053 public void run() {
1054 if (DEBUG_LOADERS) {
1055 Log.d(TAG, "bound workspace in "
1056 + (SystemClock.uptimeMillis()-t) + "ms");
1057 }
1058 }
1059 });
1060 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001061
Joe Onorato36115782010-06-17 13:28:48 -04001062 private void loadAndBindAllApps() {
1063 if (DEBUG_LOADERS) {
1064 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1065 }
1066 if (!mAllAppsLoaded) {
1067 loadAllAppsByBatch();
1068 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001069 return;
1070 }
Joe Onorato36115782010-06-17 13:28:48 -04001071 mAllAppsLoaded = true;
1072 } else {
1073 onlyBindAllApps();
1074 }
1075 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001076
Joe Onorato36115782010-06-17 13:28:48 -04001077 private void onlyBindAllApps() {
1078 final Callbacks oldCallbacks = mCallbacks.get();
1079 if (oldCallbacks == null) {
1080 // This launcher has exited and nobody bothered to tell us. Just bail.
1081 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1082 return;
1083 }
1084
1085 // shallow copy
1086 final ArrayList<ApplicationInfo> list
1087 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1088 mHandler.post(new Runnable() {
1089 public void run() {
1090 final long t = SystemClock.uptimeMillis();
1091 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1092 if (callbacks != null) {
1093 callbacks.bindAllApplications(list);
1094 }
1095 if (DEBUG_LOADERS) {
1096 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1097 + (SystemClock.uptimeMillis()-t) + "ms");
1098 }
1099 }
1100 });
1101
1102 }
1103
1104 private void loadAllAppsByBatch() {
1105 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1106
1107 // Don't use these two variables in any of the callback runnables.
1108 // Otherwise we hold a reference to them.
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 (loadAllAppsByBatch)");
1113 return;
1114 }
1115
1116 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1117 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1118
1119 final PackageManager packageManager = mContext.getPackageManager();
1120 List<ResolveInfo> apps = null;
1121
1122 int N = Integer.MAX_VALUE;
1123
1124 int startIndex;
1125 int i=0;
1126 int batchSize = -1;
1127 while (i < N && !mStopped) {
1128 if (i == 0) {
1129 mAllAppsList.clear();
1130 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1131 apps = packageManager.queryIntentActivities(mainIntent, 0);
1132 if (DEBUG_LOADERS) {
1133 Log.d(TAG, "queryIntentActivities took "
1134 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1135 }
1136 if (apps == null) {
1137 return;
1138 }
1139 N = apps.size();
1140 if (DEBUG_LOADERS) {
1141 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1142 }
1143 if (N == 0) {
1144 // There are no apps?!?
1145 return;
1146 }
1147 if (mBatchSize == 0) {
1148 batchSize = N;
1149 } else {
1150 batchSize = mBatchSize;
1151 }
1152
1153 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1154 Collections.sort(apps,
1155 new ResolveInfo.DisplayNameComparator(packageManager));
1156 if (DEBUG_LOADERS) {
1157 Log.d(TAG, "sort took "
1158 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1159 }
1160 }
1161
1162 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1163
1164 startIndex = i;
1165 for (int j=0; i<N && j<batchSize; j++) {
1166 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001167 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001168 i++;
1169 }
1170
1171 final boolean first = i <= batchSize;
1172 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1173 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1174 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1175
Joe Onoratocc67f472010-06-08 10:54:30 -07001176 mHandler.post(new Runnable() {
1177 public void run() {
1178 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001179 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001180 if (first) {
1181 callbacks.bindAllApplications(added);
1182 } else {
1183 callbacks.bindAppsAdded(added);
1184 }
1185 if (DEBUG_LOADERS) {
1186 Log.d(TAG, "bound " + added.size() + " apps in "
1187 + (SystemClock.uptimeMillis() - t) + "ms");
1188 }
1189 } else {
1190 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001191 }
1192 }
1193 });
1194
Daniel Sandlerdca66122010-04-13 16:23:58 -04001195 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001196 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1197 + (SystemClock.uptimeMillis()-t2) + "ms");
1198 }
1199
1200 if (mAllAppsLoadDelay > 0 && i < N) {
1201 try {
1202 if (DEBUG_LOADERS) {
1203 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1204 }
1205 Thread.sleep(mAllAppsLoadDelay);
1206 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 }
1208 }
1209
Joe Onorato36115782010-06-17 13:28:48 -04001210 if (DEBUG_LOADERS) {
1211 Log.d(TAG, "cached all " + N + " apps in "
1212 + (SystemClock.uptimeMillis()-t) + "ms"
1213 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001214 }
1215 }
1216
1217 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001218 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1219 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1220 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1221 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1222 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1223 }
1224 }
1225
1226 void enqueuePackageUpdated(PackageUpdatedTask task) {
1227 mWorker.post(task);
1228 }
1229
1230 private class PackageUpdatedTask implements Runnable {
1231 int mOp;
1232 String[] mPackages;
1233
1234 public static final int OP_NONE = 0;
1235 public static final int OP_ADD = 1;
1236 public static final int OP_UPDATE = 2;
1237 public static final int OP_REMOVE = 3; // uninstlled
1238 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1239
1240
1241 public PackageUpdatedTask(int op, String[] packages) {
1242 mOp = op;
1243 mPackages = packages;
1244 }
1245
1246 public void run() {
1247 final Context context = mApp;
1248
1249 final String[] packages = mPackages;
1250 final int N = packages.length;
1251 switch (mOp) {
1252 case OP_ADD:
1253 for (int i=0; i<N; i++) {
1254 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1255 mAllAppsList.addPackage(context, packages[i]);
1256 }
1257 break;
1258 case OP_UPDATE:
1259 for (int i=0; i<N; i++) {
1260 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1261 mAllAppsList.updatePackage(context, packages[i]);
1262 }
1263 break;
1264 case OP_REMOVE:
1265 case OP_UNAVAILABLE:
1266 for (int i=0; i<N; i++) {
1267 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1268 mAllAppsList.removePackage(packages[i]);
1269 }
1270 break;
1271 }
1272
1273 ArrayList<ApplicationInfo> added = null;
1274 ArrayList<ApplicationInfo> removed = null;
1275 ArrayList<ApplicationInfo> modified = null;
1276
1277 if (mAllAppsList.added.size() > 0) {
1278 added = mAllAppsList.added;
1279 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1280 }
1281 if (mAllAppsList.removed.size() > 0) {
1282 removed = mAllAppsList.removed;
1283 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1284 for (ApplicationInfo info: removed) {
1285 mIconCache.remove(info.intent.getComponent());
1286 }
1287 }
1288 if (mAllAppsList.modified.size() > 0) {
1289 modified = mAllAppsList.modified;
1290 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1291 }
1292
1293 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1294 if (callbacks == null) {
1295 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1296 return;
1297 }
1298
1299 if (added != null) {
1300 final ArrayList<ApplicationInfo> addedFinal = added;
1301 mHandler.post(new Runnable() {
1302 public void run() {
1303 if (callbacks == mCallbacks.get()) {
1304 callbacks.bindAppsAdded(addedFinal);
1305 }
1306 }
1307 });
1308 }
1309 if (modified != null) {
1310 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1311 mHandler.post(new Runnable() {
1312 public void run() {
1313 if (callbacks == mCallbacks.get()) {
1314 callbacks.bindAppsUpdated(modifiedFinal);
1315 }
1316 }
1317 });
1318 }
1319 if (removed != null) {
1320 final boolean permanent = mOp != OP_UNAVAILABLE;
1321 final ArrayList<ApplicationInfo> removedFinal = removed;
1322 mHandler.post(new Runnable() {
1323 public void run() {
1324 if (callbacks == mCallbacks.get()) {
1325 callbacks.bindAppsRemoved(removedFinal, permanent);
1326 }
1327 }
1328 });
Joe Onoratobe386092009-11-17 17:32:16 -08001329 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001330
1331 mHandler.post(new Runnable() {
1332 @Override
1333 public void run() {
1334 if (callbacks == mCallbacks.get()) {
1335 callbacks.bindPackagesUpdated();
1336 }
1337 }
1338 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 }
1340 }
1341
1342 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001343 * This is called from the code that adds shortcuts from the intent receiver. This
1344 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001345 */
Joe Onorato56d82912010-03-07 14:32:10 -05001346 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001347 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001348 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001349
Joe Onorato56d82912010-03-07 14:32:10 -05001350 /**
1351 * Make an ShortcutInfo object for a shortcut that is an application.
1352 *
1353 * If c is not null, then it will be used to fill in missing data like the title and icon.
1354 */
1355 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1356 Cursor c, int iconIndex, int titleIndex) {
1357 Bitmap icon = null;
1358 final ShortcutInfo info = new ShortcutInfo();
1359
1360 ComponentName componentName = intent.getComponent();
1361 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001362 return null;
1363 }
1364
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001365 // TODO: See if the PackageManager knows about this case. If it doesn't
1366 // then return null & delete this.
1367
Joe Onorato56d82912010-03-07 14:32:10 -05001368 // the resource -- This may implicitly give us back the fallback icon,
1369 // but don't worry about that. All we're doing with usingFallbackIcon is
1370 // to avoid saving lots of copies of that in the database, and most apps
1371 // have icons anyway.
1372 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1373 if (resolveInfo != null) {
1374 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001375 }
Joe Onorato56d82912010-03-07 14:32:10 -05001376 // the db
1377 if (icon == null) {
1378 if (c != null) {
1379 icon = getIconFromCursor(c, iconIndex);
1380 }
1381 }
1382 // the fallback icon
1383 if (icon == null) {
1384 icon = getFallbackIcon();
1385 info.usingFallbackIcon = true;
1386 }
1387 info.setIcon(icon);
1388
1389 // from the resource
1390 if (resolveInfo != null) {
1391 info.title = resolveInfo.activityInfo.loadLabel(manager);
1392 }
1393 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001395 if (c != null) {
1396 info.title = c.getString(titleIndex);
1397 }
1398 }
1399 // fall back to the class name of the activity
1400 if (info.title == null) {
1401 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001402 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1404 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001408 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001410 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001411 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1412 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413
Joe Onorato56d82912010-03-07 14:32:10 -05001414 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001415 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001416 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001418 // TODO: If there's an explicit component and we can't install that, delete it.
1419
Joe Onorato56d82912010-03-07 14:32:10 -05001420 info.title = c.getString(titleIndex);
1421
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422 int iconType = c.getInt(iconTypeIndex);
1423 switch (iconType) {
1424 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1425 String packageName = c.getString(iconPackageIndex);
1426 String resourceName = c.getString(iconResourceIndex);
1427 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001428 info.customIcon = false;
1429 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001432 if (resources != null) {
1433 final int id = resources.getIdentifier(resourceName, null, null);
1434 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1435 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001436 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001437 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
Joe Onorato56d82912010-03-07 14:32:10 -05001439 // the db
1440 if (icon == null) {
1441 icon = getIconFromCursor(c, iconIndex);
1442 }
1443 // the fallback icon
1444 if (icon == null) {
1445 icon = getFallbackIcon();
1446 info.usingFallbackIcon = true;
1447 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448 break;
1449 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001450 icon = getIconFromCursor(c, iconIndex);
1451 if (icon == null) {
1452 icon = getFallbackIcon();
1453 info.customIcon = false;
1454 info.usingFallbackIcon = true;
1455 } else {
1456 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001457 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001458 break;
1459 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001460 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001461 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 info.customIcon = false;
1463 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001465 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466 return info;
1467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468
Joe Onorato56d82912010-03-07 14:32:10 -05001469 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1470 if (false) {
1471 Log.d(TAG, "getIconFromCursor app="
1472 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1473 }
1474 byte[] data = c.getBlob(iconIndex);
1475 try {
1476 return BitmapFactory.decodeByteArray(data, 0, data.length);
1477 } catch (Exception e) {
1478 return null;
1479 }
1480 }
1481
Joe Onorato0589f0f2010-02-08 13:44:00 -08001482 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001483 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001484
1485 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1486 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001487 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001488
1489 return info;
1490 }
1491
1492 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1493 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1494 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1495 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1496
1497 Bitmap icon = null;
1498 boolean filtered = false;
1499 boolean customIcon = false;
1500 ShortcutIconResource iconResource = null;
1501
1502 if (bitmap != null && bitmap instanceof Bitmap) {
1503 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1504 filtered = true;
1505 customIcon = true;
1506 } else {
1507 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1508 if (extra != null && extra instanceof ShortcutIconResource) {
1509 try {
1510 iconResource = (ShortcutIconResource) extra;
1511 final PackageManager packageManager = context.getPackageManager();
1512 Resources resources = packageManager.getResourcesForApplication(
1513 iconResource.packageName);
1514 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1515 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1516 } catch (Exception e) {
1517 Log.w(TAG, "Could not load shortcut icon: " + extra);
1518 }
1519 }
1520 }
1521
Joe Onorato0589f0f2010-02-08 13:44:00 -08001522 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001523
1524 if (icon == null) {
1525 icon = getFallbackIcon();
1526 info.usingFallbackIcon = true;
1527 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001528 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001529
Joe Onorato0589f0f2010-02-08 13:44:00 -08001530 info.title = name;
1531 info.intent = intent;
1532 info.customIcon = customIcon;
1533 info.iconResource = iconResource;
1534
1535 return info;
1536 }
1537
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1539 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1540
1541 int iconType = c.getInt(iconTypeIndex);
1542 switch (iconType) {
1543 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1544 String packageName = c.getString(iconPackageIndex);
1545 String resourceName = c.getString(iconResourceIndex);
1546 PackageManager packageManager = context.getPackageManager();
1547 try {
1548 Resources resources = packageManager.getResourcesForApplication(packageName);
1549 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001550 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1551 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001553 liveFolderInfo.icon = Utilities.createIconBitmap(
1554 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1555 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 }
1557 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1558 liveFolderInfo.iconResource.packageName = packageName;
1559 liveFolderInfo.iconResource.resourceName = resourceName;
1560 break;
1561 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001562 liveFolderInfo.icon = Utilities.createIconBitmap(
1563 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1564 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 }
1566 }
1567
Joe Onorato56d82912010-03-07 14:32:10 -05001568 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1569 // If this icon doesn't have a custom icon, check to see
1570 // what's stored in the DB, and if it doesn't match what
1571 // we're going to show, store what we are going to show back
1572 // into the DB. We do this so when we're loading, if the
1573 // package manager can't find an icon (for example because
1574 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001575 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001576 boolean needSave;
1577 byte[] data = c.getBlob(iconIndex);
1578 try {
1579 if (data != null) {
1580 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1581 Bitmap loaded = info.getIcon(mIconCache);
1582 needSave = !saved.sameAs(loaded);
1583 } else {
1584 needSave = true;
1585 }
1586 } catch (Exception e) {
1587 needSave = true;
1588 }
1589 if (needSave) {
1590 Log.d(TAG, "going to save icon bitmap for info=" + info);
1591 // This is slower than is ideal, but this only happens either
1592 // after the froyo OTA or when the app is updated with a new
1593 // icon.
1594 updateItemInDatabase(context, info);
1595 }
1596 }
1597 }
1598
Joe Onorato9c1289c2009-08-17 11:03:03 -04001599 /**
1600 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1601 * or make a new one.
1602 */
1603 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1604 // See if a placeholder was created for us already
1605 FolderInfo folderInfo = folders.get(id);
1606 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1607 // No placeholder -- create a new instance
1608 folderInfo = new UserFolderInfo();
1609 folders.put(id, folderInfo);
1610 }
1611 return (UserFolderInfo) folderInfo;
1612 }
1613
1614 /**
1615 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1616 * new one.
1617 */
1618 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1619 // See if a placeholder was created for us already
1620 FolderInfo folderInfo = folders.get(id);
1621 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1622 // No placeholder -- create a new instance
1623 folderInfo = new LiveFolderInfo();
1624 folders.put(id, folderInfo);
1625 }
1626 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 }
1628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1630 String label = activityInfo.loadLabel(manager).toString();
1631 if (label == null) {
1632 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1633 if (label == null) {
1634 label = activityInfo.name;
1635 }
1636 }
1637 return label;
1638 }
1639
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001641 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001642 = new Comparator<ApplicationInfo>() {
1643 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1644 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001646 };
Joe Onoratobe386092009-11-17 17:32:16 -08001647
1648 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001649 Log.d(TAG, "mCallbacks=" + mCallbacks);
1650 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1651 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1652 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1653 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001654 Log.d(TAG, "mItems size=" + mItems.size());
1655 if (mLoaderTask != null) {
1656 mLoaderTask.dumpState();
1657 } else {
1658 Log.d(TAG, "mLoaderTask=null");
1659 }
Joe Onoratobe386092009-11-17 17:32:16 -08001660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661}