blob: eff5ed63b85b1ce96b4c41255302d085cc5633bc [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chungaafa03c2010-06-11 17:34:16 -070019import java.lang.ref.WeakReference;
20import java.net.URISyntaxException;
21import java.text.Collator;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.List;
27
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040030import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080032import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ContentResolver;
34import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.ActivityInfo;
39import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080040import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.pm.ResolveInfo;
42import android.content.res.Resources;
43import android.database.Cursor;
44import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040047import android.os.Handler;
48import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040052import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import java.lang.ref.WeakReference;
56import java.net.URISyntaxException;
57import java.text.Collator;
58import java.util.ArrayList;
59import java.util.Arrays;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import java.util.Collections;
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070061import java.util.Comparator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import java.util.HashMap;
63import java.util.List;
64
Romain Guyedcce092010-03-04 13:03:17 -080065import com.android.launcher.R;
66
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067/**
68 * Maintains in-memory state of the Launcher. It is expected that there should be only one
69 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070070 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040072public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080073 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070075
Joe Onorato36115782010-06-17 13:28:48 -040076 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onoratod65d08e2010-04-20 15:43:37 -040077 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040078 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040079
Joe Onoratof99f8c12009-10-31 17:27:36 -040080 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 private final Object mLock = new Object();
82 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040083 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070085 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
86 static {
87 sWorkerThread.start();
88 }
89 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
90
Joe Onoratocc67f472010-06-08 10:54:30 -070091 // We start off with everything not loaded. After that, we assume that
92 // our monitoring of the package manager provides all updates and we never
93 // need to do a requery. These are only ever touched from the loader thread.
94 private boolean mWorkspaceLoaded;
95 private boolean mAllAppsLoaded;
96
Joe Onorato9c1289c2009-08-17 11:03:03 -040097 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098
Joe Onorato36115782010-06-17 13:28:48 -040099 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -0800100 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -0400101 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
102 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
103 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800104
105 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
Adam Cohend22015c2010-07-26 22:02:18 -0700107 private static int mCellCountX;
108 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 public interface Callbacks {
111 public int getCurrentWorkspaceScreen();
112 public void startBinding();
113 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500114 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400115 public void finishBindingItems();
116 public void bindAppWidget(LauncherAppWidgetInfo info);
117 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500118 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
119 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400120 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700121 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400122 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato0589f0f2010-02-08 13:44:00 -0800125 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400126 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800127 mAllAppsList = new AllAppsList(iconCache);
128 mIconCache = iconCache;
129
130 mDefaultIcon = Utilities.createIconBitmap(
131 app.getPackageManager().getDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400132
133 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400134
135 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136 }
137
Joe Onorato56d82912010-03-07 14:32:10 -0500138 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800139 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Joe Onorato9c1289c2009-08-17 11:03:03 -0400142 /**
143 * Adds an item to the DB if it was not created previously, or move it to a new
144 * <container, screen, cellX, cellY>
145 */
146 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
147 int screen, int cellX, int cellY) {
148 if (item.container == ItemInfo.NO_ID) {
149 // From all apps
150 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
151 } else {
152 // From somewhere else
153 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 }
155 }
156
157 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700159 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
161 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700162
Joe Onorato9c1289c2009-08-17 11:03:03 -0400163 item.container = container;
164 item.screen = screen;
165 item.cellX = cellX;
166 item.cellY = cellY;
167
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700168 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 final ContentValues values = new ContentValues();
170 final ContentResolver cr = context.getContentResolver();
171
172 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700173 values.put(LauncherSettings.Favorites.CELLX, cellX);
174 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
176
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700177 sWorker.post(new Runnable() {
178 public void run() {
179 cr.update(uri, values, null, null);
180 }
181 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700182 }
183
184 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 * Returns true if the shortcuts already exists in the database.
186 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 static boolean shortcutExists(Context context, String title, Intent intent) {
189 final ContentResolver cr = context.getContentResolver();
190 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
191 new String[] { "title", "intent" }, "title=? and intent=?",
192 new String[] { title, intent.toUri(0) }, null);
193 boolean result = false;
194 try {
195 result = c.moveToFirst();
196 } finally {
197 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700200 }
201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700203 * Returns an ItemInfo array containing all the items in the LauncherModel.
204 * The ItemInfo.id is not set through this function.
205 */
206 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
207 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
208 final ContentResolver cr = context.getContentResolver();
209 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
210 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
211 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
212 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
213
214 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
215 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
216 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
217 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
218 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
219 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
220 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
221
222 try {
223 while (c.moveToNext()) {
224 ItemInfo item = new ItemInfo();
225 item.cellX = c.getInt(cellXIndex);
226 item.cellY = c.getInt(cellYIndex);
227 item.spanX = c.getInt(spanXIndex);
228 item.spanY = c.getInt(spanYIndex);
229 item.container = c.getInt(containerIndex);
230 item.itemType = c.getInt(itemTypeIndex);
231 item.screen = c.getInt(screenIndex);
232
233 items.add(item);
234 }
235 } catch (Exception e) {
236 items.clear();
237 } finally {
238 c.close();
239 }
240
241 return items;
242 }
243
244 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
246 */
247 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
248 final ContentResolver cr = context.getContentResolver();
249 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
250 "_id=? and (itemType=? or itemType=?)",
251 new String[] { String.valueOf(id),
252 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
253 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700254
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 try {
256 if (c.moveToFirst()) {
257 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
258 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
259 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
260 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
261 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
262 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
Joe Onorato9c1289c2009-08-17 11:03:03 -0400264 FolderInfo folderInfo = null;
265 switch (c.getInt(itemTypeIndex)) {
266 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
267 folderInfo = findOrMakeUserFolder(folderList, id);
268 break;
269 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
270 folderInfo = findOrMakeLiveFolder(folderList, id);
271 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700272 }
273
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 folderInfo.title = c.getString(titleIndex);
275 folderInfo.id = id;
276 folderInfo.container = c.getInt(containerIndex);
277 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700278 folderInfo.cellX = c.getInt(cellXIndex);
279 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400280
281 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700282 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 } finally {
284 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700285 }
286
287 return null;
288 }
289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 /**
291 * Add an item to the database in a specified container. Sets the container, screen, cellX and
292 * cellY fields of the item. Also assigns an ID to the item.
293 */
294 static void addItemToDatabase(Context context, ItemInfo item, long container,
295 int screen, int cellX, int cellY, boolean notify) {
296 item.container = container;
297 item.screen = screen;
298 item.cellX = cellX;
299 item.cellY = cellY;
300
301 final ContentValues values = new ContentValues();
302 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400303 item.onAddToDatabase(values);
304
Adam Cohend22015c2010-07-26 22:02:18 -0700305 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700306
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
308 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
309
310 if (result != null) {
311 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700312 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700313 }
314
Joe Onorato9c1289c2009-08-17 11:03:03 -0400315 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700316 * Creates a new unique child id, for a given cell span across all layouts.
317 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700318 static int getCellLayoutChildId(
319 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
320 return ((cellId & 0xFF) << 24)
321 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700322 }
323
Adam Cohend22015c2010-07-26 22:02:18 -0700324 static int getCellCountX() {
325 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700326 }
327
Adam Cohend22015c2010-07-26 22:02:18 -0700328 static int getCellCountY() {
329 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700330 }
331
332 /**
333 * Updates the model orientation helper to take into account the current layout dimensions
334 * when performing local/canonical coordinate transformations.
335 */
336 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700337 mCellCountX = shortAxisCellCount;
338 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700339 }
340
341 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342 * Update an item to the database in a specified container.
343 */
344 static void updateItemInDatabase(Context context, ItemInfo item) {
345 final ContentValues values = new ContentValues();
346 final ContentResolver cr = context.getContentResolver();
347
348 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700349 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700350
Joe Onorato9c1289c2009-08-17 11:03:03 -0400351 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
352 }
353
354 /**
355 * Removes the specified item from the database
356 * @param context
357 * @param item
358 */
359 static void deleteItemFromDatabase(Context context, ItemInfo item) {
360 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700361 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700362 sWorker.post(new Runnable() {
363 public void run() {
364 cr.delete(uriToDelete, null, null);
365 }
366 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 }
368
369 /**
370 * Remove the contents of the specified folder from the database
371 */
372 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
373 final ContentResolver cr = context.getContentResolver();
374
375 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
376 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
377 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
378 }
379
380 /**
381 * Set this as the current Launcher activity object for the loader.
382 */
383 public void initialize(Callbacks callbacks) {
384 synchronized (mLock) {
385 mCallbacks = new WeakReference<Callbacks>(callbacks);
386 }
387 }
388
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700389 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
391 * ACTION_PACKAGE_CHANGED.
392 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400393 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400394 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700395
Joe Onorato36115782010-06-17 13:28:48 -0400396 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400397
Joe Onorato36115782010-06-17 13:28:48 -0400398 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
399 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
400 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
401 final String packageName = intent.getData().getSchemeSpecificPart();
402 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403
Joe Onorato36115782010-06-17 13:28:48 -0400404 int op = PackageUpdatedTask.OP_NONE;
405
406 if (packageName == null || packageName.length() == 0) {
407 // they sent us a bad intent
408 return;
409 }
410
411 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
412 op = PackageUpdatedTask.OP_UPDATE;
413 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
414 if (!replacing) {
415 op = PackageUpdatedTask.OP_REMOVE;
416 }
417 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
418 // later, we will update the package at this time
419 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
420 if (!replacing) {
421 op = PackageUpdatedTask.OP_ADD;
422 } else {
423 op = PackageUpdatedTask.OP_UPDATE;
424 }
425 }
426
427 if (op != PackageUpdatedTask.OP_NONE) {
428 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
429 }
430
431 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400432 // First, schedule to add these apps back in.
433 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
434 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
435 // Then, rebind everything.
Joe Onorato789065d2010-09-30 16:29:59 -0700436 startLoader(mApp, false);
Joe Onorato36115782010-06-17 13:28:48 -0400437
438 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
439 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
440 enqueuePackageUpdated(new PackageUpdatedTask(
441 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onorato790c2d92010-06-11 00:14:11 -0700442 }
Joe Onorato36115782010-06-17 13:28:48 -0400443 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400444
Joe Onorato36115782010-06-17 13:28:48 -0400445 public void startLoader(Context context, boolean isLaunching) {
446 synchronized (mLock) {
447 if (DEBUG_LOADERS) {
448 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
449 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450
Joe Onorato36115782010-06-17 13:28:48 -0400451 // Don't bother to start the thread if we know it's not going to do anything
452 if (mCallbacks != null && mCallbacks.get() != null) {
453 // If there is already one running, tell it to stop.
454 LoaderTask oldTask = mLoaderTask;
455 if (oldTask != null) {
456 if (oldTask.isLaunching()) {
457 // don't downgrade isLaunching if we're already running
458 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500459 }
Joe Onorato36115782010-06-17 13:28:48 -0400460 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500461 }
Joe Onorato36115782010-06-17 13:28:48 -0400462 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700463 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400464 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400465 }
466 }
467
Joe Onorato36115782010-06-17 13:28:48 -0400468 public void stopLoader() {
469 synchronized (mLock) {
470 if (mLoaderTask != null) {
471 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400472 }
473 }
Joe Onorato36115782010-06-17 13:28:48 -0400474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400475
Joe Onorato36115782010-06-17 13:28:48 -0400476 /**
477 * Runnable for the thread that loads the contents of the launcher:
478 * - workspace icons
479 * - widgets
480 * - all apps icons
481 */
482 private class LoaderTask implements Runnable {
483 private Context mContext;
484 private Thread mWaitThread;
485 private boolean mIsLaunching;
486 private boolean mStopped;
487 private boolean mLoadAndBindStepFinished;
488
489 LoaderTask(Context context, boolean isLaunching) {
490 mContext = context;
491 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400492 }
493
Joe Onorato36115782010-06-17 13:28:48 -0400494 boolean isLaunching() {
495 return mIsLaunching;
496 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400497
Joe Onorato36115782010-06-17 13:28:48 -0400498 private void loadAndBindWorkspace() {
499 // Load the workspace
500
501 // For now, just always reload the workspace. It's ~100 ms vs. the
502 // binding which takes many hundreds of ms.
503 // We can reconsider.
504 if (DEBUG_LOADERS) {
505 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400506 }
Joe Onorato36115782010-06-17 13:28:48 -0400507 if (true || !mWorkspaceLoaded) {
508 loadWorkspace();
509 if (mStopped) {
510 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400511 }
Joe Onorato36115782010-06-17 13:28:48 -0400512 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400513 }
514
Joe Onorato36115782010-06-17 13:28:48 -0400515 // Bind the workspace
516 bindWorkspace();
517 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400518
Joe Onorato36115782010-06-17 13:28:48 -0400519 private void waitForIdle() {
520 // Wait until the either we're stopped or the other threads are done.
521 // This way we don't start loading all apps until the workspace has settled
522 // down.
523 synchronized (LoaderTask.this) {
524 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700525
Joe Onorato36115782010-06-17 13:28:48 -0400526 mHandler.postIdle(new Runnable() {
527 public void run() {
528 synchronized (LoaderTask.this) {
529 mLoadAndBindStepFinished = true;
530 if (DEBUG_LOADERS) {
531 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400532 }
Joe Onorato36115782010-06-17 13:28:48 -0400533 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400534 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400535 }
Joe Onorato36115782010-06-17 13:28:48 -0400536 });
537
538 while (!mStopped && !mLoadAndBindStepFinished) {
539 try {
540 this.wait();
541 } catch (InterruptedException ex) {
542 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400543 }
544 }
Joe Onorato36115782010-06-17 13:28:48 -0400545 if (DEBUG_LOADERS) {
546 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700547 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400548 + "ms for previous step to finish binding");
549 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400550 }
Joe Onorato36115782010-06-17 13:28:48 -0400551 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400552
Joe Onorato36115782010-06-17 13:28:48 -0400553 public void run() {
554 // Optimize for end-user experience: if the Launcher is up and // running with the
555 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
556 // workspace first (default).
557 final Callbacks cbk = mCallbacks.get();
558 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400559
Joe Onorato36115782010-06-17 13:28:48 -0400560 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400561 // Elevate priority when Home launches for the first time to avoid
562 // starving at boot time. Staring at a blank home is not cool.
563 synchronized (mLock) {
564 android.os.Process.setThreadPriority(mIsLaunching
565 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
566 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400567 if (loadWorkspaceFirst) {
568 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
569 loadAndBindWorkspace();
570 } else {
571 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700572 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400573 }
574
Joe Onorato36115782010-06-17 13:28:48 -0400575 if (mStopped) {
576 break keep_running;
577 }
578
579 // Whew! Hard work done. Slow us down, and wait until the UI thread has
580 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400581 synchronized (mLock) {
582 if (mIsLaunching) {
583 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
584 }
585 }
Joe Onorato36115782010-06-17 13:28:48 -0400586 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400587
588 // second step
589 if (loadWorkspaceFirst) {
590 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700591 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400592 } else {
593 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
594 loadAndBindWorkspace();
595 }
Joe Onorato36115782010-06-17 13:28:48 -0400596 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400597
Joe Onorato36115782010-06-17 13:28:48 -0400598 // Clear out this reference, otherwise we end up holding it until all of the
599 // callback runnables are done.
600 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601
Joe Onorato36115782010-06-17 13:28:48 -0400602 synchronized (mLock) {
603 // If we are still the last one to be scheduled, remove ourselves.
604 if (mLoaderTask == this) {
605 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400606 }
Joe Onorato36115782010-06-17 13:28:48 -0400607 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700608
Joe Onorato36115782010-06-17 13:28:48 -0400609 // Trigger a gc to try to clean up after the stuff is done, since the
610 // renderscript allocations aren't charged to the java heap.
611 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700612 mHandler.post(new Runnable() {
613 public void run() {
614 System.gc();
615 }
616 });
Joe Onorato36115782010-06-17 13:28:48 -0400617 } else {
618 mHandler.postIdle(new Runnable() {
619 public void run() {
620 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400621 }
Joe Onorato36115782010-06-17 13:28:48 -0400622 });
623 }
624 }
625
626 public void stopLocked() {
627 synchronized (LoaderTask.this) {
628 mStopped = true;
629 this.notify();
630 }
631 }
632
633 /**
634 * Gets the callbacks object. If we've been stopped, or if the launcher object
635 * has somehow been garbage collected, return null instead. Pass in the Callbacks
636 * object that was around when the deferred message was scheduled, and if there's
637 * a new Callbacks object around then also return null. This will save us from
638 * calling onto it with data that will be ignored.
639 */
640 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
641 synchronized (mLock) {
642 if (mStopped) {
643 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400644 }
Joe Onorato36115782010-06-17 13:28:48 -0400645
646 if (mCallbacks == null) {
647 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400648 }
Joe Onorato36115782010-06-17 13:28:48 -0400649
650 final Callbacks callbacks = mCallbacks.get();
651 if (callbacks != oldCallbacks) {
652 return null;
653 }
654 if (callbacks == null) {
655 Log.w(TAG, "no mCallbacks");
656 return null;
657 }
658
659 return callbacks;
660 }
661 }
662
663 // check & update map of what's occupied; used to discard overlapping/invalid items
664 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
665 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400666 return true;
667 }
Joe Onorato36115782010-06-17 13:28:48 -0400668 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
669 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
670 if (occupied[item.screen][x][y] != null) {
671 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700672 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400673 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700674 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400675 + occupied[item.screen][x][y]);
676 return false;
677 }
678 }
679 }
680 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
681 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
682 occupied[item.screen][x][y] = item;
683 }
684 }
685 return true;
686 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687
Joe Onorato36115782010-06-17 13:28:48 -0400688 private void loadWorkspace() {
689 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690
Joe Onorato36115782010-06-17 13:28:48 -0400691 final Context context = mContext;
692 final ContentResolver contentResolver = context.getContentResolver();
693 final PackageManager manager = context.getPackageManager();
694 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
695 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400696
Joe Onorato36115782010-06-17 13:28:48 -0400697 mItems.clear();
698 mAppWidgets.clear();
699 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800700
Joe Onorato36115782010-06-17 13:28:48 -0400701 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702
Joe Onorato36115782010-06-17 13:28:48 -0400703 final Cursor c = contentResolver.query(
704 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400705
Adam Cohend22015c2010-07-26 22:02:18 -0700706 final ItemInfo occupied[][][] =
707 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708
Joe Onorato36115782010-06-17 13:28:48 -0400709 try {
710 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
711 final int intentIndex = c.getColumnIndexOrThrow
712 (LauncherSettings.Favorites.INTENT);
713 final int titleIndex = c.getColumnIndexOrThrow
714 (LauncherSettings.Favorites.TITLE);
715 final int iconTypeIndex = c.getColumnIndexOrThrow(
716 LauncherSettings.Favorites.ICON_TYPE);
717 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
718 final int iconPackageIndex = c.getColumnIndexOrThrow(
719 LauncherSettings.Favorites.ICON_PACKAGE);
720 final int iconResourceIndex = c.getColumnIndexOrThrow(
721 LauncherSettings.Favorites.ICON_RESOURCE);
722 final int containerIndex = c.getColumnIndexOrThrow(
723 LauncherSettings.Favorites.CONTAINER);
724 final int itemTypeIndex = c.getColumnIndexOrThrow(
725 LauncherSettings.Favorites.ITEM_TYPE);
726 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
727 LauncherSettings.Favorites.APPWIDGET_ID);
728 final int screenIndex = c.getColumnIndexOrThrow(
729 LauncherSettings.Favorites.SCREEN);
730 final int cellXIndex = c.getColumnIndexOrThrow
731 (LauncherSettings.Favorites.CELLX);
732 final int cellYIndex = c.getColumnIndexOrThrow
733 (LauncherSettings.Favorites.CELLY);
734 final int spanXIndex = c.getColumnIndexOrThrow
735 (LauncherSettings.Favorites.SPANX);
736 final int spanYIndex = c.getColumnIndexOrThrow(
737 LauncherSettings.Favorites.SPANY);
738 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
739 final int displayModeIndex = c.getColumnIndexOrThrow(
740 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741
Joe Onorato36115782010-06-17 13:28:48 -0400742 ShortcutInfo info;
743 String intentDescription;
744 LauncherAppWidgetInfo appWidgetInfo;
745 int container;
746 long id;
747 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748
Joe Onorato36115782010-06-17 13:28:48 -0400749 while (!mStopped && c.moveToNext()) {
750 try {
751 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752
Joe Onorato36115782010-06-17 13:28:48 -0400753 switch (itemType) {
754 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
755 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
756 intentDescription = c.getString(intentIndex);
757 try {
758 intent = Intent.parseUri(intentDescription, 0);
759 } catch (URISyntaxException e) {
760 continue;
761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762
Joe Onorato36115782010-06-17 13:28:48 -0400763 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
764 info = getShortcutInfo(manager, intent, context, c, iconIndex,
765 titleIndex);
766 } else {
767 info = getShortcutInfo(c, context, iconTypeIndex,
768 iconPackageIndex, iconResourceIndex, iconIndex,
769 titleIndex);
770 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771
Joe Onorato36115782010-06-17 13:28:48 -0400772 if (info != null) {
773 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774
Joe Onorato36115782010-06-17 13:28:48 -0400775 info.intent = intent;
776 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400777 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400778 info.container = container;
779 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700780 info.cellX = c.getInt(cellXIndex);
781 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782
Daniel Sandler8802e962010-05-26 16:28:16 -0400783 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400784 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400785 break;
786 }
787
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400789 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
790 mItems.add(info);
791 break;
792 default:
793 // Item is in a user folder
794 UserFolderInfo folderInfo =
795 findOrMakeUserFolder(mFolders, container);
796 folderInfo.add(info);
797 break;
798 }
799 } else {
800 // Failed to load the shortcut, probably because the
801 // activity manager couldn't resolve it (maybe the app
802 // was uninstalled), or the db row was somehow screwed up.
803 // Delete it.
804 id = c.getLong(idIndex);
805 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
806 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
807 id, false), null, null);
808 }
809 break;
810
811 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
812 id = c.getLong(idIndex);
813 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
814
Winson Chungaafa03c2010-06-11 17:34:16 -0700815 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400816 folderInfo.id = id;
817 container = c.getInt(containerIndex);
818 folderInfo.container = container;
819 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700820 folderInfo.cellX = c.getInt(cellXIndex);
821 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400822
823 // check & update map of what's occupied
824 if (!checkItemPlacement(occupied, folderInfo)) {
825 break;
826 }
Joe Onorato36115782010-06-17 13:28:48 -0400827 switch (container) {
828 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
829 mItems.add(folderInfo);
830 break;
831 }
832
833 mFolders.put(folderInfo.id, folderInfo);
834 break;
835
836 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
837 id = c.getLong(idIndex);
838 Uri uri = Uri.parse(c.getString(uriIndex));
839
840 // Make sure the live folder exists
841 final ProviderInfo providerInfo =
842 context.getPackageManager().resolveContentProvider(
843 uri.getAuthority(), 0);
844
845 if (providerInfo == null && !isSafeMode) {
846 itemsToRemove.add(id);
847 } else {
848 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400849 intentDescription = c.getString(intentIndex);
850 intent = null;
851 if (intentDescription != null) {
852 try {
853 intent = Intent.parseUri(intentDescription, 0);
854 } catch (URISyntaxException e) {
855 // Ignore, a live folder might not have a base intent
856 }
857 }
858
859 liveFolderInfo.title = c.getString(titleIndex);
860 liveFolderInfo.id = id;
861 liveFolderInfo.uri = uri;
862 container = c.getInt(containerIndex);
863 liveFolderInfo.container = container;
864 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700865 liveFolderInfo.cellX = c.getInt(cellXIndex);
866 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400867 liveFolderInfo.baseIntent = intent;
868 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
869
870 // check & update map of what's occupied
871 if (!checkItemPlacement(occupied, liveFolderInfo)) {
872 break;
873 }
874
875 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
876 iconResourceIndex, liveFolderInfo);
877
878 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400880 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881 break;
882 }
Joe Onorato36115782010-06-17 13:28:48 -0400883 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 }
Joe Onorato36115782010-06-17 13:28:48 -0400885 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800886
Joe Onorato36115782010-06-17 13:28:48 -0400887 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
888 // Read all Launcher-specific widget details
889 int appWidgetId = c.getInt(appWidgetIdIndex);
890 id = c.getLong(idIndex);
891
892 final AppWidgetProviderInfo provider =
893 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700894
Joe Onorato36115782010-06-17 13:28:48 -0400895 if (!isSafeMode && (provider == null || provider.provider == null ||
896 provider.provider.getPackageName() == null)) {
897 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
898 + id + " appWidgetId=" + appWidgetId);
899 itemsToRemove.add(id);
900 } else {
901 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
902 appWidgetInfo.id = id;
903 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700904 appWidgetInfo.cellX = c.getInt(cellXIndex);
905 appWidgetInfo.cellY = c.getInt(cellYIndex);
906 appWidgetInfo.spanX = c.getInt(spanXIndex);
907 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400908
909 container = c.getInt(containerIndex);
910 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
911 Log.e(TAG, "Widget found where container "
912 + "!= CONTAINER_DESKTOP -- ignoring!");
913 continue;
914 }
915 appWidgetInfo.container = c.getInt(containerIndex);
916
917 // check & update map of what's occupied
918 if (!checkItemPlacement(occupied, appWidgetInfo)) {
919 break;
920 }
921
922 mAppWidgets.add(appWidgetInfo);
923 }
924 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800925 }
Joe Onorato36115782010-06-17 13:28:48 -0400926 } catch (Exception e) {
927 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800928 }
929 }
Joe Onorato36115782010-06-17 13:28:48 -0400930 } finally {
931 c.close();
932 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800933
Joe Onorato36115782010-06-17 13:28:48 -0400934 if (itemsToRemove.size() > 0) {
935 ContentProviderClient client = contentResolver.acquireContentProviderClient(
936 LauncherSettings.Favorites.CONTENT_URI);
937 // Remove dead items
938 for (long id : itemsToRemove) {
939 if (DEBUG_LOADERS) {
940 Log.d(TAG, "Removed id = " + id);
941 }
942 // Don't notify content observers
943 try {
944 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
945 null, null);
946 } catch (RemoteException e) {
947 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400948 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 }
951
Joe Onorato36115782010-06-17 13:28:48 -0400952 if (DEBUG_LOADERS) {
953 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
954 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700955 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400956 String line = "";
957 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
958 if (s > 0) {
959 line += " | ";
960 }
Adam Cohend22015c2010-07-26 22:02:18 -0700961 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400962 line += ((occupied[s][x][y] != null) ? "#" : ".");
963 }
964 }
965 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 }
Joe Onorato36115782010-06-17 13:28:48 -0400967 }
968 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969
Joe Onorato36115782010-06-17 13:28:48 -0400970 /**
971 * Read everything out of our database.
972 */
973 private void bindWorkspace() {
974 final long t = SystemClock.uptimeMillis();
975
976 // Don't use these two variables in any of the callback runnables.
977 // Otherwise we hold a reference to them.
978 final Callbacks oldCallbacks = mCallbacks.get();
979 if (oldCallbacks == null) {
980 // This launcher has exited and nobody bothered to tell us. Just bail.
981 Log.w(TAG, "LoaderTask running with no launcher");
982 return;
983 }
984
985 int N;
986 // Tell the workspace that we're about to start firing items at it
987 mHandler.post(new Runnable() {
988 public void run() {
989 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
990 if (callbacks != null) {
991 callbacks.startBinding();
992 }
993 }
994 });
995 // Add the items to the workspace.
996 N = mItems.size();
997 for (int i=0; i<N; i+=ITEMS_CHUNK) {
998 final int start = i;
999 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 mHandler.post(new Runnable() {
1001 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001002 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001004 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005 }
1006 }
1007 });
Joe Onorato36115782010-06-17 13:28:48 -04001008 }
1009 mHandler.post(new Runnable() {
1010 public void run() {
1011 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1012 if (callbacks != null) {
1013 callbacks.bindFolders(mFolders);
1014 }
1015 }
1016 });
1017 // Wait until the queue goes empty.
1018 mHandler.post(new Runnable() {
1019 public void run() {
1020 if (DEBUG_LOADERS) {
1021 Log.d(TAG, "Going to start binding widgets soon.");
1022 }
1023 }
1024 });
1025 // Bind the widgets, one at a time.
1026 // WARNING: this is calling into the workspace from the background thread,
1027 // but since getCurrentScreen() just returns the int, we should be okay. This
1028 // is just a hint for the order, and if it's wrong, we'll be okay.
1029 // TODO: instead, we should have that push the current screen into here.
1030 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1031 N = mAppWidgets.size();
1032 // once for the current screen
1033 for (int i=0; i<N; i++) {
1034 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1035 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 mHandler.post(new Runnable() {
1037 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001038 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001040 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 }
1042 }
1043 });
1044 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 }
Joe Onorato36115782010-06-17 13:28:48 -04001046 // once for the other screens
1047 for (int i=0; i<N; i++) {
1048 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1049 if (widget.screen != currentScreen) {
1050 mHandler.post(new Runnable() {
1051 public void run() {
1052 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1053 if (callbacks != null) {
1054 callbacks.bindAppWidget(widget);
1055 }
1056 }
1057 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001058 }
1059 }
Joe Onorato36115782010-06-17 13:28:48 -04001060 // Tell the workspace that we're done.
1061 mHandler.post(new Runnable() {
1062 public void run() {
1063 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1064 if (callbacks != null) {
1065 callbacks.finishBindingItems();
1066 }
1067 }
1068 });
1069 // If we're profiling, this is the last thing in the queue.
1070 mHandler.post(new Runnable() {
1071 public void run() {
1072 if (DEBUG_LOADERS) {
1073 Log.d(TAG, "bound workspace in "
1074 + (SystemClock.uptimeMillis()-t) + "ms");
1075 }
1076 }
1077 });
1078 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001079
Joe Onorato36115782010-06-17 13:28:48 -04001080 private void loadAndBindAllApps() {
1081 if (DEBUG_LOADERS) {
1082 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1083 }
1084 if (!mAllAppsLoaded) {
1085 loadAllAppsByBatch();
1086 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001087 return;
1088 }
Joe Onorato36115782010-06-17 13:28:48 -04001089 mAllAppsLoaded = true;
1090 } else {
1091 onlyBindAllApps();
1092 }
1093 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001094
Joe Onorato36115782010-06-17 13:28:48 -04001095 private void onlyBindAllApps() {
1096 final Callbacks oldCallbacks = mCallbacks.get();
1097 if (oldCallbacks == null) {
1098 // This launcher has exited and nobody bothered to tell us. Just bail.
1099 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1100 return;
1101 }
1102
1103 // shallow copy
1104 final ArrayList<ApplicationInfo> list
1105 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1106 mHandler.post(new Runnable() {
1107 public void run() {
1108 final long t = SystemClock.uptimeMillis();
1109 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1110 if (callbacks != null) {
1111 callbacks.bindAllApplications(list);
1112 }
1113 if (DEBUG_LOADERS) {
1114 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1115 + (SystemClock.uptimeMillis()-t) + "ms");
1116 }
1117 }
1118 });
1119
1120 }
1121
1122 private void loadAllAppsByBatch() {
1123 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1124
1125 // Don't use these two variables in any of the callback runnables.
1126 // Otherwise we hold a reference to them.
1127 final Callbacks oldCallbacks = mCallbacks.get();
1128 if (oldCallbacks == null) {
1129 // This launcher has exited and nobody bothered to tell us. Just bail.
1130 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1131 return;
1132 }
1133
1134 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1135 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1136
1137 final PackageManager packageManager = mContext.getPackageManager();
1138 List<ResolveInfo> apps = null;
1139
1140 int N = Integer.MAX_VALUE;
1141
1142 int startIndex;
1143 int i=0;
1144 int batchSize = -1;
1145 while (i < N && !mStopped) {
1146 if (i == 0) {
1147 mAllAppsList.clear();
1148 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1149 apps = packageManager.queryIntentActivities(mainIntent, 0);
1150 if (DEBUG_LOADERS) {
1151 Log.d(TAG, "queryIntentActivities took "
1152 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1153 }
1154 if (apps == null) {
1155 return;
1156 }
1157 N = apps.size();
1158 if (DEBUG_LOADERS) {
1159 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1160 }
1161 if (N == 0) {
1162 // There are no apps?!?
1163 return;
1164 }
1165 if (mBatchSize == 0) {
1166 batchSize = N;
1167 } else {
1168 batchSize = mBatchSize;
1169 }
1170
1171 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1172 Collections.sort(apps,
1173 new ResolveInfo.DisplayNameComparator(packageManager));
1174 if (DEBUG_LOADERS) {
1175 Log.d(TAG, "sort took "
1176 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1177 }
1178 }
1179
1180 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1181
1182 startIndex = i;
1183 for (int j=0; i<N && j<batchSize; j++) {
1184 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001185 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001186 i++;
1187 }
1188
1189 final boolean first = i <= batchSize;
1190 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1191 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1192 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1193
Joe Onoratocc67f472010-06-08 10:54:30 -07001194 mHandler.post(new Runnable() {
1195 public void run() {
1196 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001197 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001198 if (first) {
1199 callbacks.bindAllApplications(added);
1200 } else {
1201 callbacks.bindAppsAdded(added);
1202 }
1203 if (DEBUG_LOADERS) {
1204 Log.d(TAG, "bound " + added.size() + " apps in "
1205 + (SystemClock.uptimeMillis() - t) + "ms");
1206 }
1207 } else {
1208 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001209 }
1210 }
1211 });
1212
Daniel Sandlerdca66122010-04-13 16:23:58 -04001213 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001214 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1215 + (SystemClock.uptimeMillis()-t2) + "ms");
1216 }
1217
1218 if (mAllAppsLoadDelay > 0 && i < N) {
1219 try {
1220 if (DEBUG_LOADERS) {
1221 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1222 }
1223 Thread.sleep(mAllAppsLoadDelay);
1224 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 }
1226 }
1227
Joe Onorato36115782010-06-17 13:28:48 -04001228 if (DEBUG_LOADERS) {
1229 Log.d(TAG, "cached all " + N + " apps in "
1230 + (SystemClock.uptimeMillis()-t) + "ms"
1231 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001232 }
1233 }
1234
1235 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001236 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1237 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1238 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1239 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1240 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1241 }
1242 }
1243
1244 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001245 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001246 }
1247
1248 private class PackageUpdatedTask implements Runnable {
1249 int mOp;
1250 String[] mPackages;
1251
1252 public static final int OP_NONE = 0;
1253 public static final int OP_ADD = 1;
1254 public static final int OP_UPDATE = 2;
1255 public static final int OP_REMOVE = 3; // uninstlled
1256 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1257
1258
1259 public PackageUpdatedTask(int op, String[] packages) {
1260 mOp = op;
1261 mPackages = packages;
1262 }
1263
1264 public void run() {
1265 final Context context = mApp;
1266
1267 final String[] packages = mPackages;
1268 final int N = packages.length;
1269 switch (mOp) {
1270 case OP_ADD:
1271 for (int i=0; i<N; i++) {
1272 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1273 mAllAppsList.addPackage(context, packages[i]);
1274 }
1275 break;
1276 case OP_UPDATE:
1277 for (int i=0; i<N; i++) {
1278 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1279 mAllAppsList.updatePackage(context, packages[i]);
1280 }
1281 break;
1282 case OP_REMOVE:
1283 case OP_UNAVAILABLE:
1284 for (int i=0; i<N; i++) {
1285 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1286 mAllAppsList.removePackage(packages[i]);
1287 }
1288 break;
1289 }
1290
1291 ArrayList<ApplicationInfo> added = null;
1292 ArrayList<ApplicationInfo> removed = null;
1293 ArrayList<ApplicationInfo> modified = null;
1294
1295 if (mAllAppsList.added.size() > 0) {
1296 added = mAllAppsList.added;
1297 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1298 }
1299 if (mAllAppsList.removed.size() > 0) {
1300 removed = mAllAppsList.removed;
1301 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1302 for (ApplicationInfo info: removed) {
1303 mIconCache.remove(info.intent.getComponent());
1304 }
1305 }
1306 if (mAllAppsList.modified.size() > 0) {
1307 modified = mAllAppsList.modified;
1308 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1309 }
1310
1311 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1312 if (callbacks == null) {
1313 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1314 return;
1315 }
1316
1317 if (added != null) {
1318 final ArrayList<ApplicationInfo> addedFinal = added;
1319 mHandler.post(new Runnable() {
1320 public void run() {
1321 if (callbacks == mCallbacks.get()) {
1322 callbacks.bindAppsAdded(addedFinal);
1323 }
1324 }
1325 });
1326 }
1327 if (modified != null) {
1328 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1329 mHandler.post(new Runnable() {
1330 public void run() {
1331 if (callbacks == mCallbacks.get()) {
1332 callbacks.bindAppsUpdated(modifiedFinal);
1333 }
1334 }
1335 });
1336 }
1337 if (removed != null) {
1338 final boolean permanent = mOp != OP_UNAVAILABLE;
1339 final ArrayList<ApplicationInfo> removedFinal = removed;
1340 mHandler.post(new Runnable() {
1341 public void run() {
1342 if (callbacks == mCallbacks.get()) {
1343 callbacks.bindAppsRemoved(removedFinal, permanent);
1344 }
1345 }
1346 });
Joe Onoratobe386092009-11-17 17:32:16 -08001347 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001348
1349 mHandler.post(new Runnable() {
1350 @Override
1351 public void run() {
1352 if (callbacks == mCallbacks.get()) {
1353 callbacks.bindPackagesUpdated();
1354 }
1355 }
1356 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 }
1358 }
1359
1360 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001361 * This is called from the code that adds shortcuts from the intent receiver. This
1362 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 */
Joe Onorato56d82912010-03-07 14:32:10 -05001364 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001365 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001366 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367
Joe Onorato56d82912010-03-07 14:32:10 -05001368 /**
1369 * Make an ShortcutInfo object for a shortcut that is an application.
1370 *
1371 * If c is not null, then it will be used to fill in missing data like the title and icon.
1372 */
1373 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1374 Cursor c, int iconIndex, int titleIndex) {
1375 Bitmap icon = null;
1376 final ShortcutInfo info = new ShortcutInfo();
1377
1378 ComponentName componentName = intent.getComponent();
1379 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001380 return null;
1381 }
1382
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001383 // TODO: See if the PackageManager knows about this case. If it doesn't
1384 // then return null & delete this.
1385
Joe Onorato56d82912010-03-07 14:32:10 -05001386 // the resource -- This may implicitly give us back the fallback icon,
1387 // but don't worry about that. All we're doing with usingFallbackIcon is
1388 // to avoid saving lots of copies of that in the database, and most apps
1389 // have icons anyway.
1390 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1391 if (resolveInfo != null) {
1392 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001393 }
Joe Onorato56d82912010-03-07 14:32:10 -05001394 // the db
1395 if (icon == null) {
1396 if (c != null) {
1397 icon = getIconFromCursor(c, iconIndex);
1398 }
1399 }
1400 // the fallback icon
1401 if (icon == null) {
1402 icon = getFallbackIcon();
1403 info.usingFallbackIcon = true;
1404 }
1405 info.setIcon(icon);
1406
1407 // from the resource
1408 if (resolveInfo != null) {
1409 info.title = resolveInfo.activityInfo.loadLabel(manager);
1410 }
1411 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001413 if (c != null) {
1414 info.title = c.getString(titleIndex);
1415 }
1416 }
1417 // fall back to the class name of the activity
1418 if (info.title == null) {
1419 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001420 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001421 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1422 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001424
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001426 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001428 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001429 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1430 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431
Joe Onorato56d82912010-03-07 14:32:10 -05001432 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001433 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001436 // TODO: If there's an explicit component and we can't install that, delete it.
1437
Joe Onorato56d82912010-03-07 14:32:10 -05001438 info.title = c.getString(titleIndex);
1439
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440 int iconType = c.getInt(iconTypeIndex);
1441 switch (iconType) {
1442 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1443 String packageName = c.getString(iconPackageIndex);
1444 String resourceName = c.getString(iconResourceIndex);
1445 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001446 info.customIcon = false;
1447 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001449 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001450 if (resources != null) {
1451 final int id = resources.getIdentifier(resourceName, null, null);
1452 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1453 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001454 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001455 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
Joe Onorato56d82912010-03-07 14:32:10 -05001457 // the db
1458 if (icon == null) {
1459 icon = getIconFromCursor(c, iconIndex);
1460 }
1461 // the fallback icon
1462 if (icon == null) {
1463 icon = getFallbackIcon();
1464 info.usingFallbackIcon = true;
1465 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466 break;
1467 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001468 icon = getIconFromCursor(c, iconIndex);
1469 if (icon == null) {
1470 icon = getFallbackIcon();
1471 info.customIcon = false;
1472 info.usingFallbackIcon = true;
1473 } else {
1474 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001475 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001476 break;
1477 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001478 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001479 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480 info.customIcon = false;
1481 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001483 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484 return info;
1485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486
Joe Onorato56d82912010-03-07 14:32:10 -05001487 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1488 if (false) {
1489 Log.d(TAG, "getIconFromCursor app="
1490 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1491 }
1492 byte[] data = c.getBlob(iconIndex);
1493 try {
1494 return BitmapFactory.decodeByteArray(data, 0, data.length);
1495 } catch (Exception e) {
1496 return null;
1497 }
1498 }
1499
Joe Onorato0589f0f2010-02-08 13:44:00 -08001500 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001501 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001502
1503 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1504 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001505 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001506
1507 return info;
1508 }
1509
1510 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1511 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1512 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1513 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1514
1515 Bitmap icon = null;
1516 boolean filtered = false;
1517 boolean customIcon = false;
1518 ShortcutIconResource iconResource = null;
1519
1520 if (bitmap != null && bitmap instanceof Bitmap) {
1521 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1522 filtered = true;
1523 customIcon = true;
1524 } else {
1525 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1526 if (extra != null && extra instanceof ShortcutIconResource) {
1527 try {
1528 iconResource = (ShortcutIconResource) extra;
1529 final PackageManager packageManager = context.getPackageManager();
1530 Resources resources = packageManager.getResourcesForApplication(
1531 iconResource.packageName);
1532 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1533 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1534 } catch (Exception e) {
1535 Log.w(TAG, "Could not load shortcut icon: " + extra);
1536 }
1537 }
1538 }
1539
Joe Onorato0589f0f2010-02-08 13:44:00 -08001540 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001541
1542 if (icon == null) {
1543 icon = getFallbackIcon();
1544 info.usingFallbackIcon = true;
1545 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001546 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001547
Joe Onorato0589f0f2010-02-08 13:44:00 -08001548 info.title = name;
1549 info.intent = intent;
1550 info.customIcon = customIcon;
1551 info.iconResource = iconResource;
1552
1553 return info;
1554 }
1555
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1557 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1558
1559 int iconType = c.getInt(iconTypeIndex);
1560 switch (iconType) {
1561 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1562 String packageName = c.getString(iconPackageIndex);
1563 String resourceName = c.getString(iconResourceIndex);
1564 PackageManager packageManager = context.getPackageManager();
1565 try {
1566 Resources resources = packageManager.getResourcesForApplication(packageName);
1567 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001568 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1569 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001571 liveFolderInfo.icon = Utilities.createIconBitmap(
1572 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1573 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 }
1575 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1576 liveFolderInfo.iconResource.packageName = packageName;
1577 liveFolderInfo.iconResource.resourceName = resourceName;
1578 break;
1579 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001580 liveFolderInfo.icon = Utilities.createIconBitmap(
1581 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1582 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 }
1584 }
1585
Joe Onorato56d82912010-03-07 14:32:10 -05001586 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1587 // If this icon doesn't have a custom icon, check to see
1588 // what's stored in the DB, and if it doesn't match what
1589 // we're going to show, store what we are going to show back
1590 // into the DB. We do this so when we're loading, if the
1591 // package manager can't find an icon (for example because
1592 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001593 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001594 boolean needSave;
1595 byte[] data = c.getBlob(iconIndex);
1596 try {
1597 if (data != null) {
1598 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1599 Bitmap loaded = info.getIcon(mIconCache);
1600 needSave = !saved.sameAs(loaded);
1601 } else {
1602 needSave = true;
1603 }
1604 } catch (Exception e) {
1605 needSave = true;
1606 }
1607 if (needSave) {
1608 Log.d(TAG, "going to save icon bitmap for info=" + info);
1609 // This is slower than is ideal, but this only happens either
1610 // after the froyo OTA or when the app is updated with a new
1611 // icon.
1612 updateItemInDatabase(context, info);
1613 }
1614 }
1615 }
1616
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 /**
1618 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1619 * or make a new one.
1620 */
1621 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1622 // See if a placeholder was created for us already
1623 FolderInfo folderInfo = folders.get(id);
1624 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1625 // No placeholder -- create a new instance
1626 folderInfo = new UserFolderInfo();
1627 folders.put(id, folderInfo);
1628 }
1629 return (UserFolderInfo) folderInfo;
1630 }
1631
1632 /**
1633 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1634 * new one.
1635 */
1636 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1637 // See if a placeholder was created for us already
1638 FolderInfo folderInfo = folders.get(id);
1639 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1640 // No placeholder -- create a new instance
1641 folderInfo = new LiveFolderInfo();
1642 folders.put(id, folderInfo);
1643 }
1644 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
1646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1648 String label = activityInfo.loadLabel(manager).toString();
1649 if (label == null) {
1650 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1651 if (label == null) {
1652 label = activityInfo.name;
1653 }
1654 }
1655 return label;
1656 }
1657
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001659 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 = new Comparator<ApplicationInfo>() {
1661 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1662 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001664 };
Joe Onoratobe386092009-11-17 17:32:16 -08001665
1666 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001667 Log.d(TAG, "mCallbacks=" + mCallbacks);
1668 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1669 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1670 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1671 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001672 Log.d(TAG, "mItems size=" + mItems.size());
1673 if (mLoaderTask != null) {
1674 mLoaderTask.dumpState();
1675 } else {
1676 Log.d(TAG, "mLoaderTask=null");
1677 }
Joe Onoratobe386092009-11-17 17:32:16 -08001678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679}