blob: 3ee273214789bf454b89d26321ebc0f1bf2fb392 [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
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Joe Onoratof99f8c12009-10-31 17:27:36 -040022import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Romain Guy5c16f3e2010-01-12 17:24:58 -080024import android.content.ContentProviderClient;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
26import android.content.ContentValues;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070033import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.res.Resources;
35import android.database.Cursor;
36import android.graphics.Bitmap;
37import android.graphics.BitmapFactory;
38import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080039import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040040import android.os.Handler;
41import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040045import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
Winson Chung68846fd2010-10-29 11:00:27 -070048import com.android.launcher.R;
49import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080050
Michael Jurkac2f801e2011-07-12 14:19:46 -070051import java.lang.ref.WeakReference;
52import java.net.URISyntaxException;
53import java.text.Collator;
54import java.util.ArrayList;
55import java.util.Collections;
56import java.util.Comparator;
57import java.util.HashMap;
58import java.util.List;
59import java.util.Locale;
60
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061/**
62 * Maintains in-memory state of the Launcher. It is expected that there should be only one
63 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070064 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040066public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080067 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040068 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070069
Joe Onorato36115782010-06-17 13:28:48 -040070 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080071 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040072 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040073 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040074
Joe Onoratof99f8c12009-10-31 17:27:36 -040075 private final LauncherApplication mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040076 private final Object mLock = new Object();
77 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040078 private LoaderTask mLoaderTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070080 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
81 static {
82 sWorkerThread.start();
83 }
84 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
85
Joe Onoratocc67f472010-06-08 10:54:30 -070086 // We start off with everything not loaded. After that, we assume that
87 // our monitoring of the package manager provides all updates and we never
88 // need to do a requery. These are only ever touched from the loader thread.
89 private boolean mWorkspaceLoaded;
90 private boolean mAllAppsLoaded;
91
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093
Michael Jurkaa8c760d2011-04-28 14:59:33 -070094 // < only access in worker thread >
95 private AllAppsList mAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -080096
Michael Jurkaa8c760d2011-04-28 14:59:33 -070097 // sItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
98 // LauncherModel to their ids
99 static final HashMap<Long, ItemInfo> sItemsIdMap = new HashMap<Long, ItemInfo>();
100
101 // sItems is passed to bindItems, which expects a list of all folders and shortcuts created by
102 // LauncherModel that are directly on the home screen (however, no widgets or shortcuts
103 // within folders).
Adam Cohen4eac29a2011-07-11 17:53:37 -0700104 static final ArrayList<ItemInfo> sWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700105
106 // sAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
107 static final ArrayList<LauncherAppWidgetInfo> sAppWidgets =
108 new ArrayList<LauncherAppWidgetInfo>();
109
110 // sFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
111 static final HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700112
113 // sDbIconCache is the set of ItemInfos that need to have their icons updated in the database
114 static final HashMap<Object, byte[]> sDbIconCache = new HashMap<Object, byte[]>();
115
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700116 // </ only access in worker thread >
117
118 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800119 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohend22015c2010-07-26 22:02:18 -0700121 private static int mCellCountX;
122 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700123
Reena Lee93f824a2011-09-23 17:20:28 -0700124 protected Configuration mPreviousConfig;
125
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700127 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400128 public int getCurrentWorkspaceScreen();
129 public void startBinding();
130 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Joe Onoratoad72e172009-11-06 16:25:04 -0500131 public void bindFolders(HashMap<Long,FolderInfo> folders);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400132 public void finishBindingItems();
133 public void bindAppWidget(LauncherAppWidgetInfo info);
134 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500135 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
136 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Joe Onorato36115782010-06-17 13:28:48 -0400137 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent);
Winson Chung80baf5a2010-08-09 16:03:15 -0700138 public void bindPackagesUpdated();
Daniel Sandler843e8602010-06-07 14:59:01 -0400139 public boolean isAllAppsVisible();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100140 public void bindSearchablesChanged();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143 LauncherModel(LauncherApplication app, IconCache iconCache) {
Joe Onorato17a89222011-02-08 17:26:11 -0800144 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400145 mApp = app;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146 mAllAppsList = new AllAppsList(iconCache);
147 mIconCache = iconCache;
148
149 mDefaultIcon = Utilities.createIconBitmap(
Michael Jurkac9a96192010-11-01 11:52:08 -0700150 mIconCache.getFullResDefaultActivityIcon(), app);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400151
Reena Lee93f824a2011-09-23 17:20:28 -0700152 final Resources res = app.getResources();
153 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
154 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
155 mPreviousConfig = res.getConfiguration();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800156 }
157
Joe Onorato56d82912010-03-07 14:32:10 -0500158 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800159 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chung603bcb92011-09-02 11:45:39 -0700162 public void unbindWorkspaceItems() {
163 sWorker.post(new Runnable() {
164 @Override
165 public void run() {
166 unbindWorkspaceItemsOnMainThread();
167 }
168 });
169 }
170
171 /** Unbinds all the sWorkspaceItems on the main thread, and return a copy of sWorkspaceItems
172 * that is save to reference from the main thread. */
173 private ArrayList<ItemInfo> unbindWorkspaceItemsOnMainThread() {
174 // Ensure that we don't use the same workspace items data structure on the main thread
175 // by making a copy of workspace items first.
176 final ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(sWorkspaceItems);
177 mHandler.post(new Runnable() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700178 @Override
Winson Chung603bcb92011-09-02 11:45:39 -0700179 public void run() {
180 for (ItemInfo item : workspaceItems) {
181 item.unbind();
182 }
183 }
184 });
185
186 return workspaceItems;
Adam Cohen4eac29a2011-07-11 17:53:37 -0700187 }
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 /**
190 * Adds an item to the DB if it was not created previously, or move it to a new
191 * <container, screen, cellX, cellY>
192 */
193 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
194 int screen, int cellX, int cellY) {
195 if (item.container == ItemInfo.NO_ID) {
196 // From all apps
197 addItemToDatabase(context, item, container, screen, cellX, cellY, false);
198 } else {
199 // From somewhere else
200 moveItemInDatabase(context, item, container, screen, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 }
202 }
203
Michael Jurkac9d95c52011-08-29 14:03:34 -0700204 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
205 final ItemInfo item, final String callingFunction) {
206 final long itemId = item.id;
207 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
208 final ContentResolver cr = context.getContentResolver();
209
210 Runnable r = new Runnable() {
211 public void run() {
212 cr.update(uri, values, null, null);
213
214 ItemInfo modelItem = sItemsIdMap.get(itemId);
215 if (item != modelItem) {
216 // the modelItem needs to match up perfectly with item if our model is to be
217 // consistent with the database-- for now, just require modelItem == item
218 String msg = "item: " + ((item != null) ? item.toString() : "null") +
219 "modelItem: " + ((modelItem != null) ? modelItem.toString() : "null") +
220 "Error: ItemInfo passed to " + callingFunction + " doesn't match original";
221 throw new RuntimeException(msg);
222 }
223
224 // Items are added/removed from the corresponding FolderInfo elsewhere, such
225 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
226 // that are on the desktop, as appropriate
227 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
228 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
229 if (!sWorkspaceItems.contains(modelItem)) {
230 sWorkspaceItems.add(modelItem);
231 }
232 } else {
233 sWorkspaceItems.remove(modelItem);
234 }
235 }
236 };
237
238 if (sWorkerThread.getThreadId() == Process.myTid()) {
239 r.run();
240 } else {
241 sWorker.post(r);
242 }
243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700246 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700247 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
248 final int screen, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400249 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250 item.cellX = cellX;
251 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700252
Winson Chung3d503fb2011-07-13 17:25:49 -0700253 // We store hotseat items in canonical form which is this orientation invariant position
254 // in the hotseat
255 if (context instanceof Launcher && screen < 0 &&
256 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
257 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
258 } else {
259 item.screen = screen;
260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400261
262 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700264 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
265 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266 values.put(LauncherSettings.Favorites.SCREEN, item.screen);
267
Michael Jurkac9d95c52011-08-29 14:03:34 -0700268 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700269 }
270
271 /**
Adam Cohen1b607ed2011-03-03 17:26:50 -0800272 * Resize an item in the DB to a new <spanX, spanY, cellX, cellY>
Adam Cohend4844c32011-02-18 19:25:06 -0800273 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700274 static void resizeItemInDatabase(Context context, final ItemInfo item, final int cellX,
275 final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800276 item.spanX = spanX;
277 item.spanY = spanY;
278 item.cellX = cellX;
279 item.cellY = cellY;
280
Adam Cohend4844c32011-02-18 19:25:06 -0800281 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800282 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
283 values.put(LauncherSettings.Favorites.SPANX, spanX);
284 values.put(LauncherSettings.Favorites.SPANY, spanY);
285 values.put(LauncherSettings.Favorites.CELLX, cellX);
286 values.put(LauncherSettings.Favorites.CELLY, cellY);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700287 updateItemInDatabaseHelper(context, values, item, "resizeItemInDatabase");
288 }
Adam Cohend4844c32011-02-18 19:25:06 -0800289
Michael Jurkac9d95c52011-08-29 14:03:34 -0700290
291 /**
292 * Update an item to the database in a specified container.
293 */
294 static void updateItemInDatabase(Context context, final ItemInfo item) {
295 final ContentValues values = new ContentValues();
296 item.onAddToDatabase(values);
297 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
298 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800299 }
300
301 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 * Returns true if the shortcuts already exists in the database.
303 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 static boolean shortcutExists(Context context, String title, Intent intent) {
306 final ContentResolver cr = context.getContentResolver();
307 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
308 new String[] { "title", "intent" }, "title=? and intent=?",
309 new String[] { title, intent.toUri(0) }, null);
310 boolean result = false;
311 try {
312 result = c.moveToFirst();
313 } finally {
314 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700317 }
318
Joe Onorato9c1289c2009-08-17 11:03:03 -0400319 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700320 * Returns an ItemInfo array containing all the items in the LauncherModel.
321 * The ItemInfo.id is not set through this function.
322 */
323 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
324 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
325 final ContentResolver cr = context.getContentResolver();
326 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
327 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
328 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
329 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
330
331 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
332 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
333 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
334 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
335 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
336 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
337 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
338
339 try {
340 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700341 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700342 item.cellX = c.getInt(cellXIndex);
343 item.cellY = c.getInt(cellYIndex);
344 item.spanX = c.getInt(spanXIndex);
345 item.spanY = c.getInt(spanYIndex);
346 item.container = c.getInt(containerIndex);
347 item.itemType = c.getInt(itemTypeIndex);
348 item.screen = c.getInt(screenIndex);
349
350 items.add(item);
351 }
352 } catch (Exception e) {
353 items.clear();
354 } finally {
355 c.close();
356 }
357
358 return items;
359 }
360
361 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400362 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
363 */
364 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
365 final ContentResolver cr = context.getContentResolver();
366 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
367 "_id=? and (itemType=? or itemType=?)",
368 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700369 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700370
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371 try {
372 if (c.moveToFirst()) {
373 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
374 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
375 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
376 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
377 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
378 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 FolderInfo folderInfo = null;
381 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700382 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
383 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700385 }
386
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 folderInfo.title = c.getString(titleIndex);
388 folderInfo.id = id;
389 folderInfo.container = c.getInt(containerIndex);
390 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700391 folderInfo.cellX = c.getInt(cellXIndex);
392 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400393
394 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700395 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400396 } finally {
397 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700398 }
399
400 return null;
401 }
402
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 /**
404 * Add an item to the database in a specified container. Sets the container, screen, cellX and
405 * cellY fields of the item. Also assigns an ID to the item.
406 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700407 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
408 final int screen, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400410 item.cellX = cellX;
411 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700412 // We store hotseat items in canonical form which is this orientation invariant position
413 // in the hotseat
414 if (context instanceof Launcher && screen < 0 &&
415 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
416 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
417 } else {
418 item.screen = screen;
419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400420
421 final ContentValues values = new ContentValues();
422 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400423 item.onAddToDatabase(values);
424
Michael Jurka7578ec62011-08-03 14:11:54 -0700425 LauncherApplication app = (LauncherApplication) context.getApplicationContext();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700426 item.id = app.getLauncherProvider().generateNewId();
427 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700428 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700429
Michael Jurkac9d95c52011-08-29 14:03:34 -0700430 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700431 public void run() {
432 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
433 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400434
Winson Chungb1094bd2011-08-24 16:14:08 -0700435 if (sItemsIdMap.containsKey(item.id)) {
436 // we should not be adding new items in the db with the same id
437 throw new RuntimeException("Error: ItemInfo id (" + item.id + ") passed to " +
438 "addItemToDatabase already exists." + item.toString());
439 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700440 sItemsIdMap.put(item.id, item);
441 switch (item.itemType) {
442 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
443 sFolders.put(item.id, (FolderInfo) item);
Winson Chung3d503fb2011-07-13 17:25:49 -0700444 // Fall through
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700445 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
446 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700447 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
448 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohen4eac29a2011-07-11 17:53:37 -0700449 sWorkspaceItems.add(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700450 }
451 break;
452 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
453 sAppWidgets.add((LauncherAppWidgetInfo) item);
454 break;
455 }
456 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700457 };
458
459 if (sWorkerThread.getThreadId() == Process.myTid()) {
460 r.run();
461 } else {
462 sWorker.post(r);
463 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700464 }
465
Joe Onorato9c1289c2009-08-17 11:03:03 -0400466 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700467 * Creates a new unique child id, for a given cell span across all layouts.
468 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700469 static int getCellLayoutChildId(
Winson Chung3d503fb2011-07-13 17:25:49 -0700470 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) {
471 return (((int) container & 0xFF) << 24)
Michael Jurka845ba3b2010-09-28 17:09:46 -0700472 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700473 }
474
Adam Cohend22015c2010-07-26 22:02:18 -0700475 static int getCellCountX() {
476 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700477 }
478
Adam Cohend22015c2010-07-26 22:02:18 -0700479 static int getCellCountY() {
480 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700481 }
482
483 /**
484 * Updates the model orientation helper to take into account the current layout dimensions
485 * when performing local/canonical coordinate transformations.
486 */
487 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700488 mCellCountX = shortAxisCellCount;
489 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700490 }
491
492 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700493 * Removes the specified item from the database
494 * @param context
495 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400496 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700497 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400498 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700499 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Michael Jurka83df1882011-08-31 20:59:26 -0700500 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700501 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700502 cr.delete(uriToDelete, null, null);
503 switch (item.itemType) {
504 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
505 sFolders.remove(item.id);
506 sWorkspaceItems.remove(item);
507 break;
508 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
509 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
510 sWorkspaceItems.remove(item);
511 break;
512 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
513 sAppWidgets.remove((LauncherAppWidgetInfo) item);
514 break;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700515 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700516 sItemsIdMap.remove(item.id);
517 sDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700518 }
Michael Jurka83df1882011-08-31 20:59:26 -0700519 };
520 if (sWorkerThread.getThreadId() == Process.myTid()) {
521 r.run();
522 } else {
523 sWorker.post(r);
524 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525 }
526
527 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400528 * Remove the contents of the specified folder from the database
529 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700530 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400531 final ContentResolver cr = context.getContentResolver();
532
Michael Jurkac9d95c52011-08-29 14:03:34 -0700533 Runnable r = new Runnable() {
534 public void run() {
535 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
536 sItemsIdMap.remove(info.id);
537 sFolders.remove(info.id);
538 sDbIconCache.remove(info);
539 sWorkspaceItems.remove(info);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700540
Michael Jurkac9d95c52011-08-29 14:03:34 -0700541 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
542 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
543 for (ItemInfo childInfo : info.contents) {
544 sItemsIdMap.remove(childInfo.id);
545 sDbIconCache.remove(childInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700546 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700547 }
548 };
549 if (sWorkerThread.getThreadId() == Process.myTid()) {
550 r.run();
551 } else {
552 sWorker.post(r);
553 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 }
555
556 /**
557 * Set this as the current Launcher activity object for the loader.
558 */
559 public void initialize(Callbacks callbacks) {
560 synchronized (mLock) {
561 mCallbacks = new WeakReference<Callbacks>(callbacks);
562 }
563 }
564
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700565 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
567 * ACTION_PACKAGE_CHANGED.
568 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100569 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400570 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400571 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700572
Joe Onorato36115782010-06-17 13:28:48 -0400573 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400574
Joe Onorato36115782010-06-17 13:28:48 -0400575 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
576 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
577 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
578 final String packageName = intent.getData().getSchemeSpecificPart();
579 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400580
Joe Onorato36115782010-06-17 13:28:48 -0400581 int op = PackageUpdatedTask.OP_NONE;
582
583 if (packageName == null || packageName.length() == 0) {
584 // they sent us a bad intent
585 return;
586 }
587
588 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
589 op = PackageUpdatedTask.OP_UPDATE;
590 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
591 if (!replacing) {
592 op = PackageUpdatedTask.OP_REMOVE;
593 }
594 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
595 // later, we will update the package at this time
596 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
597 if (!replacing) {
598 op = PackageUpdatedTask.OP_ADD;
599 } else {
600 op = PackageUpdatedTask.OP_UPDATE;
601 }
602 }
603
604 if (op != PackageUpdatedTask.OP_NONE) {
605 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
606 }
607
608 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -0400609 // First, schedule to add these apps back in.
610 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
611 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
612 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700613 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -0400614 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
615 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
616 enqueuePackageUpdated(new PackageUpdatedTask(
617 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700618 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700619 // If we have changed locale we need to clear out the labels in all apps/workspace.
620 forceReload();
621 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
622 // Check if configuration change was an mcc/mnc change which would affect app resources
623 // and we would need to clear out the labels in all apps/workspace. Same handling as
624 // above for ACTION_LOCALE_CHANGED
625 Configuration currentConfig = context.getResources().getConfiguration();
626 if((mPreviousConfig.diff(currentConfig) & ActivityInfo.CONFIG_MCC) != 0){
627 Log.d(TAG, "Reload apps on config change. curr_mcc:"
628 + currentConfig.mcc + " prevmcc:" + mPreviousConfig.mcc);
629 forceReload();
630 }
631 // Update previousConfig
632 mPreviousConfig = currentConfig;
Winson Chungcbf7c4d2011-08-23 11:58:54 -0700633 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
634 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -0700635 if (mCallbacks != null) {
636 Callbacks callbacks = mCallbacks.get();
637 if (callbacks != null) {
638 callbacks.bindSearchablesChanged();
639 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -0700640 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700641 }
642 }
643
Reena Lee93f824a2011-09-23 17:20:28 -0700644 private void forceReload() {
645 synchronized (mLock) {
646 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
647 // mWorkspaceLoaded to true later
648 stopLoaderLocked();
649 mAllAppsLoaded = false;
650 mWorkspaceLoaded = false;
651 }
652 // Do this here because if the launcher activity is running it will be restarted.
653 // If it's not running startLoaderFromBackground will merely tell it that it needs
654 // to reload.
655 startLoaderFromBackground();
656 }
657
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700658 /**
659 * When the launcher is in the background, it's possible for it to miss paired
660 * configuration changes. So whenever we trigger the loader from the background
661 * tell the launcher that it needs to re-run the loader when it comes back instead
662 * of doing it now.
663 */
664 public void startLoaderFromBackground() {
665 boolean runLoader = false;
666 if (mCallbacks != null) {
667 Callbacks callbacks = mCallbacks.get();
668 if (callbacks != null) {
669 // Only actually run the loader if they're not paused.
670 if (!callbacks.setLoadOnResume()) {
671 runLoader = true;
672 }
673 }
674 }
675 if (runLoader) {
676 startLoader(mApp, false);
Joe Onorato790c2d92010-06-11 00:14:11 -0700677 }
Joe Onorato36115782010-06-17 13:28:48 -0400678 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400679
Reena Lee93f824a2011-09-23 17:20:28 -0700680 // If there is already a loader task running, tell it to stop.
681 // returns true if isLaunching() was true on the old task
682 private boolean stopLoaderLocked() {
683 boolean isLaunching = false;
684 LoaderTask oldTask = mLoaderTask;
685 if (oldTask != null) {
686 if (oldTask.isLaunching()) {
687 isLaunching = true;
688 }
689 oldTask.stopLocked();
690 }
691 return isLaunching;
692 }
693
Joe Onorato36115782010-06-17 13:28:48 -0400694 public void startLoader(Context context, boolean isLaunching) {
695 synchronized (mLock) {
696 if (DEBUG_LOADERS) {
697 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
698 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400699
Joe Onorato36115782010-06-17 13:28:48 -0400700 // Don't bother to start the thread if we know it's not going to do anything
701 if (mCallbacks != null && mCallbacks.get() != null) {
702 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -0700703 // also, don't downgrade isLaunching if we're already running
704 isLaunching = isLaunching || stopLoaderLocked();
Joe Onorato36115782010-06-17 13:28:48 -0400705 mLoaderTask = new LoaderTask(context, isLaunching);
Winson Chung7ed37742011-09-08 15:45:51 -0700706 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700707 sWorker.post(mLoaderTask);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 }
710 }
711
Joe Onorato36115782010-06-17 13:28:48 -0400712 public void stopLoader() {
713 synchronized (mLock) {
714 if (mLoaderTask != null) {
715 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 }
717 }
Joe Onorato36115782010-06-17 13:28:48 -0400718 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719
Michael Jurkac57b7a82011-08-09 22:02:20 -0700720 public boolean isAllAppsLoaded() {
721 return mAllAppsLoaded;
722 }
723
Joe Onorato36115782010-06-17 13:28:48 -0400724 /**
725 * Runnable for the thread that loads the contents of the launcher:
726 * - workspace icons
727 * - widgets
728 * - all apps icons
729 */
730 private class LoaderTask implements Runnable {
731 private Context mContext;
732 private Thread mWaitThread;
733 private boolean mIsLaunching;
734 private boolean mStopped;
735 private boolean mLoadAndBindStepFinished;
Winson Chungc3eecff2011-07-11 17:44:15 -0700736 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -0400737
738 LoaderTask(Context context, boolean isLaunching) {
739 mContext = context;
740 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -0700741 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 }
743
Joe Onorato36115782010-06-17 13:28:48 -0400744 boolean isLaunching() {
745 return mIsLaunching;
746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747
Joe Onorato36115782010-06-17 13:28:48 -0400748 private void loadAndBindWorkspace() {
749 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400750 if (DEBUG_LOADERS) {
751 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700753
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700754 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -0400755 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700756 synchronized (LoaderTask.this) {
757 if (mStopped) {
758 return;
759 }
760 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761 }
762 }
763
Joe Onorato36115782010-06-17 13:28:48 -0400764 // Bind the workspace
765 bindWorkspace();
766 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400767
Joe Onorato36115782010-06-17 13:28:48 -0400768 private void waitForIdle() {
769 // Wait until the either we're stopped or the other threads are done.
770 // This way we don't start loading all apps until the workspace has settled
771 // down.
772 synchronized (LoaderTask.this) {
773 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700774
Joe Onorato36115782010-06-17 13:28:48 -0400775 mHandler.postIdle(new Runnable() {
776 public void run() {
777 synchronized (LoaderTask.this) {
778 mLoadAndBindStepFinished = true;
779 if (DEBUG_LOADERS) {
780 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400781 }
Joe Onorato36115782010-06-17 13:28:48 -0400782 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400783 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400784 }
Joe Onorato36115782010-06-17 13:28:48 -0400785 });
786
787 while (!mStopped && !mLoadAndBindStepFinished) {
788 try {
789 this.wait();
790 } catch (InterruptedException ex) {
791 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400792 }
793 }
Joe Onorato36115782010-06-17 13:28:48 -0400794 if (DEBUG_LOADERS) {
795 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700796 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400797 + "ms for previous step to finish binding");
798 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400799 }
Joe Onorato36115782010-06-17 13:28:48 -0400800 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400801
Joe Onorato36115782010-06-17 13:28:48 -0400802 public void run() {
803 // Optimize for end-user experience: if the Launcher is up and // running with the
804 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
805 // workspace first (default).
806 final Callbacks cbk = mCallbacks.get();
807 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400808
Joe Onorato36115782010-06-17 13:28:48 -0400809 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -0400810 // Elevate priority when Home launches for the first time to avoid
811 // starving at boot time. Staring at a blank home is not cool.
812 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -0700813 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
814 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -0400815 android.os.Process.setThreadPriority(mIsLaunching
816 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
817 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400818 if (loadWorkspaceFirst) {
819 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
820 loadAndBindWorkspace();
821 } else {
822 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700823 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400824 }
825
Joe Onorato36115782010-06-17 13:28:48 -0400826 if (mStopped) {
827 break keep_running;
828 }
829
830 // Whew! Hard work done. Slow us down, and wait until the UI thread has
831 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -0400832 synchronized (mLock) {
833 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -0700834 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -0400835 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
836 }
837 }
Joe Onorato36115782010-06-17 13:28:48 -0400838 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400839
840 // second step
841 if (loadWorkspaceFirst) {
842 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -0700843 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -0400844 } else {
845 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
846 loadAndBindWorkspace();
847 }
Winson Chung7ed37742011-09-08 15:45:51 -0700848
849 // Restore the default thread priority after we are done loading items
850 synchronized (mLock) {
851 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
852 }
Joe Onorato36115782010-06-17 13:28:48 -0400853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854
Winson Chungaac01e12011-08-17 10:37:13 -0700855
856 // Update the saved icons if necessary
857 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chungb1094bd2011-08-24 16:14:08 -0700858 for (Object key : sDbIconCache.keySet()) {
859 updateSavedIcon(mContext, (ShortcutInfo) key, sDbIconCache.get(key));
Winson Chungaac01e12011-08-17 10:37:13 -0700860 }
Winson Chungb1094bd2011-08-24 16:14:08 -0700861 sDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -0700862
Joe Onorato36115782010-06-17 13:28:48 -0400863 // Clear out this reference, otherwise we end up holding it until all of the
864 // callback runnables are done.
865 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866
Joe Onorato36115782010-06-17 13:28:48 -0400867 synchronized (mLock) {
868 // If we are still the last one to be scheduled, remove ourselves.
869 if (mLoaderTask == this) {
870 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 }
Joe Onorato36115782010-06-17 13:28:48 -0400872 }
Joe Onorato36115782010-06-17 13:28:48 -0400873 }
874
875 public void stopLocked() {
876 synchronized (LoaderTask.this) {
877 mStopped = true;
878 this.notify();
879 }
880 }
881
882 /**
883 * Gets the callbacks object. If we've been stopped, or if the launcher object
884 * has somehow been garbage collected, return null instead. Pass in the Callbacks
885 * object that was around when the deferred message was scheduled, and if there's
886 * a new Callbacks object around then also return null. This will save us from
887 * calling onto it with data that will be ignored.
888 */
889 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
890 synchronized (mLock) {
891 if (mStopped) {
892 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400893 }
Joe Onorato36115782010-06-17 13:28:48 -0400894
895 if (mCallbacks == null) {
896 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400897 }
Joe Onorato36115782010-06-17 13:28:48 -0400898
899 final Callbacks callbacks = mCallbacks.get();
900 if (callbacks != oldCallbacks) {
901 return null;
902 }
903 if (callbacks == null) {
904 Log.w(TAG, "no mCallbacks");
905 return null;
906 }
907
908 return callbacks;
909 }
910 }
911
912 // check & update map of what's occupied; used to discard overlapping/invalid items
913 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700914 int containerIndex = item.screen;
915 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700916 // Return early if we detect that an item is under the hotseat button
917 if (Hotseat.isAllAppsButtonRank(item.screen)) {
918 return false;
919 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700920
921 // We use the last index to refer to the hotseat and the screen as the rank, so
922 // test and update the occupied state accordingly
923 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) {
924 Log.e(TAG, "Error loading shortcut into hotseat " + item
925 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY
926 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]);
927 return false;
928 } else {
929 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item;
930 return true;
931 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700932 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
933 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -0400934 return true;
935 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700936
Winson Chung6ba2a1b2011-09-02 16:22:11 -0700937 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -0400938 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
939 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700940 if (occupied[containerIndex][x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -0400941 Log.e(TAG, "Error loading shortcut " + item
Winson Chungf30ad5f2011-08-08 10:55:42 -0700942 + " into cell (" + containerIndex + "-" + item.screen + ":"
Joe Onorato36115782010-06-17 13:28:48 -0400943 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -0700944 + ") occupied by "
Winson Chungf30ad5f2011-08-08 10:55:42 -0700945 + occupied[containerIndex][x][y]);
Joe Onorato36115782010-06-17 13:28:48 -0400946 return false;
947 }
948 }
949 }
950 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
951 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Winson Chungf30ad5f2011-08-08 10:55:42 -0700952 occupied[containerIndex][x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -0400953 }
954 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700955
Joe Onorato36115782010-06-17 13:28:48 -0400956 return true;
957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958
Joe Onorato36115782010-06-17 13:28:48 -0400959 private void loadWorkspace() {
960 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961
Joe Onorato36115782010-06-17 13:28:48 -0400962 final Context context = mContext;
963 final ContentResolver contentResolver = context.getContentResolver();
964 final PackageManager manager = context.getPackageManager();
965 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
966 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400967
Adam Cohen4eac29a2011-07-11 17:53:37 -0700968 sWorkspaceItems.clear();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700969 sAppWidgets.clear();
970 sFolders.clear();
971 sItemsIdMap.clear();
Winson Chungb1094bd2011-08-24 16:14:08 -0700972 sDbIconCache.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -0800973
Joe Onorato36115782010-06-17 13:28:48 -0400974 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975
Joe Onorato36115782010-06-17 13:28:48 -0400976 final Cursor c = contentResolver.query(
Winson Chunge61e93e2011-09-12 10:59:49 -0700977 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -0400978
Winson Chungf30ad5f2011-08-08 10:55:42 -0700979 // +1 for the hotseat (it can be larger than the workspace)
Winson Chung36f97362011-09-07 11:20:10 -0700980 // Load workspace in reverse order to ensure that latest items are loaded first (and
981 // before any earlier duplicates)
Adam Cohend22015c2010-07-26 22:02:18 -0700982 final ItemInfo occupied[][][] =
Winson Chungf30ad5f2011-08-08 10:55:42 -0700983 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984
Joe Onorato36115782010-06-17 13:28:48 -0400985 try {
986 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
987 final int intentIndex = c.getColumnIndexOrThrow
988 (LauncherSettings.Favorites.INTENT);
989 final int titleIndex = c.getColumnIndexOrThrow
990 (LauncherSettings.Favorites.TITLE);
991 final int iconTypeIndex = c.getColumnIndexOrThrow(
992 LauncherSettings.Favorites.ICON_TYPE);
993 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
994 final int iconPackageIndex = c.getColumnIndexOrThrow(
995 LauncherSettings.Favorites.ICON_PACKAGE);
996 final int iconResourceIndex = c.getColumnIndexOrThrow(
997 LauncherSettings.Favorites.ICON_RESOURCE);
998 final int containerIndex = c.getColumnIndexOrThrow(
999 LauncherSettings.Favorites.CONTAINER);
1000 final int itemTypeIndex = c.getColumnIndexOrThrow(
1001 LauncherSettings.Favorites.ITEM_TYPE);
1002 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1003 LauncherSettings.Favorites.APPWIDGET_ID);
1004 final int screenIndex = c.getColumnIndexOrThrow(
1005 LauncherSettings.Favorites.SCREEN);
1006 final int cellXIndex = c.getColumnIndexOrThrow
1007 (LauncherSettings.Favorites.CELLX);
1008 final int cellYIndex = c.getColumnIndexOrThrow
1009 (LauncherSettings.Favorites.CELLY);
1010 final int spanXIndex = c.getColumnIndexOrThrow
1011 (LauncherSettings.Favorites.SPANX);
1012 final int spanYIndex = c.getColumnIndexOrThrow(
1013 LauncherSettings.Favorites.SPANY);
1014 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1015 final int displayModeIndex = c.getColumnIndexOrThrow(
1016 LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017
Joe Onorato36115782010-06-17 13:28:48 -04001018 ShortcutInfo info;
1019 String intentDescription;
1020 LauncherAppWidgetInfo appWidgetInfo;
1021 int container;
1022 long id;
1023 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024
Joe Onorato36115782010-06-17 13:28:48 -04001025 while (!mStopped && c.moveToNext()) {
1026 try {
1027 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028
Joe Onorato36115782010-06-17 13:28:48 -04001029 switch (itemType) {
1030 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1031 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1032 intentDescription = c.getString(intentIndex);
1033 try {
1034 intent = Intent.parseUri(intentDescription, 0);
1035 } catch (URISyntaxException e) {
1036 continue;
1037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038
Joe Onorato36115782010-06-17 13:28:48 -04001039 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1040 info = getShortcutInfo(manager, intent, context, c, iconIndex,
Winson Chungc3eecff2011-07-11 17:44:15 -07001041 titleIndex, mLabelCache);
Joe Onorato36115782010-06-17 13:28:48 -04001042 } else {
1043 info = getShortcutInfo(c, context, iconTypeIndex,
1044 iconPackageIndex, iconResourceIndex, iconIndex,
1045 titleIndex);
1046 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047
Joe Onorato36115782010-06-17 13:28:48 -04001048 if (info != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001049 info.intent = intent;
1050 info.id = c.getLong(idIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 container = c.getInt(containerIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001052 info.container = container;
1053 info.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001054 info.cellX = c.getInt(cellXIndex);
1055 info.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056
Daniel Sandler8802e962010-05-26 16:28:16 -04001057 // check & update map of what's occupied
Joe Onorato36115782010-06-17 13:28:48 -04001058 if (!checkItemPlacement(occupied, info)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001059 break;
1060 }
1061
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062 switch (container) {
Joe Onorato36115782010-06-17 13:28:48 -04001063 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001064 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001065 sWorkspaceItems.add(info);
Joe Onorato36115782010-06-17 13:28:48 -04001066 break;
1067 default:
1068 // Item is in a user folder
Adam Cohendf2cc412011-04-27 16:56:57 -07001069 FolderInfo folderInfo =
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001070 findOrMakeFolder(sFolders, container);
Joe Onorato36115782010-06-17 13:28:48 -04001071 folderInfo.add(info);
1072 break;
1073 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001074 sItemsIdMap.put(info.id, info);
Joe Onorato17a89222011-02-08 17:26:11 -08001075
1076 // now that we've loaded everthing re-save it with the
1077 // icon in case it disappears somehow.
Winson Chungb1094bd2011-08-24 16:14:08 -07001078 queueIconToBeChecked(sDbIconCache, info, c, iconIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001079 } else {
1080 // Failed to load the shortcut, probably because the
1081 // activity manager couldn't resolve it (maybe the app
1082 // was uninstalled), or the db row was somehow screwed up.
1083 // Delete it.
1084 id = c.getLong(idIndex);
1085 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1086 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1087 id, false), null, null);
1088 }
1089 break;
1090
Adam Cohendf2cc412011-04-27 16:56:57 -07001091 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato36115782010-06-17 13:28:48 -04001092 id = c.getLong(idIndex);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001093 FolderInfo folderInfo = findOrMakeFolder(sFolders, id);
Joe Onorato36115782010-06-17 13:28:48 -04001094
Winson Chungaafa03c2010-06-11 17:34:16 -07001095 folderInfo.title = c.getString(titleIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001096 folderInfo.id = id;
1097 container = c.getInt(containerIndex);
1098 folderInfo.container = container;
1099 folderInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001100 folderInfo.cellX = c.getInt(cellXIndex);
1101 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001102
1103 // check & update map of what's occupied
1104 if (!checkItemPlacement(occupied, folderInfo)) {
1105 break;
1106 }
Joe Onorato36115782010-06-17 13:28:48 -04001107 switch (container) {
1108 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
Winson Chung3d503fb2011-07-13 17:25:49 -07001109 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
Adam Cohen4eac29a2011-07-11 17:53:37 -07001110 sWorkspaceItems.add(folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001111 break;
1112 }
1113
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001114 sItemsIdMap.put(folderInfo.id, folderInfo);
1115 sFolders.put(folderInfo.id, folderInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001116 break;
1117
Joe Onorato36115782010-06-17 13:28:48 -04001118 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1119 // Read all Launcher-specific widget details
1120 int appWidgetId = c.getInt(appWidgetIdIndex);
1121 id = c.getLong(idIndex);
1122
1123 final AppWidgetProviderInfo provider =
1124 widgets.getAppWidgetInfo(appWidgetId);
Winson Chungaafa03c2010-06-11 17:34:16 -07001125
Joe Onorato36115782010-06-17 13:28:48 -04001126 if (!isSafeMode && (provider == null || provider.provider == null ||
1127 provider.provider.getPackageName() == null)) {
Adam Cohen16d7ffc2011-10-05 17:49:14 -07001128 String log = "Deleting widget that isn't installed anymore: id="
1129 + id + " appWidgetId=" + appWidgetId;
1130 Log.e(TAG, log);
1131 Launcher.sDumpLogs.add(log);
Joe Onorato36115782010-06-17 13:28:48 -04001132 itemsToRemove.add(id);
1133 } else {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001134 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001135 appWidgetInfo.id = id;
1136 appWidgetInfo.screen = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -07001137 appWidgetInfo.cellX = c.getInt(cellXIndex);
1138 appWidgetInfo.cellY = c.getInt(cellYIndex);
1139 appWidgetInfo.spanX = c.getInt(spanXIndex);
1140 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001141
1142 container = c.getInt(containerIndex);
Winson Chung3d503fb2011-07-13 17:25:49 -07001143 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1144 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Joe Onorato36115782010-06-17 13:28:48 -04001145 Log.e(TAG, "Widget found where container "
Winson Chung3d503fb2011-07-13 17:25:49 -07001146 + "!= CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Joe Onorato36115782010-06-17 13:28:48 -04001147 continue;
1148 }
1149 appWidgetInfo.container = c.getInt(containerIndex);
1150
1151 // check & update map of what's occupied
1152 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1153 break;
1154 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001155 sItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1156 sAppWidgets.add(appWidgetInfo);
Joe Onorato36115782010-06-17 13:28:48 -04001157 }
1158 break;
Romain Guy5c16f3e2010-01-12 17:24:58 -08001159 }
Joe Onorato36115782010-06-17 13:28:48 -04001160 } catch (Exception e) {
1161 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001162 }
1163 }
Joe Onorato36115782010-06-17 13:28:48 -04001164 } finally {
1165 c.close();
1166 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001167
Joe Onorato36115782010-06-17 13:28:48 -04001168 if (itemsToRemove.size() > 0) {
1169 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1170 LauncherSettings.Favorites.CONTENT_URI);
1171 // Remove dead items
1172 for (long id : itemsToRemove) {
1173 if (DEBUG_LOADERS) {
1174 Log.d(TAG, "Removed id = " + id);
1175 }
1176 // Don't notify content observers
1177 try {
1178 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1179 null, null);
1180 } catch (RemoteException e) {
1181 Log.w(TAG, "Could not remove id = " + id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001182 }
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184 }
1185
Joe Onorato36115782010-06-17 13:28:48 -04001186 if (DEBUG_LOADERS) {
1187 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1188 Log.d(TAG, "workspace layout: ");
Adam Cohend22015c2010-07-26 22:02:18 -07001189 for (int y = 0; y < mCellCountY; y++) {
Joe Onorato36115782010-06-17 13:28:48 -04001190 String line = "";
1191 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) {
1192 if (s > 0) {
1193 line += " | ";
1194 }
Adam Cohend22015c2010-07-26 22:02:18 -07001195 for (int x = 0; x < mCellCountX; x++) {
Joe Onorato36115782010-06-17 13:28:48 -04001196 line += ((occupied[s][x][y] != null) ? "#" : ".");
1197 }
1198 }
1199 Log.d(TAG, "[ " + line + " ]");
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 }
Joe Onorato36115782010-06-17 13:28:48 -04001201 }
1202 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203
Joe Onorato36115782010-06-17 13:28:48 -04001204 /**
1205 * Read everything out of our database.
1206 */
1207 private void bindWorkspace() {
1208 final long t = SystemClock.uptimeMillis();
1209
1210 // Don't use these two variables in any of the callback runnables.
1211 // Otherwise we hold a reference to them.
1212 final Callbacks oldCallbacks = mCallbacks.get();
1213 if (oldCallbacks == null) {
1214 // This launcher has exited and nobody bothered to tell us. Just bail.
1215 Log.w(TAG, "LoaderTask running with no launcher");
1216 return;
1217 }
1218
1219 int N;
1220 // Tell the workspace that we're about to start firing items at it
1221 mHandler.post(new Runnable() {
1222 public void run() {
1223 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1224 if (callbacks != null) {
1225 callbacks.startBinding();
1226 }
1227 }
1228 });
Winson Chung603bcb92011-09-02 11:45:39 -07001229
1230 // Unbind previously bound workspace items to prevent a leak of AppWidgetHostViews.
1231 final ArrayList<ItemInfo> workspaceItems = unbindWorkspaceItemsOnMainThread();
1232
Joe Onorato36115782010-06-17 13:28:48 -04001233 // Add the items to the workspace.
Winson Chung603bcb92011-09-02 11:45:39 -07001234 N = workspaceItems.size();
Joe Onorato36115782010-06-17 13:28:48 -04001235 for (int i=0; i<N; i+=ITEMS_CHUNK) {
1236 final int start = i;
1237 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 mHandler.post(new Runnable() {
1239 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001240 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001242 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 }
1244 }
1245 });
Joe Onorato36115782010-06-17 13:28:48 -04001246 }
Winson Chung603bcb92011-09-02 11:45:39 -07001247 // Ensure that we don't use the same folders data structure on the main thread
1248 final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(sFolders);
Joe Onorato36115782010-06-17 13:28:48 -04001249 mHandler.post(new Runnable() {
1250 public void run() {
1251 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1252 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001253 callbacks.bindFolders(folders);
Joe Onorato36115782010-06-17 13:28:48 -04001254 }
1255 }
1256 });
1257 // Wait until the queue goes empty.
1258 mHandler.post(new Runnable() {
1259 public void run() {
1260 if (DEBUG_LOADERS) {
1261 Log.d(TAG, "Going to start binding widgets soon.");
1262 }
1263 }
1264 });
1265 // Bind the widgets, one at a time.
1266 // WARNING: this is calling into the workspace from the background thread,
1267 // but since getCurrentScreen() just returns the int, we should be okay. This
1268 // is just a hint for the order, and if it's wrong, we'll be okay.
1269 // TODO: instead, we should have that push the current screen into here.
1270 final int currentScreen = oldCallbacks.getCurrentWorkspaceScreen();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001271 N = sAppWidgets.size();
Joe Onorato36115782010-06-17 13:28:48 -04001272 // once for the current screen
1273 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001274 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001275 if (widget.screen == currentScreen) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276 mHandler.post(new Runnable() {
1277 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001278 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001280 callbacks.bindAppWidget(widget);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001281 }
1282 }
1283 });
1284 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 }
Joe Onorato36115782010-06-17 13:28:48 -04001286 // once for the other screens
1287 for (int i=0; i<N; i++) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001288 final LauncherAppWidgetInfo widget = sAppWidgets.get(i);
Joe Onorato36115782010-06-17 13:28:48 -04001289 if (widget.screen != currentScreen) {
1290 mHandler.post(new Runnable() {
1291 public void run() {
1292 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1293 if (callbacks != null) {
1294 callbacks.bindAppWidget(widget);
1295 }
1296 }
1297 });
Joe Onoratocc67f472010-06-08 10:54:30 -07001298 }
1299 }
Joe Onorato36115782010-06-17 13:28:48 -04001300 // Tell the workspace that we're done.
1301 mHandler.post(new Runnable() {
1302 public void run() {
1303 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1304 if (callbacks != null) {
1305 callbacks.finishBindingItems();
1306 }
1307 }
1308 });
1309 // If we're profiling, this is the last thing in the queue.
1310 mHandler.post(new Runnable() {
1311 public void run() {
1312 if (DEBUG_LOADERS) {
1313 Log.d(TAG, "bound workspace in "
1314 + (SystemClock.uptimeMillis()-t) + "ms");
1315 }
1316 }
1317 });
1318 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001319
Joe Onorato36115782010-06-17 13:28:48 -04001320 private void loadAndBindAllApps() {
1321 if (DEBUG_LOADERS) {
1322 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1323 }
1324 if (!mAllAppsLoaded) {
1325 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07001326 synchronized (LoaderTask.this) {
1327 if (mStopped) {
1328 return;
1329 }
1330 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001331 }
Joe Onorato36115782010-06-17 13:28:48 -04001332 } else {
1333 onlyBindAllApps();
1334 }
1335 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001336
Joe Onorato36115782010-06-17 13:28:48 -04001337 private void onlyBindAllApps() {
1338 final Callbacks oldCallbacks = mCallbacks.get();
1339 if (oldCallbacks == null) {
1340 // This launcher has exited and nobody bothered to tell us. Just bail.
1341 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1342 return;
1343 }
1344
1345 // shallow copy
1346 final ArrayList<ApplicationInfo> list
1347 = (ArrayList<ApplicationInfo>)mAllAppsList.data.clone();
1348 mHandler.post(new Runnable() {
1349 public void run() {
1350 final long t = SystemClock.uptimeMillis();
1351 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1352 if (callbacks != null) {
1353 callbacks.bindAllApplications(list);
1354 }
1355 if (DEBUG_LOADERS) {
1356 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1357 + (SystemClock.uptimeMillis()-t) + "ms");
1358 }
1359 }
1360 });
1361
1362 }
1363
1364 private void loadAllAppsByBatch() {
1365 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1366
1367 // Don't use these two variables in any of the callback runnables.
1368 // Otherwise we hold a reference to them.
1369 final Callbacks oldCallbacks = mCallbacks.get();
1370 if (oldCallbacks == null) {
1371 // This launcher has exited and nobody bothered to tell us. Just bail.
1372 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
1373 return;
1374 }
1375
1376 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1377 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1378
1379 final PackageManager packageManager = mContext.getPackageManager();
1380 List<ResolveInfo> apps = null;
1381
1382 int N = Integer.MAX_VALUE;
1383
1384 int startIndex;
1385 int i=0;
1386 int batchSize = -1;
1387 while (i < N && !mStopped) {
1388 if (i == 0) {
1389 mAllAppsList.clear();
1390 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1391 apps = packageManager.queryIntentActivities(mainIntent, 0);
1392 if (DEBUG_LOADERS) {
1393 Log.d(TAG, "queryIntentActivities took "
1394 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
1395 }
1396 if (apps == null) {
1397 return;
1398 }
1399 N = apps.size();
1400 if (DEBUG_LOADERS) {
1401 Log.d(TAG, "queryIntentActivities got " + N + " apps");
1402 }
1403 if (N == 0) {
1404 // There are no apps?!?
1405 return;
1406 }
1407 if (mBatchSize == 0) {
1408 batchSize = N;
1409 } else {
1410 batchSize = mBatchSize;
1411 }
1412
1413 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1414 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07001415 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001416 if (DEBUG_LOADERS) {
1417 Log.d(TAG, "sort took "
1418 + (SystemClock.uptimeMillis()-sortTime) + "ms");
1419 }
1420 }
1421
1422 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1423
1424 startIndex = i;
1425 for (int j=0; i<N && j<batchSize; j++) {
1426 // This builds the icon bitmaps.
Winson Chungc3eecff2011-07-11 17:44:15 -07001427 mAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07001428 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04001429 i++;
1430 }
1431
1432 final boolean first = i <= batchSize;
1433 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1434 final ArrayList<ApplicationInfo> added = mAllAppsList.added;
1435 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1436
Joe Onoratocc67f472010-06-08 10:54:30 -07001437 mHandler.post(new Runnable() {
1438 public void run() {
1439 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07001440 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001441 if (first) {
1442 callbacks.bindAllApplications(added);
1443 } else {
1444 callbacks.bindAppsAdded(added);
1445 }
1446 if (DEBUG_LOADERS) {
1447 Log.d(TAG, "bound " + added.size() + " apps in "
1448 + (SystemClock.uptimeMillis() - t) + "ms");
1449 }
1450 } else {
1451 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07001452 }
1453 }
1454 });
1455
Daniel Sandlerdca66122010-04-13 16:23:58 -04001456 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04001457 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
1458 + (SystemClock.uptimeMillis()-t2) + "ms");
1459 }
1460
1461 if (mAllAppsLoadDelay > 0 && i < N) {
1462 try {
1463 if (DEBUG_LOADERS) {
1464 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
1465 }
1466 Thread.sleep(mAllAppsLoadDelay);
1467 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468 }
1469 }
1470
Joe Onorato36115782010-06-17 13:28:48 -04001471 if (DEBUG_LOADERS) {
1472 Log.d(TAG, "cached all " + N + " apps in "
1473 + (SystemClock.uptimeMillis()-t) + "ms"
1474 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08001475 }
1476 }
1477
1478 public void dumpState() {
Joe Onorato36115782010-06-17 13:28:48 -04001479 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
1480 Log.d(TAG, "mLoaderTask.mWaitThread=" + mWaitThread);
1481 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
1482 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1483 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Adam Cohen4eac29a2011-07-11 17:53:37 -07001484 Log.d(TAG, "mItems size=" + sWorkspaceItems.size());
Joe Onorato36115782010-06-17 13:28:48 -04001485 }
1486 }
1487
1488 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07001489 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04001490 }
1491
1492 private class PackageUpdatedTask implements Runnable {
1493 int mOp;
1494 String[] mPackages;
1495
1496 public static final int OP_NONE = 0;
1497 public static final int OP_ADD = 1;
1498 public static final int OP_UPDATE = 2;
1499 public static final int OP_REMOVE = 3; // uninstlled
1500 public static final int OP_UNAVAILABLE = 4; // external media unmounted
1501
1502
1503 public PackageUpdatedTask(int op, String[] packages) {
1504 mOp = op;
1505 mPackages = packages;
1506 }
1507
1508 public void run() {
1509 final Context context = mApp;
1510
1511 final String[] packages = mPackages;
1512 final int N = packages.length;
1513 switch (mOp) {
1514 case OP_ADD:
1515 for (int i=0; i<N; i++) {
1516 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
1517 mAllAppsList.addPackage(context, packages[i]);
1518 }
1519 break;
1520 case OP_UPDATE:
1521 for (int i=0; i<N; i++) {
1522 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
1523 mAllAppsList.updatePackage(context, packages[i]);
1524 }
1525 break;
1526 case OP_REMOVE:
1527 case OP_UNAVAILABLE:
1528 for (int i=0; i<N; i++) {
1529 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
1530 mAllAppsList.removePackage(packages[i]);
1531 }
1532 break;
1533 }
1534
1535 ArrayList<ApplicationInfo> added = null;
1536 ArrayList<ApplicationInfo> removed = null;
1537 ArrayList<ApplicationInfo> modified = null;
1538
1539 if (mAllAppsList.added.size() > 0) {
1540 added = mAllAppsList.added;
1541 mAllAppsList.added = new ArrayList<ApplicationInfo>();
1542 }
1543 if (mAllAppsList.removed.size() > 0) {
1544 removed = mAllAppsList.removed;
1545 mAllAppsList.removed = new ArrayList<ApplicationInfo>();
1546 for (ApplicationInfo info: removed) {
1547 mIconCache.remove(info.intent.getComponent());
1548 }
1549 }
1550 if (mAllAppsList.modified.size() > 0) {
1551 modified = mAllAppsList.modified;
1552 mAllAppsList.modified = new ArrayList<ApplicationInfo>();
1553 }
1554
1555 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
1556 if (callbacks == null) {
1557 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
1558 return;
1559 }
1560
1561 if (added != null) {
1562 final ArrayList<ApplicationInfo> addedFinal = added;
1563 mHandler.post(new Runnable() {
1564 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001565 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1566 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001567 callbacks.bindAppsAdded(addedFinal);
1568 }
1569 }
1570 });
1571 }
1572 if (modified != null) {
1573 final ArrayList<ApplicationInfo> modifiedFinal = modified;
1574 mHandler.post(new Runnable() {
1575 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001576 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1577 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001578 callbacks.bindAppsUpdated(modifiedFinal);
1579 }
1580 }
1581 });
1582 }
1583 if (removed != null) {
1584 final boolean permanent = mOp != OP_UNAVAILABLE;
1585 final ArrayList<ApplicationInfo> removedFinal = removed;
1586 mHandler.post(new Runnable() {
1587 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001588 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1589 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001590 callbacks.bindAppsRemoved(removedFinal, permanent);
1591 }
1592 }
1593 });
Joe Onoratobe386092009-11-17 17:32:16 -08001594 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001595
1596 mHandler.post(new Runnable() {
1597 @Override
1598 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07001599 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1600 if (callbacks == cb && cb != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001601 callbacks.bindPackagesUpdated();
1602 }
1603 }
1604 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04001605 }
1606 }
1607
1608 /**
Joe Onorato56d82912010-03-07 14:32:10 -05001609 * This is called from the code that adds shortcuts from the intent receiver. This
1610 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04001611 */
Joe Onorato56d82912010-03-07 14:32:10 -05001612 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001613 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05001614 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001615
Joe Onorato56d82912010-03-07 14:32:10 -05001616 /**
1617 * Make an ShortcutInfo object for a shortcut that is an application.
1618 *
1619 * If c is not null, then it will be used to fill in missing data like the title and icon.
1620 */
1621 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07001622 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05001623 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001624 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001625
1626 ComponentName componentName = intent.getComponent();
1627 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001628 return null;
1629 }
1630
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001631 // TODO: See if the PackageManager knows about this case. If it doesn't
1632 // then return null & delete this.
1633
Joe Onorato56d82912010-03-07 14:32:10 -05001634 // the resource -- This may implicitly give us back the fallback icon,
1635 // but don't worry about that. All we're doing with usingFallbackIcon is
1636 // to avoid saving lots of copies of that in the database, and most apps
1637 // have icons anyway.
1638 final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0);
1639 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07001640 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001641 }
Joe Onorato56d82912010-03-07 14:32:10 -05001642 // the db
1643 if (icon == null) {
1644 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001645 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001646 }
1647 }
1648 // the fallback icon
1649 if (icon == null) {
1650 icon = getFallbackIcon();
1651 info.usingFallbackIcon = true;
1652 }
1653 info.setIcon(icon);
1654
1655 // from the resource
1656 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001657 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
1658 if (labelCache != null && labelCache.containsKey(key)) {
1659 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07001660 } else {
1661 info.title = resolveInfo.activityInfo.loadLabel(manager);
1662 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07001663 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07001664 }
1665 }
Joe Onorato56d82912010-03-07 14:32:10 -05001666 }
1667 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05001669 if (c != null) {
1670 info.title = c.getString(titleIndex);
1671 }
1672 }
1673 // fall back to the class name of the activity
1674 if (info.title == null) {
1675 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001676 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
1678 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07001680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08001682 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001684 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05001685 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
1686 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687
Joe Onorato56d82912010-03-07 14:32:10 -05001688 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001689 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07001692 // TODO: If there's an explicit component and we can't install that, delete it.
1693
Joe Onorato56d82912010-03-07 14:32:10 -05001694 info.title = c.getString(titleIndex);
1695
Joe Onorato9c1289c2009-08-17 11:03:03 -04001696 int iconType = c.getInt(iconTypeIndex);
1697 switch (iconType) {
1698 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
1699 String packageName = c.getString(iconPackageIndex);
1700 String resourceName = c.getString(iconResourceIndex);
1701 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05001702 info.customIcon = false;
1703 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001705 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05001706 if (resources != null) {
1707 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001708 icon = Utilities.createIconBitmap(
1709 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001710 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001711 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05001712 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
Joe Onorato56d82912010-03-07 14:32:10 -05001714 // the db
1715 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07001716 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001717 }
1718 // the fallback icon
1719 if (icon == null) {
1720 icon = getFallbackIcon();
1721 info.usingFallbackIcon = true;
1722 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001723 break;
1724 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07001725 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05001726 if (icon == null) {
1727 icon = getFallbackIcon();
1728 info.customIcon = false;
1729 info.usingFallbackIcon = true;
1730 } else {
1731 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733 break;
1734 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08001735 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05001736 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001737 info.customIcon = false;
1738 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08001740 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741 return info;
1742 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743
Michael Jurka931dc972011-08-05 15:08:15 -07001744 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Joe Onorato56d82912010-03-07 14:32:10 -05001745 if (false) {
1746 Log.d(TAG, "getIconFromCursor app="
1747 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
1748 }
1749 byte[] data = c.getBlob(iconIndex);
1750 try {
Michael Jurka931dc972011-08-05 15:08:15 -07001751 return Utilities.createIconBitmap(
1752 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05001753 } catch (Exception e) {
1754 return null;
1755 }
1756 }
1757
Winson Chung3d503fb2011-07-13 17:25:49 -07001758 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
1759 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001760 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07001761 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001762
1763 return info;
1764 }
1765
Winson Chunga9abd0e2010-10-27 17:18:37 -07001766 /**
Winson Chung55cef262010-10-28 14:14:18 -07001767 * Attempts to find an AppWidgetProviderInfo that matches the given component.
1768 */
1769 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
1770 ComponentName component) {
1771 List<AppWidgetProviderInfo> widgets =
1772 AppWidgetManager.getInstance(context).getInstalledProviders();
1773 for (AppWidgetProviderInfo info : widgets) {
1774 if (info.provider.equals(component)) {
1775 return info;
1776 }
1777 }
1778 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07001779 }
1780
Winson Chung68846fd2010-10-29 11:00:27 -07001781 /**
1782 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
1783 */
1784 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
1785 final PackageManager packageManager = context.getPackageManager();
1786 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
1787 new ArrayList<WidgetMimeTypeHandlerData>();
1788
1789 final Intent supportsIntent =
1790 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
1791 supportsIntent.setType(mimeType);
1792
1793 // Create a set of widget configuration components that we can test against
1794 final List<AppWidgetProviderInfo> widgets =
1795 AppWidgetManager.getInstance(context).getInstalledProviders();
1796 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
1797 new HashMap<ComponentName, AppWidgetProviderInfo>();
1798 for (AppWidgetProviderInfo info : widgets) {
1799 configurationComponentToWidget.put(info.configure, info);
1800 }
1801
1802 // Run through each of the intents that can handle this type of clip data, and cross
1803 // reference them with the components that are actual configuration components
1804 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
1805 PackageManager.MATCH_DEFAULT_ONLY);
1806 for (ResolveInfo info : activities) {
1807 final ActivityInfo activityInfo = info.activityInfo;
1808 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
1809 activityInfo.name);
1810 if (configurationComponentToWidget.containsKey(infoComponent)) {
1811 supportedConfigurationActivities.add(
1812 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
1813 configurationComponentToWidget.get(infoComponent)));
1814 }
1815 }
1816 return supportedConfigurationActivities;
1817 }
1818
Winson Chunga9abd0e2010-10-27 17:18:37 -07001819 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001820 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
1821 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1822 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
1823
1824 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001825 boolean customIcon = false;
1826 ShortcutIconResource iconResource = null;
1827
1828 if (bitmap != null && bitmap instanceof Bitmap) {
1829 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001830 customIcon = true;
1831 } else {
1832 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
1833 if (extra != null && extra instanceof ShortcutIconResource) {
1834 try {
1835 iconResource = (ShortcutIconResource) extra;
1836 final PackageManager packageManager = context.getPackageManager();
1837 Resources resources = packageManager.getResourcesForApplication(
1838 iconResource.packageName);
1839 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07001840 icon = Utilities.createIconBitmap(
1841 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001842 } catch (Exception e) {
1843 Log.w(TAG, "Could not load shortcut icon: " + extra);
1844 }
1845 }
1846 }
1847
Michael Jurkac9d95c52011-08-29 14:03:34 -07001848 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05001849
1850 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001851 if (fallbackIcon != null) {
1852 icon = fallbackIcon;
1853 } else {
1854 icon = getFallbackIcon();
1855 info.usingFallbackIcon = true;
1856 }
Joe Onorato56d82912010-03-07 14:32:10 -05001857 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001858 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05001859
Joe Onorato0589f0f2010-02-08 13:44:00 -08001860 info.title = name;
1861 info.intent = intent;
1862 info.customIcon = customIcon;
1863 info.iconResource = iconResource;
1864
1865 return info;
1866 }
1867
Winson Chungaac01e12011-08-17 10:37:13 -07001868 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
1869 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08001870 // If apps can't be on SD, don't even bother.
1871 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07001872 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08001873 }
Joe Onorato56d82912010-03-07 14:32:10 -05001874 // If this icon doesn't have a custom icon, check to see
1875 // what's stored in the DB, and if it doesn't match what
1876 // we're going to show, store what we are going to show back
1877 // into the DB. We do this so when we're loading, if the
1878 // package manager can't find an icon (for example because
1879 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07001880 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07001881 cache.put(info, c.getBlob(iconIndex));
1882 return true;
1883 }
1884 return false;
1885 }
1886 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
1887 boolean needSave = false;
1888 try {
1889 if (data != null) {
1890 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
1891 Bitmap loaded = info.getIcon(mIconCache);
1892 needSave = !saved.sameAs(loaded);
1893 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05001894 needSave = true;
1895 }
Winson Chungaac01e12011-08-17 10:37:13 -07001896 } catch (Exception e) {
1897 needSave = true;
1898 }
1899 if (needSave) {
1900 Log.d(TAG, "going to save icon bitmap for info=" + info);
1901 // This is slower than is ideal, but this only happens once
1902 // or when the app is updated with a new icon.
1903 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05001904 }
1905 }
1906
Joe Onorato9c1289c2009-08-17 11:03:03 -04001907 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07001908 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001909 * or make a new one.
1910 */
Adam Cohendf2cc412011-04-27 16:56:57 -07001911 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912 // See if a placeholder was created for us already
1913 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07001914 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001915 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07001916 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001917 folders.put(id, folderInfo);
1918 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001919 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
1921
Joe Onorato9c1289c2009-08-17 11:03:03 -04001922 private static final Collator sCollator = Collator.getInstance();
Joe Onoratob0c27f22009-12-01 16:19:38 -08001923 public static final Comparator<ApplicationInfo> APP_NAME_COMPARATOR
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 = new Comparator<ApplicationInfo>() {
1925 public final int compare(ApplicationInfo a, ApplicationInfo b) {
Michael Jurka5b1808d2011-07-11 19:59:46 -07001926 int result = sCollator.compare(a.title.toString(), b.title.toString());
1927 if (result == 0) {
1928 result = a.componentName.compareTo(b.componentName);
1929 }
1930 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 };
Winson Chung78403fe2011-01-21 15:38:02 -08001933 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
1934 = new Comparator<ApplicationInfo>() {
1935 public final int compare(ApplicationInfo a, ApplicationInfo b) {
1936 if (a.firstInstallTime < b.firstInstallTime) return 1;
1937 if (a.firstInstallTime > b.firstInstallTime) return -1;
1938 return 0;
1939 }
1940 };
Winson Chung785d2eb2011-04-14 16:08:02 -07001941 public static final Comparator<AppWidgetProviderInfo> WIDGET_NAME_COMPARATOR
1942 = new Comparator<AppWidgetProviderInfo>() {
1943 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
1944 return sCollator.compare(a.label.toString(), b.label.toString());
1945 }
1946 };
Winson Chung5308f242011-08-18 12:12:41 -07001947 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
1948 if (info.activityInfo != null) {
1949 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
1950 } else {
1951 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
1952 }
1953 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001954 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
1955 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07001956 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001957 ShortcutNameComparator(PackageManager pm) {
1958 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07001959 mLabelCache = new HashMap<Object, CharSequence>();
1960 }
1961 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
1962 mPackageManager = pm;
1963 mLabelCache = labelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07001964 }
1965 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07001966 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07001967 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
1968 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
1969 if (mLabelCache.containsKey(keyA)) {
1970 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001971 } else {
1972 labelA = a.loadLabel(mPackageManager).toString();
1973
Winson Chung5308f242011-08-18 12:12:41 -07001974 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07001975 }
Winson Chung5308f242011-08-18 12:12:41 -07001976 if (mLabelCache.containsKey(keyB)) {
1977 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001978 } else {
1979 labelB = b.loadLabel(mPackageManager).toString();
1980
Winson Chung5308f242011-08-18 12:12:41 -07001981 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07001982 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001983 return sCollator.compare(labelA, labelB);
1984 }
1985 };
Winson Chung1ed747a2011-05-03 16:18:34 -07001986 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
1987 private PackageManager mPackageManager;
1988 private HashMap<Object, String> mLabelCache;
1989 WidgetAndShortcutNameComparator(PackageManager pm) {
1990 mPackageManager = pm;
1991 mLabelCache = new HashMap<Object, String>();
1992 }
1993 public final int compare(Object a, Object b) {
1994 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07001995 if (mLabelCache.containsKey(a)) {
1996 labelA = mLabelCache.get(a);
1997 } else {
1998 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07001999 ((AppWidgetProviderInfo) a).label :
2000 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002001 mLabelCache.put(a, labelA);
2002 }
2003 if (mLabelCache.containsKey(b)) {
2004 labelB = mLabelCache.get(b);
2005 } else {
2006 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002007 ((AppWidgetProviderInfo) b).label :
2008 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002009 mLabelCache.put(b, labelB);
2010 }
Winson Chung1ed747a2011-05-03 16:18:34 -07002011 return sCollator.compare(labelA, labelB);
2012 }
2013 };
Joe Onoratobe386092009-11-17 17:32:16 -08002014
2015 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002016 Log.d(TAG, "mCallbacks=" + mCallbacks);
2017 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mAllAppsList.data);
2018 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mAllAppsList.added);
2019 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mAllAppsList.removed);
2020 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002021 if (mLoaderTask != null) {
2022 mLoaderTask.dumpState();
2023 } else {
2024 Log.d(TAG, "mLoaderTask=null");
2025 }
Joe Onoratobe386092009-11-17 17:32:16 -08002026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027}