blob: 67aa311260541508860ed04d88fdd2c76dd10ba1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chungaafa03c2010-06-11 17:34:16 -070019import java.lang.ref.WeakReference;
20import java.net.URISyntaxException;
21import java.text.Collator;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.List;
27
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040030import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080032import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ContentResolver;
34import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.ActivityInfo;
39import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080040import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.pm.ResolveInfo;
42import android.content.res.Resources;
43import android.database.Cursor;
44import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040047import android.os.Handler;
48import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040052import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
Romain Guyedcce092010-03-04 13:03:17 -080055import com.android.launcher.R;
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * Maintains in-memory state of the Launcher. It is expected that there should be only one
59 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070060 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040062public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080063 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040064 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070065
Joe Onorato36115782010-06-17 13:28:48 -040066 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onoratod65d08e2010-04-20 15:43:37 -040067 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040068 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040069
Joe Onoratof99f8c12009-10-31 17:27:36 -040070 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 private final Object mLock = new Object();
72 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040073 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070075 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
76 static {
77 sWorkerThread.start();
78 }
79 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
80
Joe Onoratocc67f472010-06-08 10:54:30 -070081 // We start off with everything not loaded. After that, we assume that
82 // our monitoring of the package manager provides all updates and we never
83 // need to do a requery. These are only ever touched from the loader thread.
84 private boolean mWorkspaceLoaded;
85 private boolean mAllAppsLoaded;
86
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Joe Onorato36115782010-06-17 13:28:48 -040089 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -080090 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -040091 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
92 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
93 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -080094
95 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Adam Cohend22015c2010-07-26 22:02:18 -070097 private static int mCellCountX;
98 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -070099
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700101 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400102 public int getCurrentWorkspaceScreen();
103 public void startBinding();
104 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500105 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 public void finishBindingItems();
107 public void bindAppWidget(LauncherAppWidgetInfo info);
108 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500109 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
110 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400111 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400113 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato0589f0f2010-02-08 13:44:00 -0800116 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400117 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800118 mAllAppsList = new AllAppsList(iconCache);
119 mIconCache = iconCache;
120
121 mDefaultIcon = Utilities.createIconBitmap(
122 app.getPackageManager().getDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400123
124 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400125
126 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800127 }
128
Joe Onorato56d82912010-03-07 14:32:10 -0500129 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800130 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato9c1289c2009-08-17 11:03:03 -0400133 /**
134 * Adds an item to the DB if it was not created previously, or move it to a new
135 * <container, screen, cellX, cellY>
136 */
137 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
138 int screen, int cellX, int cellY) {
139 if (item.container == ItemInfo.NO_ID) {
140 // From all apps
141 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
142 } else {
143 // From somewhere else
144 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
146 }
147
148 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700150 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
152 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700153
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 item.container = container;
155 item.screen = screen;
156 item.cellX = cellX;
157 item.cellY = cellY;
158
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700159 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 final ContentValues values = new ContentValues();
161 final ContentResolver cr = context.getContentResolver();
162
163 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700164 values.put(LauncherSettings.Favorites.CELLX, cellX);
165 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
167
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700168 sWorker.post(new Runnable() {
169 public void run() {
170 cr.update(uri, values, null, null);
171 }
172 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700173 }
174
175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 * Returns true if the shortcuts already exists in the database.
177 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 static boolean shortcutExists(Context context, String title, Intent intent) {
180 final ContentResolver cr = context.getContentResolver();
181 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
182 new String[] { "title", "intent" }, "title=? and intent=?",
183 new String[] { title, intent.toUri(0) }, null);
184 boolean result = false;
185 try {
186 result = c.moveToFirst();
187 } finally {
188 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700191 }
192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700194 * Returns an ItemInfo array containing all the items in the LauncherModel.
195 * The ItemInfo.id is not set through this function.
196 */
197 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
198 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
199 final ContentResolver cr = context.getContentResolver();
200 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
201 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
202 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
203 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
204
205 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
206 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
207 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
208 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
209 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
210 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
211 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
212
213 try {
214 while (c.moveToNext()) {
215 ItemInfo item = new ItemInfo();
216 item.cellX = c.getInt(cellXIndex);
217 item.cellY = c.getInt(cellYIndex);
218 item.spanX = c.getInt(spanXIndex);
219 item.spanY = c.getInt(spanYIndex);
220 item.container = c.getInt(containerIndex);
221 item.itemType = c.getInt(itemTypeIndex);
222 item.screen = c.getInt(screenIndex);
223
224 items.add(item);
225 }
226 } catch (Exception e) {
227 items.clear();
228 } finally {
229 c.close();
230 }
231
232 return items;
233 }
234
235 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
237 */
238 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
239 final ContentResolver cr = context.getContentResolver();
240 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
241 "_id=? and (itemType=? or itemType=?)",
242 new String[] { String.valueOf(id),
243 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
244 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700245
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246 try {
247 if (c.moveToFirst()) {
248 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
249 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
250 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
251 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
252 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
253 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 FolderInfo folderInfo = null;
256 switch (c.getInt(itemTypeIndex)) {
257 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
258 folderInfo = findOrMakeUserFolder(folderList, id);
259 break;
260 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
261 folderInfo = findOrMakeLiveFolder(folderList, id);
262 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700263 }
264
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 folderInfo.title = c.getString(titleIndex);
266 folderInfo.id = id;
267 folderInfo.container = c.getInt(containerIndex);
268 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700269 folderInfo.cellX = c.getInt(cellXIndex);
270 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
272 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700273 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 } finally {
275 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700276 }
277
278 return null;
279 }
280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 /**
282 * Add an item to the database in a specified container. Sets the container, screen, cellX and
283 * cellY fields of the item. Also assigns an ID to the item.
284 */
285 static void addItemToDatabase(Context context, ItemInfo item, long container,
286 int screen, int cellX, int cellY, boolean notify) {
287 item.container = container;
288 item.screen = screen;
289 item.cellX = cellX;
290 item.cellY = cellY;
291
292 final ContentValues values = new ContentValues();
293 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 item.onAddToDatabase(values);
295
Adam Cohend22015c2010-07-26 22:02:18 -0700296 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700297
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
299 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
300
301 if (result != null) {
302 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700303 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700304 }
305
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700307 * Creates a new unique child id, for a given cell span across all layouts.
308 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700309 static int getCellLayoutChildId(
310 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
311 return ((cellId & 0xFF) << 24)
312 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700313 }
314
Adam Cohend22015c2010-07-26 22:02:18 -0700315 static int getCellCountX() {
316 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700317 }
318
Adam Cohend22015c2010-07-26 22:02:18 -0700319 static int getCellCountY() {
320 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700321 }
322
323 /**
324 * Updates the model orientation helper to take into account the current layout dimensions
325 * when performing local/canonical coordinate transformations.
326 */
327 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700328 mCellCountX = shortAxisCellCount;
329 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700330 }
331
332 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400333 * Update an item to the database in a specified container.
334 */
335 static void updateItemInDatabase(Context context, ItemInfo item) {
336 final ContentValues values = new ContentValues();
337 final ContentResolver cr = context.getContentResolver();
338
339 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700340 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700341
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
343 }
344
345 /**
346 * Removes the specified item from the database
347 * @param context
348 * @param item
349 */
350 static void deleteItemFromDatabase(Context context, ItemInfo item) {
351 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700352 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700353 sWorker.post(new Runnable() {
354 public void run() {
355 cr.delete(uriToDelete, null, null);
356 }
357 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 }
359
360 /**
361 * Remove the contents of the specified folder from the database
362 */
363 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
364 final ContentResolver cr = context.getContentResolver();
365
366 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
367 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
368 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
369 }
370
371 /**
372 * Set this as the current Launcher activity object for the loader.
373 */
374 public void initialize(Callbacks callbacks) {
375 synchronized (mLock) {
376 mCallbacks = new WeakReference<Callbacks>(callbacks);
377 }
378 }
379
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700380 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
382 * ACTION_PACKAGE_CHANGED.
383 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400384 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400385 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700386
Joe Onorato36115782010-06-17 13:28:48 -0400387 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400388
Joe Onorato36115782010-06-17 13:28:48 -0400389 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
390 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
391 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
392 final String packageName = intent.getData().getSchemeSpecificPart();
393 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400394
Joe Onorato36115782010-06-17 13:28:48 -0400395 int op = PackageUpdatedTask.OP_NONE;
396
397 if (packageName == null || packageName.length() == 0) {
398 // they sent us a bad intent
399 return;
400 }
401
402 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
403 op = PackageUpdatedTask.OP_UPDATE;
404 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
405 if (!replacing) {
406 op = PackageUpdatedTask.OP_REMOVE;
407 }
408 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
409 // later, we will update the package at this time
410 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
411 if (!replacing) {
412 op = PackageUpdatedTask.OP_ADD;
413 } else {
414 op = PackageUpdatedTask.OP_UPDATE;
415 }
416 }
417
418 if (op != PackageUpdatedTask.OP_NONE) {
419 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
420 }
421
422 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400423 // First, schedule to add these apps back in.
424 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
425 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
426 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700427 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400428 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
429 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
430 enqueuePackageUpdated(new PackageUpdatedTask(
431 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700432 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
433 // If we have changed locale we need to clear out the labels in all apps.
434 // Do this here because if the launcher activity is running it will be restarted.
435 // If it's not running startLoaderFromBackground will merely tell it that it needs
436 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
437 // next time.
438 mAllAppsLoaded = false;
439 startLoaderFromBackground();
440 }
441 }
442
443 /**
444 * When the launcher is in the background, it's possible for it to miss paired
445 * configuration changes. So whenever we trigger the loader from the background
446 * tell the launcher that it needs to re-run the loader when it comes back instead
447 * of doing it now.
448 */
449 public void startLoaderFromBackground() {
450 boolean runLoader = false;
451 if (mCallbacks != null) {
452 Callbacks callbacks = mCallbacks.get();
453 if (callbacks != null) {
454 // Only actually run the loader if they're not paused.
455 if (!callbacks.setLoadOnResume()) {
456 runLoader = true;
457 }
458 }
459 }
460 if (runLoader) {
461 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700462 }
Joe Onorato36115782010-06-17 13:28:48 -0400463 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400464
Joe Onorato36115782010-06-17 13:28:48 -0400465 public void startLoader(Context context, boolean isLaunching) {
466 synchronized (mLock) {
467 if (DEBUG_LOADERS) {
468 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
469 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400470
Joe Onorato36115782010-06-17 13:28:48 -0400471 // Don't bother to start the thread if we know it's not going to do anything
472 if (mCallbacks != null && mCallbacks.get() != null) {
473 // If there is already one running, tell it to stop.
474 LoaderTask oldTask = mLoaderTask;
475 if (oldTask != null) {
476 if (oldTask.isLaunching()) {
477 // don't downgrade isLaunching if we're already running
478 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500479 }
Joe Onorato36115782010-06-17 13:28:48 -0400480 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500481 }
Joe Onorato36115782010-06-17 13:28:48 -0400482 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700483 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400484 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400485 }
486 }
487
Joe Onorato36115782010-06-17 13:28:48 -0400488 public void stopLoader() {
489 synchronized (mLock) {
490 if (mLoaderTask != null) {
491 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400492 }
493 }
Joe Onorato36115782010-06-17 13:28:48 -0400494 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400495
Joe Onorato36115782010-06-17 13:28:48 -0400496 /**
497 * Runnable for the thread that loads the contents of the launcher:
498 * - workspace icons
499 * - widgets
500 * - all apps icons
501 */
502 private class LoaderTask implements Runnable {
503 private Context mContext;
504 private Thread mWaitThread;
505 private boolean mIsLaunching;
506 private boolean mStopped;
507 private boolean mLoadAndBindStepFinished;
508
509 LoaderTask(Context context, boolean isLaunching) {
510 mContext = context;
511 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 }
513
Joe Onorato36115782010-06-17 13:28:48 -0400514 boolean isLaunching() {
515 return mIsLaunching;
516 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517
Joe Onorato36115782010-06-17 13:28:48 -0400518 private void loadAndBindWorkspace() {
519 // Load the workspace
520
521 // For now, just always reload the workspace. It's ~100 ms vs. the
522 // binding which takes many hundreds of ms.
523 // We can reconsider.
524 if (DEBUG_LOADERS) {
525 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526 }
Joe Onorato36115782010-06-17 13:28:48 -0400527 if (true || !mWorkspaceLoaded) {
528 loadWorkspace();
529 if (mStopped) {
530 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400531 }
Joe Onorato36115782010-06-17 13:28:48 -0400532 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 }
534
Joe Onorato36115782010-06-17 13:28:48 -0400535 // Bind the workspace
536 bindWorkspace();
537 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400538
Joe Onorato36115782010-06-17 13:28:48 -0400539 private void waitForIdle() {
540 // Wait until the either we're stopped or the other threads are done.
541 // This way we don't start loading all apps until the workspace has settled
542 // down.
543 synchronized (LoaderTask.this) {
544 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700545
Joe Onorato36115782010-06-17 13:28:48 -0400546 mHandler.postIdle(new Runnable() {
547 public void run() {
548 synchronized (LoaderTask.this) {
549 mLoadAndBindStepFinished = true;
550 if (DEBUG_LOADERS) {
551 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400552 }
Joe Onorato36115782010-06-17 13:28:48 -0400553 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400554 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400555 }
Joe Onorato36115782010-06-17 13:28:48 -0400556 });
557
558 while (!mStopped && !mLoadAndBindStepFinished) {
559 try {
560 this.wait();
561 } catch (InterruptedException ex) {
562 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400563 }
564 }
Joe Onorato36115782010-06-17 13:28:48 -0400565 if (DEBUG_LOADERS) {
566 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700567 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400568 + "ms for previous step to finish binding");
569 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400570 }
Joe Onorato36115782010-06-17 13:28:48 -0400571 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400572
Joe Onorato36115782010-06-17 13:28:48 -0400573 public void run() {
574 // Optimize for end-user experience: if the Launcher is up and // running with the
575 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
576 // workspace first (default).
577 final Callbacks cbk = mCallbacks.get();
578 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400579
Joe Onorato36115782010-06-17 13:28:48 -0400580 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400581 // Elevate priority when Home launches for the first time to avoid
582 // starving at boot time. Staring at a blank home is not cool.
583 synchronized (mLock) {
584 android.os.Process.setThreadPriority(mIsLaunching
585 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
586 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400587 if (loadWorkspaceFirst) {
588 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
589 loadAndBindWorkspace();
590 } else {
591 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700592 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400593 }
594
Joe Onorato36115782010-06-17 13:28:48 -0400595 if (mStopped) {
596 break keep_running;
597 }
598
599 // Whew! Hard work done. Slow us down, and wait until the UI thread has
600 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400601 synchronized (mLock) {
602 if (mIsLaunching) {
603 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
604 }
605 }
Joe Onorato36115782010-06-17 13:28:48 -0400606 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400607
608 // second step
609 if (loadWorkspaceFirst) {
610 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700611 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400612 } else {
613 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
614 loadAndBindWorkspace();
615 }
Joe Onorato36115782010-06-17 13:28:48 -0400616 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400617
Joe Onorato36115782010-06-17 13:28:48 -0400618 // Clear out this reference, otherwise we end up holding it until all of the
619 // callback runnables are done.
620 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621
Joe Onorato36115782010-06-17 13:28:48 -0400622 synchronized (mLock) {
623 // If we are still the last one to be scheduled, remove ourselves.
624 if (mLoaderTask == this) {
625 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 }
Joe Onorato36115782010-06-17 13:28:48 -0400627 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700628
Joe Onorato36115782010-06-17 13:28:48 -0400629 // Trigger a gc to try to clean up after the stuff is done, since the
630 // renderscript allocations aren't charged to the java heap.
631 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700632 mHandler.post(new Runnable() {
633 public void run() {
634 System.gc();
635 }
636 });
Joe Onorato36115782010-06-17 13:28:48 -0400637 } else {
638 mHandler.postIdle(new Runnable() {
639 public void run() {
640 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400641 }
Joe Onorato36115782010-06-17 13:28:48 -0400642 });
643 }
644 }
645
646 public void stopLocked() {
647 synchronized (LoaderTask.this) {
648 mStopped = true;
649 this.notify();
650 }
651 }
652
653 /**
654 * Gets the callbacks object. If we've been stopped, or if the launcher object
655 * has somehow been garbage collected, return null instead. Pass in the Callbacks
656 * object that was around when the deferred message was scheduled, and if there's
657 * a new Callbacks object around then also return null. This will save us from
658 * calling onto it with data that will be ignored.
659 */
660 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
661 synchronized (mLock) {
662 if (mStopped) {
663 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400664 }
Joe Onorato36115782010-06-17 13:28:48 -0400665
666 if (mCallbacks == null) {
667 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400668 }
Joe Onorato36115782010-06-17 13:28:48 -0400669
670 final Callbacks callbacks = mCallbacks.get();
671 if (callbacks != oldCallbacks) {
672 return null;
673 }
674 if (callbacks == null) {
675 Log.w(TAG, "no mCallbacks");
676 return null;
677 }
678
679 return callbacks;
680 }
681 }
682
683 // check & update map of what's occupied; used to discard overlapping/invalid items
684 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
685 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400686 return true;
687 }
Joe Onorato36115782010-06-17 13:28:48 -0400688 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
689 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
690 if (occupied[item.screen][x][y] != null) {
691 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400693 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700694 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400695 + occupied[item.screen][x][y]);
696 return false;
697 }
698 }
699 }
700 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
701 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
702 occupied[item.screen][x][y] = item;
703 }
704 }
705 return true;
706 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707
Joe Onorato36115782010-06-17 13:28:48 -0400708 private void loadWorkspace() {
709 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710
Joe Onorato36115782010-06-17 13:28:48 -0400711 final Context context = mContext;
712 final ContentResolver contentResolver = context.getContentResolver();
713 final PackageManager manager = context.getPackageManager();
714 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
715 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400716
Joe Onorato36115782010-06-17 13:28:48 -0400717 mItems.clear();
718 mAppWidgets.clear();
719 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800720
Joe Onorato36115782010-06-17 13:28:48 -0400721 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722
Joe Onorato36115782010-06-17 13:28:48 -0400723 final Cursor c = contentResolver.query(
724 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400725
Adam Cohend22015c2010-07-26 22:02:18 -0700726 final ItemInfo occupied[][][] =
727 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728
Joe Onorato36115782010-06-17 13:28:48 -0400729 try {
730 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
731 final int intentIndex = c.getColumnIndexOrThrow
732 (LauncherSettings.Favorites.INTENT);
733 final int titleIndex = c.getColumnIndexOrThrow
734 (LauncherSettings.Favorites.TITLE);
735 final int iconTypeIndex = c.getColumnIndexOrThrow(
736 LauncherSettings.Favorites.ICON_TYPE);
737 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
738 final int iconPackageIndex = c.getColumnIndexOrThrow(
739 LauncherSettings.Favorites.ICON_PACKAGE);
740 final int iconResourceIndex = c.getColumnIndexOrThrow(
741 LauncherSettings.Favorites.ICON_RESOURCE);
742 final int containerIndex = c.getColumnIndexOrThrow(
743 LauncherSettings.Favorites.CONTAINER);
744 final int itemTypeIndex = c.getColumnIndexOrThrow(
745 LauncherSettings.Favorites.ITEM_TYPE);
746 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
747 LauncherSettings.Favorites.APPWIDGET_ID);
748 final int screenIndex = c.getColumnIndexOrThrow(
749 LauncherSettings.Favorites.SCREEN);
750 final int cellXIndex = c.getColumnIndexOrThrow
751 (LauncherSettings.Favorites.CELLX);
752 final int cellYIndex = c.getColumnIndexOrThrow
753 (LauncherSettings.Favorites.CELLY);
754 final int spanXIndex = c.getColumnIndexOrThrow
755 (LauncherSettings.Favorites.SPANX);
756 final int spanYIndex = c.getColumnIndexOrThrow(
757 LauncherSettings.Favorites.SPANY);
758 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
759 final int displayModeIndex = c.getColumnIndexOrThrow(
760 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761
Joe Onorato36115782010-06-17 13:28:48 -0400762 ShortcutInfo info;
763 String intentDescription;
764 LauncherAppWidgetInfo appWidgetInfo;
765 int container;
766 long id;
767 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768
Joe Onorato36115782010-06-17 13:28:48 -0400769 while (!mStopped && c.moveToNext()) {
770 try {
771 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772
Joe Onorato36115782010-06-17 13:28:48 -0400773 switch (itemType) {
774 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
775 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
776 intentDescription = c.getString(intentIndex);
777 try {
778 intent = Intent.parseUri(intentDescription, 0);
779 } catch (URISyntaxException e) {
780 continue;
781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782
Joe Onorato36115782010-06-17 13:28:48 -0400783 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
784 info = getShortcutInfo(manager, intent, context, c, iconIndex,
785 titleIndex);
786 } else {
787 info = getShortcutInfo(c, context, iconTypeIndex,
788 iconPackageIndex, iconResourceIndex, iconIndex,
789 titleIndex);
790 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791
Joe Onorato36115782010-06-17 13:28:48 -0400792 if (info != null) {
793 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794
Joe Onorato36115782010-06-17 13:28:48 -0400795 info.intent = intent;
796 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400798 info.container = container;
799 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700800 info.cellX = c.getInt(cellXIndex);
801 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802
Daniel Sandler8802e962010-05-26 16:28:16 -0400803 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400804 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400805 break;
806 }
807
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400809 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
810 mItems.add(info);
811 break;
812 default:
813 // Item is in a user folder
814 UserFolderInfo folderInfo =
815 findOrMakeUserFolder(mFolders, container);
816 folderInfo.add(info);
817 break;
818 }
819 } else {
820 // Failed to load the shortcut, probably because the
821 // activity manager couldn't resolve it (maybe the app
822 // was uninstalled), or the db row was somehow screwed up.
823 // Delete it.
824 id = c.getLong(idIndex);
825 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
826 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
827 id, false), null, null);
828 }
829 break;
830
831 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
832 id = c.getLong(idIndex);
833 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
834
Winson Chungaafa03c2010-06-11 17:34:16 -0700835 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400836 folderInfo.id = id;
837 container = c.getInt(containerIndex);
838 folderInfo.container = container;
839 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700840 folderInfo.cellX = c.getInt(cellXIndex);
841 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400842
843 // check & update map of what's occupied
844 if (!checkItemPlacement(occupied, folderInfo)) {
845 break;
846 }
Joe Onorato36115782010-06-17 13:28:48 -0400847 switch (container) {
848 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
849 mItems.add(folderInfo);
850 break;
851 }
852
853 mFolders.put(folderInfo.id, folderInfo);
854 break;
855
856 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
857 id = c.getLong(idIndex);
858 Uri uri = Uri.parse(c.getString(uriIndex));
859
860 // Make sure the live folder exists
861 final ProviderInfo providerInfo =
862 context.getPackageManager().resolveContentProvider(
863 uri.getAuthority(), 0);
864
865 if (providerInfo == null && !isSafeMode) {
866 itemsToRemove.add(id);
867 } else {
868 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400869 intentDescription = c.getString(intentIndex);
870 intent = null;
871 if (intentDescription != null) {
872 try {
873 intent = Intent.parseUri(intentDescription, 0);
874 } catch (URISyntaxException e) {
875 // Ignore, a live folder might not have a base intent
876 }
877 }
878
879 liveFolderInfo.title = c.getString(titleIndex);
880 liveFolderInfo.id = id;
881 liveFolderInfo.uri = uri;
882 container = c.getInt(containerIndex);
883 liveFolderInfo.container = container;
884 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700885 liveFolderInfo.cellX = c.getInt(cellXIndex);
886 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400887 liveFolderInfo.baseIntent = intent;
888 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
889
890 // check & update map of what's occupied
891 if (!checkItemPlacement(occupied, liveFolderInfo)) {
892 break;
893 }
894
895 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
896 iconResourceIndex, liveFolderInfo);
897
898 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400900 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901 break;
902 }
Joe Onorato36115782010-06-17 13:28:48 -0400903 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904 }
Joe Onorato36115782010-06-17 13:28:48 -0400905 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800906
Joe Onorato36115782010-06-17 13:28:48 -0400907 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
908 // Read all Launcher-specific widget details
909 int appWidgetId = c.getInt(appWidgetIdIndex);
910 id = c.getLong(idIndex);
911
912 final AppWidgetProviderInfo provider =
913 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700914
Joe Onorato36115782010-06-17 13:28:48 -0400915 if (!isSafeMode && (provider == null || provider.provider == null ||
916 provider.provider.getPackageName() == null)) {
917 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
918 + id + " appWidgetId=" + appWidgetId);
919 itemsToRemove.add(id);
920 } else {
921 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
922 appWidgetInfo.id = id;
923 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700924 appWidgetInfo.cellX = c.getInt(cellXIndex);
925 appWidgetInfo.cellY = c.getInt(cellYIndex);
926 appWidgetInfo.spanX = c.getInt(spanXIndex);
927 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400928
929 container = c.getInt(containerIndex);
930 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
931 Log.e(TAG, "Widget found where container "
932 + "!= CONTAINER_DESKTOP -- ignoring!");
933 continue;
934 }
935 appWidgetInfo.container = c.getInt(containerIndex);
936
937 // check & update map of what's occupied
938 if (!checkItemPlacement(occupied, appWidgetInfo)) {
939 break;
940 }
941
942 mAppWidgets.add(appWidgetInfo);
943 }
944 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800945 }
Joe Onorato36115782010-06-17 13:28:48 -0400946 } catch (Exception e) {
947 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800948 }
949 }
Joe Onorato36115782010-06-17 13:28:48 -0400950 } finally {
951 c.close();
952 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800953
Joe Onorato36115782010-06-17 13:28:48 -0400954 if (itemsToRemove.size() > 0) {
955 ContentProviderClient client = contentResolver.acquireContentProviderClient(
956 LauncherSettings.Favorites.CONTENT_URI);
957 // Remove dead items
958 for (long id : itemsToRemove) {
959 if (DEBUG_LOADERS) {
960 Log.d(TAG, "Removed id = " + id);
961 }
962 // Don't notify content observers
963 try {
964 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
965 null, null);
966 } catch (RemoteException e) {
967 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400968 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800969 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 }
971
Joe Onorato36115782010-06-17 13:28:48 -0400972 if (DEBUG_LOADERS) {
973 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
974 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700975 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400976 String line = "";
977 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
978 if (s > 0) {
979 line += " | ";
980 }
Adam Cohend22015c2010-07-26 22:02:18 -0700981 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400982 line += ((occupied[s][x][y] != null) ? "#" : ".");
983 }
984 }
985 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 }
Joe Onorato36115782010-06-17 13:28:48 -0400987 }
988 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989
Joe Onorato36115782010-06-17 13:28:48 -0400990 /**
991 * Read everything out of our database.
992 */
993 private void bindWorkspace() {
994 final long t = SystemClock.uptimeMillis();
995
996 // Don't use these two variables in any of the callback runnables.
997 // Otherwise we hold a reference to them.
998 final Callbacks oldCallbacks = mCallbacks.get();
999 if (oldCallbacks == null) {
1000 // This launcher has exited and nobody bothered to tell us. Just bail.
1001 Log.w(TAG, "LoaderTask running with no launcher");
1002 return;
1003 }
1004
1005 int N;
1006 // Tell the workspace that we're about to start firing items at it
1007 mHandler.post(new Runnable() {
1008 public void run() {
1009 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1010 if (callbacks != null) {
1011 callbacks.startBinding();
1012 }
1013 }
1014 });
1015 // Add the items to the workspace.
1016 N = mItems.size();
1017 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1018 final int start = i;
1019 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 mHandler.post(new Runnable() {
1021 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001022 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001024 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 }
1026 }
1027 });
Joe Onorato36115782010-06-17 13:28:48 -04001028 }
1029 mHandler.post(new Runnable() {
1030 public void run() {
1031 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1032 if (callbacks != null) {
1033 callbacks.bindFolders(mFolders);
1034 }
1035 }
1036 });
1037 // Wait until the queue goes empty.
1038 mHandler.post(new Runnable() {
1039 public void run() {
1040 if (DEBUG_LOADERS) {
1041 Log.d(TAG, "Going to start binding widgets soon.");
1042 }
1043 }
1044 });
1045 // Bind the widgets, one at a time.
1046 // WARNING: this is calling into the workspace from the background thread,
1047 // but since getCurrentScreen() just returns the int, we should be okay. This
1048 // is just a hint for the order, and if it's wrong, we'll be okay.
1049 // TODO: instead, we should have that push the current screen into here.
1050 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1051 N = mAppWidgets.size();
1052 // once for the current screen
1053 for (int i=0; i<N; i++) {
1054 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1055 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056 mHandler.post(new Runnable() {
1057 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001058 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001060 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061 }
1062 }
1063 });
1064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 }
Joe Onorato36115782010-06-17 13:28:48 -04001066 // once for the other screens
1067 for (int i=0; i<N; i++) {
1068 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1069 if (widget.screen != currentScreen) {
1070 mHandler.post(new Runnable() {
1071 public void run() {
1072 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1073 if (callbacks != null) {
1074 callbacks.bindAppWidget(widget);
1075 }
1076 }
1077 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001078 }
1079 }
Joe Onorato36115782010-06-17 13:28:48 -04001080 // Tell the workspace that we're done.
1081 mHandler.post(new Runnable() {
1082 public void run() {
1083 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1084 if (callbacks != null) {
1085 callbacks.finishBindingItems();
1086 }
1087 }
1088 });
1089 // If we're profiling, this is the last thing in the queue.
1090 mHandler.post(new Runnable() {
1091 public void run() {
1092 if (DEBUG_LOADERS) {
1093 Log.d(TAG, "bound workspace in "
1094 + (SystemClock.uptimeMillis()-t) + "ms");
1095 }
1096 }
1097 });
1098 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001099
Joe Onorato36115782010-06-17 13:28:48 -04001100 private void loadAndBindAllApps() {
1101 if (DEBUG_LOADERS) {
1102 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1103 }
1104 if (!mAllAppsLoaded) {
1105 loadAllAppsByBatch();
1106 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001107 return;
1108 }
Joe Onorato36115782010-06-17 13:28:48 -04001109 mAllAppsLoaded = true;
1110 } else {
1111 onlyBindAllApps();
1112 }
1113 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001114
Joe Onorato36115782010-06-17 13:28:48 -04001115 private void onlyBindAllApps() {
1116 final Callbacks oldCallbacks = mCallbacks.get();
1117 if (oldCallbacks == null) {
1118 // This launcher has exited and nobody bothered to tell us. Just bail.
1119 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1120 return;
1121 }
1122
1123 // shallow copy
1124 final ArrayList<ApplicationInfo> list
1125 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1126 mHandler.post(new Runnable() {
1127 public void run() {
1128 final long t = SystemClock.uptimeMillis();
1129 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1130 if (callbacks != null) {
1131 callbacks.bindAllApplications(list);
1132 }
1133 if (DEBUG_LOADERS) {
1134 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1135 + (SystemClock.uptimeMillis()-t) + "ms");
1136 }
1137 }
1138 });
1139
1140 }
1141
1142 private void loadAllAppsByBatch() {
1143 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1144
1145 // Don't use these two variables in any of the callback runnables.
1146 // Otherwise we hold a reference to them.
1147 final Callbacks oldCallbacks = mCallbacks.get();
1148 if (oldCallbacks == null) {
1149 // This launcher has exited and nobody bothered to tell us. Just bail.
1150 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1151 return;
1152 }
1153
1154 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1155 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1156
1157 final PackageManager packageManager = mContext.getPackageManager();
1158 List<ResolveInfo> apps = null;
1159
1160 int N = Integer.MAX_VALUE;
1161
1162 int startIndex;
1163 int i=0;
1164 int batchSize = -1;
1165 while (i < N && !mStopped) {
1166 if (i == 0) {
1167 mAllAppsList.clear();
1168 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1169 apps = packageManager.queryIntentActivities(mainIntent, 0);
1170 if (DEBUG_LOADERS) {
1171 Log.d(TAG, "queryIntentActivities took "
1172 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1173 }
1174 if (apps == null) {
1175 return;
1176 }
1177 N = apps.size();
1178 if (DEBUG_LOADERS) {
1179 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1180 }
1181 if (N == 0) {
1182 // There are no apps?!?
1183 return;
1184 }
1185 if (mBatchSize == 0) {
1186 batchSize = N;
1187 } else {
1188 batchSize = mBatchSize;
1189 }
1190
1191 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1192 Collections.sort(apps,
1193 new ResolveInfo.DisplayNameComparator(packageManager));
1194 if (DEBUG_LOADERS) {
1195 Log.d(TAG, "sort took "
1196 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1197 }
1198 }
1199
1200 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1201
1202 startIndex = i;
1203 for (int j=0; i<N && j<batchSize; j++) {
1204 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001205 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001206 i++;
1207 }
1208
1209 final boolean first = i <= batchSize;
1210 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1211 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1212 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1213
Joe Onoratocc67f472010-06-08 10:54:30 -07001214 mHandler.post(new Runnable() {
1215 public void run() {
1216 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001217 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001218 if (first) {
1219 callbacks.bindAllApplications(added);
1220 } else {
1221 callbacks.bindAppsAdded(added);
1222 }
1223 if (DEBUG_LOADERS) {
1224 Log.d(TAG, "bound " + added.size() + " apps in "
1225 + (SystemClock.uptimeMillis() - t) + "ms");
1226 }
1227 } else {
1228 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001229 }
1230 }
1231 });
1232
Daniel Sandlerdca66122010-04-13 16:23:58 -04001233 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001234 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1235 + (SystemClock.uptimeMillis()-t2) + "ms");
1236 }
1237
1238 if (mAllAppsLoadDelay > 0 && i < N) {
1239 try {
1240 if (DEBUG_LOADERS) {
1241 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1242 }
1243 Thread.sleep(mAllAppsLoadDelay);
1244 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245 }
1246 }
1247
Joe Onorato36115782010-06-17 13:28:48 -04001248 if (DEBUG_LOADERS) {
1249 Log.d(TAG, "cached all " + N + " apps in "
1250 + (SystemClock.uptimeMillis()-t) + "ms"
1251 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001252 }
1253 }
1254
1255 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001256 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1257 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1258 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1259 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1260 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1261 }
1262 }
1263
1264 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001265 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001266 }
1267
1268 private class PackageUpdatedTask implements Runnable {
1269 int mOp;
1270 String[] mPackages;
1271
1272 public static final int OP_NONE = 0;
1273 public static final int OP_ADD = 1;
1274 public static final int OP_UPDATE = 2;
1275 public static final int OP_REMOVE = 3; // uninstlled
1276 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1277
1278
1279 public PackageUpdatedTask(int op, String[] packages) {
1280 mOp = op;
1281 mPackages = packages;
1282 }
1283
1284 public void run() {
1285 final Context context = mApp;
1286
1287 final String[] packages = mPackages;
1288 final int N = packages.length;
1289 switch (mOp) {
1290 case OP_ADD:
1291 for (int i=0; i<N; i++) {
1292 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1293 mAllAppsList.addPackage(context, packages[i]);
1294 }
1295 break;
1296 case OP_UPDATE:
1297 for (int i=0; i<N; i++) {
1298 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1299 mAllAppsList.updatePackage(context, packages[i]);
1300 }
1301 break;
1302 case OP_REMOVE:
1303 case OP_UNAVAILABLE:
1304 for (int i=0; i<N; i++) {
1305 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1306 mAllAppsList.removePackage(packages[i]);
1307 }
1308 break;
1309 }
1310
1311 ArrayList<ApplicationInfo> added = null;
1312 ArrayList<ApplicationInfo> removed = null;
1313 ArrayList<ApplicationInfo> modified = null;
1314
1315 if (mAllAppsList.added.size() > 0) {
1316 added = mAllAppsList.added;
1317 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1318 }
1319 if (mAllAppsList.removed.size() > 0) {
1320 removed = mAllAppsList.removed;
1321 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1322 for (ApplicationInfo info: removed) {
1323 mIconCache.remove(info.intent.getComponent());
1324 }
1325 }
1326 if (mAllAppsList.modified.size() > 0) {
1327 modified = mAllAppsList.modified;
1328 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1329 }
1330
1331 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1332 if (callbacks == null) {
1333 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1334 return;
1335 }
1336
1337 if (added != null) {
1338 final ArrayList<ApplicationInfo> addedFinal = added;
1339 mHandler.post(new Runnable() {
1340 public void run() {
1341 if (callbacks == mCallbacks.get()) {
1342 callbacks.bindAppsAdded(addedFinal);
1343 }
1344 }
1345 });
1346 }
1347 if (modified != null) {
1348 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1349 mHandler.post(new Runnable() {
1350 public void run() {
1351 if (callbacks == mCallbacks.get()) {
1352 callbacks.bindAppsUpdated(modifiedFinal);
1353 }
1354 }
1355 });
1356 }
1357 if (removed != null) {
1358 final boolean permanent = mOp != OP_UNAVAILABLE;
1359 final ArrayList<ApplicationInfo> removedFinal = removed;
1360 mHandler.post(new Runnable() {
1361 public void run() {
1362 if (callbacks == mCallbacks.get()) {
1363 callbacks.bindAppsRemoved(removedFinal, permanent);
1364 }
1365 }
1366 });
Joe Onoratobe386092009-11-17 17:32:16 -08001367 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001368
1369 mHandler.post(new Runnable() {
1370 @Override
1371 public void run() {
1372 if (callbacks == mCallbacks.get()) {
1373 callbacks.bindPackagesUpdated();
1374 }
1375 }
1376 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377 }
1378 }
1379
1380 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001381 * This is called from the code that adds shortcuts from the intent receiver. This
1382 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 */
Joe Onorato56d82912010-03-07 14:32:10 -05001384 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001385 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001386 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387
Joe Onorato56d82912010-03-07 14:32:10 -05001388 /**
1389 * Make an ShortcutInfo object for a shortcut that is an application.
1390 *
1391 * If c is not null, then it will be used to fill in missing data like the title and icon.
1392 */
1393 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1394 Cursor c, int iconIndex, int titleIndex) {
1395 Bitmap icon = null;
1396 final ShortcutInfo info = new ShortcutInfo();
1397
1398 ComponentName componentName = intent.getComponent();
1399 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001400 return null;
1401 }
1402
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001403 // TODO: See if the PackageManager knows about this case. If it doesn't
1404 // then return null & delete this.
1405
Joe Onorato56d82912010-03-07 14:32:10 -05001406 // the resource -- This may implicitly give us back the fallback icon,
1407 // but don't worry about that. All we're doing with usingFallbackIcon is
1408 // to avoid saving lots of copies of that in the database, and most apps
1409 // have icons anyway.
1410 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1411 if (resolveInfo != null) {
1412 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001413 }
Joe Onorato56d82912010-03-07 14:32:10 -05001414 // the db
1415 if (icon == null) {
1416 if (c != null) {
1417 icon = getIconFromCursor(c, iconIndex);
1418 }
1419 }
1420 // the fallback icon
1421 if (icon == null) {
1422 icon = getFallbackIcon();
1423 info.usingFallbackIcon = true;
1424 }
1425 info.setIcon(icon);
1426
1427 // from the resource
1428 if (resolveInfo != null) {
1429 info.title = resolveInfo.activityInfo.loadLabel(manager);
1430 }
1431 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001433 if (c != null) {
1434 info.title = c.getString(titleIndex);
1435 }
1436 }
1437 // fall back to the class name of the activity
1438 if (info.title == null) {
1439 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001440 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1442 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001446 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001448 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001449 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1450 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451
Joe Onorato56d82912010-03-07 14:32:10 -05001452 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001453 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001454 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001456 // TODO: If there's an explicit component and we can't install that, delete it.
1457
Joe Onorato56d82912010-03-07 14:32:10 -05001458 info.title = c.getString(titleIndex);
1459
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 int iconType = c.getInt(iconTypeIndex);
1461 switch (iconType) {
1462 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1463 String packageName = c.getString(iconPackageIndex);
1464 String resourceName = c.getString(iconResourceIndex);
1465 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001466 info.customIcon = false;
1467 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001470 if (resources != null) {
1471 final int id = resources.getIdentifier(resourceName, null, null);
1472 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1473 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001475 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
Joe Onorato56d82912010-03-07 14:32:10 -05001477 // the db
1478 if (icon == null) {
1479 icon = getIconFromCursor(c, iconIndex);
1480 }
1481 // the fallback icon
1482 if (icon == null) {
1483 icon = getFallbackIcon();
1484 info.usingFallbackIcon = true;
1485 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001486 break;
1487 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001488 icon = getIconFromCursor(c, iconIndex);
1489 if (icon == null) {
1490 icon = getFallbackIcon();
1491 info.customIcon = false;
1492 info.usingFallbackIcon = true;
1493 } else {
1494 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496 break;
1497 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001498 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001499 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500 info.customIcon = false;
1501 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001503 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504 return info;
1505 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506
Joe Onorato56d82912010-03-07 14:32:10 -05001507 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1508 if (false) {
1509 Log.d(TAG, "getIconFromCursor app="
1510 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1511 }
1512 byte[] data = c.getBlob(iconIndex);
1513 try {
1514 return BitmapFactory.decodeByteArray(data, 0, data.length);
1515 } catch (Exception e) {
1516 return null;
1517 }
1518 }
1519
Joe Onorato0589f0f2010-02-08 13:44:00 -08001520 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001521 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001522
Winson Chunga9abd0e2010-10-27 17:18:37 -07001523 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001524 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001525 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001526
1527 return info;
1528 }
1529
Winson Chunga9abd0e2010-10-27 17:18:37 -07001530 /**
1531 * Ensures that a given shortcut intent actually has all the fields that we need to create a
1532 * proper ShortcutInfo.
1533 */
1534 boolean validateShortcutIntent(Intent data) {
1535 // We don't require Intent.EXTRA_SHORTCUT_ICON, since we can pull a default fallback icon
1536 return InstallShortcutReceiver.ACTION_INSTALL_SHORTCUT.equals(data.getAction()) &&
Winson Chung55cef262010-10-28 14:14:18 -07001537 (data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME) != null) &&
1538 (data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT) != null);
1539 }
1540
1541 /**
1542 * Ensures that a given widget intent actually has all the fields that we need to create a
1543 * proper widget.
1544 */
1545 boolean validateWidgetIntent(Intent data) {
1546 // We don't require Intent.EXTRA_APPWIDGET_CONFIGURATION_DATA, since that will just be
1547 // forwarded onto the widget's configuration activity if it exists
1548 return InstallWidgetReceiver.ACTION_INSTALL_WIDGET.equals(data.getAction()) &&
1549 (data.getStringExtra(InstallWidgetReceiver.EXTRA_APPWIDGET_COMPONENT) != null);
1550 }
1551
1552 /**
1553 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1554 */
1555 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1556 ComponentName component) {
1557 List<AppWidgetProviderInfo> widgets =
1558 AppWidgetManager.getInstance(context).getInstalledProviders();
1559 for (AppWidgetProviderInfo info : widgets) {
1560 if (info.provider.equals(component)) {
1561 return info;
1562 }
1563 }
1564 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001565 }
1566
1567 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001568 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1569 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1570 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1571
1572 Bitmap icon = null;
1573 boolean filtered = false;
1574 boolean customIcon = false;
1575 ShortcutIconResource iconResource = null;
1576
1577 if (bitmap != null && bitmap instanceof Bitmap) {
1578 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1579 filtered = true;
1580 customIcon = true;
1581 } else {
1582 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1583 if (extra != null && extra instanceof ShortcutIconResource) {
1584 try {
1585 iconResource = (ShortcutIconResource) extra;
1586 final PackageManager packageManager = context.getPackageManager();
1587 Resources resources = packageManager.getResourcesForApplication(
1588 iconResource.packageName);
1589 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1590 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1591 } catch (Exception e) {
1592 Log.w(TAG, "Could not load shortcut icon: " + extra);
1593 }
1594 }
1595 }
1596
Joe Onorato0589f0f2010-02-08 13:44:00 -08001597 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001598
1599 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001600 if (fallbackIcon != null) {
1601 icon = fallbackIcon;
1602 } else {
1603 icon = getFallbackIcon();
1604 info.usingFallbackIcon = true;
1605 }
Joe Onorato56d82912010-03-07 14:32:10 -05001606 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001607 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001608
Joe Onorato0589f0f2010-02-08 13:44:00 -08001609 info.title = name;
1610 info.intent = intent;
1611 info.customIcon = customIcon;
1612 info.iconResource = iconResource;
1613
1614 return info;
1615 }
1616
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1618 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1619
1620 int iconType = c.getInt(iconTypeIndex);
1621 switch (iconType) {
1622 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1623 String packageName = c.getString(iconPackageIndex);
1624 String resourceName = c.getString(iconResourceIndex);
1625 PackageManager packageManager = context.getPackageManager();
1626 try {
1627 Resources resources = packageManager.getResourcesForApplication(packageName);
1628 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001629 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1630 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001632 liveFolderInfo.icon = Utilities.createIconBitmap(
1633 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1634 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635 }
1636 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1637 liveFolderInfo.iconResource.packageName = packageName;
1638 liveFolderInfo.iconResource.resourceName = resourceName;
1639 break;
1640 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001641 liveFolderInfo.icon = Utilities.createIconBitmap(
1642 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1643 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001644 }
1645 }
1646
Joe Onorato56d82912010-03-07 14:32:10 -05001647 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1648 // If this icon doesn't have a custom icon, check to see
1649 // what's stored in the DB, and if it doesn't match what
1650 // we're going to show, store what we are going to show back
1651 // into the DB. We do this so when we're loading, if the
1652 // package manager can't find an icon (for example because
1653 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001654 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001655 boolean needSave;
1656 byte[] data = c.getBlob(iconIndex);
1657 try {
1658 if (data != null) {
1659 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1660 Bitmap loaded = info.getIcon(mIconCache);
1661 needSave = !saved.sameAs(loaded);
1662 } else {
1663 needSave = true;
1664 }
1665 } catch (Exception e) {
1666 needSave = true;
1667 }
1668 if (needSave) {
1669 Log.d(TAG, "going to save icon bitmap for info=" + info);
1670 // This is slower than is ideal, but this only happens either
1671 // after the froyo OTA or when the app is updated with a new
1672 // icon.
1673 updateItemInDatabase(context, info);
1674 }
1675 }
1676 }
1677
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678 /**
1679 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1680 * or make a new one.
1681 */
1682 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1683 // See if a placeholder was created for us already
1684 FolderInfo folderInfo = folders.get(id);
1685 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1686 // No placeholder -- create a new instance
1687 folderInfo = new UserFolderInfo();
1688 folders.put(id, folderInfo);
1689 }
1690 return (UserFolderInfo) folderInfo;
1691 }
1692
1693 /**
1694 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1695 * new one.
1696 */
1697 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1698 // See if a placeholder was created for us already
1699 FolderInfo folderInfo = folders.get(id);
1700 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1701 // No placeholder -- create a new instance
1702 folderInfo = new LiveFolderInfo();
1703 folders.put(id, folderInfo);
1704 }
1705 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
1707
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1709 String label = activityInfo.loadLabel(manager).toString();
1710 if (label == null) {
1711 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1712 if (label == null) {
1713 label = activityInfo.name;
1714 }
1715 }
1716 return label;
1717 }
1718
Joe Onorato9c1289c2009-08-17 11:03:03 -04001719 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001720 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001721 = new Comparator<ApplicationInfo>() {
1722 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1723 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001725 };
Joe Onoratobe386092009-11-17 17:32:16 -08001726
1727 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001728 Log.d(TAG, "mCallbacks=" + mCallbacks);
1729 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1730 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1731 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1732 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001733 Log.d(TAG, "mItems size=" + mItems.size());
1734 if (mLoaderTask != null) {
1735 mLoaderTask.dumpState();
1736 } else {
1737 Log.d(TAG, "mLoaderTask=null");
1738 }
Joe Onoratobe386092009-11-17 17:32:16 -08001739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740}