blob: 005273749ae814be9f3deecc3fa2638088259340 [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 Chung68846fd2010-10-29 11:00:27 -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;
Winson Chungaafa03c2010-06-11 17:34:16 -070027
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
Winson Chung68846fd2010-10-29 11:00:27 -070055import com.android.launcher.R;
56import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080057
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * Maintains in-memory state of the Launcher. It is expected that there should be only one
60 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070061 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040063public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080064 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070066
Joe Onorato36115782010-06-17 13:28:48 -040067 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onoratod65d08e2010-04-20 15:43:37 -040068 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040069 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040070
Joe Onoratof99f8c12009-10-31 17:27:36 -040071 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 private final Object mLock = new Object();
73 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040074 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070076 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
77 static {
78 sWorkerThread.start();
79 }
80 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
81
Joe Onoratocc67f472010-06-08 10:54:30 -070082 // We start off with everything not loaded. After that, we assume that
83 // our monitoring of the package manager provides all updates and we never
84 // need to do a requery. These are only ever touched from the loader thread.
85 private boolean mWorkspaceLoaded;
86 private boolean mAllAppsLoaded;
87
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Joe Onorato36115782010-06-17 13:28:48 -040090 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -080091 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -040092 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
93 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
94 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -080095
96 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Adam Cohend22015c2010-07-26 22:02:18 -070098 private static int mCellCountX;
99 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700100
Joe Onorato9c1289c2009-08-17 11:03:03 -0400101 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700102 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 public int getCurrentWorkspaceScreen();
104 public void startBinding();
105 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500106 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 public void finishBindingItems();
108 public void bindAppWidget(LauncherAppWidgetInfo info);
109 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500110 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
111 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400112 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700113 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400114 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato0589f0f2010-02-08 13:44:00 -0800117 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400118 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800119 mAllAppsList = new AllAppsList(iconCache);
120 mIconCache = iconCache;
121
122 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700123 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400124
125 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400126
127 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800128 }
129
Joe Onorato56d82912010-03-07 14:32:10 -0500130 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800131 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato9c1289c2009-08-17 11:03:03 -0400134 /**
135 * Adds an item to the DB if it was not created previously, or move it to a new
136 * <container, screen, cellX, cellY>
137 */
138 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
139 int screen, int cellX, int cellY) {
140 if (item.container == ItemInfo.NO_ID) {
141 // From all apps
142 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
143 } else {
144 // From somewhere else
145 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 }
147 }
148
149 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700151 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
153 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 item.container = container;
156 item.screen = screen;
157 item.cellX = cellX;
158 item.cellY = cellY;
159
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700160 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 final ContentValues values = new ContentValues();
162 final ContentResolver cr = context.getContentResolver();
163
164 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700165 values.put(LauncherSettings.Favorites.CELLX, cellX);
166 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400167 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
168
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700169 sWorker.post(new Runnable() {
170 public void run() {
171 cr.update(uri, values, null, null);
172 }
173 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700174 }
175
176 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 * Returns true if the shortcuts already exists in the database.
178 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 static boolean shortcutExists(Context context, String title, Intent intent) {
181 final ContentResolver cr = context.getContentResolver();
182 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
183 new String[] { "title", "intent" }, "title=? and intent=?",
184 new String[] { title, intent.toUri(0) }, null);
185 boolean result = false;
186 try {
187 result = c.moveToFirst();
188 } finally {
189 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700192 }
193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700195 * Returns an ItemInfo array containing all the items in the LauncherModel.
196 * The ItemInfo.id is not set through this function.
197 */
198 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
199 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
200 final ContentResolver cr = context.getContentResolver();
201 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
202 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
203 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
204 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
205
206 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
207 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
208 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
209 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
210 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
211 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
212 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
213
214 try {
215 while (c.moveToNext()) {
216 ItemInfo item = new ItemInfo();
217 item.cellX = c.getInt(cellXIndex);
218 item.cellY = c.getInt(cellYIndex);
219 item.spanX = c.getInt(spanXIndex);
220 item.spanY = c.getInt(spanYIndex);
221 item.container = c.getInt(containerIndex);
222 item.itemType = c.getInt(itemTypeIndex);
223 item.screen = c.getInt(screenIndex);
224
225 items.add(item);
226 }
227 } catch (Exception e) {
228 items.clear();
229 } finally {
230 c.close();
231 }
232
233 return items;
234 }
235
236 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
238 */
239 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
240 final ContentResolver cr = context.getContentResolver();
241 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
242 "_id=? and (itemType=? or itemType=?)",
243 new String[] { String.valueOf(id),
244 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
245 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700246
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247 try {
248 if (c.moveToFirst()) {
249 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
250 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
251 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
252 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
253 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
254 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 FolderInfo folderInfo = null;
257 switch (c.getInt(itemTypeIndex)) {
258 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
259 folderInfo = findOrMakeUserFolder(folderList, id);
260 break;
261 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
262 folderInfo = findOrMakeLiveFolder(folderList, id);
263 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700264 }
265
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266 folderInfo.title = c.getString(titleIndex);
267 folderInfo.id = id;
268 folderInfo.container = c.getInt(containerIndex);
269 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700270 folderInfo.cellX = c.getInt(cellXIndex);
271 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272
273 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700274 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 } finally {
276 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700277 }
278
279 return null;
280 }
281
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 /**
283 * Add an item to the database in a specified container. Sets the container, screen, cellX and
284 * cellY fields of the item. Also assigns an ID to the item.
285 */
286 static void addItemToDatabase(Context context, ItemInfo item, long container,
287 int screen, int cellX, int cellY, boolean notify) {
288 item.container = container;
289 item.screen = screen;
290 item.cellX = cellX;
291 item.cellY = cellY;
292
293 final ContentValues values = new ContentValues();
294 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 item.onAddToDatabase(values);
296
Adam Cohend22015c2010-07-26 22:02:18 -0700297 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
300 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
301
302 if (result != null) {
303 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700304 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700305 }
306
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700308 * Creates a new unique child id, for a given cell span across all layouts.
309 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700310 static int getCellLayoutChildId(
311 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
312 return ((cellId & 0xFF) << 24)
313 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700314 }
315
Adam Cohend22015c2010-07-26 22:02:18 -0700316 static int getCellCountX() {
317 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700318 }
319
Adam Cohend22015c2010-07-26 22:02:18 -0700320 static int getCellCountY() {
321 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700322 }
323
324 /**
325 * Updates the model orientation helper to take into account the current layout dimensions
326 * when performing local/canonical coordinate transformations.
327 */
328 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700329 mCellCountX = shortAxisCellCount;
330 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700331 }
332
333 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400334 * Update an item to the database in a specified container.
335 */
336 static void updateItemInDatabase(Context context, ItemInfo item) {
337 final ContentValues values = new ContentValues();
338 final ContentResolver cr = context.getContentResolver();
339
340 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700341 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700342
Joe Onorato9c1289c2009-08-17 11:03:03 -0400343 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
344 }
345
346 /**
347 * Removes the specified item from the database
348 * @param context
349 * @param item
350 */
351 static void deleteItemFromDatabase(Context context, ItemInfo item) {
352 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700353 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700354 sWorker.post(new Runnable() {
355 public void run() {
356 cr.delete(uriToDelete, null, null);
357 }
358 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359 }
360
361 /**
362 * Remove the contents of the specified folder from the database
363 */
364 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
365 final ContentResolver cr = context.getContentResolver();
366
367 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
368 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
369 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
370 }
371
372 /**
373 * Set this as the current Launcher activity object for the loader.
374 */
375 public void initialize(Callbacks callbacks) {
376 synchronized (mLock) {
377 mCallbacks = new WeakReference<Callbacks>(callbacks);
378 }
379 }
380
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700381 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400382 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
383 * ACTION_PACKAGE_CHANGED.
384 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400385 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400386 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700387
Joe Onorato36115782010-06-17 13:28:48 -0400388 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400389
Joe Onorato36115782010-06-17 13:28:48 -0400390 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
391 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
392 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
393 final String packageName = intent.getData().getSchemeSpecificPart();
394 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395
Joe Onorato36115782010-06-17 13:28:48 -0400396 int op = PackageUpdatedTask.OP_NONE;
397
398 if (packageName == null || packageName.length() == 0) {
399 // they sent us a bad intent
400 return;
401 }
402
403 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
404 op = PackageUpdatedTask.OP_UPDATE;
405 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
406 if (!replacing) {
407 op = PackageUpdatedTask.OP_REMOVE;
408 }
409 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
410 // later, we will update the package at this time
411 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
412 if (!replacing) {
413 op = PackageUpdatedTask.OP_ADD;
414 } else {
415 op = PackageUpdatedTask.OP_UPDATE;
416 }
417 }
418
419 if (op != PackageUpdatedTask.OP_NONE) {
420 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
421 }
422
423 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400424 // First, schedule to add these apps back in.
425 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
426 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
427 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700428 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400429 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
430 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
431 enqueuePackageUpdated(new PackageUpdatedTask(
432 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700433 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
434 // If we have changed locale we need to clear out the labels in all apps.
435 // Do this here because if the launcher activity is running it will be restarted.
436 // If it's not running startLoaderFromBackground will merely tell it that it needs
437 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
438 // next time.
439 mAllAppsLoaded = false;
440 startLoaderFromBackground();
441 }
442 }
443
444 /**
445 * When the launcher is in the background, it's possible for it to miss paired
446 * configuration changes. So whenever we trigger the loader from the background
447 * tell the launcher that it needs to re-run the loader when it comes back instead
448 * of doing it now.
449 */
450 public void startLoaderFromBackground() {
451 boolean runLoader = false;
452 if (mCallbacks != null) {
453 Callbacks callbacks = mCallbacks.get();
454 if (callbacks != null) {
455 // Only actually run the loader if they're not paused.
456 if (!callbacks.setLoadOnResume()) {
457 runLoader = true;
458 }
459 }
460 }
461 if (runLoader) {
462 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700463 }
Joe Onorato36115782010-06-17 13:28:48 -0400464 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400465
Joe Onorato36115782010-06-17 13:28:48 -0400466 public void startLoader(Context context, boolean isLaunching) {
467 synchronized (mLock) {
468 if (DEBUG_LOADERS) {
469 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
470 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400471
Joe Onorato36115782010-06-17 13:28:48 -0400472 // Don't bother to start the thread if we know it's not going to do anything
473 if (mCallbacks != null && mCallbacks.get() != null) {
474 // If there is already one running, tell it to stop.
475 LoaderTask oldTask = mLoaderTask;
476 if (oldTask != null) {
477 if (oldTask.isLaunching()) {
478 // don't downgrade isLaunching if we're already running
479 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500480 }
Joe Onorato36115782010-06-17 13:28:48 -0400481 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500482 }
Joe Onorato36115782010-06-17 13:28:48 -0400483 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700484 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400485 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400486 }
487 }
488
Joe Onorato36115782010-06-17 13:28:48 -0400489 public void stopLoader() {
490 synchronized (mLock) {
491 if (mLoaderTask != null) {
492 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400493 }
494 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800495 mItems.clear();
496 mAppWidgets.clear();
497 mFolders.clear();
Joe Onorato36115782010-06-17 13:28:48 -0400498 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400499
Joe Onorato36115782010-06-17 13:28:48 -0400500 /**
501 * Runnable for the thread that loads the contents of the launcher:
502 * - workspace icons
503 * - widgets
504 * - all apps icons
505 */
506 private class LoaderTask implements Runnable {
507 private Context mContext;
508 private Thread mWaitThread;
509 private boolean mIsLaunching;
510 private boolean mStopped;
511 private boolean mLoadAndBindStepFinished;
512
513 LoaderTask(Context context, boolean isLaunching) {
514 mContext = context;
515 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 }
517
Joe Onorato36115782010-06-17 13:28:48 -0400518 boolean isLaunching() {
519 return mIsLaunching;
520 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400521
Joe Onorato36115782010-06-17 13:28:48 -0400522 private void loadAndBindWorkspace() {
523 // Load the workspace
524
525 // For now, just always reload the workspace. It's ~100 ms vs. the
526 // binding which takes many hundreds of ms.
527 // We can reconsider.
528 if (DEBUG_LOADERS) {
529 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 }
Joe Onorato36115782010-06-17 13:28:48 -0400531 if (true || !mWorkspaceLoaded) {
532 loadWorkspace();
533 if (mStopped) {
534 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 }
Joe Onorato36115782010-06-17 13:28:48 -0400536 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 }
538
Joe Onorato36115782010-06-17 13:28:48 -0400539 // Bind the workspace
540 bindWorkspace();
541 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400542
Joe Onorato36115782010-06-17 13:28:48 -0400543 private void waitForIdle() {
544 // Wait until the either we're stopped or the other threads are done.
545 // This way we don't start loading all apps until the workspace has settled
546 // down.
547 synchronized (LoaderTask.this) {
548 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700549
Joe Onorato36115782010-06-17 13:28:48 -0400550 mHandler.postIdle(new Runnable() {
551 public void run() {
552 synchronized (LoaderTask.this) {
553 mLoadAndBindStepFinished = true;
554 if (DEBUG_LOADERS) {
555 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400556 }
Joe Onorato36115782010-06-17 13:28:48 -0400557 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400558 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400559 }
Joe Onorato36115782010-06-17 13:28:48 -0400560 });
561
562 while (!mStopped && !mLoadAndBindStepFinished) {
563 try {
564 this.wait();
565 } catch (InterruptedException ex) {
566 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400567 }
568 }
Joe Onorato36115782010-06-17 13:28:48 -0400569 if (DEBUG_LOADERS) {
570 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700571 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400572 + "ms for previous step to finish binding");
573 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400574 }
Joe Onorato36115782010-06-17 13:28:48 -0400575 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400576
Joe Onorato36115782010-06-17 13:28:48 -0400577 public void run() {
578 // Optimize for end-user experience: if the Launcher is up and // running with the
579 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
580 // workspace first (default).
581 final Callbacks cbk = mCallbacks.get();
582 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400583
Joe Onorato36115782010-06-17 13:28:48 -0400584 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400585 // Elevate priority when Home launches for the first time to avoid
586 // starving at boot time. Staring at a blank home is not cool.
587 synchronized (mLock) {
588 android.os.Process.setThreadPriority(mIsLaunching
589 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
590 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400591 if (loadWorkspaceFirst) {
592 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
593 loadAndBindWorkspace();
594 } else {
595 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700596 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400597 }
598
Joe Onorato36115782010-06-17 13:28:48 -0400599 if (mStopped) {
600 break keep_running;
601 }
602
603 // Whew! Hard work done. Slow us down, and wait until the UI thread has
604 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400605 synchronized (mLock) {
606 if (mIsLaunching) {
607 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
608 }
609 }
Joe Onorato36115782010-06-17 13:28:48 -0400610 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400611
612 // second step
613 if (loadWorkspaceFirst) {
614 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700615 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400616 } else {
617 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
618 loadAndBindWorkspace();
619 }
Joe Onorato36115782010-06-17 13:28:48 -0400620 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621
Joe Onorato36115782010-06-17 13:28:48 -0400622 // Clear out this reference, otherwise we end up holding it until all of the
623 // callback runnables are done.
624 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625
Joe Onorato36115782010-06-17 13:28:48 -0400626 synchronized (mLock) {
627 // If we are still the last one to be scheduled, remove ourselves.
628 if (mLoaderTask == this) {
629 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 }
Joe Onorato36115782010-06-17 13:28:48 -0400631 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700632
Joe Onorato36115782010-06-17 13:28:48 -0400633 // Trigger a gc to try to clean up after the stuff is done, since the
634 // renderscript allocations aren't charged to the java heap.
635 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700636 mHandler.post(new Runnable() {
637 public void run() {
638 System.gc();
639 }
640 });
Joe Onorato36115782010-06-17 13:28:48 -0400641 } else {
642 mHandler.postIdle(new Runnable() {
643 public void run() {
644 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400645 }
Joe Onorato36115782010-06-17 13:28:48 -0400646 });
647 }
648 }
649
650 public void stopLocked() {
651 synchronized (LoaderTask.this) {
652 mStopped = true;
653 this.notify();
654 }
655 }
656
657 /**
658 * Gets the callbacks object. If we've been stopped, or if the launcher object
659 * has somehow been garbage collected, return null instead. Pass in the Callbacks
660 * object that was around when the deferred message was scheduled, and if there's
661 * a new Callbacks object around then also return null. This will save us from
662 * calling onto it with data that will be ignored.
663 */
664 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
665 synchronized (mLock) {
666 if (mStopped) {
667 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400668 }
Joe Onorato36115782010-06-17 13:28:48 -0400669
670 if (mCallbacks == null) {
671 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400672 }
Joe Onorato36115782010-06-17 13:28:48 -0400673
674 final Callbacks callbacks = mCallbacks.get();
675 if (callbacks != oldCallbacks) {
676 return null;
677 }
678 if (callbacks == null) {
679 Log.w(TAG, "no mCallbacks");
680 return null;
681 }
682
683 return callbacks;
684 }
685 }
686
687 // check & update map of what's occupied; used to discard overlapping/invalid items
688 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
689 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400690 return true;
691 }
Joe Onorato36115782010-06-17 13:28:48 -0400692 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
693 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
694 if (occupied[item.screen][x][y] != null) {
695 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700696 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400697 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700698 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400699 + occupied[item.screen][x][y]);
700 return false;
701 }
702 }
703 }
704 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
705 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
706 occupied[item.screen][x][y] = item;
707 }
708 }
709 return true;
710 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711
Joe Onorato36115782010-06-17 13:28:48 -0400712 private void loadWorkspace() {
713 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714
Joe Onorato36115782010-06-17 13:28:48 -0400715 final Context context = mContext;
716 final ContentResolver contentResolver = context.getContentResolver();
717 final PackageManager manager = context.getPackageManager();
718 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
719 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400720
Joe Onorato36115782010-06-17 13:28:48 -0400721 mItems.clear();
722 mAppWidgets.clear();
723 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800724
Joe Onorato36115782010-06-17 13:28:48 -0400725 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400726
Joe Onorato36115782010-06-17 13:28:48 -0400727 final Cursor c = contentResolver.query(
728 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400729
Adam Cohend22015c2010-07-26 22:02:18 -0700730 final ItemInfo occupied[][][] =
731 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732
Joe Onorato36115782010-06-17 13:28:48 -0400733 try {
734 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
735 final int intentIndex = c.getColumnIndexOrThrow
736 (LauncherSettings.Favorites.INTENT);
737 final int titleIndex = c.getColumnIndexOrThrow
738 (LauncherSettings.Favorites.TITLE);
739 final int iconTypeIndex = c.getColumnIndexOrThrow(
740 LauncherSettings.Favorites.ICON_TYPE);
741 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
742 final int iconPackageIndex = c.getColumnIndexOrThrow(
743 LauncherSettings.Favorites.ICON_PACKAGE);
744 final int iconResourceIndex = c.getColumnIndexOrThrow(
745 LauncherSettings.Favorites.ICON_RESOURCE);
746 final int containerIndex = c.getColumnIndexOrThrow(
747 LauncherSettings.Favorites.CONTAINER);
748 final int itemTypeIndex = c.getColumnIndexOrThrow(
749 LauncherSettings.Favorites.ITEM_TYPE);
750 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
751 LauncherSettings.Favorites.APPWIDGET_ID);
752 final int screenIndex = c.getColumnIndexOrThrow(
753 LauncherSettings.Favorites.SCREEN);
754 final int cellXIndex = c.getColumnIndexOrThrow
755 (LauncherSettings.Favorites.CELLX);
756 final int cellYIndex = c.getColumnIndexOrThrow
757 (LauncherSettings.Favorites.CELLY);
758 final int spanXIndex = c.getColumnIndexOrThrow
759 (LauncherSettings.Favorites.SPANX);
760 final int spanYIndex = c.getColumnIndexOrThrow(
761 LauncherSettings.Favorites.SPANY);
762 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
763 final int displayModeIndex = c.getColumnIndexOrThrow(
764 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400765
Joe Onorato36115782010-06-17 13:28:48 -0400766 ShortcutInfo info;
767 String intentDescription;
768 LauncherAppWidgetInfo appWidgetInfo;
769 int container;
770 long id;
771 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772
Joe Onorato36115782010-06-17 13:28:48 -0400773 while (!mStopped && c.moveToNext()) {
774 try {
775 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776
Joe Onorato36115782010-06-17 13:28:48 -0400777 switch (itemType) {
778 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
779 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
780 intentDescription = c.getString(intentIndex);
781 try {
782 intent = Intent.parseUri(intentDescription, 0);
783 } catch (URISyntaxException e) {
784 continue;
785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786
Joe Onorato36115782010-06-17 13:28:48 -0400787 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
788 info = getShortcutInfo(manager, intent, context, c, iconIndex,
789 titleIndex);
790 } else {
791 info = getShortcutInfo(c, context, iconTypeIndex,
792 iconPackageIndex, iconResourceIndex, iconIndex,
793 titleIndex);
794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795
Joe Onorato36115782010-06-17 13:28:48 -0400796 if (info != null) {
797 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798
Joe Onorato36115782010-06-17 13:28:48 -0400799 info.intent = intent;
800 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400802 info.container = container;
803 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700804 info.cellX = c.getInt(cellXIndex);
805 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400806
Daniel Sandler8802e962010-05-26 16:28:16 -0400807 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400808 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400809 break;
810 }
811
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400813 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
814 mItems.add(info);
815 break;
816 default:
817 // Item is in a user folder
818 UserFolderInfo folderInfo =
819 findOrMakeUserFolder(mFolders, container);
820 folderInfo.add(info);
821 break;
822 }
823 } else {
824 // Failed to load the shortcut, probably because the
825 // activity manager couldn't resolve it (maybe the app
826 // was uninstalled), or the db row was somehow screwed up.
827 // Delete it.
828 id = c.getLong(idIndex);
829 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
830 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
831 id, false), null, null);
832 }
833 break;
834
835 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
836 id = c.getLong(idIndex);
837 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
838
Winson Chungaafa03c2010-06-11 17:34:16 -0700839 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400840 folderInfo.id = id;
841 container = c.getInt(containerIndex);
842 folderInfo.container = container;
843 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700844 folderInfo.cellX = c.getInt(cellXIndex);
845 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400846
847 // check & update map of what's occupied
848 if (!checkItemPlacement(occupied, folderInfo)) {
849 break;
850 }
Joe Onorato36115782010-06-17 13:28:48 -0400851 switch (container) {
852 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
853 mItems.add(folderInfo);
854 break;
855 }
856
857 mFolders.put(folderInfo.id, folderInfo);
858 break;
859
860 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
861 id = c.getLong(idIndex);
862 Uri uri = Uri.parse(c.getString(uriIndex));
863
864 // Make sure the live folder exists
865 final ProviderInfo providerInfo =
866 context.getPackageManager().resolveContentProvider(
867 uri.getAuthority(), 0);
868
869 if (providerInfo == null && !isSafeMode) {
870 itemsToRemove.add(id);
871 } else {
872 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400873 intentDescription = c.getString(intentIndex);
874 intent = null;
875 if (intentDescription != null) {
876 try {
877 intent = Intent.parseUri(intentDescription, 0);
878 } catch (URISyntaxException e) {
879 // Ignore, a live folder might not have a base intent
880 }
881 }
882
883 liveFolderInfo.title = c.getString(titleIndex);
884 liveFolderInfo.id = id;
885 liveFolderInfo.uri = uri;
886 container = c.getInt(containerIndex);
887 liveFolderInfo.container = container;
888 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700889 liveFolderInfo.cellX = c.getInt(cellXIndex);
890 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400891 liveFolderInfo.baseIntent = intent;
892 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
893
894 // check & update map of what's occupied
895 if (!checkItemPlacement(occupied, liveFolderInfo)) {
896 break;
897 }
898
899 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
900 iconResourceIndex, liveFolderInfo);
901
902 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400904 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905 break;
906 }
Joe Onorato36115782010-06-17 13:28:48 -0400907 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 }
Joe Onorato36115782010-06-17 13:28:48 -0400909 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800910
Joe Onorato36115782010-06-17 13:28:48 -0400911 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
912 // Read all Launcher-specific widget details
913 int appWidgetId = c.getInt(appWidgetIdIndex);
914 id = c.getLong(idIndex);
915
916 final AppWidgetProviderInfo provider =
917 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700918
Joe Onorato36115782010-06-17 13:28:48 -0400919 if (!isSafeMode && (provider == null || provider.provider == null ||
920 provider.provider.getPackageName() == null)) {
921 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
922 + id + " appWidgetId=" + appWidgetId);
923 itemsToRemove.add(id);
924 } else {
925 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
926 appWidgetInfo.id = id;
927 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700928 appWidgetInfo.cellX = c.getInt(cellXIndex);
929 appWidgetInfo.cellY = c.getInt(cellYIndex);
930 appWidgetInfo.spanX = c.getInt(spanXIndex);
931 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400932
933 container = c.getInt(containerIndex);
934 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
935 Log.e(TAG, "Widget found where container "
936 + "!= CONTAINER_DESKTOP -- ignoring!");
937 continue;
938 }
939 appWidgetInfo.container = c.getInt(containerIndex);
940
941 // check & update map of what's occupied
942 if (!checkItemPlacement(occupied, appWidgetInfo)) {
943 break;
944 }
945
946 mAppWidgets.add(appWidgetInfo);
947 }
948 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800949 }
Joe Onorato36115782010-06-17 13:28:48 -0400950 } catch (Exception e) {
951 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800952 }
953 }
Joe Onorato36115782010-06-17 13:28:48 -0400954 } finally {
955 c.close();
956 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800957
Joe Onorato36115782010-06-17 13:28:48 -0400958 if (itemsToRemove.size() > 0) {
959 ContentProviderClient client = contentResolver.acquireContentProviderClient(
960 LauncherSettings.Favorites.CONTENT_URI);
961 // Remove dead items
962 for (long id : itemsToRemove) {
963 if (DEBUG_LOADERS) {
964 Log.d(TAG, "Removed id = " + id);
965 }
966 // Don't notify content observers
967 try {
968 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
969 null, null);
970 } catch (RemoteException e) {
971 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400972 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 }
975
Joe Onorato36115782010-06-17 13:28:48 -0400976 if (DEBUG_LOADERS) {
977 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
978 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700979 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400980 String line = "";
981 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
982 if (s > 0) {
983 line += " | ";
984 }
Adam Cohend22015c2010-07-26 22:02:18 -0700985 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400986 line += ((occupied[s][x][y] != null) ? "#" : ".");
987 }
988 }
989 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 }
Joe Onorato36115782010-06-17 13:28:48 -0400991 }
992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993
Joe Onorato36115782010-06-17 13:28:48 -0400994 /**
995 * Read everything out of our database.
996 */
997 private void bindWorkspace() {
998 final long t = SystemClock.uptimeMillis();
999
1000 // Don't use these two variables in any of the callback runnables.
1001 // Otherwise we hold a reference to them.
1002 final Callbacks oldCallbacks = mCallbacks.get();
1003 if (oldCallbacks == null) {
1004 // This launcher has exited and nobody bothered to tell us. Just bail.
1005 Log.w(TAG, "LoaderTask running with no launcher");
1006 return;
1007 }
1008
1009 int N;
1010 // Tell the workspace that we're about to start firing items at it
1011 mHandler.post(new Runnable() {
1012 public void run() {
1013 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1014 if (callbacks != null) {
1015 callbacks.startBinding();
1016 }
1017 }
1018 });
1019 // Add the items to the workspace.
1020 N = mItems.size();
1021 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1022 final int start = i;
1023 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 mHandler.post(new Runnable() {
1025 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001026 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001028 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001029 }
1030 }
1031 });
Joe Onorato36115782010-06-17 13:28:48 -04001032 }
1033 mHandler.post(new Runnable() {
1034 public void run() {
1035 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1036 if (callbacks != null) {
1037 callbacks.bindFolders(mFolders);
1038 }
1039 }
1040 });
1041 // Wait until the queue goes empty.
1042 mHandler.post(new Runnable() {
1043 public void run() {
1044 if (DEBUG_LOADERS) {
1045 Log.d(TAG, "Going to start binding widgets soon.");
1046 }
1047 }
1048 });
1049 // Bind the widgets, one at a time.
1050 // WARNING: this is calling into the workspace from the background thread,
1051 // but since getCurrentScreen() just returns the int, we should be okay. This
1052 // is just a hint for the order, and if it's wrong, we'll be okay.
1053 // TODO: instead, we should have that push the current screen into here.
1054 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1055 N = mAppWidgets.size();
1056 // once for the current screen
1057 for (int i=0; i<N; i++) {
1058 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1059 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060 mHandler.post(new Runnable() {
1061 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001062 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001064 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 }
1066 }
1067 });
1068 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069 }
Joe Onorato36115782010-06-17 13:28:48 -04001070 // once for the other screens
1071 for (int i=0; i<N; i++) {
1072 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1073 if (widget.screen != currentScreen) {
1074 mHandler.post(new Runnable() {
1075 public void run() {
1076 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1077 if (callbacks != null) {
1078 callbacks.bindAppWidget(widget);
1079 }
1080 }
1081 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001082 }
1083 }
Joe Onorato36115782010-06-17 13:28:48 -04001084 // Tell the workspace that we're done.
1085 mHandler.post(new Runnable() {
1086 public void run() {
1087 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1088 if (callbacks != null) {
1089 callbacks.finishBindingItems();
1090 }
1091 }
1092 });
1093 // If we're profiling, this is the last thing in the queue.
1094 mHandler.post(new Runnable() {
1095 public void run() {
1096 if (DEBUG_LOADERS) {
1097 Log.d(TAG, "bound workspace in "
1098 + (SystemClock.uptimeMillis()-t) + "ms");
1099 }
1100 }
1101 });
1102 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001103
Joe Onorato36115782010-06-17 13:28:48 -04001104 private void loadAndBindAllApps() {
1105 if (DEBUG_LOADERS) {
1106 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1107 }
1108 if (!mAllAppsLoaded) {
1109 loadAllAppsByBatch();
1110 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001111 return;
1112 }
Joe Onorato36115782010-06-17 13:28:48 -04001113 mAllAppsLoaded = true;
1114 } else {
1115 onlyBindAllApps();
1116 }
1117 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001118
Joe Onorato36115782010-06-17 13:28:48 -04001119 private void onlyBindAllApps() {
1120 final Callbacks oldCallbacks = mCallbacks.get();
1121 if (oldCallbacks == null) {
1122 // This launcher has exited and nobody bothered to tell us. Just bail.
1123 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1124 return;
1125 }
1126
1127 // shallow copy
1128 final ArrayList<ApplicationInfo> list
1129 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1130 mHandler.post(new Runnable() {
1131 public void run() {
1132 final long t = SystemClock.uptimeMillis();
1133 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1134 if (callbacks != null) {
1135 callbacks.bindAllApplications(list);
1136 }
1137 if (DEBUG_LOADERS) {
1138 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1139 + (SystemClock.uptimeMillis()-t) + "ms");
1140 }
1141 }
1142 });
1143
1144 }
1145
1146 private void loadAllAppsByBatch() {
1147 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1148
1149 // Don't use these two variables in any of the callback runnables.
1150 // Otherwise we hold a reference to them.
1151 final Callbacks oldCallbacks = mCallbacks.get();
1152 if (oldCallbacks == null) {
1153 // This launcher has exited and nobody bothered to tell us. Just bail.
1154 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1155 return;
1156 }
1157
1158 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1159 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1160
1161 final PackageManager packageManager = mContext.getPackageManager();
1162 List<ResolveInfo> apps = null;
1163
1164 int N = Integer.MAX_VALUE;
1165
1166 int startIndex;
1167 int i=0;
1168 int batchSize = -1;
1169 while (i < N && !mStopped) {
1170 if (i == 0) {
1171 mAllAppsList.clear();
1172 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1173 apps = packageManager.queryIntentActivities(mainIntent, 0);
1174 if (DEBUG_LOADERS) {
1175 Log.d(TAG, "queryIntentActivities took "
1176 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1177 }
1178 if (apps == null) {
1179 return;
1180 }
1181 N = apps.size();
1182 if (DEBUG_LOADERS) {
1183 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1184 }
1185 if (N == 0) {
1186 // There are no apps?!?
1187 return;
1188 }
1189 if (mBatchSize == 0) {
1190 batchSize = N;
1191 } else {
1192 batchSize = mBatchSize;
1193 }
1194
1195 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1196 Collections.sort(apps,
1197 new ResolveInfo.DisplayNameComparator(packageManager));
1198 if (DEBUG_LOADERS) {
1199 Log.d(TAG, "sort took "
1200 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1201 }
1202 }
1203
1204 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1205
1206 startIndex = i;
1207 for (int j=0; i<N && j<batchSize; j++) {
1208 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001209 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001210 i++;
1211 }
1212
1213 final boolean first = i <= batchSize;
1214 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1215 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1216 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1217
Joe Onoratocc67f472010-06-08 10:54:30 -07001218 mHandler.post(new Runnable() {
1219 public void run() {
1220 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001221 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001222 if (first) {
1223 callbacks.bindAllApplications(added);
1224 } else {
1225 callbacks.bindAppsAdded(added);
1226 }
1227 if (DEBUG_LOADERS) {
1228 Log.d(TAG, "bound " + added.size() + " apps in "
1229 + (SystemClock.uptimeMillis() - t) + "ms");
1230 }
1231 } else {
1232 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001233 }
1234 }
1235 });
1236
Daniel Sandlerdca66122010-04-13 16:23:58 -04001237 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001238 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1239 + (SystemClock.uptimeMillis()-t2) + "ms");
1240 }
1241
1242 if (mAllAppsLoadDelay > 0 && i < N) {
1243 try {
1244 if (DEBUG_LOADERS) {
1245 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1246 }
1247 Thread.sleep(mAllAppsLoadDelay);
1248 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249 }
1250 }
1251
Joe Onorato36115782010-06-17 13:28:48 -04001252 if (DEBUG_LOADERS) {
1253 Log.d(TAG, "cached all " + N + " apps in "
1254 + (SystemClock.uptimeMillis()-t) + "ms"
1255 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001256 }
1257 }
1258
1259 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001260 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1261 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1262 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1263 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1264 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1265 }
1266 }
1267
1268 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001269 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001270 }
1271
1272 private class PackageUpdatedTask implements Runnable {
1273 int mOp;
1274 String[] mPackages;
1275
1276 public static final int OP_NONE = 0;
1277 public static final int OP_ADD = 1;
1278 public static final int OP_UPDATE = 2;
1279 public static final int OP_REMOVE = 3; // uninstlled
1280 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1281
1282
1283 public PackageUpdatedTask(int op, String[] packages) {
1284 mOp = op;
1285 mPackages = packages;
1286 }
1287
1288 public void run() {
1289 final Context context = mApp;
1290
1291 final String[] packages = mPackages;
1292 final int N = packages.length;
1293 switch (mOp) {
1294 case OP_ADD:
1295 for (int i=0; i<N; i++) {
1296 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1297 mAllAppsList.addPackage(context, packages[i]);
1298 }
1299 break;
1300 case OP_UPDATE:
1301 for (int i=0; i<N; i++) {
1302 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1303 mAllAppsList.updatePackage(context, packages[i]);
1304 }
1305 break;
1306 case OP_REMOVE:
1307 case OP_UNAVAILABLE:
1308 for (int i=0; i<N; i++) {
1309 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1310 mAllAppsList.removePackage(packages[i]);
1311 }
1312 break;
1313 }
1314
1315 ArrayList<ApplicationInfo> added = null;
1316 ArrayList<ApplicationInfo> removed = null;
1317 ArrayList<ApplicationInfo> modified = null;
1318
1319 if (mAllAppsList.added.size() > 0) {
1320 added = mAllAppsList.added;
1321 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1322 }
1323 if (mAllAppsList.removed.size() > 0) {
1324 removed = mAllAppsList.removed;
1325 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1326 for (ApplicationInfo info: removed) {
1327 mIconCache.remove(info.intent.getComponent());
1328 }
1329 }
1330 if (mAllAppsList.modified.size() > 0) {
1331 modified = mAllAppsList.modified;
1332 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1333 }
1334
1335 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1336 if (callbacks == null) {
1337 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1338 return;
1339 }
1340
1341 if (added != null) {
1342 final ArrayList<ApplicationInfo> addedFinal = added;
1343 mHandler.post(new Runnable() {
1344 public void run() {
1345 if (callbacks == mCallbacks.get()) {
1346 callbacks.bindAppsAdded(addedFinal);
1347 }
1348 }
1349 });
1350 }
1351 if (modified != null) {
1352 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1353 mHandler.post(new Runnable() {
1354 public void run() {
1355 if (callbacks == mCallbacks.get()) {
1356 callbacks.bindAppsUpdated(modifiedFinal);
1357 }
1358 }
1359 });
1360 }
1361 if (removed != null) {
1362 final boolean permanent = mOp != OP_UNAVAILABLE;
1363 final ArrayList<ApplicationInfo> removedFinal = removed;
1364 mHandler.post(new Runnable() {
1365 public void run() {
1366 if (callbacks == mCallbacks.get()) {
1367 callbacks.bindAppsRemoved(removedFinal, permanent);
1368 }
1369 }
1370 });
Joe Onoratobe386092009-11-17 17:32:16 -08001371 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001372
1373 mHandler.post(new Runnable() {
1374 @Override
1375 public void run() {
1376 if (callbacks == mCallbacks.get()) {
1377 callbacks.bindPackagesUpdated();
1378 }
1379 }
1380 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 }
1382 }
1383
1384 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001385 * This is called from the code that adds shortcuts from the intent receiver. This
1386 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 */
Joe Onorato56d82912010-03-07 14:32:10 -05001388 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001389 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001390 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391
Joe Onorato56d82912010-03-07 14:32:10 -05001392 /**
1393 * Make an ShortcutInfo object for a shortcut that is an application.
1394 *
1395 * If c is not null, then it will be used to fill in missing data like the title and icon.
1396 */
1397 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1398 Cursor c, int iconIndex, int titleIndex) {
1399 Bitmap icon = null;
1400 final ShortcutInfo info = new ShortcutInfo();
1401
1402 ComponentName componentName = intent.getComponent();
1403 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001404 return null;
1405 }
1406
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001407 // TODO: See if the PackageManager knows about this case. If it doesn't
1408 // then return null & delete this.
1409
Joe Onorato56d82912010-03-07 14:32:10 -05001410 // the resource -- This may implicitly give us back the fallback icon,
1411 // but don't worry about that. All we're doing with usingFallbackIcon is
1412 // to avoid saving lots of copies of that in the database, and most apps
1413 // have icons anyway.
1414 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1415 if (resolveInfo != null) {
1416 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001417 }
Joe Onorato56d82912010-03-07 14:32:10 -05001418 // the db
1419 if (icon == null) {
1420 if (c != null) {
1421 icon = getIconFromCursor(c, iconIndex);
1422 }
1423 }
1424 // the fallback icon
1425 if (icon == null) {
1426 icon = getFallbackIcon();
1427 info.usingFallbackIcon = true;
1428 }
1429 info.setIcon(icon);
1430
1431 // from the resource
1432 if (resolveInfo != null) {
1433 info.title = resolveInfo.activityInfo.loadLabel(manager);
1434 }
1435 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001436 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001437 if (c != null) {
1438 info.title = c.getString(titleIndex);
1439 }
1440 }
1441 // fall back to the class name of the activity
1442 if (info.title == null) {
1443 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1446 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001450 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001452 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001453 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1454 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455
Joe Onorato56d82912010-03-07 14:32:10 -05001456 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001457 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001458 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001460 // TODO: If there's an explicit component and we can't install that, delete it.
1461
Joe Onorato56d82912010-03-07 14:32:10 -05001462 info.title = c.getString(titleIndex);
1463
Joe Onorato9c1289c2009-08-17 11:03:03 -04001464 int iconType = c.getInt(iconTypeIndex);
1465 switch (iconType) {
1466 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1467 String packageName = c.getString(iconPackageIndex);
1468 String resourceName = c.getString(iconResourceIndex);
1469 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001470 info.customIcon = false;
1471 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001473 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001474 if (resources != null) {
1475 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001476 icon = Utilities.createIconBitmap(
1477 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001480 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 }
Joe Onorato56d82912010-03-07 14:32:10 -05001482 // the db
1483 if (icon == null) {
1484 icon = getIconFromCursor(c, iconIndex);
1485 }
1486 // the fallback icon
1487 if (icon == null) {
1488 icon = getFallbackIcon();
1489 info.usingFallbackIcon = true;
1490 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491 break;
1492 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001493 icon = getIconFromCursor(c, iconIndex);
1494 if (icon == null) {
1495 icon = getFallbackIcon();
1496 info.customIcon = false;
1497 info.usingFallbackIcon = true;
1498 } else {
1499 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 break;
1502 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001503 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001504 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001505 info.customIcon = false;
1506 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001508 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 return info;
1510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
Joe Onorato56d82912010-03-07 14:32:10 -05001512 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1513 if (false) {
1514 Log.d(TAG, "getIconFromCursor app="
1515 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1516 }
1517 byte[] data = c.getBlob(iconIndex);
1518 try {
1519 return BitmapFactory.decodeByteArray(data, 0, data.length);
1520 } catch (Exception e) {
1521 return null;
1522 }
1523 }
1524
Joe Onorato0589f0f2010-02-08 13:44:00 -08001525 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001526 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001527
Winson Chunga9abd0e2010-10-27 17:18:37 -07001528 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001529 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001530 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001531
1532 return info;
1533 }
1534
Winson Chunga9abd0e2010-10-27 17:18:37 -07001535 /**
Winson Chung55cef262010-10-28 14:14:18 -07001536 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1537 */
1538 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1539 ComponentName component) {
1540 List<AppWidgetProviderInfo> widgets =
1541 AppWidgetManager.getInstance(context).getInstalledProviders();
1542 for (AppWidgetProviderInfo info : widgets) {
1543 if (info.provider.equals(component)) {
1544 return info;
1545 }
1546 }
1547 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001548 }
1549
Winson Chung68846fd2010-10-29 11:00:27 -07001550 /**
1551 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1552 */
1553 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1554 final PackageManager packageManager = context.getPackageManager();
1555 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1556 new ArrayList<WidgetMimeTypeHandlerData>();
1557
1558 final Intent supportsIntent =
1559 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1560 supportsIntent.setType(mimeType);
1561
1562 // Create a set of widget configuration components that we can test against
1563 final List<AppWidgetProviderInfo> widgets =
1564 AppWidgetManager.getInstance(context).getInstalledProviders();
1565 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1566 new HashMap<ComponentName, AppWidgetProviderInfo>();
1567 for (AppWidgetProviderInfo info : widgets) {
1568 configurationComponentToWidget.put(info.configure, info);
1569 }
1570
1571 // Run through each of the intents that can handle this type of clip data, and cross
1572 // reference them with the components that are actual configuration components
1573 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1574 PackageManager.MATCH_DEFAULT_ONLY);
1575 for (ResolveInfo info : activities) {
1576 final ActivityInfo activityInfo = info.activityInfo;
1577 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1578 activityInfo.name);
1579 if (configurationComponentToWidget.containsKey(infoComponent)) {
1580 supportedConfigurationActivities.add(
1581 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1582 configurationComponentToWidget.get(infoComponent)));
1583 }
1584 }
1585 return supportedConfigurationActivities;
1586 }
1587
Winson Chunga9abd0e2010-10-27 17:18:37 -07001588 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001589 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1590 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1591 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1592
1593 Bitmap icon = null;
1594 boolean filtered = false;
1595 boolean customIcon = false;
1596 ShortcutIconResource iconResource = null;
1597
1598 if (bitmap != null && bitmap instanceof Bitmap) {
1599 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1600 filtered = true;
1601 customIcon = true;
1602 } else {
1603 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1604 if (extra != null && extra instanceof ShortcutIconResource) {
1605 try {
1606 iconResource = (ShortcutIconResource) extra;
1607 final PackageManager packageManager = context.getPackageManager();
1608 Resources resources = packageManager.getResourcesForApplication(
1609 iconResource.packageName);
1610 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001611 icon = Utilities.createIconBitmap(
1612 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001613 } catch (Exception e) {
1614 Log.w(TAG, "Could not load shortcut icon: " + extra);
1615 }
1616 }
1617 }
1618
Joe Onorato0589f0f2010-02-08 13:44:00 -08001619 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001620
1621 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001622 if (fallbackIcon != null) {
1623 icon = fallbackIcon;
1624 } else {
1625 icon = getFallbackIcon();
1626 info.usingFallbackIcon = true;
1627 }
Joe Onorato56d82912010-03-07 14:32:10 -05001628 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001629 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001630
Joe Onorato0589f0f2010-02-08 13:44:00 -08001631 info.title = name;
1632 info.intent = intent;
1633 info.customIcon = customIcon;
1634 info.iconResource = iconResource;
1635
1636 return info;
1637 }
1638
Michael Jurkac9a96192010-11-01 11:52:08 -07001639 private void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1641
1642 int iconType = c.getInt(iconTypeIndex);
1643 switch (iconType) {
1644 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1645 String packageName = c.getString(iconPackageIndex);
1646 String resourceName = c.getString(iconResourceIndex);
1647 PackageManager packageManager = context.getPackageManager();
1648 try {
Michael Jurkac9a96192010-11-01 11:52:08 -07001649 Resources appResources = packageManager.getResourcesForApplication(packageName);
1650 final int id = appResources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001651 liveFolderInfo.icon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -07001652 mIconCache.getFullResIcon(appResources, id), context);
1653 } catch (Exception e) {
1654 Resources resources = context.getResources();
1655 liveFolderInfo.icon = Utilities.createIconBitmap(
1656 mIconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001657 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 }
1659 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1660 liveFolderInfo.iconResource.packageName = packageName;
1661 liveFolderInfo.iconResource.resourceName = resourceName;
1662 break;
1663 default:
Michael Jurkac9a96192010-11-01 11:52:08 -07001664 Resources resources = context.getResources();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001665 liveFolderInfo.icon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -07001666 mIconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001667 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668 }
1669 }
1670
Joe Onorato56d82912010-03-07 14:32:10 -05001671 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1672 // If this icon doesn't have a custom icon, check to see
1673 // what's stored in the DB, and if it doesn't match what
1674 // we're going to show, store what we are going to show back
1675 // into the DB. We do this so when we're loading, if the
1676 // package manager can't find an icon (for example because
1677 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001678 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001679 boolean needSave;
1680 byte[] data = c.getBlob(iconIndex);
1681 try {
1682 if (data != null) {
1683 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1684 Bitmap loaded = info.getIcon(mIconCache);
1685 needSave = !saved.sameAs(loaded);
1686 } else {
1687 needSave = true;
1688 }
1689 } catch (Exception e) {
1690 needSave = true;
1691 }
1692 if (needSave) {
1693 Log.d(TAG, "going to save icon bitmap for info=" + info);
1694 // This is slower than is ideal, but this only happens either
1695 // after the froyo OTA or when the app is updated with a new
1696 // icon.
1697 updateItemInDatabase(context, info);
1698 }
1699 }
1700 }
1701
Joe Onorato9c1289c2009-08-17 11:03:03 -04001702 /**
1703 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1704 * or make a new one.
1705 */
1706 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1707 // See if a placeholder was created for us already
1708 FolderInfo folderInfo = folders.get(id);
1709 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1710 // No placeholder -- create a new instance
1711 folderInfo = new UserFolderInfo();
1712 folders.put(id, folderInfo);
1713 }
1714 return (UserFolderInfo) folderInfo;
1715 }
1716
1717 /**
1718 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1719 * new one.
1720 */
1721 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1722 // See if a placeholder was created for us already
1723 FolderInfo folderInfo = folders.get(id);
1724 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1725 // No placeholder -- create a new instance
1726 folderInfo = new LiveFolderInfo();
1727 folders.put(id, folderInfo);
1728 }
1729 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 }
1731
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1733 String label = activityInfo.loadLabel(manager).toString();
1734 if (label == null) {
1735 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1736 if (label == null) {
1737 label = activityInfo.name;
1738 }
1739 }
1740 return label;
1741 }
1742
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001744 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 = new Comparator<ApplicationInfo>() {
1746 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1747 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749 };
Winson Chung78403fe2011-01-21 15:38:02 -08001750 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1751 = new Comparator<ApplicationInfo>() {
1752 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1753 if (a.firstInstallTime < b.firstInstallTime) return 1;
1754 if (a.firstInstallTime > b.firstInstallTime) return -1;
1755 return 0;
1756 }
1757 };
Joe Onoratobe386092009-11-17 17:32:16 -08001758
1759 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001760 Log.d(TAG, "mCallbacks=" + mCallbacks);
1761 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1762 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1763 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1764 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001765 Log.d(TAG, "mItems size=" + mItems.size());
1766 if (mLoaderTask != null) {
1767 mLoaderTask.dumpState();
1768 } else {
1769 Log.d(TAG, "mLoaderTask=null");
1770 }
Joe Onoratobe386092009-11-17 17:32:16 -08001771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772}