blob: 797bf686c26d0b4b983007c8656057e364dac5f5 [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();
344
345 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
346 }
347
348 /**
349 * Remove the contents of the specified folder from the database
350 */
351 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
352 final ContentResolver cr = context.getContentResolver();
353
354 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
355 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
356 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
357 }
358
359 /**
360 * Set this as the current Launcher activity object for the loader.
361 */
362 public void initialize(Callbacks callbacks) {
363 synchronized (mLock) {
364 mCallbacks = new WeakReference<Callbacks>(callbacks);
365 }
366 }
367
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700368 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
370 * ACTION_PACKAGE_CHANGED.
371 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400372 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400373 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700374
Joe Onorato36115782010-06-17 13:28:48 -0400375 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400376
Joe Onorato36115782010-06-17 13:28:48 -0400377 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
378 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
379 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
380 final String packageName = intent.getData().getSchemeSpecificPart();
381 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400382
Joe Onorato36115782010-06-17 13:28:48 -0400383 int op = PackageUpdatedTask.OP_NONE;
384
385 if (packageName == null || packageName.length() == 0) {
386 // they sent us a bad intent
387 return;
388 }
389
390 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
391 op = PackageUpdatedTask.OP_UPDATE;
392 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
393 if (!replacing) {
394 op = PackageUpdatedTask.OP_REMOVE;
395 }
396 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
397 // later, we will update the package at this time
398 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
399 if (!replacing) {
400 op = PackageUpdatedTask.OP_ADD;
401 } else {
402 op = PackageUpdatedTask.OP_UPDATE;
403 }
404 }
405
406 if (op != PackageUpdatedTask.OP_NONE) {
407 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
408 }
409
410 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
411 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
412 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
413
414 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
415 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
416 enqueuePackageUpdated(new PackageUpdatedTask(
417 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onorato790c2d92010-06-11 00:14:11 -0700418 }
Joe Onorato36115782010-06-17 13:28:48 -0400419 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400420
Joe Onorato36115782010-06-17 13:28:48 -0400421 public void startLoader(Context context, boolean isLaunching) {
422 synchronized (mLock) {
423 if (DEBUG_LOADERS) {
424 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
425 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400426
Joe Onorato36115782010-06-17 13:28:48 -0400427 // Don't bother to start the thread if we know it's not going to do anything
428 if (mCallbacks != null && mCallbacks.get() != null) {
429 // If there is already one running, tell it to stop.
430 LoaderTask oldTask = mLoaderTask;
431 if (oldTask != null) {
432 if (oldTask.isLaunching()) {
433 // don't downgrade isLaunching if we're already running
434 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500435 }
Joe Onorato36115782010-06-17 13:28:48 -0400436 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500437 }
Joe Onorato36115782010-06-17 13:28:48 -0400438 mLoaderTask = new LoaderTask(context, isLaunching);
439 mWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 }
442 }
443
Joe Onorato36115782010-06-17 13:28:48 -0400444 public void stopLoader() {
445 synchronized (mLock) {
446 if (mLoaderTask != null) {
447 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448 }
449 }
Joe Onorato36115782010-06-17 13:28:48 -0400450 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451
Joe Onorato36115782010-06-17 13:28:48 -0400452 /**
453 * Runnable for the thread that loads the contents of the launcher:
454 * - workspace icons
455 * - widgets
456 * - all apps icons
457 */
458 private class LoaderTask implements Runnable {
459 private Context mContext;
460 private Thread mWaitThread;
461 private boolean mIsLaunching;
462 private boolean mStopped;
463 private boolean mLoadAndBindStepFinished;
464
465 LoaderTask(Context context, boolean isLaunching) {
466 mContext = context;
467 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400468 }
469
Joe Onorato36115782010-06-17 13:28:48 -0400470 boolean isLaunching() {
471 return mIsLaunching;
472 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400473
Joe Onorato36115782010-06-17 13:28:48 -0400474 private void loadAndBindWorkspace() {
475 // Load the workspace
476
477 // For now, just always reload the workspace. It's ~100 ms vs. the
478 // binding which takes many hundreds of ms.
479 // We can reconsider.
480 if (DEBUG_LOADERS) {
481 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400482 }
Joe Onorato36115782010-06-17 13:28:48 -0400483 if (true || !mWorkspaceLoaded) {
484 loadWorkspace();
485 if (mStopped) {
486 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400487 }
Joe Onorato36115782010-06-17 13:28:48 -0400488 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400489 }
490
Joe Onorato36115782010-06-17 13:28:48 -0400491 // Bind the workspace
492 bindWorkspace();
493 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400494
Joe Onorato36115782010-06-17 13:28:48 -0400495 private void waitForIdle() {
496 // Wait until the either we're stopped or the other threads are done.
497 // This way we don't start loading all apps until the workspace has settled
498 // down.
499 synchronized (LoaderTask.this) {
500 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700501
Joe Onorato36115782010-06-17 13:28:48 -0400502 mHandler.postIdle(new Runnable() {
503 public void run() {
504 synchronized (LoaderTask.this) {
505 mLoadAndBindStepFinished = true;
506 if (DEBUG_LOADERS) {
507 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400508 }
Joe Onorato36115782010-06-17 13:28:48 -0400509 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400510 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400511 }
Joe Onorato36115782010-06-17 13:28:48 -0400512 });
513
514 while (!mStopped && !mLoadAndBindStepFinished) {
515 try {
516 this.wait();
517 } catch (InterruptedException ex) {
518 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400519 }
520 }
Joe Onorato36115782010-06-17 13:28:48 -0400521 if (DEBUG_LOADERS) {
522 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700523 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400524 + "ms for previous step to finish binding");
525 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400526 }
Joe Onorato36115782010-06-17 13:28:48 -0400527 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400528
Joe Onorato36115782010-06-17 13:28:48 -0400529 public void run() {
530 // Optimize for end-user experience: if the Launcher is up and // running with the
531 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
532 // workspace first (default).
533 final Callbacks cbk = mCallbacks.get();
534 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400535
Joe Onorato36115782010-06-17 13:28:48 -0400536 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400537 // Elevate priority when Home launches for the first time to avoid
538 // starving at boot time. Staring at a blank home is not cool.
539 synchronized (mLock) {
540 android.os.Process.setThreadPriority(mIsLaunching
541 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
542 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400543 if (loadWorkspaceFirst) {
544 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
545 loadAndBindWorkspace();
546 } else {
547 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700548 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400549 }
550
Joe Onorato36115782010-06-17 13:28:48 -0400551 if (mStopped) {
552 break keep_running;
553 }
554
555 // Whew! Hard work done. Slow us down, and wait until the UI thread has
556 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400557 synchronized (mLock) {
558 if (mIsLaunching) {
559 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
560 }
561 }
Joe Onorato36115782010-06-17 13:28:48 -0400562 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400563
564 // second step
565 if (loadWorkspaceFirst) {
566 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700567 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400568 } else {
569 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
570 loadAndBindWorkspace();
571 }
Joe Onorato36115782010-06-17 13:28:48 -0400572 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573
Joe Onorato36115782010-06-17 13:28:48 -0400574 // Clear out this reference, otherwise we end up holding it until all of the
575 // callback runnables are done.
576 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577
Joe Onorato36115782010-06-17 13:28:48 -0400578 synchronized (mLock) {
579 // If we are still the last one to be scheduled, remove ourselves.
580 if (mLoaderTask == this) {
581 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400582 }
Joe Onorato36115782010-06-17 13:28:48 -0400583 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700584
Joe Onorato36115782010-06-17 13:28:48 -0400585 // Trigger a gc to try to clean up after the stuff is done, since the
586 // renderscript allocations aren't charged to the java heap.
587 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700588 mHandler.post(new Runnable() {
589 public void run() {
590 System.gc();
591 }
592 });
Joe Onorato36115782010-06-17 13:28:48 -0400593 } else {
594 mHandler.postIdle(new Runnable() {
595 public void run() {
596 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400597 }
Joe Onorato36115782010-06-17 13:28:48 -0400598 });
599 }
600 }
601
602 public void stopLocked() {
603 synchronized (LoaderTask.this) {
604 mStopped = true;
605 this.notify();
606 }
607 }
608
609 /**
610 * Gets the callbacks object. If we've been stopped, or if the launcher object
611 * has somehow been garbage collected, return null instead. Pass in the Callbacks
612 * object that was around when the deferred message was scheduled, and if there's
613 * a new Callbacks object around then also return null. This will save us from
614 * calling onto it with data that will be ignored.
615 */
616 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
617 synchronized (mLock) {
618 if (mStopped) {
619 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400620 }
Joe Onorato36115782010-06-17 13:28:48 -0400621
622 if (mCallbacks == null) {
623 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400624 }
Joe Onorato36115782010-06-17 13:28:48 -0400625
626 final Callbacks callbacks = mCallbacks.get();
627 if (callbacks != oldCallbacks) {
628 return null;
629 }
630 if (callbacks == null) {
631 Log.w(TAG, "no mCallbacks");
632 return null;
633 }
634
635 return callbacks;
636 }
637 }
638
639 // check & update map of what's occupied; used to discard overlapping/invalid items
640 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
641 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400642 return true;
643 }
Joe Onorato36115782010-06-17 13:28:48 -0400644 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
645 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
646 if (occupied[item.screen][x][y] != null) {
647 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700648 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400649 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400651 + occupied[item.screen][x][y]);
652 return false;
653 }
654 }
655 }
656 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
657 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
658 occupied[item.screen][x][y] = item;
659 }
660 }
661 return true;
662 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663
Joe Onorato36115782010-06-17 13:28:48 -0400664 private void loadWorkspace() {
665 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666
Joe Onorato36115782010-06-17 13:28:48 -0400667 final Context context = mContext;
668 final ContentResolver contentResolver = context.getContentResolver();
669 final PackageManager manager = context.getPackageManager();
670 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
671 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400672
Joe Onorato36115782010-06-17 13:28:48 -0400673 mItems.clear();
674 mAppWidgets.clear();
675 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800676
Joe Onorato36115782010-06-17 13:28:48 -0400677 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400678
Joe Onorato36115782010-06-17 13:28:48 -0400679 final Cursor c = contentResolver.query(
680 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400681
Adam Cohend22015c2010-07-26 22:02:18 -0700682 final ItemInfo occupied[][][] =
683 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684
Joe Onorato36115782010-06-17 13:28:48 -0400685 try {
686 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
687 final int intentIndex = c.getColumnIndexOrThrow
688 (LauncherSettings.Favorites.INTENT);
689 final int titleIndex = c.getColumnIndexOrThrow
690 (LauncherSettings.Favorites.TITLE);
691 final int iconTypeIndex = c.getColumnIndexOrThrow(
692 LauncherSettings.Favorites.ICON_TYPE);
693 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
694 final int iconPackageIndex = c.getColumnIndexOrThrow(
695 LauncherSettings.Favorites.ICON_PACKAGE);
696 final int iconResourceIndex = c.getColumnIndexOrThrow(
697 LauncherSettings.Favorites.ICON_RESOURCE);
698 final int containerIndex = c.getColumnIndexOrThrow(
699 LauncherSettings.Favorites.CONTAINER);
700 final int itemTypeIndex = c.getColumnIndexOrThrow(
701 LauncherSettings.Favorites.ITEM_TYPE);
702 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
703 LauncherSettings.Favorites.APPWIDGET_ID);
704 final int screenIndex = c.getColumnIndexOrThrow(
705 LauncherSettings.Favorites.SCREEN);
706 final int cellXIndex = c.getColumnIndexOrThrow
707 (LauncherSettings.Favorites.CELLX);
708 final int cellYIndex = c.getColumnIndexOrThrow
709 (LauncherSettings.Favorites.CELLY);
710 final int spanXIndex = c.getColumnIndexOrThrow
711 (LauncherSettings.Favorites.SPANX);
712 final int spanYIndex = c.getColumnIndexOrThrow(
713 LauncherSettings.Favorites.SPANY);
714 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
715 final int displayModeIndex = c.getColumnIndexOrThrow(
716 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717
Joe Onorato36115782010-06-17 13:28:48 -0400718 ShortcutInfo info;
719 String intentDescription;
720 LauncherAppWidgetInfo appWidgetInfo;
721 int container;
722 long id;
723 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724
Joe Onorato36115782010-06-17 13:28:48 -0400725 while (!mStopped && c.moveToNext()) {
726 try {
727 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728
Joe Onorato36115782010-06-17 13:28:48 -0400729 switch (itemType) {
730 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
731 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
732 intentDescription = c.getString(intentIndex);
733 try {
734 intent = Intent.parseUri(intentDescription, 0);
735 } catch (URISyntaxException e) {
736 continue;
737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738
Joe Onorato36115782010-06-17 13:28:48 -0400739 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
740 info = getShortcutInfo(manager, intent, context, c, iconIndex,
741 titleIndex);
742 } else {
743 info = getShortcutInfo(c, context, iconTypeIndex,
744 iconPackageIndex, iconResourceIndex, iconIndex,
745 titleIndex);
746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747
Joe Onorato36115782010-06-17 13:28:48 -0400748 if (info != null) {
749 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750
Joe Onorato36115782010-06-17 13:28:48 -0400751 info.intent = intent;
752 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400754 info.container = container;
755 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700756 info.cellX = c.getInt(cellXIndex);
757 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758
Daniel Sandler8802e962010-05-26 16:28:16 -0400759 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400760 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400761 break;
762 }
763
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400765 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
766 mItems.add(info);
767 break;
768 default:
769 // Item is in a user folder
770 UserFolderInfo folderInfo =
771 findOrMakeUserFolder(mFolders, container);
772 folderInfo.add(info);
773 break;
774 }
775 } else {
776 // Failed to load the shortcut, probably because the
777 // activity manager couldn't resolve it (maybe the app
778 // was uninstalled), or the db row was somehow screwed up.
779 // Delete it.
780 id = c.getLong(idIndex);
781 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
782 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
783 id, false), null, null);
784 }
785 break;
786
787 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
788 id = c.getLong(idIndex);
789 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
790
Winson Chungaafa03c2010-06-11 17:34:16 -0700791 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400792 folderInfo.id = id;
793 container = c.getInt(containerIndex);
794 folderInfo.container = container;
795 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700796 folderInfo.cellX = c.getInt(cellXIndex);
797 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400798
799 // check & update map of what's occupied
800 if (!checkItemPlacement(occupied, folderInfo)) {
801 break;
802 }
Joe Onorato36115782010-06-17 13:28:48 -0400803 switch (container) {
804 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
805 mItems.add(folderInfo);
806 break;
807 }
808
809 mFolders.put(folderInfo.id, folderInfo);
810 break;
811
812 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
813 id = c.getLong(idIndex);
814 Uri uri = Uri.parse(c.getString(uriIndex));
815
816 // Make sure the live folder exists
817 final ProviderInfo providerInfo =
818 context.getPackageManager().resolveContentProvider(
819 uri.getAuthority(), 0);
820
821 if (providerInfo == null && !isSafeMode) {
822 itemsToRemove.add(id);
823 } else {
824 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400825 intentDescription = c.getString(intentIndex);
826 intent = null;
827 if (intentDescription != null) {
828 try {
829 intent = Intent.parseUri(intentDescription, 0);
830 } catch (URISyntaxException e) {
831 // Ignore, a live folder might not have a base intent
832 }
833 }
834
835 liveFolderInfo.title = c.getString(titleIndex);
836 liveFolderInfo.id = id;
837 liveFolderInfo.uri = uri;
838 container = c.getInt(containerIndex);
839 liveFolderInfo.container = container;
840 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700841 liveFolderInfo.cellX = c.getInt(cellXIndex);
842 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400843 liveFolderInfo.baseIntent = intent;
844 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
845
846 // check & update map of what's occupied
847 if (!checkItemPlacement(occupied, liveFolderInfo)) {
848 break;
849 }
850
851 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
852 iconResourceIndex, liveFolderInfo);
853
854 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400856 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857 break;
858 }
Joe Onorato36115782010-06-17 13:28:48 -0400859 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860 }
Joe Onorato36115782010-06-17 13:28:48 -0400861 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800862
Joe Onorato36115782010-06-17 13:28:48 -0400863 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
864 // Read all Launcher-specific widget details
865 int appWidgetId = c.getInt(appWidgetIdIndex);
866 id = c.getLong(idIndex);
867
868 final AppWidgetProviderInfo provider =
869 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700870
Joe Onorato36115782010-06-17 13:28:48 -0400871 if (!isSafeMode && (provider == null || provider.provider == null ||
872 provider.provider.getPackageName() == null)) {
873 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
874 + id + " appWidgetId=" + appWidgetId);
875 itemsToRemove.add(id);
876 } else {
877 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
878 appWidgetInfo.id = id;
879 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700880 appWidgetInfo.cellX = c.getInt(cellXIndex);
881 appWidgetInfo.cellY = c.getInt(cellYIndex);
882 appWidgetInfo.spanX = c.getInt(spanXIndex);
883 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400884
885 container = c.getInt(containerIndex);
886 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
887 Log.e(TAG, "Widget found where container "
888 + "!= CONTAINER_DESKTOP -- ignoring!");
889 continue;
890 }
891 appWidgetInfo.container = c.getInt(containerIndex);
892
893 // check & update map of what's occupied
894 if (!checkItemPlacement(occupied, appWidgetInfo)) {
895 break;
896 }
897
898 mAppWidgets.add(appWidgetInfo);
899 }
900 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800901 }
Joe Onorato36115782010-06-17 13:28:48 -0400902 } catch (Exception e) {
903 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800904 }
905 }
Joe Onorato36115782010-06-17 13:28:48 -0400906 } finally {
907 c.close();
908 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800909
Joe Onorato36115782010-06-17 13:28:48 -0400910 if (itemsToRemove.size() > 0) {
911 ContentProviderClient client = contentResolver.acquireContentProviderClient(
912 LauncherSettings.Favorites.CONTENT_URI);
913 // Remove dead items
914 for (long id : itemsToRemove) {
915 if (DEBUG_LOADERS) {
916 Log.d(TAG, "Removed id = " + id);
917 }
918 // Don't notify content observers
919 try {
920 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
921 null, null);
922 } catch (RemoteException e) {
923 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400924 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800925 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 }
927
Joe Onorato36115782010-06-17 13:28:48 -0400928 if (DEBUG_LOADERS) {
929 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
930 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700931 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400932 String line = "";
933 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
934 if (s > 0) {
935 line += " | ";
936 }
Adam Cohend22015c2010-07-26 22:02:18 -0700937 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400938 line += ((occupied[s][x][y] != null) ? "#" : ".");
939 }
940 }
941 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 }
Joe Onorato36115782010-06-17 13:28:48 -0400943 }
944 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945
Joe Onorato36115782010-06-17 13:28:48 -0400946 /**
947 * Read everything out of our database.
948 */
949 private void bindWorkspace() {
950 final long t = SystemClock.uptimeMillis();
951
952 // Don't use these two variables in any of the callback runnables.
953 // Otherwise we hold a reference to them.
954 final Callbacks oldCallbacks = mCallbacks.get();
955 if (oldCallbacks == null) {
956 // This launcher has exited and nobody bothered to tell us. Just bail.
957 Log.w(TAG, "LoaderTask running with no launcher");
958 return;
959 }
960
961 int N;
962 // Tell the workspace that we're about to start firing items at it
963 mHandler.post(new Runnable() {
964 public void run() {
965 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
966 if (callbacks != null) {
967 callbacks.startBinding();
968 }
969 }
970 });
971 // Add the items to the workspace.
972 N = mItems.size();
973 for (int i=0; i<N; i+=ITEMS_CHUNK) {
974 final int start = i;
975 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 mHandler.post(new Runnable() {
977 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800978 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400980 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 }
982 }
983 });
Joe Onorato36115782010-06-17 13:28:48 -0400984 }
985 mHandler.post(new Runnable() {
986 public void run() {
987 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
988 if (callbacks != null) {
989 callbacks.bindFolders(mFolders);
990 }
991 }
992 });
993 // Wait until the queue goes empty.
994 mHandler.post(new Runnable() {
995 public void run() {
996 if (DEBUG_LOADERS) {
997 Log.d(TAG, "Going to start binding widgets soon.");
998 }
999 }
1000 });
1001 // Bind the widgets, one at a time.
1002 // WARNING: this is calling into the workspace from the background thread,
1003 // but since getCurrentScreen() just returns the int, we should be okay. This
1004 // is just a hint for the order, and if it's wrong, we'll be okay.
1005 // TODO: instead, we should have that push the current screen into here.
1006 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1007 N = mAppWidgets.size();
1008 // once for the current screen
1009 for (int i=0; i<N; i++) {
1010 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1011 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012 mHandler.post(new Runnable() {
1013 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001014 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001016 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 }
1018 }
1019 });
1020 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 }
Joe Onorato36115782010-06-17 13:28:48 -04001022 // once for the other screens
1023 for (int i=0; i<N; i++) {
1024 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1025 if (widget.screen != currentScreen) {
1026 mHandler.post(new Runnable() {
1027 public void run() {
1028 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1029 if (callbacks != null) {
1030 callbacks.bindAppWidget(widget);
1031 }
1032 }
1033 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001034 }
1035 }
Joe Onorato36115782010-06-17 13:28:48 -04001036 // Tell the workspace that we're done.
1037 mHandler.post(new Runnable() {
1038 public void run() {
1039 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1040 if (callbacks != null) {
1041 callbacks.finishBindingItems();
1042 }
1043 }
1044 });
1045 // If we're profiling, this is the last thing in the queue.
1046 mHandler.post(new Runnable() {
1047 public void run() {
1048 if (DEBUG_LOADERS) {
1049 Log.d(TAG, "bound workspace in "
1050 + (SystemClock.uptimeMillis()-t) + "ms");
1051 }
1052 }
1053 });
1054 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001055
Joe Onorato36115782010-06-17 13:28:48 -04001056 private void loadAndBindAllApps() {
1057 if (DEBUG_LOADERS) {
1058 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1059 }
1060 if (!mAllAppsLoaded) {
1061 loadAllAppsByBatch();
1062 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001063 return;
1064 }
Joe Onorato36115782010-06-17 13:28:48 -04001065 mAllAppsLoaded = true;
1066 } else {
1067 onlyBindAllApps();
1068 }
1069 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001070
Joe Onorato36115782010-06-17 13:28:48 -04001071 private void onlyBindAllApps() {
1072 final Callbacks oldCallbacks = mCallbacks.get();
1073 if (oldCallbacks == null) {
1074 // This launcher has exited and nobody bothered to tell us. Just bail.
1075 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1076 return;
1077 }
1078
1079 // shallow copy
1080 final ArrayList<ApplicationInfo> list
1081 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1082 mHandler.post(new Runnable() {
1083 public void run() {
1084 final long t = SystemClock.uptimeMillis();
1085 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1086 if (callbacks != null) {
1087 callbacks.bindAllApplications(list);
1088 }
1089 if (DEBUG_LOADERS) {
1090 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1091 + (SystemClock.uptimeMillis()-t) + "ms");
1092 }
1093 }
1094 });
1095
1096 }
1097
1098 private void loadAllAppsByBatch() {
1099 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1100
1101 // Don't use these two variables in any of the callback runnables.
1102 // Otherwise we hold a reference to them.
1103 final Callbacks oldCallbacks = mCallbacks.get();
1104 if (oldCallbacks == null) {
1105 // This launcher has exited and nobody bothered to tell us. Just bail.
1106 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1107 return;
1108 }
1109
1110 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1111 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1112
1113 final PackageManager packageManager = mContext.getPackageManager();
1114 List<ResolveInfo> apps = null;
1115
1116 int N = Integer.MAX_VALUE;
1117
1118 int startIndex;
1119 int i=0;
1120 int batchSize = -1;
1121 while (i < N && !mStopped) {
1122 if (i == 0) {
1123 mAllAppsList.clear();
1124 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1125 apps = packageManager.queryIntentActivities(mainIntent, 0);
1126 if (DEBUG_LOADERS) {
1127 Log.d(TAG, "queryIntentActivities took "
1128 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1129 }
1130 if (apps == null) {
1131 return;
1132 }
1133 N = apps.size();
1134 if (DEBUG_LOADERS) {
1135 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1136 }
1137 if (N == 0) {
1138 // There are no apps?!?
1139 return;
1140 }
1141 if (mBatchSize == 0) {
1142 batchSize = N;
1143 } else {
1144 batchSize = mBatchSize;
1145 }
1146
1147 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1148 Collections.sort(apps,
1149 new ResolveInfo.DisplayNameComparator(packageManager));
1150 if (DEBUG_LOADERS) {
1151 Log.d(TAG, "sort took "
1152 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1153 }
1154 }
1155
1156 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1157
1158 startIndex = i;
1159 for (int j=0; i<N && j<batchSize; j++) {
1160 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001161 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001162 i++;
1163 }
1164
1165 final boolean first = i <= batchSize;
1166 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1167 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1168 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1169
Joe Onoratocc67f472010-06-08 10:54:30 -07001170 mHandler.post(new Runnable() {
1171 public void run() {
1172 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001173 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001174 if (first) {
1175 callbacks.bindAllApplications(added);
1176 } else {
1177 callbacks.bindAppsAdded(added);
1178 }
1179 if (DEBUG_LOADERS) {
1180 Log.d(TAG, "bound " + added.size() + " apps in "
1181 + (SystemClock.uptimeMillis() - t) + "ms");
1182 }
1183 } else {
1184 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001185 }
1186 }
1187 });
1188
Daniel Sandlerdca66122010-04-13 16:23:58 -04001189 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001190 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1191 + (SystemClock.uptimeMillis()-t2) + "ms");
1192 }
1193
1194 if (mAllAppsLoadDelay > 0 && i < N) {
1195 try {
1196 if (DEBUG_LOADERS) {
1197 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1198 }
1199 Thread.sleep(mAllAppsLoadDelay);
1200 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
1202 }
1203
Joe Onorato36115782010-06-17 13:28:48 -04001204 if (DEBUG_LOADERS) {
1205 Log.d(TAG, "cached all " + N + " apps in "
1206 + (SystemClock.uptimeMillis()-t) + "ms"
1207 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001208 }
1209 }
1210
1211 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001212 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1213 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1214 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1215 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1216 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1217 }
1218 }
1219
1220 void enqueuePackageUpdated(PackageUpdatedTask task) {
1221 mWorker.post(task);
1222 }
1223
1224 private class PackageUpdatedTask implements Runnable {
1225 int mOp;
1226 String[] mPackages;
1227
1228 public static final int OP_NONE = 0;
1229 public static final int OP_ADD = 1;
1230 public static final int OP_UPDATE = 2;
1231 public static final int OP_REMOVE = 3; // uninstlled
1232 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1233
1234
1235 public PackageUpdatedTask(int op, String[] packages) {
1236 mOp = op;
1237 mPackages = packages;
1238 }
1239
1240 public void run() {
1241 final Context context = mApp;
1242
1243 final String[] packages = mPackages;
1244 final int N = packages.length;
1245 switch (mOp) {
1246 case OP_ADD:
1247 for (int i=0; i<N; i++) {
1248 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1249 mAllAppsList.addPackage(context, packages[i]);
1250 }
1251 break;
1252 case OP_UPDATE:
1253 for (int i=0; i<N; i++) {
1254 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1255 mAllAppsList.updatePackage(context, packages[i]);
1256 }
1257 break;
1258 case OP_REMOVE:
1259 case OP_UNAVAILABLE:
1260 for (int i=0; i<N; i++) {
1261 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1262 mAllAppsList.removePackage(packages[i]);
1263 }
1264 break;
1265 }
1266
1267 ArrayList<ApplicationInfo> added = null;
1268 ArrayList<ApplicationInfo> removed = null;
1269 ArrayList<ApplicationInfo> modified = null;
1270
1271 if (mAllAppsList.added.size() > 0) {
1272 added = mAllAppsList.added;
1273 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1274 }
1275 if (mAllAppsList.removed.size() > 0) {
1276 removed = mAllAppsList.removed;
1277 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1278 for (ApplicationInfo info: removed) {
1279 mIconCache.remove(info.intent.getComponent());
1280 }
1281 }
1282 if (mAllAppsList.modified.size() > 0) {
1283 modified = mAllAppsList.modified;
1284 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1285 }
1286
1287 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1288 if (callbacks == null) {
1289 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1290 return;
1291 }
1292
1293 if (added != null) {
1294 final ArrayList<ApplicationInfo> addedFinal = added;
1295 mHandler.post(new Runnable() {
1296 public void run() {
1297 if (callbacks == mCallbacks.get()) {
1298 callbacks.bindAppsAdded(addedFinal);
1299 }
1300 }
1301 });
1302 }
1303 if (modified != null) {
1304 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1305 mHandler.post(new Runnable() {
1306 public void run() {
1307 if (callbacks == mCallbacks.get()) {
1308 callbacks.bindAppsUpdated(modifiedFinal);
1309 }
1310 }
1311 });
1312 }
1313 if (removed != null) {
1314 final boolean permanent = mOp != OP_UNAVAILABLE;
1315 final ArrayList<ApplicationInfo> removedFinal = removed;
1316 mHandler.post(new Runnable() {
1317 public void run() {
1318 if (callbacks == mCallbacks.get()) {
1319 callbacks.bindAppsRemoved(removedFinal, permanent);
1320 }
1321 }
1322 });
Joe Onoratobe386092009-11-17 17:32:16 -08001323 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001324
1325 mHandler.post(new Runnable() {
1326 @Override
1327 public void run() {
1328 if (callbacks == mCallbacks.get()) {
1329 callbacks.bindPackagesUpdated();
1330 }
1331 }
1332 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 }
1334 }
1335
1336 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001337 * This is called from the code that adds shortcuts from the intent receiver. This
1338 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 */
Joe Onorato56d82912010-03-07 14:32:10 -05001340 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001341 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001342 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001343
Joe Onorato56d82912010-03-07 14:32:10 -05001344 /**
1345 * Make an ShortcutInfo object for a shortcut that is an application.
1346 *
1347 * If c is not null, then it will be used to fill in missing data like the title and icon.
1348 */
1349 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1350 Cursor c, int iconIndex, int titleIndex) {
1351 Bitmap icon = null;
1352 final ShortcutInfo info = new ShortcutInfo();
1353
1354 ComponentName componentName = intent.getComponent();
1355 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001356 return null;
1357 }
1358
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001359 // TODO: See if the PackageManager knows about this case. If it doesn't
1360 // then return null & delete this.
1361
Joe Onorato56d82912010-03-07 14:32:10 -05001362 // the resource -- This may implicitly give us back the fallback icon,
1363 // but don't worry about that. All we're doing with usingFallbackIcon is
1364 // to avoid saving lots of copies of that in the database, and most apps
1365 // have icons anyway.
1366 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1367 if (resolveInfo != null) {
1368 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001369 }
Joe Onorato56d82912010-03-07 14:32:10 -05001370 // the db
1371 if (icon == null) {
1372 if (c != null) {
1373 icon = getIconFromCursor(c, iconIndex);
1374 }
1375 }
1376 // the fallback icon
1377 if (icon == null) {
1378 icon = getFallbackIcon();
1379 info.usingFallbackIcon = true;
1380 }
1381 info.setIcon(icon);
1382
1383 // from the resource
1384 if (resolveInfo != null) {
1385 info.title = resolveInfo.activityInfo.loadLabel(manager);
1386 }
1387 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001389 if (c != null) {
1390 info.title = c.getString(titleIndex);
1391 }
1392 }
1393 // fall back to the class name of the activity
1394 if (info.title == null) {
1395 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001396 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001397 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1398 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001402 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001404 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001405 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1406 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407
Joe Onorato56d82912010-03-07 14:32:10 -05001408 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001409 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001410 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001412 // TODO: If there's an explicit component and we can't install that, delete it.
1413
Joe Onorato56d82912010-03-07 14:32:10 -05001414 info.title = c.getString(titleIndex);
1415
Joe Onorato9c1289c2009-08-17 11:03:03 -04001416 int iconType = c.getInt(iconTypeIndex);
1417 switch (iconType) {
1418 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1419 String packageName = c.getString(iconPackageIndex);
1420 String resourceName = c.getString(iconResourceIndex);
1421 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001422 info.customIcon = false;
1423 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001426 if (resources != null) {
1427 final int id = resources.getIdentifier(resourceName, null, null);
1428 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1429 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001431 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
Joe Onorato56d82912010-03-07 14:32:10 -05001433 // the db
1434 if (icon == null) {
1435 icon = getIconFromCursor(c, iconIndex);
1436 }
1437 // the fallback icon
1438 if (icon == null) {
1439 icon = getFallbackIcon();
1440 info.usingFallbackIcon = true;
1441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 break;
1443 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001444 icon = getIconFromCursor(c, iconIndex);
1445 if (icon == null) {
1446 icon = getFallbackIcon();
1447 info.customIcon = false;
1448 info.usingFallbackIcon = true;
1449 } else {
1450 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 break;
1453 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001454 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001455 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001456 info.customIcon = false;
1457 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001459 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 return info;
1461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462
Joe Onorato56d82912010-03-07 14:32:10 -05001463 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1464 if (false) {
1465 Log.d(TAG, "getIconFromCursor app="
1466 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1467 }
1468 byte[] data = c.getBlob(iconIndex);
1469 try {
1470 return BitmapFactory.decodeByteArray(data, 0, data.length);
1471 } catch (Exception e) {
1472 return null;
1473 }
1474 }
1475
Joe Onorato0589f0f2010-02-08 13:44:00 -08001476 ShortcutInfo addShortcut(Context context, Intent data,
1477 CellLayout.CellInfo cellInfo, boolean notify) {
1478
1479 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1480 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1481 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1482
1483 return info;
1484 }
1485
1486 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1487 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1488 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1489 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1490
1491 Bitmap icon = null;
1492 boolean filtered = false;
1493 boolean customIcon = false;
1494 ShortcutIconResource iconResource = null;
1495
1496 if (bitmap != null && bitmap instanceof Bitmap) {
1497 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1498 filtered = true;
1499 customIcon = true;
1500 } else {
1501 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1502 if (extra != null && extra instanceof ShortcutIconResource) {
1503 try {
1504 iconResource = (ShortcutIconResource) extra;
1505 final PackageManager packageManager = context.getPackageManager();
1506 Resources resources = packageManager.getResourcesForApplication(
1507 iconResource.packageName);
1508 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1509 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1510 } catch (Exception e) {
1511 Log.w(TAG, "Could not load shortcut icon: " + extra);
1512 }
1513 }
1514 }
1515
Joe Onorato0589f0f2010-02-08 13:44:00 -08001516 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001517
1518 if (icon == null) {
1519 icon = getFallbackIcon();
1520 info.usingFallbackIcon = true;
1521 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001522 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001523
Joe Onorato0589f0f2010-02-08 13:44:00 -08001524 info.title = name;
1525 info.intent = intent;
1526 info.customIcon = customIcon;
1527 info.iconResource = iconResource;
1528
1529 return info;
1530 }
1531
Joe Onorato9c1289c2009-08-17 11:03:03 -04001532 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1533 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1534
1535 int iconType = c.getInt(iconTypeIndex);
1536 switch (iconType) {
1537 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1538 String packageName = c.getString(iconPackageIndex);
1539 String resourceName = c.getString(iconResourceIndex);
1540 PackageManager packageManager = context.getPackageManager();
1541 try {
1542 Resources resources = packageManager.getResourcesForApplication(packageName);
1543 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001544 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1545 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001546 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001547 liveFolderInfo.icon = Utilities.createIconBitmap(
1548 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1549 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550 }
1551 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1552 liveFolderInfo.iconResource.packageName = packageName;
1553 liveFolderInfo.iconResource.resourceName = resourceName;
1554 break;
1555 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001556 liveFolderInfo.icon = Utilities.createIconBitmap(
1557 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1558 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001559 }
1560 }
1561
Joe Onorato56d82912010-03-07 14:32:10 -05001562 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1563 // If this icon doesn't have a custom icon, check to see
1564 // what's stored in the DB, and if it doesn't match what
1565 // we're going to show, store what we are going to show back
1566 // into the DB. We do this so when we're loading, if the
1567 // package manager can't find an icon (for example because
1568 // the app is on SD) then we can use that instead.
1569 if (info.onExternalStorage && !info.customIcon && !info.usingFallbackIcon) {
1570 boolean needSave;
1571 byte[] data = c.getBlob(iconIndex);
1572 try {
1573 if (data != null) {
1574 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1575 Bitmap loaded = info.getIcon(mIconCache);
1576 needSave = !saved.sameAs(loaded);
1577 } else {
1578 needSave = true;
1579 }
1580 } catch (Exception e) {
1581 needSave = true;
1582 }
1583 if (needSave) {
1584 Log.d(TAG, "going to save icon bitmap for info=" + info);
1585 // This is slower than is ideal, but this only happens either
1586 // after the froyo OTA or when the app is updated with a new
1587 // icon.
1588 updateItemInDatabase(context, info);
1589 }
1590 }
1591 }
1592
Joe Onorato9c1289c2009-08-17 11:03:03 -04001593 /**
1594 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1595 * or make a new one.
1596 */
1597 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1598 // See if a placeholder was created for us already
1599 FolderInfo folderInfo = folders.get(id);
1600 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1601 // No placeholder -- create a new instance
1602 folderInfo = new UserFolderInfo();
1603 folders.put(id, folderInfo);
1604 }
1605 return (UserFolderInfo) folderInfo;
1606 }
1607
1608 /**
1609 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1610 * new one.
1611 */
1612 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1613 // See if a placeholder was created for us already
1614 FolderInfo folderInfo = folders.get(id);
1615 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1616 // No placeholder -- create a new instance
1617 folderInfo = new LiveFolderInfo();
1618 folders.put(id, folderInfo);
1619 }
1620 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 }
1622
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1624 String label = activityInfo.loadLabel(manager).toString();
1625 if (label == null) {
1626 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1627 if (label == null) {
1628 label = activityInfo.name;
1629 }
1630 }
1631 return label;
1632 }
1633
Joe Onorato9c1289c2009-08-17 11:03:03 -04001634 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001635 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001636 = new Comparator<ApplicationInfo>() {
1637 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1638 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640 };
Joe Onoratobe386092009-11-17 17:32:16 -08001641
1642 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001643 Log.d(TAG, "mCallbacks=" + mCallbacks);
1644 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1645 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1646 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1647 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001648 Log.d(TAG, "mItems size=" + mItems.size());
1649 if (mLoaderTask != null) {
1650 mLoaderTask.dumpState();
1651 } else {
1652 Log.d(TAG, "mLoaderTask=null");
1653 }
Joe Onoratobe386092009-11-17 17:32:16 -08001654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655}