blob: 4ad31b1ad2e15c8ff9576e473002e7acf7f8df91 [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 */
Adam Cohend22015c2010-07-26 22:02:18 -0700303 static int getCellLayoutChildId(int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
304 return ((cellId & 0xFF) << 16) | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700305 }
306
Adam Cohend22015c2010-07-26 22:02:18 -0700307 static int getCellCountX() {
308 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700309 }
310
Adam Cohend22015c2010-07-26 22:02:18 -0700311 static int getCellCountY() {
312 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700313 }
314
315 /**
316 * Updates the model orientation helper to take into account the current layout dimensions
317 * when performing local/canonical coordinate transformations.
318 */
319 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700320 mCellCountX = shortAxisCellCount;
321 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700322 }
323
324 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400325 * Update an item to the database in a specified container.
326 */
327 static void updateItemInDatabase(Context context, ItemInfo item) {
328 final ContentValues values = new ContentValues();
329 final ContentResolver cr = context.getContentResolver();
330
331 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700332 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700333
Joe Onorato9c1289c2009-08-17 11:03:03 -0400334 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
335 }
336
337 /**
338 * Removes the specified item from the database
339 * @param context
340 * @param item
341 */
342 static void deleteItemFromDatabase(Context context, ItemInfo item) {
343 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700344 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
345 new Thread("deleteItemFromDatabase") {
346 public void run() {
347 cr.delete(uriToDelete, null, null);
348 }
349 }.start();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400350 }
351
352 /**
353 * Remove the contents of the specified folder from the database
354 */
355 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
356 final ContentResolver cr = context.getContentResolver();
357
358 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
359 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
360 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
361 }
362
363 /**
364 * Set this as the current Launcher activity object for the loader.
365 */
366 public void initialize(Callbacks callbacks) {
367 synchronized (mLock) {
368 mCallbacks = new WeakReference<Callbacks>(callbacks);
369 }
370 }
371
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700372 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
374 * ACTION_PACKAGE_CHANGED.
375 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400376 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400377 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700378
Joe Onorato36115782010-06-17 13:28:48 -0400379 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400380
Joe Onorato36115782010-06-17 13:28:48 -0400381 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
382 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
383 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
384 final String packageName = intent.getData().getSchemeSpecificPart();
385 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386
Joe Onorato36115782010-06-17 13:28:48 -0400387 int op = PackageUpdatedTask.OP_NONE;
388
389 if (packageName == null || packageName.length() == 0) {
390 // they sent us a bad intent
391 return;
392 }
393
394 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
395 op = PackageUpdatedTask.OP_UPDATE;
396 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
397 if (!replacing) {
398 op = PackageUpdatedTask.OP_REMOVE;
399 }
400 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
401 // later, we will update the package at this time
402 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
403 if (!replacing) {
404 op = PackageUpdatedTask.OP_ADD;
405 } else {
406 op = PackageUpdatedTask.OP_UPDATE;
407 }
408 }
409
410 if (op != PackageUpdatedTask.OP_NONE) {
411 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
412 }
413
414 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
415 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
416 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
417
418 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
419 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
420 enqueuePackageUpdated(new PackageUpdatedTask(
421 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onorato790c2d92010-06-11 00:14:11 -0700422 }
Joe Onorato36115782010-06-17 13:28:48 -0400423 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400424
Joe Onorato36115782010-06-17 13:28:48 -0400425 public void startLoader(Context context, boolean isLaunching) {
426 synchronized (mLock) {
427 if (DEBUG_LOADERS) {
428 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
429 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430
Joe Onorato36115782010-06-17 13:28:48 -0400431 // Don't bother to start the thread if we know it's not going to do anything
432 if (mCallbacks != null && mCallbacks.get() != null) {
433 // If there is already one running, tell it to stop.
434 LoaderTask oldTask = mLoaderTask;
435 if (oldTask != null) {
436 if (oldTask.isLaunching()) {
437 // don't downgrade isLaunching if we're already running
438 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500439 }
Joe Onorato36115782010-06-17 13:28:48 -0400440 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500441 }
Joe Onorato36115782010-06-17 13:28:48 -0400442 mLoaderTask = new LoaderTask(context, isLaunching);
443 mWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400445 }
446 }
447
Joe Onorato36115782010-06-17 13:28:48 -0400448 public void stopLoader() {
449 synchronized (mLock) {
450 if (mLoaderTask != null) {
451 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400452 }
453 }
Joe Onorato36115782010-06-17 13:28:48 -0400454 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455
Joe Onorato36115782010-06-17 13:28:48 -0400456 /**
457 * Runnable for the thread that loads the contents of the launcher:
458 * - workspace icons
459 * - widgets
460 * - all apps icons
461 */
462 private class LoaderTask implements Runnable {
463 private Context mContext;
464 private Thread mWaitThread;
465 private boolean mIsLaunching;
466 private boolean mStopped;
467 private boolean mLoadAndBindStepFinished;
468
469 LoaderTask(Context context, boolean isLaunching) {
470 mContext = context;
471 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400472 }
473
Joe Onorato36115782010-06-17 13:28:48 -0400474 boolean isLaunching() {
475 return mIsLaunching;
476 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400477
Joe Onorato36115782010-06-17 13:28:48 -0400478 private void loadAndBindWorkspace() {
479 // Load the workspace
480
481 // For now, just always reload the workspace. It's ~100 ms vs. the
482 // binding which takes many hundreds of ms.
483 // We can reconsider.
484 if (DEBUG_LOADERS) {
485 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400486 }
Joe Onorato36115782010-06-17 13:28:48 -0400487 if (true || !mWorkspaceLoaded) {
488 loadWorkspace();
489 if (mStopped) {
490 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400491 }
Joe Onorato36115782010-06-17 13:28:48 -0400492 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400493 }
494
Joe Onorato36115782010-06-17 13:28:48 -0400495 // Bind the workspace
496 bindWorkspace();
497 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400498
Joe Onorato36115782010-06-17 13:28:48 -0400499 private void waitForIdle() {
500 // Wait until the either we're stopped or the other threads are done.
501 // This way we don't start loading all apps until the workspace has settled
502 // down.
503 synchronized (LoaderTask.this) {
504 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700505
Joe Onorato36115782010-06-17 13:28:48 -0400506 mHandler.postIdle(new Runnable() {
507 public void run() {
508 synchronized (LoaderTask.this) {
509 mLoadAndBindStepFinished = true;
510 if (DEBUG_LOADERS) {
511 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400512 }
Joe Onorato36115782010-06-17 13:28:48 -0400513 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400514 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400515 }
Joe Onorato36115782010-06-17 13:28:48 -0400516 });
517
518 while (!mStopped && !mLoadAndBindStepFinished) {
519 try {
520 this.wait();
521 } catch (InterruptedException ex) {
522 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400523 }
524 }
Joe Onorato36115782010-06-17 13:28:48 -0400525 if (DEBUG_LOADERS) {
526 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700527 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400528 + "ms for previous step to finish binding");
529 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400530 }
Joe Onorato36115782010-06-17 13:28:48 -0400531 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400532
Joe Onorato36115782010-06-17 13:28:48 -0400533 public void run() {
534 // Optimize for end-user experience: if the Launcher is up and // running with the
535 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
536 // workspace first (default).
537 final Callbacks cbk = mCallbacks.get();
538 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400539
Joe Onorato36115782010-06-17 13:28:48 -0400540 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400541 // Elevate priority when Home launches for the first time to avoid
542 // starving at boot time. Staring at a blank home is not cool.
543 synchronized (mLock) {
544 android.os.Process.setThreadPriority(mIsLaunching
545 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
546 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400547 if (loadWorkspaceFirst) {
548 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
549 loadAndBindWorkspace();
550 } else {
551 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700552 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400553 }
554
Joe Onorato36115782010-06-17 13:28:48 -0400555 if (mStopped) {
556 break keep_running;
557 }
558
559 // Whew! Hard work done. Slow us down, and wait until the UI thread has
560 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400561 synchronized (mLock) {
562 if (mIsLaunching) {
563 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
564 }
565 }
Joe Onorato36115782010-06-17 13:28:48 -0400566 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400567
568 // second step
569 if (loadWorkspaceFirst) {
570 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700571 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400572 } else {
573 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
574 loadAndBindWorkspace();
575 }
Joe Onorato36115782010-06-17 13:28:48 -0400576 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577
Joe Onorato36115782010-06-17 13:28:48 -0400578 // Clear out this reference, otherwise we end up holding it until all of the
579 // callback runnables are done.
580 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400581
Joe Onorato36115782010-06-17 13:28:48 -0400582 synchronized (mLock) {
583 // If we are still the last one to be scheduled, remove ourselves.
584 if (mLoaderTask == this) {
585 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400586 }
Joe Onorato36115782010-06-17 13:28:48 -0400587 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700588
Joe Onorato36115782010-06-17 13:28:48 -0400589 // Trigger a gc to try to clean up after the stuff is done, since the
590 // renderscript allocations aren't charged to the java heap.
591 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700592 mHandler.post(new Runnable() {
593 public void run() {
594 System.gc();
595 }
596 });
Joe Onorato36115782010-06-17 13:28:48 -0400597 } else {
598 mHandler.postIdle(new Runnable() {
599 public void run() {
600 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400601 }
Joe Onorato36115782010-06-17 13:28:48 -0400602 });
603 }
604 }
605
606 public void stopLocked() {
607 synchronized (LoaderTask.this) {
608 mStopped = true;
609 this.notify();
610 }
611 }
612
613 /**
614 * Gets the callbacks object. If we've been stopped, or if the launcher object
615 * has somehow been garbage collected, return null instead. Pass in the Callbacks
616 * object that was around when the deferred message was scheduled, and if there's
617 * a new Callbacks object around then also return null. This will save us from
618 * calling onto it with data that will be ignored.
619 */
620 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
621 synchronized (mLock) {
622 if (mStopped) {
623 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400624 }
Joe Onorato36115782010-06-17 13:28:48 -0400625
626 if (mCallbacks == null) {
627 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400628 }
Joe Onorato36115782010-06-17 13:28:48 -0400629
630 final Callbacks callbacks = mCallbacks.get();
631 if (callbacks != oldCallbacks) {
632 return null;
633 }
634 if (callbacks == null) {
635 Log.w(TAG, "no mCallbacks");
636 return null;
637 }
638
639 return callbacks;
640 }
641 }
642
643 // check & update map of what's occupied; used to discard overlapping/invalid items
644 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
645 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400646 return true;
647 }
Joe Onorato36115782010-06-17 13:28:48 -0400648 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
649 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
650 if (occupied[item.screen][x][y] != null) {
651 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700652 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400653 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700654 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400655 + occupied[item.screen][x][y]);
656 return false;
657 }
658 }
659 }
660 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
661 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
662 occupied[item.screen][x][y] = item;
663 }
664 }
665 return true;
666 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400667
Joe Onorato36115782010-06-17 13:28:48 -0400668 private void loadWorkspace() {
669 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670
Joe Onorato36115782010-06-17 13:28:48 -0400671 final Context context = mContext;
672 final ContentResolver contentResolver = context.getContentResolver();
673 final PackageManager manager = context.getPackageManager();
674 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
675 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400676
Joe Onorato36115782010-06-17 13:28:48 -0400677 mItems.clear();
678 mAppWidgets.clear();
679 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800680
Joe Onorato36115782010-06-17 13:28:48 -0400681 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400682
Joe Onorato36115782010-06-17 13:28:48 -0400683 final Cursor c = contentResolver.query(
684 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400685
Adam Cohend22015c2010-07-26 22:02:18 -0700686 final ItemInfo occupied[][][] =
687 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400688
Joe Onorato36115782010-06-17 13:28:48 -0400689 try {
690 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
691 final int intentIndex = c.getColumnIndexOrThrow
692 (LauncherSettings.Favorites.INTENT);
693 final int titleIndex = c.getColumnIndexOrThrow
694 (LauncherSettings.Favorites.TITLE);
695 final int iconTypeIndex = c.getColumnIndexOrThrow(
696 LauncherSettings.Favorites.ICON_TYPE);
697 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
698 final int iconPackageIndex = c.getColumnIndexOrThrow(
699 LauncherSettings.Favorites.ICON_PACKAGE);
700 final int iconResourceIndex = c.getColumnIndexOrThrow(
701 LauncherSettings.Favorites.ICON_RESOURCE);
702 final int containerIndex = c.getColumnIndexOrThrow(
703 LauncherSettings.Favorites.CONTAINER);
704 final int itemTypeIndex = c.getColumnIndexOrThrow(
705 LauncherSettings.Favorites.ITEM_TYPE);
706 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
707 LauncherSettings.Favorites.APPWIDGET_ID);
708 final int screenIndex = c.getColumnIndexOrThrow(
709 LauncherSettings.Favorites.SCREEN);
710 final int cellXIndex = c.getColumnIndexOrThrow
711 (LauncherSettings.Favorites.CELLX);
712 final int cellYIndex = c.getColumnIndexOrThrow
713 (LauncherSettings.Favorites.CELLY);
714 final int spanXIndex = c.getColumnIndexOrThrow
715 (LauncherSettings.Favorites.SPANX);
716 final int spanYIndex = c.getColumnIndexOrThrow(
717 LauncherSettings.Favorites.SPANY);
718 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
719 final int displayModeIndex = c.getColumnIndexOrThrow(
720 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721
Joe Onorato36115782010-06-17 13:28:48 -0400722 ShortcutInfo info;
723 String intentDescription;
724 LauncherAppWidgetInfo appWidgetInfo;
725 int container;
726 long id;
727 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728
Joe Onorato36115782010-06-17 13:28:48 -0400729 while (!mStopped && c.moveToNext()) {
730 try {
731 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732
Joe Onorato36115782010-06-17 13:28:48 -0400733 switch (itemType) {
734 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
735 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
736 intentDescription = c.getString(intentIndex);
737 try {
738 intent = Intent.parseUri(intentDescription, 0);
739 } catch (URISyntaxException e) {
740 continue;
741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742
Joe Onorato36115782010-06-17 13:28:48 -0400743 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
744 info = getShortcutInfo(manager, intent, context, c, iconIndex,
745 titleIndex);
746 } else {
747 info = getShortcutInfo(c, context, iconTypeIndex,
748 iconPackageIndex, iconResourceIndex, iconIndex,
749 titleIndex);
750 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751
Joe Onorato36115782010-06-17 13:28:48 -0400752 if (info != null) {
753 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754
Joe Onorato36115782010-06-17 13:28:48 -0400755 info.intent = intent;
756 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400758 info.container = container;
759 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700760 info.cellX = c.getInt(cellXIndex);
761 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762
Daniel Sandler8802e962010-05-26 16:28:16 -0400763 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400764 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400765 break;
766 }
767
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400769 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
770 mItems.add(info);
771 break;
772 default:
773 // Item is in a user folder
774 UserFolderInfo folderInfo =
775 findOrMakeUserFolder(mFolders, container);
776 folderInfo.add(info);
777 break;
778 }
779 } else {
780 // Failed to load the shortcut, probably because the
781 // activity manager couldn't resolve it (maybe the app
782 // was uninstalled), or the db row was somehow screwed up.
783 // Delete it.
784 id = c.getLong(idIndex);
785 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
786 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
787 id, false), null, null);
788 }
789 break;
790
791 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
792 id = c.getLong(idIndex);
793 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
794
Winson Chungaafa03c2010-06-11 17:34:16 -0700795 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400796 folderInfo.id = id;
797 container = c.getInt(containerIndex);
798 folderInfo.container = container;
799 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700800 folderInfo.cellX = c.getInt(cellXIndex);
801 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400802
803 // check & update map of what's occupied
804 if (!checkItemPlacement(occupied, folderInfo)) {
805 break;
806 }
Joe Onorato36115782010-06-17 13:28:48 -0400807 switch (container) {
808 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
809 mItems.add(folderInfo);
810 break;
811 }
812
813 mFolders.put(folderInfo.id, folderInfo);
814 break;
815
816 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
817 id = c.getLong(idIndex);
818 Uri uri = Uri.parse(c.getString(uriIndex));
819
820 // Make sure the live folder exists
821 final ProviderInfo providerInfo =
822 context.getPackageManager().resolveContentProvider(
823 uri.getAuthority(), 0);
824
825 if (providerInfo == null && !isSafeMode) {
826 itemsToRemove.add(id);
827 } else {
828 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400829 intentDescription = c.getString(intentIndex);
830 intent = null;
831 if (intentDescription != null) {
832 try {
833 intent = Intent.parseUri(intentDescription, 0);
834 } catch (URISyntaxException e) {
835 // Ignore, a live folder might not have a base intent
836 }
837 }
838
839 liveFolderInfo.title = c.getString(titleIndex);
840 liveFolderInfo.id = id;
841 liveFolderInfo.uri = uri;
842 container = c.getInt(containerIndex);
843 liveFolderInfo.container = container;
844 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700845 liveFolderInfo.cellX = c.getInt(cellXIndex);
846 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400847 liveFolderInfo.baseIntent = intent;
848 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
849
850 // check & update map of what's occupied
851 if (!checkItemPlacement(occupied, liveFolderInfo)) {
852 break;
853 }
854
855 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
856 iconResourceIndex, liveFolderInfo);
857
858 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400860 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861 break;
862 }
Joe Onorato36115782010-06-17 13:28:48 -0400863 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864 }
Joe Onorato36115782010-06-17 13:28:48 -0400865 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800866
Joe Onorato36115782010-06-17 13:28:48 -0400867 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
868 // Read all Launcher-specific widget details
869 int appWidgetId = c.getInt(appWidgetIdIndex);
870 id = c.getLong(idIndex);
871
872 final AppWidgetProviderInfo provider =
873 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700874
Joe Onorato36115782010-06-17 13:28:48 -0400875 if (!isSafeMode && (provider == null || provider.provider == null ||
876 provider.provider.getPackageName() == null)) {
877 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
878 + id + " appWidgetId=" + appWidgetId);
879 itemsToRemove.add(id);
880 } else {
881 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
882 appWidgetInfo.id = id;
883 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700884 appWidgetInfo.cellX = c.getInt(cellXIndex);
885 appWidgetInfo.cellY = c.getInt(cellYIndex);
886 appWidgetInfo.spanX = c.getInt(spanXIndex);
887 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400888
889 container = c.getInt(containerIndex);
890 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
891 Log.e(TAG, "Widget found where container "
892 + "!= CONTAINER_DESKTOP -- ignoring!");
893 continue;
894 }
895 appWidgetInfo.container = c.getInt(containerIndex);
896
897 // check & update map of what's occupied
898 if (!checkItemPlacement(occupied, appWidgetInfo)) {
899 break;
900 }
901
902 mAppWidgets.add(appWidgetInfo);
903 }
904 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800905 }
Joe Onorato36115782010-06-17 13:28:48 -0400906 } catch (Exception e) {
907 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800908 }
909 }
Joe Onorato36115782010-06-17 13:28:48 -0400910 } finally {
911 c.close();
912 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800913
Joe Onorato36115782010-06-17 13:28:48 -0400914 if (itemsToRemove.size() > 0) {
915 ContentProviderClient client = contentResolver.acquireContentProviderClient(
916 LauncherSettings.Favorites.CONTENT_URI);
917 // Remove dead items
918 for (long id : itemsToRemove) {
919 if (DEBUG_LOADERS) {
920 Log.d(TAG, "Removed id = " + id);
921 }
922 // Don't notify content observers
923 try {
924 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
925 null, null);
926 } catch (RemoteException e) {
927 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400928 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800929 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 }
931
Joe Onorato36115782010-06-17 13:28:48 -0400932 if (DEBUG_LOADERS) {
933 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
934 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700935 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400936 String line = "";
937 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
938 if (s > 0) {
939 line += " | ";
940 }
Adam Cohend22015c2010-07-26 22:02:18 -0700941 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400942 line += ((occupied[s][x][y] != null) ? "#" : ".");
943 }
944 }
945 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 }
Joe Onorato36115782010-06-17 13:28:48 -0400947 }
948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949
Joe Onorato36115782010-06-17 13:28:48 -0400950 /**
951 * Read everything out of our database.
952 */
953 private void bindWorkspace() {
954 final long t = SystemClock.uptimeMillis();
955
956 // Don't use these two variables in any of the callback runnables.
957 // Otherwise we hold a reference to them.
958 final Callbacks oldCallbacks = mCallbacks.get();
959 if (oldCallbacks == null) {
960 // This launcher has exited and nobody bothered to tell us. Just bail.
961 Log.w(TAG, "LoaderTask running with no launcher");
962 return;
963 }
964
965 int N;
966 // Tell the workspace that we're about to start firing items at it
967 mHandler.post(new Runnable() {
968 public void run() {
969 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
970 if (callbacks != null) {
971 callbacks.startBinding();
972 }
973 }
974 });
975 // Add the items to the workspace.
976 N = mItems.size();
977 for (int i=0; i<N; i+=ITEMS_CHUNK) {
978 final int start = i;
979 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 mHandler.post(new Runnable() {
981 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800982 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400984 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 }
986 }
987 });
Joe Onorato36115782010-06-17 13:28:48 -0400988 }
989 mHandler.post(new Runnable() {
990 public void run() {
991 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
992 if (callbacks != null) {
993 callbacks.bindFolders(mFolders);
994 }
995 }
996 });
997 // Wait until the queue goes empty.
998 mHandler.post(new Runnable() {
999 public void run() {
1000 if (DEBUG_LOADERS) {
1001 Log.d(TAG, "Going to start binding widgets soon.");
1002 }
1003 }
1004 });
1005 // Bind the widgets, one at a time.
1006 // WARNING: this is calling into the workspace from the background thread,
1007 // but since getCurrentScreen() just returns the int, we should be okay. This
1008 // is just a hint for the order, and if it's wrong, we'll be okay.
1009 // TODO: instead, we should have that push the current screen into here.
1010 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1011 N = mAppWidgets.size();
1012 // once for the current screen
1013 for (int i=0; i<N; i++) {
1014 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1015 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016 mHandler.post(new Runnable() {
1017 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001018 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001020 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 }
1022 }
1023 });
1024 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 }
Joe Onorato36115782010-06-17 13:28:48 -04001026 // once for the other screens
1027 for (int i=0; i<N; i++) {
1028 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1029 if (widget.screen != currentScreen) {
1030 mHandler.post(new Runnable() {
1031 public void run() {
1032 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1033 if (callbacks != null) {
1034 callbacks.bindAppWidget(widget);
1035 }
1036 }
1037 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001038 }
1039 }
Joe Onorato36115782010-06-17 13:28:48 -04001040 // Tell the workspace that we're done.
1041 mHandler.post(new Runnable() {
1042 public void run() {
1043 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1044 if (callbacks != null) {
1045 callbacks.finishBindingItems();
1046 }
1047 }
1048 });
1049 // If we're profiling, this is the last thing in the queue.
1050 mHandler.post(new Runnable() {
1051 public void run() {
1052 if (DEBUG_LOADERS) {
1053 Log.d(TAG, "bound workspace in "
1054 + (SystemClock.uptimeMillis()-t) + "ms");
1055 }
1056 }
1057 });
1058 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001059
Joe Onorato36115782010-06-17 13:28:48 -04001060 private void loadAndBindAllApps() {
1061 if (DEBUG_LOADERS) {
1062 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1063 }
1064 if (!mAllAppsLoaded) {
1065 loadAllAppsByBatch();
1066 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001067 return;
1068 }
Joe Onorato36115782010-06-17 13:28:48 -04001069 mAllAppsLoaded = true;
1070 } else {
1071 onlyBindAllApps();
1072 }
1073 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001074
Joe Onorato36115782010-06-17 13:28:48 -04001075 private void onlyBindAllApps() {
1076 final Callbacks oldCallbacks = mCallbacks.get();
1077 if (oldCallbacks == null) {
1078 // This launcher has exited and nobody bothered to tell us. Just bail.
1079 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1080 return;
1081 }
1082
1083 // shallow copy
1084 final ArrayList<ApplicationInfo> list
1085 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1086 mHandler.post(new Runnable() {
1087 public void run() {
1088 final long t = SystemClock.uptimeMillis();
1089 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1090 if (callbacks != null) {
1091 callbacks.bindAllApplications(list);
1092 }
1093 if (DEBUG_LOADERS) {
1094 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1095 + (SystemClock.uptimeMillis()-t) + "ms");
1096 }
1097 }
1098 });
1099
1100 }
1101
1102 private void loadAllAppsByBatch() {
1103 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1104
1105 // Don't use these two variables in any of the callback runnables.
1106 // Otherwise we hold a reference to them.
1107 final Callbacks oldCallbacks = mCallbacks.get();
1108 if (oldCallbacks == null) {
1109 // This launcher has exited and nobody bothered to tell us. Just bail.
1110 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1111 return;
1112 }
1113
1114 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1115 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1116
1117 final PackageManager packageManager = mContext.getPackageManager();
1118 List<ResolveInfo> apps = null;
1119
1120 int N = Integer.MAX_VALUE;
1121
1122 int startIndex;
1123 int i=0;
1124 int batchSize = -1;
1125 while (i < N && !mStopped) {
1126 if (i == 0) {
1127 mAllAppsList.clear();
1128 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1129 apps = packageManager.queryIntentActivities(mainIntent, 0);
1130 if (DEBUG_LOADERS) {
1131 Log.d(TAG, "queryIntentActivities took "
1132 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1133 }
1134 if (apps == null) {
1135 return;
1136 }
1137 N = apps.size();
1138 if (DEBUG_LOADERS) {
1139 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1140 }
1141 if (N == 0) {
1142 // There are no apps?!?
1143 return;
1144 }
1145 if (mBatchSize == 0) {
1146 batchSize = N;
1147 } else {
1148 batchSize = mBatchSize;
1149 }
1150
1151 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1152 Collections.sort(apps,
1153 new ResolveInfo.DisplayNameComparator(packageManager));
1154 if (DEBUG_LOADERS) {
1155 Log.d(TAG, "sort took "
1156 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1157 }
1158 }
1159
1160 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1161
1162 startIndex = i;
1163 for (int j=0; i<N && j<batchSize; j++) {
1164 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001165 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001166 i++;
1167 }
1168
1169 final boolean first = i <= batchSize;
1170 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1171 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1172 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1173
Joe Onoratocc67f472010-06-08 10:54:30 -07001174 mHandler.post(new Runnable() {
1175 public void run() {
1176 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001177 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001178 if (first) {
1179 callbacks.bindAllApplications(added);
1180 } else {
1181 callbacks.bindAppsAdded(added);
1182 }
1183 if (DEBUG_LOADERS) {
1184 Log.d(TAG, "bound " + added.size() + " apps in "
1185 + (SystemClock.uptimeMillis() - t) + "ms");
1186 }
1187 } else {
1188 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001189 }
1190 }
1191 });
1192
Daniel Sandlerdca66122010-04-13 16:23:58 -04001193 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001194 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1195 + (SystemClock.uptimeMillis()-t2) + "ms");
1196 }
1197
1198 if (mAllAppsLoadDelay > 0 && i < N) {
1199 try {
1200 if (DEBUG_LOADERS) {
1201 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1202 }
1203 Thread.sleep(mAllAppsLoadDelay);
1204 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 }
1206 }
1207
Joe Onorato36115782010-06-17 13:28:48 -04001208 if (DEBUG_LOADERS) {
1209 Log.d(TAG, "cached all " + N + " apps in "
1210 + (SystemClock.uptimeMillis()-t) + "ms"
1211 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001212 }
1213 }
1214
1215 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001216 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1217 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1218 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1219 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1220 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1221 }
1222 }
1223
1224 void enqueuePackageUpdated(PackageUpdatedTask task) {
1225 mWorker.post(task);
1226 }
1227
1228 private class PackageUpdatedTask implements Runnable {
1229 int mOp;
1230 String[] mPackages;
1231
1232 public static final int OP_NONE = 0;
1233 public static final int OP_ADD = 1;
1234 public static final int OP_UPDATE = 2;
1235 public static final int OP_REMOVE = 3; // uninstlled
1236 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1237
1238
1239 public PackageUpdatedTask(int op, String[] packages) {
1240 mOp = op;
1241 mPackages = packages;
1242 }
1243
1244 public void run() {
1245 final Context context = mApp;
1246
1247 final String[] packages = mPackages;
1248 final int N = packages.length;
1249 switch (mOp) {
1250 case OP_ADD:
1251 for (int i=0; i<N; i++) {
1252 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1253 mAllAppsList.addPackage(context, packages[i]);
1254 }
1255 break;
1256 case OP_UPDATE:
1257 for (int i=0; i<N; i++) {
1258 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1259 mAllAppsList.updatePackage(context, packages[i]);
1260 }
1261 break;
1262 case OP_REMOVE:
1263 case OP_UNAVAILABLE:
1264 for (int i=0; i<N; i++) {
1265 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1266 mAllAppsList.removePackage(packages[i]);
1267 }
1268 break;
1269 }
1270
1271 ArrayList<ApplicationInfo> added = null;
1272 ArrayList<ApplicationInfo> removed = null;
1273 ArrayList<ApplicationInfo> modified = null;
1274
1275 if (mAllAppsList.added.size() > 0) {
1276 added = mAllAppsList.added;
1277 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1278 }
1279 if (mAllAppsList.removed.size() > 0) {
1280 removed = mAllAppsList.removed;
1281 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1282 for (ApplicationInfo info: removed) {
1283 mIconCache.remove(info.intent.getComponent());
1284 }
1285 }
1286 if (mAllAppsList.modified.size() > 0) {
1287 modified = mAllAppsList.modified;
1288 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1289 }
1290
1291 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1292 if (callbacks == null) {
1293 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1294 return;
1295 }
1296
1297 if (added != null) {
1298 final ArrayList<ApplicationInfo> addedFinal = added;
1299 mHandler.post(new Runnable() {
1300 public void run() {
1301 if (callbacks == mCallbacks.get()) {
1302 callbacks.bindAppsAdded(addedFinal);
1303 }
1304 }
1305 });
1306 }
1307 if (modified != null) {
1308 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1309 mHandler.post(new Runnable() {
1310 public void run() {
1311 if (callbacks == mCallbacks.get()) {
1312 callbacks.bindAppsUpdated(modifiedFinal);
1313 }
1314 }
1315 });
1316 }
1317 if (removed != null) {
1318 final boolean permanent = mOp != OP_UNAVAILABLE;
1319 final ArrayList<ApplicationInfo> removedFinal = removed;
1320 mHandler.post(new Runnable() {
1321 public void run() {
1322 if (callbacks == mCallbacks.get()) {
1323 callbacks.bindAppsRemoved(removedFinal, permanent);
1324 }
1325 }
1326 });
Joe Onoratobe386092009-11-17 17:32:16 -08001327 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001328
1329 mHandler.post(new Runnable() {
1330 @Override
1331 public void run() {
1332 if (callbacks == mCallbacks.get()) {
1333 callbacks.bindPackagesUpdated();
1334 }
1335 }
1336 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001337 }
1338 }
1339
1340 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001341 * This is called from the code that adds shortcuts from the intent receiver. This
1342 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001343 */
Joe Onorato56d82912010-03-07 14:32:10 -05001344 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001345 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001346 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347
Joe Onorato56d82912010-03-07 14:32:10 -05001348 /**
1349 * Make an ShortcutInfo object for a shortcut that is an application.
1350 *
1351 * If c is not null, then it will be used to fill in missing data like the title and icon.
1352 */
1353 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1354 Cursor c, int iconIndex, int titleIndex) {
1355 Bitmap icon = null;
1356 final ShortcutInfo info = new ShortcutInfo();
1357
1358 ComponentName componentName = intent.getComponent();
1359 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001360 return null;
1361 }
1362
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001363 // TODO: See if the PackageManager knows about this case. If it doesn't
1364 // then return null & delete this.
1365
Joe Onorato56d82912010-03-07 14:32:10 -05001366 // the resource -- This may implicitly give us back the fallback icon,
1367 // but don't worry about that. All we're doing with usingFallbackIcon is
1368 // to avoid saving lots of copies of that in the database, and most apps
1369 // have icons anyway.
1370 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1371 if (resolveInfo != null) {
1372 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001373 }
Joe Onorato56d82912010-03-07 14:32:10 -05001374 // the db
1375 if (icon == null) {
1376 if (c != null) {
1377 icon = getIconFromCursor(c, iconIndex);
1378 }
1379 }
1380 // the fallback icon
1381 if (icon == null) {
1382 icon = getFallbackIcon();
1383 info.usingFallbackIcon = true;
1384 }
1385 info.setIcon(icon);
1386
1387 // from the resource
1388 if (resolveInfo != null) {
1389 info.title = resolveInfo.activityInfo.loadLabel(manager);
1390 }
1391 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001393 if (c != null) {
1394 info.title = c.getString(titleIndex);
1395 }
1396 }
1397 // fall back to the class name of the activity
1398 if (info.title == null) {
1399 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001400 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001401 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1402 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001406 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001408 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001409 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1410 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411
Joe Onorato56d82912010-03-07 14:32:10 -05001412 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001413 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001414 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001416 // TODO: If there's an explicit component and we can't install that, delete it.
1417
Joe Onorato56d82912010-03-07 14:32:10 -05001418 info.title = c.getString(titleIndex);
1419
Joe Onorato9c1289c2009-08-17 11:03:03 -04001420 int iconType = c.getInt(iconTypeIndex);
1421 switch (iconType) {
1422 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1423 String packageName = c.getString(iconPackageIndex);
1424 String resourceName = c.getString(iconResourceIndex);
1425 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001426 info.customIcon = false;
1427 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001430 if (resources != null) {
1431 final int id = resources.getIdentifier(resourceName, null, null);
1432 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1433 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001435 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
Joe Onorato56d82912010-03-07 14:32:10 -05001437 // the db
1438 if (icon == null) {
1439 icon = getIconFromCursor(c, iconIndex);
1440 }
1441 // the fallback icon
1442 if (icon == null) {
1443 icon = getFallbackIcon();
1444 info.usingFallbackIcon = true;
1445 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446 break;
1447 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001448 icon = getIconFromCursor(c, iconIndex);
1449 if (icon == null) {
1450 icon = getFallbackIcon();
1451 info.customIcon = false;
1452 info.usingFallbackIcon = true;
1453 } else {
1454 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001455 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001456 break;
1457 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001458 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001459 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 info.customIcon = false;
1461 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001463 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001464 return info;
1465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466
Joe Onorato56d82912010-03-07 14:32:10 -05001467 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1468 if (false) {
1469 Log.d(TAG, "getIconFromCursor app="
1470 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1471 }
1472 byte[] data = c.getBlob(iconIndex);
1473 try {
1474 return BitmapFactory.decodeByteArray(data, 0, data.length);
1475 } catch (Exception e) {
1476 return null;
1477 }
1478 }
1479
Joe Onorato0589f0f2010-02-08 13:44:00 -08001480 ShortcutInfo addShortcut(Context context, Intent data,
1481 CellLayout.CellInfo cellInfo, boolean notify) {
1482
1483 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1484 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1485 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1486
1487 return info;
1488 }
1489
1490 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1491 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1492 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1493 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1494
1495 Bitmap icon = null;
1496 boolean filtered = false;
1497 boolean customIcon = false;
1498 ShortcutIconResource iconResource = null;
1499
1500 if (bitmap != null && bitmap instanceof Bitmap) {
1501 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1502 filtered = true;
1503 customIcon = true;
1504 } else {
1505 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1506 if (extra != null && extra instanceof ShortcutIconResource) {
1507 try {
1508 iconResource = (ShortcutIconResource) extra;
1509 final PackageManager packageManager = context.getPackageManager();
1510 Resources resources = packageManager.getResourcesForApplication(
1511 iconResource.packageName);
1512 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1513 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1514 } catch (Exception e) {
1515 Log.w(TAG, "Could not load shortcut icon: " + extra);
1516 }
1517 }
1518 }
1519
Joe Onorato0589f0f2010-02-08 13:44:00 -08001520 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001521
1522 if (icon == null) {
1523 icon = getFallbackIcon();
1524 info.usingFallbackIcon = true;
1525 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001526 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001527
Joe Onorato0589f0f2010-02-08 13:44:00 -08001528 info.title = name;
1529 info.intent = intent;
1530 info.customIcon = customIcon;
1531 info.iconResource = iconResource;
1532
1533 return info;
1534 }
1535
Joe Onorato9c1289c2009-08-17 11:03:03 -04001536 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1537 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1538
1539 int iconType = c.getInt(iconTypeIndex);
1540 switch (iconType) {
1541 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1542 String packageName = c.getString(iconPackageIndex);
1543 String resourceName = c.getString(iconResourceIndex);
1544 PackageManager packageManager = context.getPackageManager();
1545 try {
1546 Resources resources = packageManager.getResourcesForApplication(packageName);
1547 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001548 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1549 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001551 liveFolderInfo.icon = Utilities.createIconBitmap(
1552 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1553 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001554 }
1555 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1556 liveFolderInfo.iconResource.packageName = packageName;
1557 liveFolderInfo.iconResource.resourceName = resourceName;
1558 break;
1559 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001560 liveFolderInfo.icon = Utilities.createIconBitmap(
1561 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1562 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001563 }
1564 }
1565
Joe Onorato56d82912010-03-07 14:32:10 -05001566 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1567 // If this icon doesn't have a custom icon, check to see
1568 // what's stored in the DB, and if it doesn't match what
1569 // we're going to show, store what we are going to show back
1570 // into the DB. We do this so when we're loading, if the
1571 // package manager can't find an icon (for example because
1572 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001573 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001574 boolean needSave;
1575 byte[] data = c.getBlob(iconIndex);
1576 try {
1577 if (data != null) {
1578 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1579 Bitmap loaded = info.getIcon(mIconCache);
1580 needSave = !saved.sameAs(loaded);
1581 } else {
1582 needSave = true;
1583 }
1584 } catch (Exception e) {
1585 needSave = true;
1586 }
1587 if (needSave) {
1588 Log.d(TAG, "going to save icon bitmap for info=" + info);
1589 // This is slower than is ideal, but this only happens either
1590 // after the froyo OTA or when the app is updated with a new
1591 // icon.
1592 updateItemInDatabase(context, info);
1593 }
1594 }
1595 }
1596
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597 /**
1598 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1599 * or make a new one.
1600 */
1601 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1602 // See if a placeholder was created for us already
1603 FolderInfo folderInfo = folders.get(id);
1604 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1605 // No placeholder -- create a new instance
1606 folderInfo = new UserFolderInfo();
1607 folders.put(id, folderInfo);
1608 }
1609 return (UserFolderInfo) folderInfo;
1610 }
1611
1612 /**
1613 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1614 * new one.
1615 */
1616 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1617 // See if a placeholder was created for us already
1618 FolderInfo folderInfo = folders.get(id);
1619 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1620 // No placeholder -- create a new instance
1621 folderInfo = new LiveFolderInfo();
1622 folders.put(id, folderInfo);
1623 }
1624 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
1626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1628 String label = activityInfo.loadLabel(manager).toString();
1629 if (label == null) {
1630 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1631 if (label == null) {
1632 label = activityInfo.name;
1633 }
1634 }
1635 return label;
1636 }
1637
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001639 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640 = new Comparator<ApplicationInfo>() {
1641 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1642 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001644 };
Joe Onoratobe386092009-11-17 17:32:16 -08001645
1646 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001647 Log.d(TAG, "mCallbacks=" + mCallbacks);
1648 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1649 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1650 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1651 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001652 Log.d(TAG, "mItems size=" + mItems.size());
1653 if (mLoaderTask != null) {
1654 mLoaderTask.dumpState();
1655 } else {
1656 Log.d(TAG, "mLoaderTask=null");
1657 }
Joe Onoratobe386092009-11-17 17:32:16 -08001658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659}