blob: 23641743f78d9909dc49512bab9ebcb81f4422fe [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chung68846fd2010-10-29 11:00:27 -070019import java.lang.ref.WeakReference;
20import java.net.URISyntaxException;
21import java.text.Collator;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.List;
Winson Chungaafa03c2010-06-11 17:34:16 -070027
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040030import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080032import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ContentResolver;
34import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.ActivityInfo;
39import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080040import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.pm.ResolveInfo;
42import android.content.res.Resources;
43import android.database.Cursor;
44import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080047import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040048import android.os.Handler;
49import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080050import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040053import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070054import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
Winson Chung68846fd2010-10-29 11:00:27 -070056import com.android.launcher.R;
57import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080058
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059/**
60 * Maintains in-memory state of the Launcher. It is expected that there should be only one
61 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070062 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040064public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080065 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040066 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070067
Joe Onorato36115782010-06-17 13:28:48 -040068 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080069 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040070 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040071 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040072
Joe Onoratof99f8c12009-10-31 17:27:36 -040073 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 private final Object mLock = new Object();
75 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040076 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070078 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
79 static {
80 sWorkerThread.start();
81 }
82 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
83
Joe Onoratocc67f472010-06-08 10:54:30 -070084 // We start off with everything not loaded. After that, we assume that
85 // our monitoring of the package manager provides all updates and we never
86 // need to do a requery. These are only ever touched from the loader thread.
87 private boolean mWorkspaceLoaded;
88 private boolean mAllAppsLoaded;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Joe Onorato36115782010-06-17 13:28:48 -040092 private AllAppsList mAllAppsList; // only access in worker thread
Joe Onorato0589f0f2010-02-08 13:44:00 -080093 private IconCache mIconCache;
Joe Onorato36115782010-06-17 13:28:48 -040094 final ArrayList<ItemInfo> mItems = new ArrayList<ItemInfo>();
95 final ArrayList<LauncherAppWidgetInfo> mAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
96 final HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Joe Onorato0589f0f2010-02-08 13:44:00 -080097
98 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
Adam Cohend22015c2010-07-26 22:02:18 -0700100 private static int mCellCountX;
101 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700102
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700104 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400105 public int getCurrentWorkspaceScreen();
106 public void startBinding();
107 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500108 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 public void finishBindingItems();
110 public void bindAppWidget(LauncherAppWidgetInfo info);
111 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500112 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
113 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400114 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700115 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400116 public boolean isAllAppsVisible();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato0589f0f2010-02-08 13:44:00 -0800119 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800120 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400121 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800122 mAllAppsList = new AllAppsList(iconCache);
123 mIconCache = iconCache;
124
125 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700126 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400127
128 mAllAppsLoadDelay = app.getResources().getInteger(R.integer.config_allAppsBatchLoadDelay);
Joe Onoratod65d08e2010-04-20 15:43:37 -0400129
130 mBatchSize = app.getResources().getInteger(R.integer.config_allAppsBatchSize);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800131 }
132
Joe Onorato56d82912010-03-07 14:32:10 -0500133 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800134 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Joe Onorato9c1289c2009-08-17 11:03:03 -0400137 /**
138 * Adds an item to the DB if it was not created previously, or move it to a new
139 * <container, screen, cellX, cellY>
140 */
141 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
142 int screen, int cellX, int cellY) {
143 if (item.container == ItemInfo.NO_ID) {
144 // From all apps
145 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
146 } else {
147 // From somewhere else
148 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150 }
151
152 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700154 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen,
156 int cellX, int cellY) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700157
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 item.container = container;
159 item.screen = screen;
160 item.cellX = cellX;
161 item.cellY = cellY;
162
Brad Fitzpatrickade2f812010-10-10 15:42:06 -0700163 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 final ContentValues values = new ContentValues();
165 final ContentResolver cr = context.getContentResolver();
166
167 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohend22015c2010-07-26 22:02:18 -0700168 values.put(LauncherSettings.Favorites.CELLX, cellX);
169 values.put(LauncherSettings.Favorites.CELLY, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
171
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700172 sWorker.post(new Runnable() {
173 public void run() {
174 cr.update(uri, values, null, null);
175 }
176 });
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700177 }
178
179 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 * Returns true if the shortcuts already exists in the database.
181 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 static boolean shortcutExists(Context context, String title, Intent intent) {
184 final ContentResolver cr = context.getContentResolver();
185 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
186 new String[] { "title", "intent" }, "title=? and intent=?",
187 new String[] { title, intent.toUri(0) }, null);
188 boolean result = false;
189 try {
190 result = c.moveToFirst();
191 } finally {
192 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700195 }
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700198 * Returns an ItemInfo array containing all the items in the LauncherModel.
199 * The ItemInfo.id is not set through this function.
200 */
201 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
202 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
203 final ContentResolver cr = context.getContentResolver();
204 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
205 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
206 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
207 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
208
209 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
210 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
211 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
212 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
213 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
214 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
215 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
216
217 try {
218 while (c.moveToNext()) {
219 ItemInfo item = new ItemInfo();
220 item.cellX = c.getInt(cellXIndex);
221 item.cellY = c.getInt(cellYIndex);
222 item.spanX = c.getInt(spanXIndex);
223 item.spanY = c.getInt(spanYIndex);
224 item.container = c.getInt(containerIndex);
225 item.itemType = c.getInt(itemTypeIndex);
226 item.screen = c.getInt(screenIndex);
227
228 items.add(item);
229 }
230 } catch (Exception e) {
231 items.clear();
232 } finally {
233 c.close();
234 }
235
236 return items;
237 }
238
239 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
241 */
242 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
243 final ContentResolver cr = context.getContentResolver();
244 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
245 "_id=? and (itemType=? or itemType=?)",
246 new String[] { String.valueOf(id),
247 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER),
248 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700249
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250 try {
251 if (c.moveToFirst()) {
252 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
253 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
254 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
255 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
256 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
257 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259 FolderInfo folderInfo = null;
260 switch (c.getInt(itemTypeIndex)) {
261 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
262 folderInfo = findOrMakeUserFolder(folderList, id);
263 break;
264 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
265 folderInfo = findOrMakeLiveFolder(folderList, id);
266 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700267 }
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 folderInfo.title = c.getString(titleIndex);
270 folderInfo.id = id;
271 folderInfo.container = c.getInt(containerIndex);
272 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700273 folderInfo.cellX = c.getInt(cellXIndex);
274 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
276 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700277 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 } finally {
279 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700280 }
281
282 return null;
283 }
284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 /**
286 * Add an item to the database in a specified container. Sets the container, screen, cellX and
287 * cellY fields of the item. Also assigns an ID to the item.
288 */
289 static void addItemToDatabase(Context context, ItemInfo item, long container,
290 int screen, int cellX, int cellY, boolean notify) {
291 item.container = container;
292 item.screen = screen;
293 item.cellX = cellX;
294 item.cellY = cellY;
295
296 final ContentValues values = new ContentValues();
297 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 item.onAddToDatabase(values);
299
Adam Cohend22015c2010-07-26 22:02:18 -0700300 item.updateValuesWithCoordinates(values, cellX, cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700301
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
303 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
304
305 if (result != null) {
306 item.id = Integer.parseInt(result.getPathSegments().get(1));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700307 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700308 }
309
Joe Onorato9c1289c2009-08-17 11:03:03 -0400310 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700311 * Creates a new unique child id, for a given cell span across all layouts.
312 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700313 static int getCellLayoutChildId(
314 int cellId, int screen, int localCellX, int localCellY, int spanX, int spanY) {
315 return ((cellId & 0xFF) << 24)
316 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700317 }
318
Adam Cohend22015c2010-07-26 22:02:18 -0700319 static int getCellCountX() {
320 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700321 }
322
Adam Cohend22015c2010-07-26 22:02:18 -0700323 static int getCellCountY() {
324 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700325 }
326
327 /**
328 * Updates the model orientation helper to take into account the current layout dimensions
329 * when performing local/canonical coordinate transformations.
330 */
331 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700332 mCellCountX = shortAxisCellCount;
333 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700334 }
335
336 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400337 * Update an item to the database in a specified container.
338 */
339 static void updateItemInDatabase(Context context, ItemInfo item) {
340 final ContentValues values = new ContentValues();
341 final ContentResolver cr = context.getContentResolver();
342
343 item.onAddToDatabase(values);
Adam Cohend22015c2010-07-26 22:02:18 -0700344 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700345
Joe Onorato9c1289c2009-08-17 11:03:03 -0400346 cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null);
347 }
348
349 /**
350 * Removes the specified item from the database
351 * @param context
352 * @param item
353 */
354 static void deleteItemFromDatabase(Context context, ItemInfo item) {
355 final ContentResolver cr = context.getContentResolver();
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700356 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700357 sWorker.post(new Runnable() {
358 public void run() {
359 cr.delete(uriToDelete, null, null);
360 }
361 });
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362 }
363
364 /**
365 * Remove the contents of the specified folder from the database
366 */
367 static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) {
368 final ContentResolver cr = context.getContentResolver();
369
370 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
371 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
372 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
373 }
374
375 /**
376 * Set this as the current Launcher activity object for the loader.
377 */
378 public void initialize(Callbacks callbacks) {
379 synchronized (mLock) {
380 mCallbacks = new WeakReference<Callbacks>(callbacks);
381 }
382 }
383
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700384 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400385 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
386 * ACTION_PACKAGE_CHANGED.
387 */
Joe Onoratof99f8c12009-10-31 17:27:36 -0400388 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400389 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700390
Joe Onorato36115782010-06-17 13:28:48 -0400391 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400392
Joe Onorato36115782010-06-17 13:28:48 -0400393 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
394 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
395 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
396 final String packageName = intent.getData().getSchemeSpecificPart();
397 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400398
Joe Onorato36115782010-06-17 13:28:48 -0400399 int op = PackageUpdatedTask.OP_NONE;
400
401 if (packageName == null || packageName.length() == 0) {
402 // they sent us a bad intent
403 return;
404 }
405
406 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
407 op = PackageUpdatedTask.OP_UPDATE;
408 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
409 if (!replacing) {
410 op = PackageUpdatedTask.OP_REMOVE;
411 }
412 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
413 // later, we will update the package at this time
414 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
415 if (!replacing) {
416 op = PackageUpdatedTask.OP_ADD;
417 } else {
418 op = PackageUpdatedTask.OP_UPDATE;
419 }
420 }
421
422 if (op != PackageUpdatedTask.OP_NONE) {
423 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
424 }
425
426 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400427 // First, schedule to add these apps back in.
428 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
429 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
430 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700431 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400432 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
433 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
434 enqueuePackageUpdated(new PackageUpdatedTask(
435 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700436 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
437 // If we have changed locale we need to clear out the labels in all apps.
438 // Do this here because if the launcher activity is running it will be restarted.
439 // If it's not running startLoaderFromBackground will merely tell it that it needs
440 // to reload. Either way, mAllAppsLoaded will be cleared so it re-reads everything
441 // next time.
442 mAllAppsLoaded = false;
443 startLoaderFromBackground();
444 }
445 }
446
447 /**
448 * When the launcher is in the background, it's possible for it to miss paired
449 * configuration changes. So whenever we trigger the loader from the background
450 * tell the launcher that it needs to re-run the loader when it comes back instead
451 * of doing it now.
452 */
453 public void startLoaderFromBackground() {
454 boolean runLoader = false;
455 if (mCallbacks != null) {
456 Callbacks callbacks = mCallbacks.get();
457 if (callbacks != null) {
458 // Only actually run the loader if they're not paused.
459 if (!callbacks.setLoadOnResume()) {
460 runLoader = true;
461 }
462 }
463 }
464 if (runLoader) {
465 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700466 }
Joe Onorato36115782010-06-17 13:28:48 -0400467 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400468
Joe Onorato36115782010-06-17 13:28:48 -0400469 public void startLoader(Context context, boolean isLaunching) {
470 synchronized (mLock) {
471 if (DEBUG_LOADERS) {
472 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
473 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400474
Joe Onorato36115782010-06-17 13:28:48 -0400475 // Don't bother to start the thread if we know it's not going to do anything
476 if (mCallbacks != null && mCallbacks.get() != null) {
477 // If there is already one running, tell it to stop.
478 LoaderTask oldTask = mLoaderTask;
479 if (oldTask != null) {
480 if (oldTask.isLaunching()) {
481 // don't downgrade isLaunching if we're already running
482 isLaunching = true;
Joe Onorato64e6be72010-03-05 15:05:52 -0500483 }
Joe Onorato36115782010-06-17 13:28:48 -0400484 oldTask.stopLocked();
Joe Onorato64e6be72010-03-05 15:05:52 -0500485 }
Joe Onorato36115782010-06-17 13:28:48 -0400486 mLoaderTask = new LoaderTask(context, isLaunching);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700487 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400488 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400489 }
490 }
491
Joe Onorato36115782010-06-17 13:28:48 -0400492 public void stopLoader() {
493 synchronized (mLock) {
494 if (mLoaderTask != null) {
495 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400496 }
497 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800498 mItems.clear();
499 mAppWidgets.clear();
500 mFolders.clear();
Joe Onorato36115782010-06-17 13:28:48 -0400501 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400502
Joe Onorato36115782010-06-17 13:28:48 -0400503 /**
504 * Runnable for the thread that loads the contents of the launcher:
505 * - workspace icons
506 * - widgets
507 * - all apps icons
508 */
509 private class LoaderTask implements Runnable {
510 private Context mContext;
511 private Thread mWaitThread;
512 private boolean mIsLaunching;
513 private boolean mStopped;
514 private boolean mLoadAndBindStepFinished;
515
516 LoaderTask(Context context, boolean isLaunching) {
517 mContext = context;
518 mIsLaunching = isLaunching;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400519 }
520
Joe Onorato36115782010-06-17 13:28:48 -0400521 boolean isLaunching() {
522 return mIsLaunching;
523 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524
Joe Onorato36115782010-06-17 13:28:48 -0400525 private void loadAndBindWorkspace() {
526 // Load the workspace
527
528 // For now, just always reload the workspace. It's ~100 ms vs. the
529 // binding which takes many hundreds of ms.
530 // We can reconsider.
531 if (DEBUG_LOADERS) {
532 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 }
Joe Onorato36115782010-06-17 13:28:48 -0400534 if (true || !mWorkspaceLoaded) {
535 loadWorkspace();
536 if (mStopped) {
537 return;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 }
Joe Onorato36115782010-06-17 13:28:48 -0400539 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400540 }
541
Joe Onorato36115782010-06-17 13:28:48 -0400542 // Bind the workspace
543 bindWorkspace();
544 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400545
Joe Onorato36115782010-06-17 13:28:48 -0400546 private void waitForIdle() {
547 // Wait until the either we're stopped or the other threads are done.
548 // This way we don't start loading all apps until the workspace has settled
549 // down.
550 synchronized (LoaderTask.this) {
551 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700552
Joe Onorato36115782010-06-17 13:28:48 -0400553 mHandler.postIdle(new Runnable() {
554 public void run() {
555 synchronized (LoaderTask.this) {
556 mLoadAndBindStepFinished = true;
557 if (DEBUG_LOADERS) {
558 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400559 }
Joe Onorato36115782010-06-17 13:28:48 -0400560 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400561 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400562 }
Joe Onorato36115782010-06-17 13:28:48 -0400563 });
564
565 while (!mStopped && !mLoadAndBindStepFinished) {
566 try {
567 this.wait();
568 } catch (InterruptedException ex) {
569 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400570 }
571 }
Joe Onorato36115782010-06-17 13:28:48 -0400572 if (DEBUG_LOADERS) {
573 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700574 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400575 + "ms for previous step to finish binding");
576 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400577 }
Joe Onorato36115782010-06-17 13:28:48 -0400578 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400579
Joe Onorato36115782010-06-17 13:28:48 -0400580 public void run() {
581 // Optimize for end-user experience: if the Launcher is up and // running with the
582 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
583 // workspace first (default).
584 final Callbacks cbk = mCallbacks.get();
585 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400586
Joe Onorato36115782010-06-17 13:28:48 -0400587 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400588 // Elevate priority when Home launches for the first time to avoid
589 // starving at boot time. Staring at a blank home is not cool.
590 synchronized (mLock) {
591 android.os.Process.setThreadPriority(mIsLaunching
592 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
593 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400594 if (loadWorkspaceFirst) {
595 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
596 loadAndBindWorkspace();
597 } else {
598 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700599 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400600 }
601
Joe Onorato36115782010-06-17 13:28:48 -0400602 if (mStopped) {
603 break keep_running;
604 }
605
606 // Whew! Hard work done. Slow us down, and wait until the UI thread has
607 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400608 synchronized (mLock) {
609 if (mIsLaunching) {
610 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
611 }
612 }
Joe Onorato36115782010-06-17 13:28:48 -0400613 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400614
615 // second step
616 if (loadWorkspaceFirst) {
617 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700618 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400619 } else {
620 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
621 loadAndBindWorkspace();
622 }
Joe Onorato36115782010-06-17 13:28:48 -0400623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624
Joe Onorato36115782010-06-17 13:28:48 -0400625 // Clear out this reference, otherwise we end up holding it until all of the
626 // callback runnables are done.
627 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628
Joe Onorato36115782010-06-17 13:28:48 -0400629 synchronized (mLock) {
630 // If we are still the last one to be scheduled, remove ourselves.
631 if (mLoaderTask == this) {
632 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 }
Joe Onorato36115782010-06-17 13:28:48 -0400634 }
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700635
Joe Onorato36115782010-06-17 13:28:48 -0400636 // Trigger a gc to try to clean up after the stuff is done, since the
637 // renderscript allocations aren't charged to the java heap.
638 if (mStopped) {
Joe Onoratof3d5ea92010-05-18 18:43:51 -0700639 mHandler.post(new Runnable() {
640 public void run() {
641 System.gc();
642 }
643 });
Joe Onorato36115782010-06-17 13:28:48 -0400644 } else {
645 mHandler.postIdle(new Runnable() {
646 public void run() {
647 System.gc();
Daniel Sandler8802e962010-05-26 16:28:16 -0400648 }
Joe Onorato36115782010-06-17 13:28:48 -0400649 });
650 }
651 }
652
653 public void stopLocked() {
654 synchronized (LoaderTask.this) {
655 mStopped = true;
656 this.notify();
657 }
658 }
659
660 /**
661 * Gets the callbacks object. If we've been stopped, or if the launcher object
662 * has somehow been garbage collected, return null instead. Pass in the Callbacks
663 * object that was around when the deferred message was scheduled, and if there's
664 * a new Callbacks object around then also return null. This will save us from
665 * calling onto it with data that will be ignored.
666 */
667 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
668 synchronized (mLock) {
669 if (mStopped) {
670 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400671 }
Joe Onorato36115782010-06-17 13:28:48 -0400672
673 if (mCallbacks == null) {
674 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400675 }
Joe Onorato36115782010-06-17 13:28:48 -0400676
677 final Callbacks callbacks = mCallbacks.get();
678 if (callbacks != oldCallbacks) {
679 return null;
680 }
681 if (callbacks == null) {
682 Log.w(TAG, "no mCallbacks");
683 return null;
684 }
685
686 return callbacks;
687 }
688 }
689
690 // check & update map of what's occupied; used to discard overlapping/invalid items
691 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
692 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400693 return true;
694 }
Joe Onorato36115782010-06-17 13:28:48 -0400695 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
696 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
697 if (occupied[item.screen][x][y] != null) {
698 Log.e(TAG, "Error loading shortcut " + item
Winson Chungaafa03c2010-06-11 17:34:16 -0700699 + " into cell (" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400700 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700701 + ") occupied by "
Joe Onorato36115782010-06-17 13:28:48 -0400702 + occupied[item.screen][x][y]);
703 return false;
704 }
705 }
706 }
707 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
708 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
709 occupied[item.screen][x][y] = item;
710 }
711 }
712 return true;
713 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714
Joe Onorato36115782010-06-17 13:28:48 -0400715 private void loadWorkspace() {
716 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717
Joe Onorato36115782010-06-17 13:28:48 -0400718 final Context context = mContext;
719 final ContentResolver contentResolver = context.getContentResolver();
720 final PackageManager manager = context.getPackageManager();
721 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
722 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400723
Joe Onorato36115782010-06-17 13:28:48 -0400724 mItems.clear();
725 mAppWidgets.clear();
726 mFolders.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800727
Joe Onorato36115782010-06-17 13:28:48 -0400728 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729
Joe Onorato36115782010-06-17 13:28:48 -0400730 final Cursor c = contentResolver.query(
731 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400732
Adam Cohend22015c2010-07-26 22:02:18 -0700733 final ItemInfo occupied[][][] =
734 new ItemInfo[Launcher.SCREEN_COUNT][mCellCountX][mCellCountY];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735
Joe Onorato36115782010-06-17 13:28:48 -0400736 try {
737 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
738 final int intentIndex = c.getColumnIndexOrThrow
739 (LauncherSettings.Favorites.INTENT);
740 final int titleIndex = c.getColumnIndexOrThrow
741 (LauncherSettings.Favorites.TITLE);
742 final int iconTypeIndex = c.getColumnIndexOrThrow(
743 LauncherSettings.Favorites.ICON_TYPE);
744 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
745 final int iconPackageIndex = c.getColumnIndexOrThrow(
746 LauncherSettings.Favorites.ICON_PACKAGE);
747 final int iconResourceIndex = c.getColumnIndexOrThrow(
748 LauncherSettings.Favorites.ICON_RESOURCE);
749 final int containerIndex = c.getColumnIndexOrThrow(
750 LauncherSettings.Favorites.CONTAINER);
751 final int itemTypeIndex = c.getColumnIndexOrThrow(
752 LauncherSettings.Favorites.ITEM_TYPE);
753 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
754 LauncherSettings.Favorites.APPWIDGET_ID);
755 final int screenIndex = c.getColumnIndexOrThrow(
756 LauncherSettings.Favorites.SCREEN);
757 final int cellXIndex = c.getColumnIndexOrThrow
758 (LauncherSettings.Favorites.CELLX);
759 final int cellYIndex = c.getColumnIndexOrThrow
760 (LauncherSettings.Favorites.CELLY);
761 final int spanXIndex = c.getColumnIndexOrThrow
762 (LauncherSettings.Favorites.SPANX);
763 final int spanYIndex = c.getColumnIndexOrThrow(
764 LauncherSettings.Favorites.SPANY);
765 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
766 final int displayModeIndex = c.getColumnIndexOrThrow(
767 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768
Joe Onorato36115782010-06-17 13:28:48 -0400769 ShortcutInfo info;
770 String intentDescription;
771 LauncherAppWidgetInfo appWidgetInfo;
772 int container;
773 long id;
774 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775
Joe Onorato36115782010-06-17 13:28:48 -0400776 while (!mStopped && c.moveToNext()) {
777 try {
778 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779
Joe Onorato36115782010-06-17 13:28:48 -0400780 switch (itemType) {
781 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
782 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
783 intentDescription = c.getString(intentIndex);
784 try {
785 intent = Intent.parseUri(intentDescription, 0);
786 } catch (URISyntaxException e) {
787 continue;
788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400789
Joe Onorato36115782010-06-17 13:28:48 -0400790 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
791 info = getShortcutInfo(manager, intent, context, c, iconIndex,
792 titleIndex);
793 } else {
794 info = getShortcutInfo(c, context, iconTypeIndex,
795 iconPackageIndex, iconResourceIndex, iconIndex,
796 titleIndex);
797 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798
Joe Onorato36115782010-06-17 13:28:48 -0400799 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400800 info.intent = intent;
801 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400803 info.container = container;
804 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700805 info.cellX = c.getInt(cellXIndex);
806 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400807
Daniel Sandler8802e962010-05-26 16:28:16 -0400808 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -0400809 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -0400810 break;
811 }
812
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -0400814 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
815 mItems.add(info);
816 break;
817 default:
818 // Item is in a user folder
819 UserFolderInfo folderInfo =
820 findOrMakeUserFolder(mFolders, container);
821 folderInfo.add(info);
822 break;
823 }
Joe Onorato17a89222011-02-08 17:26:11 -0800824
825 // now that we've loaded everthing re-save it with the
826 // icon in case it disappears somehow.
827 updateSavedIcon(context, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400828 } else {
829 // Failed to load the shortcut, probably because the
830 // activity manager couldn't resolve it (maybe the app
831 // was uninstalled), or the db row was somehow screwed up.
832 // Delete it.
833 id = c.getLong(idIndex);
834 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
835 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
836 id, false), null, null);
837 }
838 break;
839
840 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
841 id = c.getLong(idIndex);
842 UserFolderInfo folderInfo = findOrMakeUserFolder(mFolders, id);
843
Winson Chungaafa03c2010-06-11 17:34:16 -0700844 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400845 folderInfo.id = id;
846 container = c.getInt(containerIndex);
847 folderInfo.container = container;
848 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700849 folderInfo.cellX = c.getInt(cellXIndex);
850 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400851
852 // check & update map of what's occupied
853 if (!checkItemPlacement(occupied, folderInfo)) {
854 break;
855 }
Joe Onorato36115782010-06-17 13:28:48 -0400856 switch (container) {
857 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
858 mItems.add(folderInfo);
859 break;
860 }
861
862 mFolders.put(folderInfo.id, folderInfo);
863 break;
864
865 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
866 id = c.getLong(idIndex);
867 Uri uri = Uri.parse(c.getString(uriIndex));
868
869 // Make sure the live folder exists
870 final ProviderInfo providerInfo =
871 context.getPackageManager().resolveContentProvider(
872 uri.getAuthority(), 0);
873
874 if (providerInfo == null && !isSafeMode) {
875 itemsToRemove.add(id);
876 } else {
877 LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(mFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -0400878 intentDescription = c.getString(intentIndex);
879 intent = null;
880 if (intentDescription != null) {
881 try {
882 intent = Intent.parseUri(intentDescription, 0);
883 } catch (URISyntaxException e) {
884 // Ignore, a live folder might not have a base intent
885 }
886 }
887
888 liveFolderInfo.title = c.getString(titleIndex);
889 liveFolderInfo.id = id;
890 liveFolderInfo.uri = uri;
891 container = c.getInt(containerIndex);
892 liveFolderInfo.container = container;
893 liveFolderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700894 liveFolderInfo.cellX = c.getInt(cellXIndex);
895 liveFolderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400896 liveFolderInfo.baseIntent = intent;
897 liveFolderInfo.displayMode = c.getInt(displayModeIndex);
898
899 // check & update map of what's occupied
900 if (!checkItemPlacement(occupied, liveFolderInfo)) {
901 break;
902 }
903
904 loadLiveFolderIcon(context, c, iconTypeIndex, iconPackageIndex,
905 iconResourceIndex, liveFolderInfo);
906
907 switch (container) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Joe Onorato36115782010-06-17 13:28:48 -0400909 mItems.add(liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 break;
911 }
Joe Onorato36115782010-06-17 13:28:48 -0400912 mFolders.put(liveFolderInfo.id, liveFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 }
Joe Onorato36115782010-06-17 13:28:48 -0400914 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800915
Joe Onorato36115782010-06-17 13:28:48 -0400916 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
917 // Read all Launcher-specific widget details
918 int appWidgetId = c.getInt(appWidgetIdIndex);
919 id = c.getLong(idIndex);
920
921 final AppWidgetProviderInfo provider =
922 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -0700923
Joe Onorato36115782010-06-17 13:28:48 -0400924 if (!isSafeMode && (provider == null || provider.provider == null ||
925 provider.provider.getPackageName() == null)) {
926 Log.e(TAG, "Deleting widget that isn't installed anymore: id="
927 + id + " appWidgetId=" + appWidgetId);
928 itemsToRemove.add(id);
929 } else {
930 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
931 appWidgetInfo.id = id;
932 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700933 appWidgetInfo.cellX = c.getInt(cellXIndex);
934 appWidgetInfo.cellY = c.getInt(cellYIndex);
935 appWidgetInfo.spanX = c.getInt(spanXIndex);
936 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -0400937
938 container = c.getInt(containerIndex);
939 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
940 Log.e(TAG, "Widget found where container "
941 + "!= CONTAINER_DESKTOP -- ignoring!");
942 continue;
943 }
944 appWidgetInfo.container = c.getInt(containerIndex);
945
946 // check & update map of what's occupied
947 if (!checkItemPlacement(occupied, appWidgetInfo)) {
948 break;
949 }
950
951 mAppWidgets.add(appWidgetInfo);
952 }
953 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -0800954 }
Joe Onorato36115782010-06-17 13:28:48 -0400955 } catch (Exception e) {
956 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -0800957 }
958 }
Joe Onorato36115782010-06-17 13:28:48 -0400959 } finally {
960 c.close();
961 }
Romain Guy5c16f3e2010-01-12 17:24:58 -0800962
Joe Onorato36115782010-06-17 13:28:48 -0400963 if (itemsToRemove.size() > 0) {
964 ContentProviderClient client = contentResolver.acquireContentProviderClient(
965 LauncherSettings.Favorites.CONTENT_URI);
966 // Remove dead items
967 for (long id : itemsToRemove) {
968 if (DEBUG_LOADERS) {
969 Log.d(TAG, "Removed id = " + id);
970 }
971 // Don't notify content observers
972 try {
973 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
974 null, null);
975 } catch (RemoteException e) {
976 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -0400977 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 }
980
Joe Onorato36115782010-06-17 13:28:48 -0400981 if (DEBUG_LOADERS) {
982 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
983 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -0700984 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -0400985 String line = "";
986 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
987 if (s > 0) {
988 line += " | ";
989 }
Adam Cohend22015c2010-07-26 22:02:18 -0700990 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -0400991 line += ((occupied[s][x][y] != null) ? "#" : ".");
992 }
993 }
994 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 }
Joe Onorato36115782010-06-17 13:28:48 -0400996 }
997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998
Joe Onorato36115782010-06-17 13:28:48 -0400999 /**
1000 * Read everything out of our database.
1001 */
1002 private void bindWorkspace() {
1003 final long t = SystemClock.uptimeMillis();
1004
1005 // Don't use these two variables in any of the callback runnables.
1006 // Otherwise we hold a reference to them.
1007 final Callbacks oldCallbacks = mCallbacks.get();
1008 if (oldCallbacks == null) {
1009 // This launcher has exited and nobody bothered to tell us. Just bail.
1010 Log.w(TAG, "LoaderTask running with no launcher");
1011 return;
1012 }
1013
1014 int N;
1015 // Tell the workspace that we're about to start firing items at it
1016 mHandler.post(new Runnable() {
1017 public void run() {
1018 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1019 if (callbacks != null) {
1020 callbacks.startBinding();
1021 }
1022 }
1023 });
1024 // Add the items to the workspace.
1025 N = mItems.size();
1026 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1027 final int start = i;
1028 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001029 mHandler.post(new Runnable() {
1030 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001031 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001033 callbacks.bindItems(mItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 }
1035 }
1036 });
Joe Onorato36115782010-06-17 13:28:48 -04001037 }
1038 mHandler.post(new Runnable() {
1039 public void run() {
1040 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1041 if (callbacks != null) {
1042 callbacks.bindFolders(mFolders);
1043 }
1044 }
1045 });
1046 // Wait until the queue goes empty.
1047 mHandler.post(new Runnable() {
1048 public void run() {
1049 if (DEBUG_LOADERS) {
1050 Log.d(TAG, "Going to start binding widgets soon.");
1051 }
1052 }
1053 });
1054 // Bind the widgets, one at a time.
1055 // WARNING: this is calling into the workspace from the background thread,
1056 // but since getCurrentScreen() just returns the int, we should be okay. This
1057 // is just a hint for the order, and if it's wrong, we'll be okay.
1058 // TODO: instead, we should have that push the current screen into here.
1059 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
1060 N = mAppWidgets.size();
1061 // once for the current screen
1062 for (int i=0; i<N; i++) {
1063 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1064 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 mHandler.post(new Runnable() {
1066 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001067 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001069 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 }
1071 }
1072 });
1073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 }
Joe Onorato36115782010-06-17 13:28:48 -04001075 // once for the other screens
1076 for (int i=0; i<N; i++) {
1077 final LauncherAppWidgetInfo widget = mAppWidgets.get(i);
1078 if (widget.screen != currentScreen) {
1079 mHandler.post(new Runnable() {
1080 public void run() {
1081 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1082 if (callbacks != null) {
1083 callbacks.bindAppWidget(widget);
1084 }
1085 }
1086 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001087 }
1088 }
Joe Onorato36115782010-06-17 13:28:48 -04001089 // Tell the workspace that we're done.
1090 mHandler.post(new Runnable() {
1091 public void run() {
1092 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1093 if (callbacks != null) {
1094 callbacks.finishBindingItems();
1095 }
1096 }
1097 });
1098 // If we're profiling, this is the last thing in the queue.
1099 mHandler.post(new Runnable() {
1100 public void run() {
1101 if (DEBUG_LOADERS) {
1102 Log.d(TAG, "bound workspace in "
1103 + (SystemClock.uptimeMillis()-t) + "ms");
1104 }
1105 }
1106 });
1107 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001108
Joe Onorato36115782010-06-17 13:28:48 -04001109 private void loadAndBindAllApps() {
1110 if (DEBUG_LOADERS) {
1111 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1112 }
1113 if (!mAllAppsLoaded) {
1114 loadAllAppsByBatch();
1115 if (mStopped) {
Joe Onoratocc67f472010-06-08 10:54:30 -07001116 return;
1117 }
Joe Onorato36115782010-06-17 13:28:48 -04001118 mAllAppsLoaded = true;
1119 } else {
1120 onlyBindAllApps();
1121 }
1122 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001123
Joe Onorato36115782010-06-17 13:28:48 -04001124 private void onlyBindAllApps() {
1125 final Callbacks oldCallbacks = mCallbacks.get();
1126 if (oldCallbacks == null) {
1127 // This launcher has exited and nobody bothered to tell us. Just bail.
1128 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1129 return;
1130 }
1131
1132 // shallow copy
1133 final ArrayList<ApplicationInfo> list
1134 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1135 mHandler.post(new Runnable() {
1136 public void run() {
1137 final long t = SystemClock.uptimeMillis();
1138 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1139 if (callbacks != null) {
1140 callbacks.bindAllApplications(list);
1141 }
1142 if (DEBUG_LOADERS) {
1143 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1144 + (SystemClock.uptimeMillis()-t) + "ms");
1145 }
1146 }
1147 });
1148
1149 }
1150
1151 private void loadAllAppsByBatch() {
1152 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1153
1154 // Don't use these two variables in any of the callback runnables.
1155 // Otherwise we hold a reference to them.
1156 final Callbacks oldCallbacks = mCallbacks.get();
1157 if (oldCallbacks == null) {
1158 // This launcher has exited and nobody bothered to tell us. Just bail.
1159 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1160 return;
1161 }
1162
1163 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1164 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1165
1166 final PackageManager packageManager = mContext.getPackageManager();
1167 List<ResolveInfo> apps = null;
1168
1169 int N = Integer.MAX_VALUE;
1170
1171 int startIndex;
1172 int i=0;
1173 int batchSize = -1;
1174 while (i < N && !mStopped) {
1175 if (i == 0) {
1176 mAllAppsList.clear();
1177 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1178 apps = packageManager.queryIntentActivities(mainIntent, 0);
1179 if (DEBUG_LOADERS) {
1180 Log.d(TAG, "queryIntentActivities took "
1181 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1182 }
1183 if (apps == null) {
1184 return;
1185 }
1186 N = apps.size();
1187 if (DEBUG_LOADERS) {
1188 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1189 }
1190 if (N == 0) {
1191 // There are no apps?!?
1192 return;
1193 }
1194 if (mBatchSize == 0) {
1195 batchSize = N;
1196 } else {
1197 batchSize = mBatchSize;
1198 }
1199
1200 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1201 Collections.sort(apps,
1202 new ResolveInfo.DisplayNameComparator(packageManager));
1203 if (DEBUG_LOADERS) {
1204 Log.d(TAG, "sort took "
1205 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1206 }
1207 }
1208
1209 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1210
1211 startIndex = i;
1212 for (int j=0; i<N && j<batchSize; j++) {
1213 // This builds the icon bitmaps.
Patrick Dubroy3d605d52010-07-29 13:59:29 -07001214 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), mIconCache));
Joe Onorato36115782010-06-17 13:28:48 -04001215 i++;
1216 }
1217
1218 final boolean first = i <= batchSize;
1219 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1220 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1221 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1222
Joe Onoratocc67f472010-06-08 10:54:30 -07001223 mHandler.post(new Runnable() {
1224 public void run() {
1225 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001226 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001227 if (first) {
1228 callbacks.bindAllApplications(added);
1229 } else {
1230 callbacks.bindAppsAdded(added);
1231 }
1232 if (DEBUG_LOADERS) {
1233 Log.d(TAG, "bound " + added.size() + " apps in "
1234 + (SystemClock.uptimeMillis() - t) + "ms");
1235 }
1236 } else {
1237 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001238 }
1239 }
1240 });
1241
Daniel Sandlerdca66122010-04-13 16:23:58 -04001242 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001243 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1244 + (SystemClock.uptimeMillis()-t2) + "ms");
1245 }
1246
1247 if (mAllAppsLoadDelay > 0 && i < N) {
1248 try {
1249 if (DEBUG_LOADERS) {
1250 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1251 }
1252 Thread.sleep(mAllAppsLoadDelay);
1253 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 }
1255 }
1256
Joe Onorato36115782010-06-17 13:28:48 -04001257 if (DEBUG_LOADERS) {
1258 Log.d(TAG, "cached all " + N + " apps in "
1259 + (SystemClock.uptimeMillis()-t) + "ms"
1260 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001261 }
1262 }
1263
1264 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001265 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1266 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1267 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1268 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1269 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
1270 }
1271 }
1272
1273 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001274 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001275 }
1276
1277 private class PackageUpdatedTask implements Runnable {
1278 int mOp;
1279 String[] mPackages;
1280
1281 public static final int OP_NONE = 0;
1282 public static final int OP_ADD = 1;
1283 public static final int OP_UPDATE = 2;
1284 public static final int OP_REMOVE = 3; // uninstlled
1285 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1286
1287
1288 public PackageUpdatedTask(int op, String[] packages) {
1289 mOp = op;
1290 mPackages = packages;
1291 }
1292
1293 public void run() {
1294 final Context context = mApp;
1295
1296 final String[] packages = mPackages;
1297 final int N = packages.length;
1298 switch (mOp) {
1299 case OP_ADD:
1300 for (int i=0; i<N; i++) {
1301 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1302 mAllAppsList.addPackage(context, packages[i]);
1303 }
1304 break;
1305 case OP_UPDATE:
1306 for (int i=0; i<N; i++) {
1307 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1308 mAllAppsList.updatePackage(context, packages[i]);
1309 }
1310 break;
1311 case OP_REMOVE:
1312 case OP_UNAVAILABLE:
1313 for (int i=0; i<N; i++) {
1314 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1315 mAllAppsList.removePackage(packages[i]);
1316 }
1317 break;
1318 }
1319
1320 ArrayList<ApplicationInfo> added = null;
1321 ArrayList<ApplicationInfo> removed = null;
1322 ArrayList<ApplicationInfo> modified = null;
1323
1324 if (mAllAppsList.added.size() > 0) {
1325 added = mAllAppsList.added;
1326 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1327 }
1328 if (mAllAppsList.removed.size() > 0) {
1329 removed = mAllAppsList.removed;
1330 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1331 for (ApplicationInfo info: removed) {
1332 mIconCache.remove(info.intent.getComponent());
1333 }
1334 }
1335 if (mAllAppsList.modified.size() > 0) {
1336 modified = mAllAppsList.modified;
1337 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1338 }
1339
1340 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1341 if (callbacks == null) {
1342 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1343 return;
1344 }
1345
1346 if (added != null) {
1347 final ArrayList<ApplicationInfo> addedFinal = added;
1348 mHandler.post(new Runnable() {
1349 public void run() {
1350 if (callbacks == mCallbacks.get()) {
1351 callbacks.bindAppsAdded(addedFinal);
1352 }
1353 }
1354 });
1355 }
1356 if (modified != null) {
1357 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1358 mHandler.post(new Runnable() {
1359 public void run() {
1360 if (callbacks == mCallbacks.get()) {
1361 callbacks.bindAppsUpdated(modifiedFinal);
1362 }
1363 }
1364 });
1365 }
1366 if (removed != null) {
1367 final boolean permanent = mOp != OP_UNAVAILABLE;
1368 final ArrayList<ApplicationInfo> removedFinal = removed;
1369 mHandler.post(new Runnable() {
1370 public void run() {
1371 if (callbacks == mCallbacks.get()) {
1372 callbacks.bindAppsRemoved(removedFinal, permanent);
1373 }
1374 }
1375 });
Joe Onoratobe386092009-11-17 17:32:16 -08001376 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001377
1378 mHandler.post(new Runnable() {
1379 @Override
1380 public void run() {
1381 if (callbacks == mCallbacks.get()) {
1382 callbacks.bindPackagesUpdated();
1383 }
1384 }
1385 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 }
1387 }
1388
1389 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001390 * This is called from the code that adds shortcuts from the intent receiver. This
1391 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392 */
Joe Onorato56d82912010-03-07 14:32:10 -05001393 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Joe Onoratoe74daed2010-03-11 12:32:24 -08001394 return getShortcutInfo(manager, intent, context, null, -1, -1);
Joe Onorato56d82912010-03-07 14:32:10 -05001395 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001396
Joe Onorato56d82912010-03-07 14:32:10 -05001397 /**
1398 * Make an ShortcutInfo object for a shortcut that is an application.
1399 *
1400 * If c is not null, then it will be used to fill in missing data like the title and icon.
1401 */
1402 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
1403 Cursor c, int iconIndex, int titleIndex) {
1404 Bitmap icon = null;
1405 final ShortcutInfo info = new ShortcutInfo();
1406
1407 ComponentName componentName = intent.getComponent();
1408 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001409 return null;
1410 }
1411
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001412 // TODO: See if the PackageManager knows about this case. If it doesn't
1413 // then return null & delete this.
1414
Joe Onorato56d82912010-03-07 14:32:10 -05001415 // the resource -- This may implicitly give us back the fallback icon,
1416 // but don't worry about that. All we're doing with usingFallbackIcon is
1417 // to avoid saving lots of copies of that in the database, and most apps
1418 // have icons anyway.
1419 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1420 if (resolveInfo != null) {
1421 icon = mIconCache.getIcon(componentName, resolveInfo);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001422 }
Joe Onorato56d82912010-03-07 14:32:10 -05001423 // the db
1424 if (icon == null) {
1425 if (c != null) {
1426 icon = getIconFromCursor(c, iconIndex);
1427 }
1428 }
1429 // the fallback icon
1430 if (icon == null) {
1431 icon = getFallbackIcon();
1432 info.usingFallbackIcon = true;
1433 }
1434 info.setIcon(icon);
1435
1436 // from the resource
1437 if (resolveInfo != null) {
1438 info.title = resolveInfo.activityInfo.loadLabel(manager);
1439 }
1440 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001442 if (c != null) {
1443 info.title = c.getString(titleIndex);
1444 }
1445 }
1446 // fall back to the class name of the activity
1447 if (info.title == null) {
1448 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001449 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1451 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001455 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001457 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001458 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1459 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460
Joe Onorato56d82912010-03-07 14:32:10 -05001461 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001462 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001465 // TODO: If there's an explicit component and we can't install that, delete it.
1466
Joe Onorato56d82912010-03-07 14:32:10 -05001467 info.title = c.getString(titleIndex);
1468
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469 int iconType = c.getInt(iconTypeIndex);
1470 switch (iconType) {
1471 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1472 String packageName = c.getString(iconPackageIndex);
1473 String resourceName = c.getString(iconResourceIndex);
1474 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001475 info.customIcon = false;
1476 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001478 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001479 if (resources != null) {
1480 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001481 icon = Utilities.createIconBitmap(
1482 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001483 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001485 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
Joe Onorato56d82912010-03-07 14:32:10 -05001487 // the db
1488 if (icon == null) {
1489 icon = getIconFromCursor(c, iconIndex);
1490 }
1491 // the fallback icon
1492 if (icon == null) {
1493 icon = getFallbackIcon();
1494 info.usingFallbackIcon = true;
1495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496 break;
1497 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Joe Onorato56d82912010-03-07 14:32:10 -05001498 icon = getIconFromCursor(c, iconIndex);
1499 if (icon == null) {
1500 icon = getFallbackIcon();
1501 info.customIcon = false;
1502 info.usingFallbackIcon = true;
1503 } else {
1504 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001505 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001506 break;
1507 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001508 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001509 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 info.customIcon = false;
1511 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001513 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001514 return info;
1515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516
Joe Onorato56d82912010-03-07 14:32:10 -05001517 Bitmap getIconFromCursor(Cursor c, int iconIndex) {
1518 if (false) {
1519 Log.d(TAG, "getIconFromCursor app="
1520 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1521 }
1522 byte[] data = c.getBlob(iconIndex);
1523 try {
1524 return BitmapFactory.decodeByteArray(data, 0, data.length);
1525 } catch (Exception e) {
1526 return null;
1527 }
1528 }
1529
Joe Onorato0589f0f2010-02-08 13:44:00 -08001530 ShortcutInfo addShortcut(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001531 int screen, int cellX, int cellY, boolean notify) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001532
Winson Chunga9abd0e2010-10-27 17:18:37 -07001533 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001534 addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001536
1537 return info;
1538 }
1539
Winson Chunga9abd0e2010-10-27 17:18:37 -07001540 /**
Winson Chung55cef262010-10-28 14:14:18 -07001541 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1542 */
1543 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1544 ComponentName component) {
1545 List<AppWidgetProviderInfo> widgets =
1546 AppWidgetManager.getInstance(context).getInstalledProviders();
1547 for (AppWidgetProviderInfo info : widgets) {
1548 if (info.provider.equals(component)) {
1549 return info;
1550 }
1551 }
1552 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001553 }
1554
Winson Chung68846fd2010-10-29 11:00:27 -07001555 /**
1556 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1557 */
1558 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1559 final PackageManager packageManager = context.getPackageManager();
1560 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1561 new ArrayList<WidgetMimeTypeHandlerData>();
1562
1563 final Intent supportsIntent =
1564 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1565 supportsIntent.setType(mimeType);
1566
1567 // Create a set of widget configuration components that we can test against
1568 final List<AppWidgetProviderInfo> widgets =
1569 AppWidgetManager.getInstance(context).getInstalledProviders();
1570 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1571 new HashMap<ComponentName, AppWidgetProviderInfo>();
1572 for (AppWidgetProviderInfo info : widgets) {
1573 configurationComponentToWidget.put(info.configure, info);
1574 }
1575
1576 // Run through each of the intents that can handle this type of clip data, and cross
1577 // reference them with the components that are actual configuration components
1578 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1579 PackageManager.MATCH_DEFAULT_ONLY);
1580 for (ResolveInfo info : activities) {
1581 final ActivityInfo activityInfo = info.activityInfo;
1582 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1583 activityInfo.name);
1584 if (configurationComponentToWidget.containsKey(infoComponent)) {
1585 supportedConfigurationActivities.add(
1586 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1587 configurationComponentToWidget.get(infoComponent)));
1588 }
1589 }
1590 return supportedConfigurationActivities;
1591 }
1592
Winson Chunga9abd0e2010-10-27 17:18:37 -07001593 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001594 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1595 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1596 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1597
1598 Bitmap icon = null;
1599 boolean filtered = false;
1600 boolean customIcon = false;
1601 ShortcutIconResource iconResource = null;
1602
1603 if (bitmap != null && bitmap instanceof Bitmap) {
1604 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
1605 filtered = true;
1606 customIcon = true;
1607 } else {
1608 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1609 if (extra != null && extra instanceof ShortcutIconResource) {
1610 try {
1611 iconResource = (ShortcutIconResource) extra;
1612 final PackageManager packageManager = context.getPackageManager();
1613 Resources resources = packageManager.getResourcesForApplication(
1614 iconResource.packageName);
1615 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001616 icon = Utilities.createIconBitmap(
1617 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001618 } catch (Exception e) {
1619 Log.w(TAG, "Could not load shortcut icon: " + extra);
1620 }
1621 }
1622 }
1623
Joe Onorato0589f0f2010-02-08 13:44:00 -08001624 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001625
1626 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001627 if (fallbackIcon != null) {
1628 icon = fallbackIcon;
1629 } else {
1630 icon = getFallbackIcon();
1631 info.usingFallbackIcon = true;
1632 }
Joe Onorato56d82912010-03-07 14:32:10 -05001633 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001634 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001635
Joe Onorato0589f0f2010-02-08 13:44:00 -08001636 info.title = name;
1637 info.intent = intent;
1638 info.customIcon = customIcon;
1639 info.iconResource = iconResource;
1640
1641 return info;
1642 }
1643
Michael Jurkac9a96192010-11-01 11:52:08 -07001644 private void loadLiveFolderIcon(Context context, Cursor c, int iconTypeIndex,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645 int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) {
1646
1647 int iconType = c.getInt(iconTypeIndex);
1648 switch (iconType) {
1649 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1650 String packageName = c.getString(iconPackageIndex);
1651 String resourceName = c.getString(iconResourceIndex);
1652 PackageManager packageManager = context.getPackageManager();
1653 try {
Michael Jurkac9a96192010-11-01 11:52:08 -07001654 Resources appResources = packageManager.getResourcesForApplication(packageName);
1655 final int id = appResources.getIdentifier(resourceName, null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001656 liveFolderInfo.icon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -07001657 mIconCache.getFullResIcon(appResources, id), context);
1658 } catch (Exception e) {
1659 Resources resources = context.getResources();
1660 liveFolderInfo.icon = Utilities.createIconBitmap(
1661 mIconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001662 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001663 }
1664 liveFolderInfo.iconResource = new Intent.ShortcutIconResource();
1665 liveFolderInfo.iconResource.packageName = packageName;
1666 liveFolderInfo.iconResource.resourceName = resourceName;
1667 break;
1668 default:
Michael Jurkac9a96192010-11-01 11:52:08 -07001669 Resources resources = context.getResources();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001670 liveFolderInfo.icon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -07001671 mIconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001672 context);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001673 }
1674 }
1675
Joe Onorato56d82912010-03-07 14:32:10 -05001676 void updateSavedIcon(Context context, ShortcutInfo info, Cursor c, int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001677 // If apps can't be on SD, don't even bother.
1678 if (!mAppsCanBeOnExternalStorage) {
1679 return;
1680 }
Joe Onorato56d82912010-03-07 14:32:10 -05001681 // If this icon doesn't have a custom icon, check to see
1682 // what's stored in the DB, and if it doesn't match what
1683 // we're going to show, store what we are going to show back
1684 // into the DB. We do this so when we're loading, if the
1685 // package manager can't find an icon (for example because
1686 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001687 if (!info.customIcon && !info.usingFallbackIcon) {
Joe Onorato56d82912010-03-07 14:32:10 -05001688 boolean needSave;
1689 byte[] data = c.getBlob(iconIndex);
1690 try {
1691 if (data != null) {
1692 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1693 Bitmap loaded = info.getIcon(mIconCache);
1694 needSave = !saved.sameAs(loaded);
1695 } else {
1696 needSave = true;
1697 }
1698 } catch (Exception e) {
1699 needSave = true;
1700 }
1701 if (needSave) {
1702 Log.d(TAG, "going to save icon bitmap for info=" + info);
Joe Onorato17a89222011-02-08 17:26:11 -08001703 // This is slower than is ideal, but this only happens once
1704 // or when the app is updated with a new icon.
Joe Onorato56d82912010-03-07 14:32:10 -05001705 updateItemInDatabase(context, info);
1706 }
1707 }
1708 }
1709
Joe Onorato9c1289c2009-08-17 11:03:03 -04001710 /**
1711 * Return an existing UserFolderInfo object if we have encountered this ID previously,
1712 * or make a new one.
1713 */
1714 private static UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) {
1715 // See if a placeholder was created for us already
1716 FolderInfo folderInfo = folders.get(id);
1717 if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) {
1718 // No placeholder -- create a new instance
1719 folderInfo = new UserFolderInfo();
1720 folders.put(id, folderInfo);
1721 }
1722 return (UserFolderInfo) folderInfo;
1723 }
1724
1725 /**
1726 * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a
1727 * new one.
1728 */
1729 private static LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) {
1730 // See if a placeholder was created for us already
1731 FolderInfo folderInfo = folders.get(id);
1732 if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) {
1733 // No placeholder -- create a new instance
1734 folderInfo = new LiveFolderInfo();
1735 folders.put(id, folderInfo);
1736 }
1737 return (LiveFolderInfo) folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 }
1739
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
1741 String label = activityInfo.loadLabel(manager).toString();
1742 if (label == null) {
1743 label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
1744 if (label == null) {
1745 label = activityInfo.name;
1746 }
1747 }
1748 return label;
1749 }
1750
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001752 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753 = new Comparator<ApplicationInfo>() {
1754 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1755 return sCollator.compare(a.title.toString(), b.title.toString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757 };
Winson Chung78403fe2011-01-21 15:38:02 -08001758 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1759 = new Comparator<ApplicationInfo>() {
1760 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1761 if (a.firstInstallTime < b.firstInstallTime) return 1;
1762 if (a.firstInstallTime > b.firstInstallTime) return -1;
1763 return 0;
1764 }
1765 };
Joe Onoratobe386092009-11-17 17:32:16 -08001766
1767 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001768 Log.d(TAG, "mCallbacks=" + mCallbacks);
1769 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
1770 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
1771 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
1772 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001773 Log.d(TAG, "mItems size=" + mItems.size());
1774 if (mLoaderTask != null) {
1775 mLoaderTask.dumpState();
1776 } else {
1777 Log.d(TAG, "mLoaderTask=null");
1778 }
Joe Onoratobe386092009-11-17 17:32:16 -08001779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780}