blob: 6c3ddd22fe98bd9d38225d8d4d0bc68c4732897f [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chungaafa03c2010-06-11 17:34:16 -070019import java.lang.ref.WeakReference;
20import java.net.URISyntaxException;
21import java.text.Collator;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.List;
27
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040030import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080032import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ContentResolver;
34import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.ActivityInfo;
39import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080040import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.pm.ResolveInfo;
42import android.content.res.Resources;
43import android.database.Cursor;
44import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040047import android.os.Handler;
48import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040052import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
Romain Guyedcce092010-03-04 13:03:17 -080055import com.android.launcher.R;
56
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * Maintains in-memory state of the Launcher. It is expected that there should be only one
59 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070060 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040062public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080063 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040064 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070065
Joe Onorato36115782010-06-17 13:28:48 -040066 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onoratod65d08e2010-04-20 15:43:37 -040067 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040068 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040069
Joe Onoratof99f8c12009-10-31 17:27:36 -040070 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 private final Object mLock = new Object();
72 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040073 private HandlerThread mWorkerThread;
74 private Handler mWorker;
75 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Joe Onoratocc67f472010-06-08 10:54:30 -070077 // We start off with everything not loaded. After that, we assume that
78 // our monitoring of the package manager provides all updates and we never
79 // need to do a requery. These are only ever touched from the loader thread.
80 private boolean mWorkspaceLoaded;
81 private boolean mAllAppsLoaded;
82
Joe Onorato9c1289c2009-08-17 11:03:03 -040083 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Joe Onorato36115782010-06-17 13:28:48 -040085 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -080086 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -040087 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
88 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
89 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -080090
91 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Adam Cohend22015c2010-07-26 22:02:18 -070093 private static int mCellCountX;
94 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -070095
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 public interface Callbacks {
97 public int getCurrentWorkspaceScreen();
98 public void startBinding();
99 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500100 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400101 public void finishBindingItems();
102 public void bindAppWidget(LauncherAppWidgetInfo info);
103 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500104 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
105 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400106 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Daniel Sandler843e8602010-06-07 14:59:01 -0400107 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Joe Onorato0589f0f2010-02-08 13:44:00 -0800110 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onoratof99f8c12009-10-31 17:27:36 -0400111 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800112 mAllAppsList = new AllAppsList(iconCache);
113 mIconCache = iconCache;
114
115 mDefaultIcon = Utilities.createIconBitmap(
116 app.getPackageManager().getDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400117
118 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400119
120 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato36115782010-06-17 13:28:48 -0400121
122 mWorkerThread = new HandlerThread("launcher-loader");
123 mWorkerThread.start();
124 mWorker = new Handler(mWorkerThread.getLooper());
Joe Onorato0589f0f2010-02-08 13:44:00 -0800125 }
126
Joe Onorato56d82912010-03-07 14:32:10 -0500127 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800128 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Joe Onorato9c1289c2009-08-17 11:03:03 -0400131 /**
132 * Adds an item to the DB if it was not created previously, or move it to a new
133 * <container, screen, cellX, cellY>
134 */
135 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
136 int screen, int cellX, int cellY) {
137 if (item.container == ItemInfo.NO_ID) {
138 // From all apps
139 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
140 } else {
141 // From somewhere else
142 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 }
144 }
145
146 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400147 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700148 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
150 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700151
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 item.container = container;
153 item.screen = screen;
154 item.cellX = cellX;
155 item.cellY = cellY;
156
157 final ContentValues values = new ContentValues();
158 final ContentResolver cr = context.getContentResolver();
159
160 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700161 values.put(LauncherSettings.Favorites.CELLX, cellX);
162 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400163 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
164
165 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700166 }
167
168 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 * Returns true if the shortcuts already exists in the database.
170 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400172 static boolean shortcutExists(Context context, String title, Intent intent) {
173 final ContentResolver cr = context.getContentResolver();
174 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
175 new String[] { "title", "intent" }, "title=? and intent=?",
176 new String[] { title, intent.toUri(0) }, null);
177 boolean result = false;
178 try {
179 result = c.moveToFirst();
180 } finally {
181 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700184 }
185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700187 * Returns an ItemInfo array containing all the items in the LauncherModel.
188 * The ItemInfo.id is not set through this function.
189 */
190 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
191 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
192 final ContentResolver cr = context.getContentResolver();
193 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
194 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
195 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
196 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
197
198 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
199 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
200 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
201 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
202 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
203 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
204 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
205
206 try {
207 while (c.moveToNext()) {
208 ItemInfo item = new ItemInfo();
209 item.cellX = c.getInt(cellXIndex);
210 item.cellY = c.getInt(cellYIndex);
211 item.spanX = c.getInt(spanXIndex);
212 item.spanY = c.getInt(spanYIndex);
213 item.container = c.getInt(containerIndex);
214 item.itemType = c.getInt(itemTypeIndex);
215 item.screen = c.getInt(screenIndex);
216
217 items.add(item);
218 }
219 } catch (Exception e) {
220 items.clear();
221 } finally {
222 c.close();
223 }
224
225 return items;
226 }
227
228 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
230 */
231 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
232 final ContentResolver cr = context.getContentResolver();
233 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
234 "_id=? and (itemType=? or itemType=?)",
235 new String[] { String.valueOf(id),
236 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
237 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700238
Joe Onorato9c1289c2009-08-17 11:03:03 -0400239 try {
240 if (c.moveToFirst()) {
241 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
242 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
243 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
244 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
245 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
246 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248 FolderInfo folderInfo = null;
249 switch (c.getInt(itemTypeIndex)) {
250 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
251 folderInfo = findOrMakeUserFolder(folderList, id);
252 break;
253 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
254 folderInfo = findOrMakeLiveFolder(folderList, id);
255 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700256 }
257
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258 folderInfo.title = c.getString(titleIndex);
259 folderInfo.id = id;
260 folderInfo.container = c.getInt(containerIndex);
261 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700262 folderInfo.cellX = c.getInt(cellXIndex);
263 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400264
265 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700266 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267 } finally {
268 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700269 }
270
271 return null;
272 }
273
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 /**
275 * Add an item to the database in a specified container. Sets the container, screen, cellX and
276 * cellY fields of the item. Also assigns an ID to the item.
277 */
278 static void addItemToDatabase(Context context, ItemInfo item, long container,
279 int screen, int cellX, int cellY, boolean notify) {
280 item.container = container;
281 item.screen = screen;
282 item.cellX = cellX;
283 item.cellY = cellY;
284
285 final ContentValues values = new ContentValues();
286 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 item.onAddToDatabase(values);
288
Adam Cohend22015c2010-07-26 22:02:18 -0700289 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
292 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
293
294 if (result != null) {
295 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700296 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700297 }
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700300 * Creates a new unique child id, for a given cell span across all layouts.
301 */
Adam Cohend22015c2010-07-26 22:02:18 -0700302 static int getCellLayoutChildId(int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
303 return ((cellId & 0xFF) << 16) | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700304 }
305
Adam Cohend22015c2010-07-26 22:02:18 -0700306 static int getCellCountX() {
307 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700308 }
309
Adam Cohend22015c2010-07-26 22:02:18 -0700310 static int getCellCountY() {
311 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700312 }
313
314 /**
315 * Updates the model orientation helper to take into account the current layout dimensions
316 * when performing local/canonical coordinate transformations.
317 */
318 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700319 mCellCountX = shortAxisCellCount;
320 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700321 }
322
323 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400324 * Update an item to the database in a specified container.
325 */
326 static void updateItemInDatabase(Context context, ItemInfo item) {
327 final ContentValues values = new ContentValues();
328 final ContentResolver cr = context.getContentResolver();
329
330 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700331 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700332
Joe Onorato9c1289c2009-08-17 11:03:03 -0400333 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
334 }
335
336 /**
337 * Removes the specified item from the database
338 * @param context
339 * @param item
340 */
341 static void deleteItemFromDatabase(Context context, ItemInfo item) {
342 final ContentResolver cr = context.getContentResolver();
343
344 cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null);
345 }
346
347 /**
348 * Remove the contents of the specified folder from the database
349 */
350 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
351 final ContentResolver cr = context.getContentResolver();
352
353 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
354 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
355 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
356 }
357
358 /**
359 * Set this as the current Launcher activity object for the loader.
360 */
361 public void initialize(Callbacks callbacks) {
362 synchronized (mLock) {
363 mCallbacks = new WeakReference<Callbacks>(callbacks);
364 }
365 }
366
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700367 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
369 * ACTION_PACKAGE_CHANGED.
370 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400371 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400372 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700373
Joe Onorato36115782010-06-17 13:28:48 -0400374 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400375
Joe Onorato36115782010-06-17 13:28:48 -0400376 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
377 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
378 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
379 final String packageName = intent.getData().getSchemeSpecificPart();
380 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381
Joe Onorato36115782010-06-17 13:28:48 -0400382 int op = PackageUpdatedTask.OP_NONE;
383
384 if (packageName == null || packageName.length() == 0) {
385 // they sent us a bad intent
386 return;
387 }
388
389 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
390 op = PackageUpdatedTask.OP_UPDATE;
391 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
392 if (!replacing) {
393 op = PackageUpdatedTask.OP_REMOVE;
394 }
395 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
396 // later, we will update the package at this time
397 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
398 if (!replacing) {
399 op = PackageUpdatedTask.OP_ADD;
400 } else {
401 op = PackageUpdatedTask.OP_UPDATE;
402 }
403 }
404
405 if (op != PackageUpdatedTask.OP_NONE) {
406 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
407 }
408
409 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
410 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
411 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
412
413 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
414 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
415 enqueuePackageUpdated(new PackageUpdatedTask(
416 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onorato790c2d92010-06-11 00:14:11 -0700417 }
Joe Onorato36115782010-06-17 13:28:48 -0400418 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400419
Joe Onorato36115782010-06-17 13:28:48 -0400420 public void startLoader(Context context, boolean isLaunching) {
421 synchronized (mLock) {
422 if (DEBUG_LOADERS) {
423 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425
Joe Onorato36115782010-06-17 13:28:48 -0400426 // Don't bother to start the thread if we know it's not going to do anything
427 if (mCallbacks != null && mCallbacks.get() != null) {
428 // If there is already one running, tell it to stop.
429 LoaderTask oldTask = mLoaderTask;
430 if (oldTask != null) {
431 if (oldTask.isLaunching()) {
432 // don't downgrade isLaunching if we're already running
433 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500434 }
Joe Onorato36115782010-06-17 13:28:48 -0400435 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500436 }
Joe Onorato36115782010-06-17 13:28:48 -0400437 mLoaderTask = new LoaderTask(context, isLaunching);
438 mWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 }
441 }
442
Joe Onorato36115782010-06-17 13:28:48 -0400443 public void stopLoader() {
444 synchronized (mLock) {
445 if (mLoaderTask != null) {
446 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447 }
448 }
Joe Onorato36115782010-06-17 13:28:48 -0400449 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450
Joe Onorato36115782010-06-17 13:28:48 -0400451 /**
452 * Runnable for the thread that loads the contents of the launcher:
453 * - workspace icons
454 * - widgets
455 * - all apps icons
456 */
457 private class LoaderTask implements Runnable {
458 private Context mContext;
459 private Thread mWaitThread;
460 private boolean mIsLaunching;
461 private boolean mStopped;
462 private boolean mLoadAndBindStepFinished;
463
464 LoaderTask(Context context, boolean isLaunching) {
465 mContext = context;
466 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400467 }
468
Joe Onorato36115782010-06-17 13:28:48 -0400469 boolean isLaunching() {
470 return mIsLaunching;
471 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400472
Joe Onorato36115782010-06-17 13:28:48 -0400473 private void loadAndBindWorkspace() {
474 // Load the workspace
475
476 // For now, just always reload the workspace. It's ~100 ms vs. the
477 // binding which takes many hundreds of ms.
478 // We can reconsider.
479 if (DEBUG_LOADERS) {
480 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400481 }
Joe Onorato36115782010-06-17 13:28:48 -0400482 if (true || !mWorkspaceLoaded) {
483 loadWorkspace();
484 if (mStopped) {
485 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400486 }
Joe Onorato36115782010-06-17 13:28:48 -0400487 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400488 }
489
Joe Onorato36115782010-06-17 13:28:48 -0400490 // Bind the workspace
491 bindWorkspace();
492 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400493
Joe Onorato36115782010-06-17 13:28:48 -0400494 private void waitForIdle() {
495 // Wait until the either we're stopped or the other threads are done.
496 // This way we don't start loading all apps until the workspace has settled
497 // down.
498 synchronized (LoaderTask.this) {
499 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700500
Joe Onorato36115782010-06-17 13:28:48 -0400501 mHandler.postIdle(new Runnable() {
502 public void run() {
503 synchronized (LoaderTask.this) {
504 mLoadAndBindStepFinished = true;
505 if (DEBUG_LOADERS) {
506 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400507 }
Joe Onorato36115782010-06-17 13:28:48 -0400508 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400509 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400510 }
Joe Onorato36115782010-06-17 13:28:48 -0400511 });
512
513 while (!mStopped && !mLoadAndBindStepFinished) {
514 try {
515 this.wait();
516 } catch (InterruptedException ex) {
517 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400518 }
519 }
Joe Onorato36115782010-06-17 13:28:48 -0400520 if (DEBUG_LOADERS) {
521 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700522 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400523 + "ms for previous step to finish binding");
524 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400525 }
Joe Onorato36115782010-06-17 13:28:48 -0400526 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400527
Joe Onorato36115782010-06-17 13:28:48 -0400528 public void run() {
529 // Optimize for end-user experience: if the Launcher is up and // running with the
530 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
531 // workspace first (default).
532 final Callbacks cbk = mCallbacks.get();
533 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400534
Joe Onorato36115782010-06-17 13:28:48 -0400535 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400536 // Elevate priority when Home launches for the first time to avoid
537 // starving at boot time. Staring at a blank home is not cool.
538 synchronized (mLock) {
539 android.os.Process.setThreadPriority(mIsLaunching
540 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
541 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400542 if (loadWorkspaceFirst) {
543 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
544 loadAndBindWorkspace();
545 } else {
546 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700547 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400548 }
549
Joe Onorato36115782010-06-17 13:28:48 -0400550 if (mStopped) {
551 break keep_running;
552 }
553
554 // Whew! Hard work done. Slow us down, and wait until the UI thread has
555 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400556 synchronized (mLock) {
557 if (mIsLaunching) {
558 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
559 }
560 }
Joe Onorato36115782010-06-17 13:28:48 -0400561 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400562
563 // second step
564 if (loadWorkspaceFirst) {
565 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700566 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400567 } else {
568 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
569 loadAndBindWorkspace();
570 }
Joe Onorato36115782010-06-17 13:28:48 -0400571 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572
Joe Onorato36115782010-06-17 13:28:48 -0400573 // Clear out this reference, otherwise we end up holding it until all of the
574 // callback runnables are done.
575 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576
Joe Onorato36115782010-06-17 13:28:48 -0400577 synchronized (mLock) {
578 // If we are still the last one to be scheduled, remove ourselves.
579 if (mLoaderTask == this) {
580 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400581 }
Joe Onorato36115782010-06-17 13:28:48 -0400582 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700583
Joe Onorato36115782010-06-17 13:28:48 -0400584 // Trigger a gc to try to clean up after the stuff is done, since the
585 // renderscript allocations aren't charged to the java heap.
586 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700587 mHandler.post(new Runnable() {
588 public void run() {
589 System.gc();
590 }
591 });
Joe Onorato36115782010-06-17 13:28:48 -0400592 } else {
593 mHandler.postIdle(new Runnable() {
594 public void run() {
595 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400596 }
Joe Onorato36115782010-06-17 13:28:48 -0400597 });
598 }
599 }
600
601 public void stopLocked() {
602 synchronized (LoaderTask.this) {
603 mStopped = true;
604 this.notify();
605 }
606 }
607
608 /**
609 * Gets the callbacks object. If we've been stopped, or if the launcher object
610 * has somehow been garbage collected, return null instead. Pass in the Callbacks
611 * object that was around when the deferred message was scheduled, and if there's
612 * a new Callbacks object around then also return null. This will save us from
613 * calling onto it with data that will be ignored.
614 */
615 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
616 synchronized (mLock) {
617 if (mStopped) {
618 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400619 }
Joe Onorato36115782010-06-17 13:28:48 -0400620
621 if (mCallbacks == null) {
622 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400623 }
Joe Onorato36115782010-06-17 13:28:48 -0400624
625 final Callbacks callbacks = mCallbacks.get();
626 if (callbacks != oldCallbacks) {
627 return null;
628 }
629 if (callbacks == null) {
630 Log.w(TAG, "no mCallbacks");
631 return null;
632 }
633
634 return callbacks;
635 }
636 }
637
638 // check & update map of what's occupied; used to discard overlapping/invalid items
639 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
640 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400641 return true;
642 }
Joe Onorato36115782010-06-17 13:28:48 -0400643 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
644 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
645 if (occupied[item.screen][x][y] != null) {
646 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700647 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400648 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400650 + occupied[item.screen][x][y]);
651 return false;
652 }
653 }
654 }
655 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
656 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
657 occupied[item.screen][x][y] = item;
658 }
659 }
660 return true;
661 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662
Joe Onorato36115782010-06-17 13:28:48 -0400663 private void loadWorkspace() {
664 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665
Joe Onorato36115782010-06-17 13:28:48 -0400666 final Context context = mContext;
667 final ContentResolver contentResolver = context.getContentResolver();
668 final PackageManager manager = context.getPackageManager();
669 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
670 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400671
Joe Onorato36115782010-06-17 13:28:48 -0400672 mItems.clear();
673 mAppWidgets.clear();
674 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800675
Joe Onorato36115782010-06-17 13:28:48 -0400676 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677
Joe Onorato36115782010-06-17 13:28:48 -0400678 final Cursor c = contentResolver.query(
679 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400680
Adam Cohend22015c2010-07-26 22:02:18 -0700681 final ItemInfo occupied[][][] =
682 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400683
Joe Onorato36115782010-06-17 13:28:48 -0400684 try {
685 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
686 final int intentIndex = c.getColumnIndexOrThrow
687 (LauncherSettings.Favorites.INTENT);
688 final int titleIndex = c.getColumnIndexOrThrow
689 (LauncherSettings.Favorites.TITLE);
690 final int iconTypeIndex = c.getColumnIndexOrThrow(
691 LauncherSettings.Favorites.ICON_TYPE);
692 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
693 final int iconPackageIndex = c.getColumnIndexOrThrow(
694 LauncherSettings.Favorites.ICON_PACKAGE);
695 final int iconResourceIndex = c.getColumnIndexOrThrow(
696 LauncherSettings.Favorites.ICON_RESOURCE);
697 final int containerIndex = c.getColumnIndexOrThrow(
698 LauncherSettings.Favorites.CONTAINER);
699 final int itemTypeIndex = c.getColumnIndexOrThrow(
700 LauncherSettings.Favorites.ITEM_TYPE);
701 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
702 LauncherSettings.Favorites.APPWIDGET_ID);
703 final int screenIndex = c.getColumnIndexOrThrow(
704 LauncherSettings.Favorites.SCREEN);
705 final int cellXIndex = c.getColumnIndexOrThrow
706 (LauncherSettings.Favorites.CELLX);
707 final int cellYIndex = c.getColumnIndexOrThrow
708 (LauncherSettings.Favorites.CELLY);
709 final int spanXIndex = c.getColumnIndexOrThrow
710 (LauncherSettings.Favorites.SPANX);
711 final int spanYIndex = c.getColumnIndexOrThrow(
712 LauncherSettings.Favorites.SPANY);
713 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
714 final int displayModeIndex = c.getColumnIndexOrThrow(
715 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716
Joe Onorato36115782010-06-17 13:28:48 -0400717 ShortcutInfo info;
718 String intentDescription;
719 LauncherAppWidgetInfo appWidgetInfo;
720 int container;
721 long id;
722 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723
Joe Onorato36115782010-06-17 13:28:48 -0400724 while (!mStopped && c.moveToNext()) {
725 try {
726 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727
Joe Onorato36115782010-06-17 13:28:48 -0400728 switch (itemType) {
729 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
730 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
731 intentDescription = c.getString(intentIndex);
732 try {
733 intent = Intent.parseUri(intentDescription, 0);
734 } catch (URISyntaxException e) {
735 continue;
736 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737
Joe Onorato36115782010-06-17 13:28:48 -0400738 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
739 info = getShortcutInfo(manager, intent, context, c, iconIndex,
740 titleIndex);
741 } else {
742 info = getShortcutInfo(c, context, iconTypeIndex,
743 iconPackageIndex, iconResourceIndex, iconIndex,
744 titleIndex);
745 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746
Joe Onorato36115782010-06-17 13:28:48 -0400747 if (info != null) {
748 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749
Joe Onorato36115782010-06-17 13:28:48 -0400750 info.intent = intent;
751 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400753 info.container = container;
754 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700755 info.cellX = c.getInt(cellXIndex);
756 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757
Daniel Sandler8802e962010-05-26 16:28:16 -0400758 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400759 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400760 break;
761 }
762
Joe Onorato9c1289c2009-08-17 11:03:03 -0400763 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400764 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
765 mItems.add(info);
766 break;
767 default:
768 // Item is in a user folder
769 UserFolderInfo folderInfo =
770 findOrMakeUserFolder(mFolders, container);
771 folderInfo.add(info);
772 break;
773 }
774 } else {
775 // Failed to load the shortcut, probably because the
776 // activity manager couldn't resolve it (maybe the app
777 // was uninstalled), or the db row was somehow screwed up.
778 // Delete it.
779 id = c.getLong(idIndex);
780 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
781 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
782 id, false), null, null);
783 }
784 break;
785
786 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
787 id = c.getLong(idIndex);
788 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
789
Winson Chungaafa03c2010-06-11 17:34:16 -0700790 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400791 folderInfo.id = id;
792 container = c.getInt(containerIndex);
793 folderInfo.container = container;
794 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700795 folderInfo.cellX = c.getInt(cellXIndex);
796 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400797
798 // check & update map of what's occupied
799 if (!checkItemPlacement(occupied, folderInfo)) {
800 break;
801 }
Joe Onorato36115782010-06-17 13:28:48 -0400802 switch (container) {
803 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
804 mItems.add(folderInfo);
805 break;
806 }
807
808 mFolders.put(folderInfo.id, folderInfo);
809 break;
810
811 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
812 id = c.getLong(idIndex);
813 Uri uri = Uri.parse(c.getString(uriIndex));
814
815 // Make sure the live folder exists
816 final ProviderInfo providerInfo =
817 context.getPackageManager().resolveContentProvider(
818 uri.getAuthority(), 0);
819
820 if (providerInfo == null && !isSafeMode) {
821 itemsToRemove.add(id);
822 } else {
823 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400824 intentDescription = c.getString(intentIndex);
825 intent = null;
826 if (intentDescription != null) {
827 try {
828 intent = Intent.parseUri(intentDescription, 0);
829 } catch (URISyntaxException e) {
830 // Ignore, a live folder might not have a base intent
831 }
832 }
833
834 liveFolderInfo.title = c.getString(titleIndex);
835 liveFolderInfo.id = id;
836 liveFolderInfo.uri = uri;
837 container = c.getInt(containerIndex);
838 liveFolderInfo.container = container;
839 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700840 liveFolderInfo.cellX = c.getInt(cellXIndex);
841 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400842 liveFolderInfo.baseIntent = intent;
843 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
844
845 // check & update map of what's occupied
846 if (!checkItemPlacement(occupied, liveFolderInfo)) {
847 break;
848 }
849
850 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
851 iconResourceIndex, liveFolderInfo);
852
853 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400855 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856 break;
857 }
Joe Onorato36115782010-06-17 13:28:48 -0400858 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 }
Joe Onorato36115782010-06-17 13:28:48 -0400860 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800861
Joe Onorato36115782010-06-17 13:28:48 -0400862 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
863 // Read all Launcher-specific widget details
864 int appWidgetId = c.getInt(appWidgetIdIndex);
865 id = c.getLong(idIndex);
866
867 final AppWidgetProviderInfo provider =
868 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700869
Joe Onorato36115782010-06-17 13:28:48 -0400870 if (!isSafeMode && (provider == null || provider.provider == null ||
871 provider.provider.getPackageName() == null)) {
872 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
873 + id + " appWidgetId=" + appWidgetId);
874 itemsToRemove.add(id);
875 } else {
876 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
877 appWidgetInfo.id = id;
878 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700879 appWidgetInfo.cellX = c.getInt(cellXIndex);
880 appWidgetInfo.cellY = c.getInt(cellYIndex);
881 appWidgetInfo.spanX = c.getInt(spanXIndex);
882 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400883
884 container = c.getInt(containerIndex);
885 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
886 Log.e(TAG, "Widget found where container "
887 + "!= CONTAINER_DESKTOP -- ignoring!");
888 continue;
889 }
890 appWidgetInfo.container = c.getInt(containerIndex);
891
892 // check & update map of what's occupied
893 if (!checkItemPlacement(occupied, appWidgetInfo)) {
894 break;
895 }
896
897 mAppWidgets.add(appWidgetInfo);
898 }
899 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800900 }
Joe Onorato36115782010-06-17 13:28:48 -0400901 } catch (Exception e) {
902 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800903 }
904 }
Joe Onorato36115782010-06-17 13:28:48 -0400905 } finally {
906 c.close();
907 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800908
Joe Onorato36115782010-06-17 13:28:48 -0400909 if (itemsToRemove.size() > 0) {
910 ContentProviderClient client = contentResolver.acquireContentProviderClient(
911 LauncherSettings.Favorites.CONTENT_URI);
912 // Remove dead items
913 for (long id : itemsToRemove) {
914 if (DEBUG_LOADERS) {
915 Log.d(TAG, "Removed id = " + id);
916 }
917 // Don't notify content observers
918 try {
919 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
920 null, null);
921 } catch (RemoteException e) {
922 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400923 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800924 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 }
926
Joe Onorato36115782010-06-17 13:28:48 -0400927 if (DEBUG_LOADERS) {
928 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
929 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700930 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400931 String line = "";
932 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
933 if (s > 0) {
934 line += " | ";
935 }
Adam Cohend22015c2010-07-26 22:02:18 -0700936 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400937 line += ((occupied[s][x][y] != null) ? "#" : ".");
938 }
939 }
940 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 }
Joe Onorato36115782010-06-17 13:28:48 -0400942 }
943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944
Joe Onorato36115782010-06-17 13:28:48 -0400945 /**
946 * Read everything out of our database.
947 */
948 private void bindWorkspace() {
949 final long t = SystemClock.uptimeMillis();
950
951 // Don't use these two variables in any of the callback runnables.
952 // Otherwise we hold a reference to them.
953 final Callbacks oldCallbacks = mCallbacks.get();
954 if (oldCallbacks == null) {
955 // This launcher has exited and nobody bothered to tell us. Just bail.
956 Log.w(TAG, "LoaderTask running with no launcher");
957 return;
958 }
959
960 int N;
961 // Tell the workspace that we're about to start firing items at it
962 mHandler.post(new Runnable() {
963 public void run() {
964 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
965 if (callbacks != null) {
966 callbacks.startBinding();
967 }
968 }
969 });
970 // Add the items to the workspace.
971 N = mItems.size();
972 for (int i=0; i<N; i+=ITEMS_CHUNK) {
973 final int start = i;
974 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 mHandler.post(new Runnable() {
976 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -0800977 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400979 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 }
981 }
982 });
Joe Onorato36115782010-06-17 13:28:48 -0400983 }
984 mHandler.post(new Runnable() {
985 public void run() {
986 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
987 if (callbacks != null) {
988 callbacks.bindFolders(mFolders);
989 }
990 }
991 });
992 // Wait until the queue goes empty.
993 mHandler.post(new Runnable() {
994 public void run() {
995 if (DEBUG_LOADERS) {
996 Log.d(TAG, "Going to start binding widgets soon.");
997 }
998 }
999 });
1000 // Bind the widgets, one at a time.
1001 // WARNING: this is calling into the workspace from the background thread,
1002 // but since getCurrentScreen() just returns the int, we should be okay. This
1003 // is just a hint for the order, and if it's wrong, we'll be okay.
1004 // TODO: instead, we should have that push the current screen into here.
1005 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1006 N = mAppWidgets.size();
1007 // once for the current screen
1008 for (int i=0; i<N; i++) {
1009 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1010 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 mHandler.post(new Runnable() {
1012 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001013 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001014 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001015 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016 }
1017 }
1018 });
1019 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 }
Joe Onorato36115782010-06-17 13:28:48 -04001021 // once for the other screens
1022 for (int i=0; i<N; i++) {
1023 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1024 if (widget.screen != currentScreen) {
1025 mHandler.post(new Runnable() {
1026 public void run() {
1027 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1028 if (callbacks != null) {
1029 callbacks.bindAppWidget(widget);
1030 }
1031 }
1032 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001033 }
1034 }
Joe Onorato36115782010-06-17 13:28:48 -04001035 // Tell the workspace that we're done.
1036 mHandler.post(new Runnable() {
1037 public void run() {
1038 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1039 if (callbacks != null) {
1040 callbacks.finishBindingItems();
1041 }
1042 }
1043 });
1044 // If we're profiling, this is the last thing in the queue.
1045 mHandler.post(new Runnable() {
1046 public void run() {
1047 if (DEBUG_LOADERS) {
1048 Log.d(TAG, "bound workspace in "
1049 + (SystemClock.uptimeMillis()-t) + "ms");
1050 }
1051 }
1052 });
1053 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 private void loadAndBindAllApps() {
1056 if (DEBUG_LOADERS) {
1057 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1058 }
1059 if (!mAllAppsLoaded) {
1060 loadAllAppsByBatch();
1061 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001062 return;
1063 }
Joe Onorato36115782010-06-17 13:28:48 -04001064 mAllAppsLoaded = true;
1065 } else {
1066 onlyBindAllApps();
1067 }
1068 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001069
Joe Onorato36115782010-06-17 13:28:48 -04001070 private void onlyBindAllApps() {
1071 final Callbacks oldCallbacks = mCallbacks.get();
1072 if (oldCallbacks == null) {
1073 // This launcher has exited and nobody bothered to tell us. Just bail.
1074 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1075 return;
1076 }
1077
1078 // shallow copy
1079 final ArrayList<ApplicationInfo> list
1080 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1081 mHandler.post(new Runnable() {
1082 public void run() {
1083 final long t = SystemClock.uptimeMillis();
1084 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1085 if (callbacks != null) {
1086 callbacks.bindAllApplications(list);
1087 }
1088 if (DEBUG_LOADERS) {
1089 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1090 + (SystemClock.uptimeMillis()-t) + "ms");
1091 }
1092 }
1093 });
1094
1095 }
1096
1097 private void loadAllAppsByBatch() {
1098 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1099
1100 // Don't use these two variables in any of the callback runnables.
1101 // Otherwise we hold a reference to them.
1102 final Callbacks oldCallbacks = mCallbacks.get();
1103 if (oldCallbacks == null) {
1104 // This launcher has exited and nobody bothered to tell us. Just bail.
1105 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1106 return;
1107 }
1108
1109 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1110 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1111
1112 final PackageManager packageManager = mContext.getPackageManager();
1113 List<ResolveInfo> apps = null;
1114
1115 int N = Integer.MAX_VALUE;
1116
1117 int startIndex;
1118 int i=0;
1119 int batchSize = -1;
1120 while (i < N && !mStopped) {
1121 if (i == 0) {
1122 mAllAppsList.clear();
1123 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1124 apps = packageManager.queryIntentActivities(mainIntent, 0);
1125 if (DEBUG_LOADERS) {
1126 Log.d(TAG, "queryIntentActivities took "
1127 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1128 }
1129 if (apps == null) {
1130 return;
1131 }
1132 N = apps.size();
1133 if (DEBUG_LOADERS) {
1134 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1135 }
1136 if (N == 0) {
1137 // There are no apps?!?
1138 return;
1139 }
1140 if (mBatchSize == 0) {
1141 batchSize = N;
1142 } else {
1143 batchSize = mBatchSize;
1144 }
1145
1146 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1147 Collections.sort(apps,
1148 new ResolveInfo.DisplayNameComparator(packageManager));
1149 if (DEBUG_LOADERS) {
1150 Log.d(TAG, "sort took "
1151 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1152 }
1153 }
1154
1155 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1156
1157 startIndex = i;
1158 for (int j=0; i<N && j<batchSize; j++) {
1159 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001160 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001161 i++;
1162 }
1163
1164 final boolean first = i <= batchSize;
1165 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1166 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1167 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1168
Joe Onoratocc67f472010-06-08 10:54:30 -07001169 mHandler.post(new Runnable() {
1170 public void run() {
1171 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001172 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001173 if (first) {
1174 callbacks.bindAllApplications(added);
1175 } else {
1176 callbacks.bindAppsAdded(added);
1177 }
1178 if (DEBUG_LOADERS) {
1179 Log.d(TAG, "bound " + added.size() + " apps in "
1180 + (SystemClock.uptimeMillis() - t) + "ms");
1181 }
1182 } else {
1183 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001184 }
1185 }
1186 });
1187
Daniel Sandlerdca66122010-04-13 16:23:58 -04001188 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001189 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1190 + (SystemClock.uptimeMillis()-t2) + "ms");
1191 }
1192
1193 if (mAllAppsLoadDelay > 0 && i < N) {
1194 try {
1195 if (DEBUG_LOADERS) {
1196 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1197 }
1198 Thread.sleep(mAllAppsLoadDelay);
1199 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 }
1201 }
1202
Joe Onorato36115782010-06-17 13:28:48 -04001203 if (DEBUG_LOADERS) {
1204 Log.d(TAG, "cached all " + N + " apps in "
1205 + (SystemClock.uptimeMillis()-t) + "ms"
1206 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001207 }
1208 }
1209
1210 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001211 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1212 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1213 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1214 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1215 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1216 }
1217 }
1218
1219 void enqueuePackageUpdated(PackageUpdatedTask task) {
1220 mWorker.post(task);
1221 }
1222
1223 private class PackageUpdatedTask implements Runnable {
1224 int mOp;
1225 String[] mPackages;
1226
1227 public static final int OP_NONE = 0;
1228 public static final int OP_ADD = 1;
1229 public static final int OP_UPDATE = 2;
1230 public static final int OP_REMOVE = 3; // uninstlled
1231 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1232
1233
1234 public PackageUpdatedTask(int op, String[] packages) {
1235 mOp = op;
1236 mPackages = packages;
1237 }
1238
1239 public void run() {
1240 final Context context = mApp;
1241
1242 final String[] packages = mPackages;
1243 final int N = packages.length;
1244 switch (mOp) {
1245 case OP_ADD:
1246 for (int i=0; i<N; i++) {
1247 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1248 mAllAppsList.addPackage(context, packages[i]);
1249 }
1250 break;
1251 case OP_UPDATE:
1252 for (int i=0; i<N; i++) {
1253 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1254 mAllAppsList.updatePackage(context, packages[i]);
1255 }
1256 break;
1257 case OP_REMOVE:
1258 case OP_UNAVAILABLE:
1259 for (int i=0; i<N; i++) {
1260 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1261 mAllAppsList.removePackage(packages[i]);
1262 }
1263 break;
1264 }
1265
1266 ArrayList<ApplicationInfo> added = null;
1267 ArrayList<ApplicationInfo> removed = null;
1268 ArrayList<ApplicationInfo> modified = null;
1269
1270 if (mAllAppsList.added.size() > 0) {
1271 added = mAllAppsList.added;
1272 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1273 }
1274 if (mAllAppsList.removed.size() > 0) {
1275 removed = mAllAppsList.removed;
1276 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1277 for (ApplicationInfo info: removed) {
1278 mIconCache.remove(info.intent.getComponent());
1279 }
1280 }
1281 if (mAllAppsList.modified.size() > 0) {
1282 modified = mAllAppsList.modified;
1283 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1284 }
1285
1286 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1287 if (callbacks == null) {
1288 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1289 return;
1290 }
1291
1292 if (added != null) {
1293 final ArrayList<ApplicationInfo> addedFinal = added;
1294 mHandler.post(new Runnable() {
1295 public void run() {
1296 if (callbacks == mCallbacks.get()) {
1297 callbacks.bindAppsAdded(addedFinal);
1298 }
1299 }
1300 });
1301 }
1302 if (modified != null) {
1303 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1304 mHandler.post(new Runnable() {
1305 public void run() {
1306 if (callbacks == mCallbacks.get()) {
1307 callbacks.bindAppsUpdated(modifiedFinal);
1308 }
1309 }
1310 });
1311 }
1312 if (removed != null) {
1313 final boolean permanent = mOp != OP_UNAVAILABLE;
1314 final ArrayList<ApplicationInfo> removedFinal = removed;
1315 mHandler.post(new Runnable() {
1316 public void run() {
1317 if (callbacks == mCallbacks.get()) {
1318 callbacks.bindAppsRemoved(removedFinal, permanent);
1319 }
1320 }
1321 });
Joe Onoratobe386092009-11-17 17:32:16 -08001322 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323 }
1324 }
1325
1326 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001327 * This is called from the code that adds shortcuts from the intent receiver. This
1328 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 */
Joe Onorato56d82912010-03-07 14:32:10 -05001330 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001331 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001332 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333
Joe Onorato56d82912010-03-07 14:32:10 -05001334 /**
1335 * Make an ShortcutInfo object for a shortcut that is an application.
1336 *
1337 * If c is not null, then it will be used to fill in missing data like the title and icon.
1338 */
1339 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1340 Cursor c, int iconIndex, int titleIndex) {
1341 Bitmap icon = null;
1342 final ShortcutInfo info = new ShortcutInfo();
1343
1344 ComponentName componentName = intent.getComponent();
1345 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001346 return null;
1347 }
1348
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001349 // TODO: See if the PackageManager knows about this case. If it doesn't
1350 // then return null & delete this.
1351
Joe Onorato56d82912010-03-07 14:32:10 -05001352 // the resource -- This may implicitly give us back the fallback icon,
1353 // but don't worry about that. All we're doing with usingFallbackIcon is
1354 // to avoid saving lots of copies of that in the database, and most apps
1355 // have icons anyway.
1356 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1357 if (resolveInfo != null) {
1358 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001359 }
Joe Onorato56d82912010-03-07 14:32:10 -05001360 // the db
1361 if (icon == null) {
1362 if (c != null) {
1363 icon = getIconFromCursor(c, iconIndex);
1364 }
1365 }
1366 // the fallback icon
1367 if (icon == null) {
1368 icon = getFallbackIcon();
1369 info.usingFallbackIcon = true;
1370 }
1371 info.setIcon(icon);
1372
1373 // from the resource
1374 if (resolveInfo != null) {
1375 info.title = resolveInfo.activityInfo.loadLabel(manager);
1376 }
1377 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001378 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001379 if (c != null) {
1380 info.title = c.getString(titleIndex);
1381 }
1382 }
1383 // fall back to the class name of the activity
1384 if (info.title == null) {
1385 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001386 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1388 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001392 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001394 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001395 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1396 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397
Joe Onorato56d82912010-03-07 14:32:10 -05001398 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001399 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001402 // TODO: If there's an explicit component and we can't install that, delete it.
1403
Joe Onorato56d82912010-03-07 14:32:10 -05001404 info.title = c.getString(titleIndex);
1405
Joe Onorato9c1289c2009-08-17 11:03:03 -04001406 int iconType = c.getInt(iconTypeIndex);
1407 switch (iconType) {
1408 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1409 String packageName = c.getString(iconPackageIndex);
1410 String resourceName = c.getString(iconResourceIndex);
1411 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001412 info.customIcon = false;
1413 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001415 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001416 if (resources != null) {
1417 final int id = resources.getIdentifier(resourceName, null, null);
1418 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001420 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001421 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 }
Joe Onorato56d82912010-03-07 14:32:10 -05001423 // the db
1424 if (icon == null) {
1425 icon = getIconFromCursor(c, iconIndex);
1426 }
1427 // the fallback icon
1428 if (icon == null) {
1429 icon = getFallbackIcon();
1430 info.usingFallbackIcon = true;
1431 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 break;
1433 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001434 icon = getIconFromCursor(c, iconIndex);
1435 if (icon == null) {
1436 icon = getFallbackIcon();
1437 info.customIcon = false;
1438 info.usingFallbackIcon = true;
1439 } else {
1440 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 break;
1443 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001444 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001445 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446 info.customIcon = false;
1447 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001449 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 return info;
1451 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452
Joe Onorato56d82912010-03-07 14:32:10 -05001453 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1454 if (false) {
1455 Log.d(TAG, "getIconFromCursor app="
1456 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1457 }
1458 byte[] data = c.getBlob(iconIndex);
1459 try {
1460 return BitmapFactory.decodeByteArray(data, 0, data.length);
1461 } catch (Exception e) {
1462 return null;
1463 }
1464 }
1465
Joe Onorato0589f0f2010-02-08 13:44:00 -08001466 ShortcutInfo addShortcut(Context context, Intent data,
1467 CellLayout.CellInfo cellInfo, boolean notify) {
1468
1469 final ShortcutInfo info = infoFromShortcutIntent(context, data);
1470 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1471 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
1472
1473 return info;
1474 }
1475
1476 private ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
1477 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1478 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1479 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1480
1481 Bitmap icon = null;
1482 boolean filtered = false;
1483 boolean customIcon = false;
1484 ShortcutIconResource iconResource = null;
1485
1486 if (bitmap != null && bitmap instanceof Bitmap) {
1487 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1488 filtered = true;
1489 customIcon = true;
1490 } else {
1491 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1492 if (extra != null && extra instanceof ShortcutIconResource) {
1493 try {
1494 iconResource = (ShortcutIconResource) extra;
1495 final PackageManager packageManager = context.getPackageManager();
1496 Resources resources = packageManager.getResourcesForApplication(
1497 iconResource.packageName);
1498 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1499 icon = Utilities.createIconBitmap(resources.getDrawable(id), context);
1500 } catch (Exception e) {
1501 Log.w(TAG, "Could not load shortcut icon: " + extra);
1502 }
1503 }
1504 }
1505
Joe Onorato0589f0f2010-02-08 13:44:00 -08001506 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001507
1508 if (icon == null) {
1509 icon = getFallbackIcon();
1510 info.usingFallbackIcon = true;
1511 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001512 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001513
Joe Onorato0589f0f2010-02-08 13:44:00 -08001514 info.title = name;
1515 info.intent = intent;
1516 info.customIcon = customIcon;
1517 info.iconResource = iconResource;
1518
1519 return info;
1520 }
1521
Joe Onorato9c1289c2009-08-17 11:03:03 -04001522 private static void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
1523 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1524
1525 int iconType = c.getInt(iconTypeIndex);
1526 switch (iconType) {
1527 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1528 String packageName = c.getString(iconPackageIndex);
1529 String resourceName = c.getString(iconResourceIndex);
1530 PackageManager packageManager = context.getPackageManager();
1531 try {
1532 Resources resources = packageManager.getResourcesForApplication(packageName);
1533 final int id = resources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001534 liveFolderInfo.icon = Utilities.createIconBitmap(resources.getDrawable(id),
1535 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001536 } catch (Exception e) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001537 liveFolderInfo.icon = Utilities.createIconBitmap(
1538 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1539 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001540 }
1541 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1542 liveFolderInfo.iconResource.packageName = packageName;
1543 liveFolderInfo.iconResource.resourceName = resourceName;
1544 break;
1545 default:
Joe Onorato0589f0f2010-02-08 13:44:00 -08001546 liveFolderInfo.icon = Utilities.createIconBitmap(
1547 context.getResources().getDrawable(R.drawable.ic_launcher_folder),
1548 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001549 }
1550 }
1551
Joe Onorato56d82912010-03-07 14:32:10 -05001552 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
1553 // If this icon doesn't have a custom icon, check to see
1554 // what's stored in the DB, and if it doesn't match what
1555 // we're going to show, store what we are going to show back
1556 // into the DB. We do this so when we're loading, if the
1557 // package manager can't find an icon (for example because
1558 // the app is on SD) then we can use that instead.
1559 if (info.onExternalStorage && !info.customIcon && !info.usingFallbackIcon) {
1560 boolean needSave;
1561 byte[] data = c.getBlob(iconIndex);
1562 try {
1563 if (data != null) {
1564 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1565 Bitmap loaded = info.getIcon(mIconCache);
1566 needSave = !saved.sameAs(loaded);
1567 } else {
1568 needSave = true;
1569 }
1570 } catch (Exception e) {
1571 needSave = true;
1572 }
1573 if (needSave) {
1574 Log.d(TAG, "going to save icon bitmap for info=" + info);
1575 // This is slower than is ideal, but this only happens either
1576 // after the froyo OTA or when the app is updated with a new
1577 // icon.
1578 updateItemInDatabase(context, info);
1579 }
1580 }
1581 }
1582
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 /**
1584 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1585 * or make a new one.
1586 */
1587 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1588 // See if a placeholder was created for us already
1589 FolderInfo folderInfo = folders.get(id);
1590 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1591 // No placeholder -- create a new instance
1592 folderInfo = new UserFolderInfo();
1593 folders.put(id, folderInfo);
1594 }
1595 return (UserFolderInfo) folderInfo;
1596 }
1597
1598 /**
1599 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1600 * new one.
1601 */
1602 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1603 // See if a placeholder was created for us already
1604 FolderInfo folderInfo = folders.get(id);
1605 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1606 // No placeholder -- create a new instance
1607 folderInfo = new LiveFolderInfo();
1608 folders.put(id, folderInfo);
1609 }
1610 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
1612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1614 String label = activityInfo.loadLabel(manager).toString();
1615 if (label == null) {
1616 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1617 if (label == null) {
1618 label = activityInfo.name;
1619 }
1620 }
1621 return label;
1622 }
1623
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001625 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626 = new Comparator<ApplicationInfo>() {
1627 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1628 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 };
Joe Onoratobe386092009-11-17 17:32:16 -08001631
1632 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001633 Log.d(TAG, "mCallbacks=" + mCallbacks);
1634 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1635 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1636 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1637 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001638 Log.d(TAG, "mItems size=" + mItems.size());
1639 if (mLoaderTask != null) {
1640 mLoaderTask.dumpState();
1641 } else {
1642 Log.d(TAG, "mLoaderTask=null");
1643 }
Joe Onoratobe386092009-11-17 17:32:16 -08001644 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645}