1 /* 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 17 package com.android.launcher2; 18 19 import android.app.SearchManager; 20 import android.appwidget.AppWidgetManager; 21 import android.appwidget.AppWidgetProviderInfo; 22 import android.content.BroadcastReceiver; 23 import android.content.ComponentName; 24 import android.content.ContentProviderClient; 25 import android.content.ContentResolver; 26 import android.content.ContentValues; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.Intent.ShortcutIconResource; 30 import android.content.pm.ActivityInfo; 31 import android.content.pm.PackageInfo; 32 import android.content.pm.PackageManager; 33 import android.content.pm.PackageManager.NameNotFoundException; 34 import android.content.pm.ResolveInfo; 35 import android.content.res.Configuration; 36 import android.content.res.Resources; 37 import android.database.Cursor; 38 import android.graphics.Bitmap; 39 import android.graphics.BitmapFactory; 40 import android.net.Uri; 41 import android.os.Environment; 42 import android.os.Handler; 43 import android.os.HandlerThread; 44 import android.os.Parcelable; 45 import android.os.Process; 46 import android.os.RemoteException; 47 import android.os.SystemClock; 48 import android.util.Log; 49 50 import com.android.launcher.R; 51 import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData; 52 53 import java.lang.ref.WeakReference; 54 import java.net.URISyntaxException; 55 import java.text.Collator; 56 import java.util.Arrays; 57 import java.util.ArrayList; 58 import java.util.Collections; 59 import java.util.Comparator; 60 import java.util.HashMap; 61 import java.util.HashSet; 62 import java.util.Iterator; 63 import java.util.List; 64 import java.util.Set; 65 66 /** 67 * Maintains in-memory state of the Launcher. It is expected that there should be only one 68 * LauncherModel object held in a static. Also provide APIs for updating the database state 69 * for the Launcher. 70 */ 71 public class LauncherModel extends BroadcastReceiver { 72 static final boolean DEBUG_LOADERS = false; 73 static final String TAG = "Launcher.Model"; 74 75 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons 76 private int mBatchSize; // 0 is all apps at once 77 private int mAllAppsLoadDelay; // milliseconds between batches 78 79 private final boolean mAppsCanBeOnRemoveableStorage; 80 81 private final LauncherApplication mApp; 82 private final Object mLock = new Object(); 83 private DeferredHandler mHandler = new DeferredHandler(); 84 private LoaderTask mLoaderTask; 85 private boolean mIsLoaderTaskRunning; 86 private volatile boolean mFlushingWorkerThread; 87 88 // Specific runnable types that are run on the main thread deferred handler, this allows us to 89 // clear all queued binding runnables when the Launcher activity is destroyed. 90 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0; 91 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1; 92 93 94 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader"); 95 static { 96 sWorkerThread.start(); 97 } 98 private static final Handler sWorker = new Handler(sWorkerThread.getLooper()); 99 100 // We start off with everything not loaded. After that, we assume that 101 // our monitoring of the package manager provides all updates and we never 102 // need to do a requery. These are only ever touched from the loader thread. 103 private boolean mWorkspaceLoaded; 104 private boolean mAllAppsLoaded; 105 106 // When we are loading pages synchronously, we can't just post the binding of items on the side 107 // pages as this delays the rotation process. Instead, we wait for a callback from the first 108 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start 109 // a normal load, we also clear this set of Runnables. 110 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>(); 111 112 private WeakReference<Callbacks> mCallbacks; 113 114 // < only access in worker thread > 115 private AllAppsList mBgAllAppsList; 116 117 // The lock that must be acquired before referencing any static bg data structures. Unlike 118 // other locks, this one can generally be held long-term because we never expect any of these 119 // static data structures to be referenced outside of the worker thread except on the first 120 // load after configuration change. 121 static final Object sBgLock = new Object(); 122 123 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by 124 // LauncherModel to their ids 125 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>(); 126 127 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts 128 // created by LauncherModel that are directly on the home screen (however, no widgets or 129 // shortcuts within folders). 130 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>(); 131 132 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget() 133 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets = 134 new ArrayList<LauncherAppWidgetInfo>(); 135 136 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders() 137 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>(); 138 139 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database 140 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>(); 141 // </ only access in worker thread > 142 143 private IconCache mIconCache; 144 private Bitmap mDefaultIcon; 145 146 private static int mCellCountX; 147 private static int mCellCountY; 148 149 protected int mPreviousConfigMcc; 150 151 public interface Callbacks { 152 public boolean setLoadOnResume(); 153 public int getCurrentWorkspaceScreen(); 154 public void startBinding(); 155 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end); 156 public void bindFolders(HashMap<Long,FolderInfo> folders); 157 public void finishBindingItems(); 158 public void bindAppWidget(LauncherAppWidgetInfo info); 159 public void bindAllApplications(ArrayList<ApplicationInfo> apps); 160 public void bindAppsAdded(ArrayList<ApplicationInfo> apps); 161 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps); 162 public void bindComponentsRemoved(ArrayList<String> packageNames, 163 ArrayList<ApplicationInfo> appInfos, 164 boolean matchPackageNamesOnly); 165 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts); 166 public boolean isAllAppsVisible(); 167 public boolean isAllAppsButtonRank(int rank); 168 public void bindSearchablesChanged(); 169 public void onPageBoundSynchronously(int page); 170 } 171 172 LauncherModel(LauncherApplication app, IconCache iconCache) { 173 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable(); 174 mApp = app; 175 mBgAllAppsList = new AllAppsList(iconCache); 176 mIconCache = iconCache; 177 178 mDefaultIcon = Utilities.createIconBitmap( 179 mIconCache.getFullResDefaultActivityIcon(), app); 180 181 final Resources res = app.getResources(); 182 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay); 183 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize); 184 Configuration config = res.getConfiguration(); 185 mPreviousConfigMcc = config.mcc; 186 } 187 188 /** Runs the specified runnable immediately if called from the main thread, otherwise it is 189 * posted on the main thread handler. */ 190 private void runOnMainThread(Runnable r) { 191 runOnMainThread(r, 0); 192 } 193 private void runOnMainThread(Runnable r, int type) { 194 if (sWorkerThread.getThreadId() == Process.myTid()) { 195 // If we are on the worker thread, post onto the main handler 196 mHandler.post(r); 197 } else { 198 r.run(); 199 } 200 } 201 202 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is 203 * posted on the worker thread handler. */ 204 private static void runOnWorkerThread(Runnable r) { 205 if (sWorkerThread.getThreadId() == Process.myTid()) { 206 r.run(); 207 } else { 208 // If we are not on the worker thread, then post to the worker handler 209 sWorker.post(r); 210 } 211 } 212 213 public Bitmap getFallbackIcon() { 214 return Bitmap.createBitmap(mDefaultIcon); 215 } 216 217 public void unbindItemInfosAndClearQueuedBindRunnables() { 218 if (sWorkerThread.getThreadId() == Process.myTid()) { 219 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " + 220 "main thread"); 221 } 222 223 // Clear any deferred bind runnables 224 mDeferredBindRunnables.clear(); 225 // Remove any queued bind runnables 226 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE); 227 // Unbind all the workspace items 228 unbindWorkspaceItemsOnMainThread(); 229 } 230 231 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */ 232 void unbindWorkspaceItemsOnMainThread() { 233 // Ensure that we don't use the same workspace items data structure on the main thread 234 // by making a copy of workspace items first. 235 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>(); 236 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>(); 237 synchronized (sBgLock) { 238 tmpWorkspaceItems.addAll(sBgWorkspaceItems); 239 tmpAppWidgets.addAll(sBgAppWidgets); 240 } 241 Runnable r = new Runnable() { 242 @Override 243 public void run() { 244 for (ItemInfo item : tmpWorkspaceItems) { 245 item.unbind(); 246 } 247 for (ItemInfo item : tmpAppWidgets) { 248 item.unbind(); 249 } 250 } 251 }; 252 runOnMainThread(r); 253 } 254 255 /** 256 * Adds an item to the DB if it was not created previously, or move it to a new 257 * <container, screen, cellX, cellY> 258 */ 259 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container, 260 int screen, int cellX, int cellY) { 261 if (item.container == ItemInfo.NO_ID) { 262 // From all apps 263 addItemToDatabase(context, item, container, screen, cellX, cellY, false); 264 } else { 265 // From somewhere else 266 moveItemInDatabase(context, item, container, screen, cellX, cellY); 267 } 268 } 269 270 static void checkItemInfoLocked( 271 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) { 272 ItemInfo modelItem = sBgItemsIdMap.get(itemId); 273 if (modelItem != null && item != modelItem) { 274 // check all the data is consistent 275 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) { 276 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem; 277 ShortcutInfo shortcut = (ShortcutInfo) item; 278 if (modelShortcut.title.toString().equals(shortcut.title.toString()) && 279 modelShortcut.intent.filterEquals(shortcut.intent) && 280 modelShortcut.id == shortcut.id && 281 modelShortcut.itemType == shortcut.itemType && 282 modelShortcut.container == shortcut.container && 283 modelShortcut.screen == shortcut.screen && 284 modelShortcut.cellX == shortcut.cellX && 285 modelShortcut.cellY == shortcut.cellY && 286 modelShortcut.spanX == shortcut.spanX && 287 modelShortcut.spanY == shortcut.spanY && 288 ((modelShortcut.dropPos == null && shortcut.dropPos == null) || 289 (modelShortcut.dropPos != null && 290 shortcut.dropPos != null && 291 modelShortcut.dropPos[0] == shortcut.dropPos[0] && 292 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) { 293 // For all intents and purposes, this is the same object 294 return; 295 } 296 } 297 298 // the modelItem needs to match up perfectly with item if our model is 299 // to be consistent with the database-- for now, just require 300 // modelItem == item or the equality check above 301 String msg = "item: " + ((item != null) ? item.toString() : "null") + 302 "modelItem: " + 303 ((modelItem != null) ? modelItem.toString() : "null") + 304 "Error: ItemInfo passed to checkItemInfo doesn't match original"; 305 RuntimeException e = new RuntimeException(msg); 306 if (stackTrace != null) { 307 e.setStackTrace(stackTrace); 308 } 309 throw e; 310 } 311 } 312 313 static void checkItemInfo(final ItemInfo item) { 314 final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); 315 final long itemId = item.id; 316 Runnable r = new Runnable() { 317 public void run() { 318 synchronized (sBgLock) { 319 checkItemInfoLocked(itemId, item, stackTrace); 320 } 321 } 322 }; 323 runOnWorkerThread(r); 324 } 325 326 static void updateItemInDatabaseHelper(Context context, final ContentValues values, 327 final ItemInfo item, final String callingFunction) { 328 final long itemId = item.id; 329 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false); 330 final ContentResolver cr = context.getContentResolver(); 331 332 final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); 333 Runnable r = new Runnable() { 334 public void run() { 335 cr.update(uri, values, null, null); 336 337 // Lock on mBgLock *after* the db operation 338 synchronized (sBgLock) { 339 checkItemInfoLocked(itemId, item, stackTrace); 340 341 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP && 342 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 343 // Item is in a folder, make sure this folder exists 344 if (!sBgFolders.containsKey(item.container)) { 345 // An items container is being set to a that of an item which is not in 346 // the list of Folders. 347 String msg = "item: " + item + " container being set to: " + 348 item.container + ", not in the list of folders"; 349 Log.e(TAG, msg); 350 Launcher.dumpDebugLogsToConsole(); 351 } 352 } 353 354 // Items are added/removed from the corresponding FolderInfo elsewhere, such 355 // as in Workspace.onDrop. Here, we just add/remove them from the list of items 356 // that are on the desktop, as appropriate 357 ItemInfo modelItem = sBgItemsIdMap.get(itemId); 358 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP || 359 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 360 switch (modelItem.itemType) { 361 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: 362 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: 363 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: 364 if (!sBgWorkspaceItems.contains(modelItem)) { 365 sBgWorkspaceItems.add(modelItem); 366 } 367 break; 368 default: 369 break; 370 } 371 } else { 372 sBgWorkspaceItems.remove(modelItem); 373 } 374 } 375 } 376 }; 377 runOnWorkerThread(r); 378 } 379 380 public void flushWorkerThread() { 381 mFlushingWorkerThread = true; 382 Runnable waiter = new Runnable() { 383 public void run() { 384 synchronized (this) { 385 notifyAll(); 386 mFlushingWorkerThread = false; 387 } 388 } 389 }; 390 391 synchronized(waiter) { 392 runOnWorkerThread(waiter); 393 if (mLoaderTask != null) { 394 synchronized(mLoaderTask) { 395 mLoaderTask.notify(); 396 } 397 } 398 boolean success = false; 399 while (!success) { 400 try { 401 waiter.wait(); 402 success = true; 403 } catch (InterruptedException e) { 404 } 405 } 406 } 407 } 408 409 /** 410 * Move an item in the DB to a new <container, screen, cellX, cellY> 411 */ 412 static void moveItemInDatabase(Context context, final ItemInfo item, final long container, 413 final int screen, final int cellX, final int cellY) { 414 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id + 415 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY + 416 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")"; 417 Launcher.sDumpLogs.add(transaction); 418 Log.d(TAG, transaction); 419 item.container = container; 420 item.cellX = cellX; 421 item.cellY = cellY; 422 423 // We store hotseat items in canonical form which is this orientation invariant position 424 // in the hotseat 425 if (context instanceof Launcher && screen < 0 && 426 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 427 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); 428 } else { 429 item.screen = screen; 430 } 431 432 final ContentValues values = new ContentValues(); 433 values.put(LauncherSettings.Favorites.CONTAINER, item.container); 434 values.put(LauncherSettings.Favorites.CELLX, item.cellX); 435 values.put(LauncherSettings.Favorites.CELLY, item.cellY); 436 values.put(LauncherSettings.Favorites.SCREEN, item.screen); 437 438 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase"); 439 } 440 441 /** 442 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY> 443 */ 444 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container, 445 final int screen, final int cellX, final int cellY, final int spanX, final int spanY) { 446 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id + 447 " (" + item.container + ", " + item.screen + ", " + item.cellX + ", " + item.cellY + 448 ") --> " + "(" + container + ", " + screen + ", " + cellX + ", " + cellY + ")"; 449 Launcher.sDumpLogs.add(transaction); 450 Log.d(TAG, transaction); 451 item.cellX = cellX; 452 item.cellY = cellY; 453 item.spanX = spanX; 454 item.spanY = spanY; 455 456 // We store hotseat items in canonical form which is this orientation invariant position 457 // in the hotseat 458 if (context instanceof Launcher && screen < 0 && 459 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 460 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); 461 } else { 462 item.screen = screen; 463 } 464 465 final ContentValues values = new ContentValues(); 466 values.put(LauncherSettings.Favorites.CONTAINER, item.container); 467 values.put(LauncherSettings.Favorites.CELLX, item.cellX); 468 values.put(LauncherSettings.Favorites.CELLY, item.cellY); 469 values.put(LauncherSettings.Favorites.SPANX, item.spanX); 470 values.put(LauncherSettings.Favorites.SPANY, item.spanY); 471 values.put(LauncherSettings.Favorites.SCREEN, item.screen); 472 473 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase"); 474 } 475 476 /** 477 * Update an item to the database in a specified container. 478 */ 479 static void updateItemInDatabase(Context context, final ItemInfo item) { 480 final ContentValues values = new ContentValues(); 481 item.onAddToDatabase(values); 482 item.updateValuesWithCoordinates(values, item.cellX, item.cellY); 483 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase"); 484 } 485 486 /** 487 * Returns true if the shortcuts already exists in the database. 488 * we identify a shortcut by its title and intent. 489 */ 490 static boolean shortcutExists(Context context, String title, Intent intent) { 491 final ContentResolver cr = context.getContentResolver(); 492 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, 493 new String[] { "title", "intent" }, "title=? and intent=?", 494 new String[] { title, intent.toUri(0) }, null); 495 boolean result = false; 496 try { 497 result = c.moveToFirst(); 498 } finally { 499 c.close(); 500 } 501 return result; 502 } 503 504 /** 505 * Returns an ItemInfo array containing all the items in the LauncherModel. 506 * The ItemInfo.id is not set through this function. 507 */ 508 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) { 509 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>(); 510 final ContentResolver cr = context.getContentResolver(); 511 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] { 512 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER, 513 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY, 514 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null); 515 516 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); 517 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); 518 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); 519 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); 520 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); 521 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX); 522 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY); 523 524 try { 525 while (c.moveToNext()) { 526 ItemInfo item = new ItemInfo(); 527 item.cellX = c.getInt(cellXIndex); 528 item.cellY = c.getInt(cellYIndex); 529 item.spanX = c.getInt(spanXIndex); 530 item.spanY = c.getInt(spanYIndex); 531 item.container = c.getInt(containerIndex); 532 item.itemType = c.getInt(itemTypeIndex); 533 item.screen = c.getInt(screenIndex); 534 535 items.add(item); 536 } 537 } catch (Exception e) { 538 items.clear(); 539 } finally { 540 c.close(); 541 } 542 543 return items; 544 } 545 546 /** 547 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList. 548 */ 549 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) { 550 final ContentResolver cr = context.getContentResolver(); 551 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null, 552 "_id=? and (itemType=? or itemType=?)", 553 new String[] { String.valueOf(id), 554 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null); 555 556 try { 557 if (c.moveToFirst()) { 558 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); 559 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); 560 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); 561 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); 562 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); 563 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); 564 565 FolderInfo folderInfo = null; 566 switch (c.getInt(itemTypeIndex)) { 567 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: 568 folderInfo = findOrMakeFolder(folderList, id); 569 break; 570 } 571 572 folderInfo.title = c.getString(titleIndex); 573 folderInfo.id = id; 574 folderInfo.container = c.getInt(containerIndex); 575 folderInfo.screen = c.getInt(screenIndex); 576 folderInfo.cellX = c.getInt(cellXIndex); 577 folderInfo.cellY = c.getInt(cellYIndex); 578 579 return folderInfo; 580 } 581 } finally { 582 c.close(); 583 } 584 585 return null; 586 } 587 588 /** 589 * Add an item to the database in a specified container. Sets the container, screen, cellX and 590 * cellY fields of the item. Also assigns an ID to the item. 591 */ 592 static void addItemToDatabase(Context context, final ItemInfo item, final long container, 593 final int screen, final int cellX, final int cellY, final boolean notify) { 594 item.container = container; 595 item.cellX = cellX; 596 item.cellY = cellY; 597 // We store hotseat items in canonical form which is this orientation invariant position 598 // in the hotseat 599 if (context instanceof Launcher && screen < 0 && 600 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 601 item.screen = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); 602 } else { 603 item.screen = screen; 604 } 605 606 final ContentValues values = new ContentValues(); 607 final ContentResolver cr = context.getContentResolver(); 608 item.onAddToDatabase(values); 609 610 LauncherApplication app = (LauncherApplication) context.getApplicationContext(); 611 item.id = app.getLauncherProvider().generateNewId(); 612 values.put(LauncherSettings.Favorites._ID, item.id); 613 item.updateValuesWithCoordinates(values, item.cellX, item.cellY); 614 615 Runnable r = new Runnable() { 616 public void run() { 617 String transaction = "DbDebug Add item (" + item.title + ") to db, id: " 618 + item.id + " (" + container + ", " + screen + ", " + cellX + ", " 619 + cellY + ")"; 620 Launcher.sDumpLogs.add(transaction); 621 Log.d(TAG, transaction); 622 623 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI : 624 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values); 625 626 // Lock on mBgLock *after* the db operation 627 synchronized (sBgLock) { 628 checkItemInfoLocked(item.id, item, null); 629 sBgItemsIdMap.put(item.id, item); 630 switch (item.itemType) { 631 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: 632 sBgFolders.put(item.id, (FolderInfo) item); 633 // Fall through 634 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: 635 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: 636 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP || 637 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 638 sBgWorkspaceItems.add(item); 639 } else { 640 if (!sBgFolders.containsKey(item.container)) { 641 // Adding an item to a folder that doesn't exist. 642 String msg = "adding item: " + item + " to a folder that " + 643 " doesn't exist"; 644 Log.e(TAG, msg); 645 Launcher.dumpDebugLogsToConsole(); 646 } 647 } 648 break; 649 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: 650 sBgAppWidgets.add((LauncherAppWidgetInfo) item); 651 break; 652 } 653 } 654 } 655 }; 656 runOnWorkerThread(r); 657 } 658 659 /** 660 * Creates a new unique child id, for a given cell span across all layouts. 661 */ 662 static int getCellLayoutChildId( 663 long container, int screen, int localCellX, int localCellY, int spanX, int spanY) { 664 return (((int) container & 0xFF) << 24) 665 | (screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF); 666 } 667 668 static int getCellCountX() { 669 return mCellCountX; 670 } 671 672 static int getCellCountY() { 673 return mCellCountY; 674 } 675 676 /** 677 * Updates the model orientation helper to take into account the current layout dimensions 678 * when performing local/canonical coordinate transformations. 679 */ 680 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) { 681 mCellCountX = shortAxisCellCount; 682 mCellCountY = longAxisCellCount; 683 } 684 685 /** 686 * Removes the specified item from the database 687 * @param context 688 * @param item 689 */ 690 static void deleteItemFromDatabase(Context context, final ItemInfo item) { 691 final ContentResolver cr = context.getContentResolver(); 692 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false); 693 694 Runnable r = new Runnable() { 695 public void run() { 696 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: " 697 + item.id + " (" + item.container + ", " + item.screen + ", " + item.cellX + 698 ", " + item.cellY + ")"; 699 Launcher.sDumpLogs.add(transaction); 700 Log.d(TAG, transaction); 701 702 cr.delete(uriToDelete, null, null); 703 704 // Lock on mBgLock *after* the db operation 705 synchronized (sBgLock) { 706 switch (item.itemType) { 707 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: 708 sBgFolders.remove(item.id); 709 for (ItemInfo info: sBgItemsIdMap.values()) { 710 if (info.container == item.id) { 711 // We are deleting a folder which still contains items that 712 // think they are contained by that folder. 713 String msg = "deleting a folder (" + item + ") which still " + 714 "contains items (" + info + ")"; 715 Log.e(TAG, msg); 716 Launcher.dumpDebugLogsToConsole(); 717 } 718 } 719 sBgWorkspaceItems.remove(item); 720 break; 721 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: 722 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: 723 sBgWorkspaceItems.remove(item); 724 break; 725 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: 726 sBgAppWidgets.remove((LauncherAppWidgetInfo) item); 727 break; 728 } 729 sBgItemsIdMap.remove(item.id); 730 sBgDbIconCache.remove(item); 731 } 732 } 733 }; 734 runOnWorkerThread(r); 735 } 736 737 /** 738 * Remove the contents of the specified folder from the database 739 */ 740 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) { 741 final ContentResolver cr = context.getContentResolver(); 742 743 Runnable r = new Runnable() { 744 public void run() { 745 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null); 746 // Lock on mBgLock *after* the db operation 747 synchronized (sBgLock) { 748 sBgItemsIdMap.remove(info.id); 749 sBgFolders.remove(info.id); 750 sBgDbIconCache.remove(info); 751 sBgWorkspaceItems.remove(info); 752 } 753 754 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, 755 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null); 756 // Lock on mBgLock *after* the db operation 757 synchronized (sBgLock) { 758 for (ItemInfo childInfo : info.contents) { 759 sBgItemsIdMap.remove(childInfo.id); 760 sBgDbIconCache.remove(childInfo); 761 } 762 } 763 } 764 }; 765 runOnWorkerThread(r); 766 } 767 768 /** 769 * Set this as the current Launcher activity object for the loader. 770 */ 771 public void initialize(Callbacks callbacks) { 772 synchronized (mLock) { 773 mCallbacks = new WeakReference<Callbacks>(callbacks); 774 } 775 } 776 777 /** 778 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and 779 * ACTION_PACKAGE_CHANGED. 780 */ 781 @Override 782 public void onReceive(Context context, Intent intent) { 783 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent); 784 785 final String action = intent.getAction(); 786 787 if (Intent.ACTION_PACKAGE_CHANGED.equals(action) 788 || Intent.ACTION_PACKAGE_REMOVED.equals(action) 789 || Intent.ACTION_PACKAGE_ADDED.equals(action)) { 790 final String packageName = intent.getData().getSchemeSpecificPart(); 791 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); 792 793 int op = PackageUpdatedTask.OP_NONE; 794 795 if (packageName == null || packageName.length() == 0) { 796 // they sent us a bad intent 797 return; 798 } 799 800 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) { 801 op = PackageUpdatedTask.OP_UPDATE; 802 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { 803 if (!replacing) { 804 op = PackageUpdatedTask.OP_REMOVE; 805 } 806 // else, we are replacing the package, so a PACKAGE_ADDED will be sent 807 // later, we will update the package at this time 808 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { 809 if (!replacing) { 810 op = PackageUpdatedTask.OP_ADD; 811 } else { 812 op = PackageUpdatedTask.OP_UPDATE; 813 } 814 } 815 816 if (op != PackageUpdatedTask.OP_NONE) { 817 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName })); 818 } 819 820 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { 821 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); 822 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); 823 if (!replacing) { 824 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages)); 825 if (mAppsCanBeOnRemoveableStorage) { 826 // Only rebind if we support removable storage. It catches the case where 827 // apps on the external sd card need to be reloaded 828 startLoaderFromBackground(); 829 } 830 } else { 831 // If we are replacing then just update the packages in the list 832 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, 833 packages)); 834 } 835 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { 836 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); 837 if (!replacing) { 838 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); 839 enqueuePackageUpdated(new PackageUpdatedTask( 840 PackageUpdatedTask.OP_UNAVAILABLE, packages)); 841 } 842 // else, we are replacing the packages, so ignore this event and wait for 843 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time 844 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { 845 // If we have changed locale we need to clear out the labels in all apps/workspace. 846 forceReload(); 847 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) { 848 // Check if configuration change was an mcc/mnc change which would affect app resources 849 // and we would need to clear out the labels in all apps/workspace. Same handling as 850 // above for ACTION_LOCALE_CHANGED 851 Configuration currentConfig = context.getResources().getConfiguration(); 852 if (mPreviousConfigMcc != currentConfig.mcc) { 853 Log.d(TAG, "Reload apps on config change. curr_mcc:" 854 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc); 855 forceReload(); 856 } 857 // Update previousConfig 858 mPreviousConfigMcc = currentConfig.mcc; 859 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) || 860 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) { 861 if (mCallbacks != null) { 862 Callbacks callbacks = mCallbacks.get(); 863 if (callbacks != null) { 864 callbacks.bindSearchablesChanged(); 865 } 866 } 867 } 868 } 869 870 private void forceReload() { 871 resetLoadedState(true, true); 872 873 // Do this here because if the launcher activity is running it will be restarted. 874 // If it's not running startLoaderFromBackground will merely tell it that it needs 875 // to reload. 876 startLoaderFromBackground(); 877 } 878 879 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) { 880 synchronized (mLock) { 881 // Stop any existing loaders first, so they don't set mAllAppsLoaded or 882 // mWorkspaceLoaded to true later 883 stopLoaderLocked(); 884 if (resetAllAppsLoaded) mAllAppsLoaded = false; 885 if (resetWorkspaceLoaded) mWorkspaceLoaded = false; 886 } 887 } 888 889 /** 890 * When the launcher is in the background, it's possible for it to miss paired 891 * configuration changes. So whenever we trigger the loader from the background 892 * tell the launcher that it needs to re-run the loader when it comes back instead 893 * of doing it now. 894 */ 895 public void startLoaderFromBackground() { 896 boolean runLoader = false; 897 if (mCallbacks != null) { 898 Callbacks callbacks = mCallbacks.get(); 899 if (callbacks != null) { 900 // Only actually run the loader if they're not paused. 901 if (!callbacks.setLoadOnResume()) { 902 runLoader = true; 903 } 904 } 905 } 906 if (runLoader) { 907 startLoader(false, -1); 908 } 909 } 910 911 // If there is already a loader task running, tell it to stop. 912 // returns true if isLaunching() was true on the old task 913 private boolean stopLoaderLocked() { 914 boolean isLaunching = false; 915 LoaderTask oldTask = mLoaderTask; 916 if (oldTask != null) { 917 if (oldTask.isLaunching()) { 918 isLaunching = true; 919 } 920 oldTask.stopLocked(); 921 } 922 return isLaunching; 923 } 924 925 public void startLoader(boolean isLaunching, int synchronousBindPage) { 926 synchronized (mLock) { 927 if (DEBUG_LOADERS) { 928 Log.d(TAG, "startLoader isLaunching=" + isLaunching); 929 } 930 931 // Clear any deferred bind-runnables from the synchronized load process 932 // We must do this before any loading/binding is scheduled below. 933 mDeferredBindRunnables.clear(); 934 935 // Don't bother to start the thread if we know it's not going to do anything 936 if (mCallbacks != null && mCallbacks.get() != null) { 937 // If there is already one running, tell it to stop. 938 // also, don't downgrade isLaunching if we're already running 939 isLaunching = isLaunching || stopLoaderLocked(); 940 mLoaderTask = new LoaderTask(mApp, isLaunching); 941 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) { 942 mLoaderTask.runBindSynchronousPage(synchronousBindPage); 943 } else { 944 sWorkerThread.setPriority(Thread.NORM_PRIORITY); 945 sWorker.post(mLoaderTask); 946 } 947 } 948 } 949 } 950 951 void bindRemainingSynchronousPages() { 952 // Post the remaining side pages to be loaded 953 if (!mDeferredBindRunnables.isEmpty()) { 954 for (final Runnable r : mDeferredBindRunnables) { 955 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE); 956 } 957 mDeferredBindRunnables.clear(); 958 } 959 } 960 961 public void stopLoader() { 962 synchronized (mLock) { 963 if (mLoaderTask != null) { 964 mLoaderTask.stopLocked(); 965 } 966 } 967 } 968 969 public boolean isAllAppsLoaded() { 970 return mAllAppsLoaded; 971 } 972 973 boolean isLoadingWorkspace() { 974 synchronized (mLock) { 975 if (mLoaderTask != null) { 976 return mLoaderTask.isLoadingWorkspace(); 977 } 978 } 979 return false; 980 } 981 982 /** 983 * Runnable for the thread that loads the contents of the launcher: 984 * - workspace icons 985 * - widgets 986 * - all apps icons 987 */ 988 private class LoaderTask implements Runnable { 989 private Context mContext; 990 private boolean mIsLaunching; 991 private boolean mIsLoadingAndBindingWorkspace; 992 private boolean mStopped; 993 private boolean mLoadAndBindStepFinished; 994 995 private HashMap<Object, CharSequence> mLabelCache; 996 997 LoaderTask(Context context, boolean isLaunching) { 998 mContext = context; 999 mIsLaunching = isLaunching; 1000 mLabelCache = new HashMap<Object, CharSequence>(); 1001 } 1002 1003 boolean isLaunching() { 1004 return mIsLaunching; 1005 } 1006 1007 boolean isLoadingWorkspace() { 1008 return mIsLoadingAndBindingWorkspace; 1009 } 1010 1011 private void loadAndBindWorkspace() { 1012 mIsLoadingAndBindingWorkspace = true; 1013 1014 // Load the workspace 1015 if (DEBUG_LOADERS) { 1016 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded); 1017 } 1018 1019 if (!mWorkspaceLoaded) { 1020 loadWorkspace(); 1021 synchronized (LoaderTask.this) { 1022 if (mStopped) { 1023 return; 1024 } 1025 mWorkspaceLoaded = true; 1026 } 1027 } 1028 1029 // Bind the workspace 1030 bindWorkspace(-1); 1031 } 1032 1033 private void waitForIdle() { 1034 // Wait until the either we're stopped or the other threads are done. 1035 // This way we don't start loading all apps until the workspace has settled 1036 // down. 1037 synchronized (LoaderTask.this) { 1038 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; 1039 1040 mHandler.postIdle(new Runnable() { 1041 public void run() { 1042 synchronized (LoaderTask.this) { 1043 mLoadAndBindStepFinished = true; 1044 if (DEBUG_LOADERS) { 1045 Log.d(TAG, "done with previous binding step"); 1046 } 1047 LoaderTask.this.notify(); 1048 } 1049 } 1050 }); 1051 1052 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) { 1053 try { 1054 // Just in case mFlushingWorkerThread changes but we aren't woken up, 1055 // wait no longer than 1sec at a time 1056 this.wait(1000); 1057 } catch (InterruptedException ex) { 1058 // Ignore 1059 } 1060 } 1061 if (DEBUG_LOADERS) { 1062 Log.d(TAG, "waited " 1063 + (SystemClock.uptimeMillis()-workspaceWaitTime) 1064 + "ms for previous step to finish binding"); 1065 } 1066 } 1067 } 1068 1069 void runBindSynchronousPage(int synchronousBindPage) { 1070 if (synchronousBindPage < 0) { 1071 // Ensure that we have a valid page index to load synchronously 1072 throw new RuntimeException("Should not call runBindSynchronousPage() without " + 1073 "valid page index"); 1074 } 1075 if (!mAllAppsLoaded || !mWorkspaceLoaded) { 1076 // Ensure that we don't try and bind a specified page when the pages have not been 1077 // loaded already (we should load everything asynchronously in that case) 1078 throw new RuntimeException("Expecting AllApps and Workspace to be loaded"); 1079 } 1080 synchronized (mLock) { 1081 if (mIsLoaderTaskRunning) { 1082 // Ensure that we are never running the background loading at this point since 1083 // we also touch the background collections 1084 throw new RuntimeException("Error! Background loading is already running"); 1085 } 1086 } 1087 1088 // XXX: Throw an exception if we are already loading (since we touch the worker thread 1089 // data structures, we can't allow any other thread to touch that data, but because 1090 // this call is synchronous, we can get away with not locking). 1091 1092 // The LauncherModel is static in the LauncherApplication and mHandler may have queued 1093 // operations from the previous activity. We need to ensure that all queued operations 1094 // are executed before any synchronous binding work is done. 1095 mHandler.flush(); 1096 1097 // Divide the set of loaded items into those that we are binding synchronously, and 1098 // everything else that is to be bound normally (asynchronously). 1099 bindWorkspace(synchronousBindPage); 1100 // XXX: For now, continue posting the binding of AllApps as there are other issues that 1101 // arise from that. 1102 onlyBindAllApps(); 1103 } 1104 1105 public void run() { 1106 synchronized (mLock) { 1107 mIsLoaderTaskRunning = true; 1108 } 1109 // Optimize for end-user experience: if the Launcher is up and // running with the 1110 // All Apps interface in the foreground, load All Apps first. Otherwise, load the 1111 // workspace first (default). 1112 final Callbacks cbk = mCallbacks.get(); 1113 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true; 1114 1115 keep_running: { 1116 // Elevate priority when Home launches for the first time to avoid 1117 // starving at boot time. Staring at a blank home is not cool. 1118 synchronized (mLock) { 1119 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " + 1120 (mIsLaunching ? "DEFAULT" : "BACKGROUND")); 1121 android.os.Process.setThreadPriority(mIsLaunching 1122 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND); 1123 } 1124 if (loadWorkspaceFirst) { 1125 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace"); 1126 loadAndBindWorkspace(); 1127 } else { 1128 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps"); 1129 loadAndBindAllApps(); 1130 } 1131 1132 if (mStopped) { 1133 break keep_running; 1134 } 1135 1136 // Whew! Hard work done. Slow us down, and wait until the UI thread has 1137 // settled down. 1138 synchronized (mLock) { 1139 if (mIsLaunching) { 1140 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND"); 1141 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 1142 } 1143 } 1144 waitForIdle(); 1145 1146 // second step 1147 if (loadWorkspaceFirst) { 1148 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps"); 1149 loadAndBindAllApps(); 1150 } else { 1151 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace"); 1152 loadAndBindWorkspace(); 1153 } 1154 1155 // Restore the default thread priority after we are done loading items 1156 synchronized (mLock) { 1157 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); 1158 } 1159 } 1160 1161 1162 // Update the saved icons if necessary 1163 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons"); 1164 synchronized (sBgLock) { 1165 for (Object key : sBgDbIconCache.keySet()) { 1166 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key)); 1167 } 1168 sBgDbIconCache.clear(); 1169 } 1170 1171 // Clear out this reference, otherwise we end up holding it until all of the 1172 // callback runnables are done. 1173 mContext = null; 1174 1175 synchronized (mLock) { 1176 // If we are still the last one to be scheduled, remove ourselves. 1177 if (mLoaderTask == this) { 1178 mLoaderTask = null; 1179 } 1180 mIsLoaderTaskRunning = false; 1181 } 1182 } 1183 1184 public void stopLocked() { 1185 synchronized (LoaderTask.this) { 1186 mStopped = true; 1187 this.notify(); 1188 } 1189 } 1190 1191 /** 1192 * Gets the callbacks object. If we've been stopped, or if the launcher object 1193 * has somehow been garbage collected, return null instead. Pass in the Callbacks 1194 * object that was around when the deferred message was scheduled, and if there's 1195 * a new Callbacks object around then also return null. This will save us from 1196 * calling onto it with data that will be ignored. 1197 */ 1198 Callbacks tryGetCallbacks(Callbacks oldCallbacks) { 1199 synchronized (mLock) { 1200 if (mStopped) { 1201 return null; 1202 } 1203 1204 if (mCallbacks == null) { 1205 return null; 1206 } 1207 1208 final Callbacks callbacks = mCallbacks.get(); 1209 if (callbacks != oldCallbacks) { 1210 return null; 1211 } 1212 if (callbacks == null) { 1213 Log.w(TAG, "no mCallbacks"); 1214 return null; 1215 } 1216 1217 return callbacks; 1218 } 1219 } 1220 1221 // check & update map of what's occupied; used to discard overlapping/invalid items 1222 private boolean checkItemPlacement(ItemInfo occupied[][][], ItemInfo item) { 1223 int containerIndex = item.screen; 1224 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 1225 // Return early if we detect that an item is under the hotseat button 1226 if (mCallbacks == null || mCallbacks.get().isAllAppsButtonRank(item.screen)) { 1227 return false; 1228 } 1229 1230 // We use the last index to refer to the hotseat and the screen as the rank, so 1231 // test and update the occupied state accordingly 1232 if (occupied[Launcher.SCREEN_COUNT][item.screen][0] != null) { 1233 Log.e(TAG, "Error loading shortcut into hotseat " + item 1234 + " into position (" + item.screen + ":" + item.cellX + "," + item.cellY 1235 + ") occupied by " + occupied[Launcher.SCREEN_COUNT][item.screen][0]); 1236 return false; 1237 } else { 1238 occupied[Launcher.SCREEN_COUNT][item.screen][0] = item; 1239 return true; 1240 } 1241 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) { 1242 // Skip further checking if it is not the hotseat or workspace container 1243 return true; 1244 } 1245 1246 // Check if any workspace icons overlap with each other 1247 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) { 1248 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) { 1249 if (occupied[containerIndex][x][y] != null) { 1250 Log.e(TAG, "Error loading shortcut " + item 1251 + " into cell (" + containerIndex + "-" + item.screen + ":" 1252 + x + "," + y 1253 + ") occupied by " 1254 + occupied[containerIndex][x][y]); 1255 return false; 1256 } 1257 } 1258 } 1259 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) { 1260 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) { 1261 occupied[containerIndex][x][y] = item; 1262 } 1263 } 1264 1265 return true; 1266 } 1267 1268 private void loadWorkspace() { 1269 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; 1270 1271 final Context context = mContext; 1272 final ContentResolver contentResolver = context.getContentResolver(); 1273 final PackageManager manager = context.getPackageManager(); 1274 final AppWidgetManager widgets = AppWidgetManager.getInstance(context); 1275 final boolean isSafeMode = manager.isSafeMode(); 1276 1277 // Make sure the default workspace is loaded, if needed 1278 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0, false); 1279 1280 synchronized (sBgLock) { 1281 sBgWorkspaceItems.clear(); 1282 sBgAppWidgets.clear(); 1283 sBgFolders.clear(); 1284 sBgItemsIdMap.clear(); 1285 sBgDbIconCache.clear(); 1286 1287 final ArrayList<Long> itemsToRemove = new ArrayList<Long>(); 1288 1289 final Cursor c = contentResolver.query( 1290 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null); 1291 1292 // +1 for the hotseat (it can be larger than the workspace) 1293 // Load workspace in reverse order to ensure that latest items are loaded first (and 1294 // before any earlier duplicates) 1295 final ItemInfo occupied[][][] = 1296 new ItemInfo[Launcher.SCREEN_COUNT + 1][mCellCountX + 1][mCellCountY + 1]; 1297 1298 try { 1299 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); 1300 final int intentIndex = c.getColumnIndexOrThrow 1301 (LauncherSettings.Favorites.INTENT); 1302 final int titleIndex = c.getColumnIndexOrThrow 1303 (LauncherSettings.Favorites.TITLE); 1304 final int iconTypeIndex = c.getColumnIndexOrThrow( 1305 LauncherSettings.Favorites.ICON_TYPE); 1306 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); 1307 final int iconPackageIndex = c.getColumnIndexOrThrow( 1308 LauncherSettings.Favorites.ICON_PACKAGE); 1309 final int iconResourceIndex = c.getColumnIndexOrThrow( 1310 LauncherSettings.Favorites.ICON_RESOURCE); 1311 final int containerIndex = c.getColumnIndexOrThrow( 1312 LauncherSettings.Favorites.CONTAINER); 1313 final int itemTypeIndex = c.getColumnIndexOrThrow( 1314 LauncherSettings.Favorites.ITEM_TYPE); 1315 final int appWidgetIdIndex = c.getColumnIndexOrThrow( 1316 LauncherSettings.Favorites.APPWIDGET_ID); 1317 final int screenIndex = c.getColumnIndexOrThrow( 1318 LauncherSettings.Favorites.SCREEN); 1319 final int cellXIndex = c.getColumnIndexOrThrow 1320 (LauncherSettings.Favorites.CELLX); 1321 final int cellYIndex = c.getColumnIndexOrThrow 1322 (LauncherSettings.Favorites.CELLY); 1323 final int spanXIndex = c.getColumnIndexOrThrow 1324 (LauncherSettings.Favorites.SPANX); 1325 final int spanYIndex = c.getColumnIndexOrThrow( 1326 LauncherSettings.Favorites.SPANY); 1327 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); 1328 //final int displayModeIndex = c.getColumnIndexOrThrow( 1329 // LauncherSettings.Favorites.DISPLAY_MODE); 1330 1331 ShortcutInfo info; 1332 String intentDescription; 1333 LauncherAppWidgetInfo appWidgetInfo; 1334 int container; 1335 long id; 1336 Intent intent; 1337 1338 while (!mStopped && c.moveToNext()) { 1339 try { 1340 int itemType = c.getInt(itemTypeIndex); 1341 1342 switch (itemType) { 1343 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: 1344 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: 1345 intentDescription = c.getString(intentIndex); 1346 try { 1347 intent = Intent.parseUri(intentDescription, 0); 1348 } catch (URISyntaxException e) { 1349 continue; 1350 } 1351 1352 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { 1353 info = getShortcutInfo(manager, intent, context, c, iconIndex, 1354 titleIndex, mLabelCache); 1355 } else { 1356 info = getShortcutInfo(c, context, iconTypeIndex, 1357 iconPackageIndex, iconResourceIndex, iconIndex, 1358 titleIndex); 1359 1360 // App shortcuts that used to be automatically added to Launcher 1361 // didn't always have the correct intent flags set, so do that 1362 // here 1363 if (intent.getAction() != null && 1364 intent.getCategories() != null && 1365 intent.getAction().equals(Intent.ACTION_MAIN) && 1366 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { 1367 intent.addFlags( 1368 Intent.FLAG_ACTIVITY_NEW_TASK | 1369 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 1370 } 1371 } 1372 1373 if (info != null) { 1374 info.intent = intent; 1375 info.id = c.getLong(idIndex); 1376 container = c.getInt(containerIndex); 1377 info.container = container; 1378 info.screen = c.getInt(screenIndex); 1379 info.cellX = c.getInt(cellXIndex); 1380 info.cellY = c.getInt(cellYIndex); 1381 1382 // check & update map of what's occupied 1383 if (!checkItemPlacement(occupied, info)) { 1384 break; 1385 } 1386 1387 switch (container) { 1388 case LauncherSettings.Favorites.CONTAINER_DESKTOP: 1389 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: 1390 sBgWorkspaceItems.add(info); 1391 break; 1392 default: 1393 // Item is in a user folder 1394 FolderInfo folderInfo = 1395 findOrMakeFolder(sBgFolders, container); 1396 folderInfo.add(info); 1397 break; 1398 } 1399 sBgItemsIdMap.put(info.id, info); 1400 1401 // now that we've loaded everthing re-save it with the 1402 // icon in case it disappears somehow. 1403 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex); 1404 } else { 1405 // Failed to load the shortcut, probably because the 1406 // activity manager couldn't resolve it (maybe the app 1407 // was uninstalled), or the db row was somehow screwed up. 1408 // Delete it. 1409 id = c.getLong(idIndex); 1410 Log.e(TAG, "Error loading shortcut " + id + ", removing it"); 1411 contentResolver.delete(LauncherSettings.Favorites.getContentUri( 1412 id, false), null, null); 1413 } 1414 break; 1415 1416 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: 1417 id = c.getLong(idIndex); 1418 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id); 1419 1420 folderInfo.title = c.getString(titleIndex); 1421 folderInfo.id = id; 1422 container = c.getInt(containerIndex); 1423 folderInfo.container = container; 1424 folderInfo.screen = c.getInt(screenIndex); 1425 folderInfo.cellX = c.getInt(cellXIndex); 1426 folderInfo.cellY = c.getInt(cellYIndex); 1427 1428 // check & update map of what's occupied 1429 if (!checkItemPlacement(occupied, folderInfo)) { 1430 break; 1431 } 1432 switch (container) { 1433 case LauncherSettings.Favorites.CONTAINER_DESKTOP: 1434 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: 1435 sBgWorkspaceItems.add(folderInfo); 1436 break; 1437 } 1438 1439 sBgItemsIdMap.put(folderInfo.id, folderInfo); 1440 sBgFolders.put(folderInfo.id, folderInfo); 1441 break; 1442 1443 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: 1444 // Read all Launcher-specific widget details 1445 int appWidgetId = c.getInt(appWidgetIdIndex); 1446 id = c.getLong(idIndex); 1447 1448 final AppWidgetProviderInfo provider = 1449 widgets.getAppWidgetInfo(appWidgetId); 1450 1451 if (!isSafeMode && (provider == null || provider.provider == null || 1452 provider.provider.getPackageName() == null)) { 1453 String log = "Deleting widget that isn't installed anymore: id=" 1454 + id + " appWidgetId=" + appWidgetId; 1455 Log.e(TAG, log); 1456 Launcher.sDumpLogs.add(log); 1457 itemsToRemove.add(id); 1458 } else { 1459 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, 1460 provider.provider); 1461 appWidgetInfo.id = id; 1462 appWidgetInfo.screen = c.getInt(screenIndex); 1463 appWidgetInfo.cellX = c.getInt(cellXIndex); 1464 appWidgetInfo.cellY = c.getInt(cellYIndex); 1465 appWidgetInfo.spanX = c.getInt(spanXIndex); 1466 appWidgetInfo.spanY = c.getInt(spanYIndex); 1467 int[] minSpan = Launcher.getMinSpanForWidget(context, provider); 1468 appWidgetInfo.minSpanX = minSpan[0]; 1469 appWidgetInfo.minSpanY = minSpan[1]; 1470 1471 container = c.getInt(containerIndex); 1472 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP && 1473 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 1474 Log.e(TAG, "Widget found where container != " + 1475 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!"); 1476 continue; 1477 } 1478 appWidgetInfo.container = c.getInt(containerIndex); 1479 1480 // check & update map of what's occupied 1481 if (!checkItemPlacement(occupied, appWidgetInfo)) { 1482 break; 1483 } 1484 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo); 1485 sBgAppWidgets.add(appWidgetInfo); 1486 } 1487 break; 1488 } 1489 } catch (Exception e) { 1490 Log.w(TAG, "Desktop items loading interrupted:", e); 1491 } 1492 } 1493 } finally { 1494 c.close(); 1495 } 1496 1497 if (itemsToRemove.size() > 0) { 1498 ContentProviderClient client = contentResolver.acquireContentProviderClient( 1499 LauncherSettings.Favorites.CONTENT_URI); 1500 // Remove dead items 1501 for (long id : itemsToRemove) { 1502 if (DEBUG_LOADERS) { 1503 Log.d(TAG, "Removed id = " + id); 1504 } 1505 // Don't notify content observers 1506 try { 1507 client.delete(LauncherSettings.Favorites.getContentUri(id, false), 1508 null, null); 1509 } catch (RemoteException e) { 1510 Log.w(TAG, "Could not remove id = " + id); 1511 } 1512 } 1513 } 1514 1515 if (DEBUG_LOADERS) { 1516 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms"); 1517 Log.d(TAG, "workspace layout: "); 1518 for (int y = 0; y < mCellCountY; y++) { 1519 String line = ""; 1520 for (int s = 0; s < Launcher.SCREEN_COUNT; s++) { 1521 if (s > 0) { 1522 line += " | "; 1523 } 1524 for (int x = 0; x < mCellCountX; x++) { 1525 line += ((occupied[s][x][y] != null) ? "#" : "."); 1526 } 1527 } 1528 Log.d(TAG, "[ " + line + " ]"); 1529 } 1530 } 1531 } 1532 } 1533 1534 /** Filters the set of items who are directly or indirectly (via another container) on the 1535 * specified screen. */ 1536 private void filterCurrentWorkspaceItems(int currentScreen, 1537 ArrayList<ItemInfo> allWorkspaceItems, 1538 ArrayList<ItemInfo> currentScreenItems, 1539 ArrayList<ItemInfo> otherScreenItems) { 1540 // Purge any null ItemInfos 1541 Iterator<ItemInfo> iter = allWorkspaceItems.iterator(); 1542 while (iter.hasNext()) { 1543 ItemInfo i = iter.next(); 1544 if (i == null) { 1545 iter.remove(); 1546 } 1547 } 1548 1549 // If we aren't filtering on a screen, then the set of items to load is the full set of 1550 // items given. 1551 if (currentScreen < 0) { 1552 currentScreenItems.addAll(allWorkspaceItems); 1553 } 1554 1555 // Order the set of items by their containers first, this allows use to walk through the 1556 // list sequentially, build up a list of containers that are in the specified screen, 1557 // as well as all items in those containers. 1558 Set<Long> itemsOnScreen = new HashSet<Long>(); 1559 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() { 1560 @Override 1561 public int compare(ItemInfo lhs, ItemInfo rhs) { 1562 return (int) (lhs.container - rhs.container); 1563 } 1564 }); 1565 for (ItemInfo info : allWorkspaceItems) { 1566 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { 1567 if (info.screen == currentScreen) { 1568 currentScreenItems.add(info); 1569 itemsOnScreen.add(info.id); 1570 } else { 1571 otherScreenItems.add(info); 1572 } 1573 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 1574 currentScreenItems.add(info); 1575 itemsOnScreen.add(info.id); 1576 } else { 1577 if (itemsOnScreen.contains(info.container)) { 1578 currentScreenItems.add(info); 1579 itemsOnScreen.add(info.id); 1580 } else { 1581 otherScreenItems.add(info); 1582 } 1583 } 1584 } 1585 } 1586 1587 /** Filters the set of widgets which are on the specified screen. */ 1588 private void filterCurrentAppWidgets(int currentScreen, 1589 ArrayList<LauncherAppWidgetInfo> appWidgets, 1590 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets, 1591 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) { 1592 // If we aren't filtering on a screen, then the set of items to load is the full set of 1593 // widgets given. 1594 if (currentScreen < 0) { 1595 currentScreenWidgets.addAll(appWidgets); 1596 } 1597 1598 for (LauncherAppWidgetInfo widget : appWidgets) { 1599 if (widget == null) continue; 1600 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && 1601 widget.screen == currentScreen) { 1602 currentScreenWidgets.add(widget); 1603 } else { 1604 otherScreenWidgets.add(widget); 1605 } 1606 } 1607 } 1608 1609 /** Filters the set of folders which are on the specified screen. */ 1610 private void filterCurrentFolders(int currentScreen, 1611 HashMap<Long, ItemInfo> itemsIdMap, 1612 HashMap<Long, FolderInfo> folders, 1613 HashMap<Long, FolderInfo> currentScreenFolders, 1614 HashMap<Long, FolderInfo> otherScreenFolders) { 1615 // If we aren't filtering on a screen, then the set of items to load is the full set of 1616 // widgets given. 1617 if (currentScreen < 0) { 1618 currentScreenFolders.putAll(folders); 1619 } 1620 1621 for (long id : folders.keySet()) { 1622 ItemInfo info = itemsIdMap.get(id); 1623 FolderInfo folder = folders.get(id); 1624 if (info == null || folder == null) continue; 1625 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && 1626 info.screen == currentScreen) { 1627 currentScreenFolders.put(id, folder); 1628 } else { 1629 otherScreenFolders.put(id, folder); 1630 } 1631 } 1632 } 1633 1634 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to 1635 * right) */ 1636 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) { 1637 // XXX: review this 1638 Collections.sort(workspaceItems, new Comparator<ItemInfo>() { 1639 @Override 1640 public int compare(ItemInfo lhs, ItemInfo rhs) { 1641 int cellCountX = LauncherModel.getCellCountX(); 1642 int cellCountY = LauncherModel.getCellCountY(); 1643 int screenOffset = cellCountX * cellCountY; 1644 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat 1645 long lr = (lhs.container * containerOffset + lhs.screen * screenOffset + 1646 lhs.cellY * cellCountX + lhs.cellX); 1647 long rr = (rhs.container * containerOffset + rhs.screen * screenOffset + 1648 rhs.cellY * cellCountX + rhs.cellX); 1649 return (int) (lr - rr); 1650 } 1651 }); 1652 } 1653 1654 private void bindWorkspaceItems(final Callbacks oldCallbacks, 1655 final ArrayList<ItemInfo> workspaceItems, 1656 final ArrayList<LauncherAppWidgetInfo> appWidgets, 1657 final HashMap<Long, FolderInfo> folders, 1658 ArrayList<Runnable> deferredBindRunnables) { 1659 1660 final boolean postOnMainThread = (deferredBindRunnables != null); 1661 1662 // Bind the workspace items 1663 int N = workspaceItems.size(); 1664 for (int i = 0; i < N; i += ITEMS_CHUNK) { 1665 final int start = i; 1666 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i); 1667 final Runnable r = new Runnable() { 1668 @Override 1669 public void run() { 1670 Callbacks callbacks = tryGetCallbacks(oldCallbacks); 1671 if (callbacks != null) { 1672 callbacks.bindItems(workspaceItems, start, start+chunkSize); 1673 } 1674 } 1675 }; 1676 if (postOnMainThread) { 1677 deferredBindRunnables.add(r); 1678 } else { 1679 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); 1680 } 1681 } 1682 1683 // Bind the folders 1684 if (!folders.isEmpty()) { 1685 final Runnable r = new Runnable() { 1686 public void run() { 1687 Callbacks callbacks = tryGetCallbacks(oldCallbacks); 1688 if (callbacks != null) { 1689 callbacks.bindFolders(folders); 1690 } 1691 } 1692 }; 1693 if (postOnMainThread) { 1694 deferredBindRunnables.add(r); 1695 } else { 1696 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); 1697 } 1698 } 1699 1700 // Bind the widgets, one at a time 1701 N = appWidgets.size(); 1702 for (int i = 0; i < N; i++) { 1703 final LauncherAppWidgetInfo widget = appWidgets.get(i); 1704 final Runnable r = new Runnable() { 1705 public void run() { 1706 Callbacks callbacks = tryGetCallbacks(oldCallbacks); 1707 if (callbacks != null) { 1708 callbacks.bindAppWidget(widget); 1709 } 1710 } 1711 }; 1712 if (postOnMainThread) { 1713 deferredBindRunnables.add(r); 1714 } else { 1715 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); 1716 } 1717 } 1718 } 1719 1720 /** 1721 * Binds all loaded data to actual views on the main thread. 1722 */ 1723 private void bindWorkspace(int synchronizeBindPage) { 1724 final long t = SystemClock.uptimeMillis(); 1725 Runnable r; 1726 1727 // Don't use these two variables in any of the callback runnables. 1728 // Otherwise we hold a reference to them. 1729 final Callbacks oldCallbacks = mCallbacks.get(); 1730 if (oldCallbacks == null) { 1731 // This launcher has exited and nobody bothered to tell us. Just bail. 1732 Log.w(TAG, "LoaderTask running with no launcher"); 1733 return; 1734 } 1735 1736 final boolean isLoadingSynchronously = (synchronizeBindPage > -1); 1737 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage : 1738 oldCallbacks.getCurrentWorkspaceScreen(); 1739 1740 // Load all the items that are on the current page first (and in the process, unbind 1741 // all the existing workspace items before we call startBinding() below. 1742 unbindWorkspaceItemsOnMainThread(); 1743 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(); 1744 ArrayList<LauncherAppWidgetInfo> appWidgets = 1745 new ArrayList<LauncherAppWidgetInfo>(); 1746 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(); 1747 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>(); 1748 synchronized (sBgLock) { 1749 workspaceItems.addAll(sBgWorkspaceItems); 1750 appWidgets.addAll(sBgAppWidgets); 1751 folders.putAll(sBgFolders); 1752 itemsIdMap.putAll(sBgItemsIdMap); 1753 } 1754 1755 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>(); 1756 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>(); 1757 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = 1758 new ArrayList<LauncherAppWidgetInfo>(); 1759 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = 1760 new ArrayList<LauncherAppWidgetInfo>(); 1761 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>(); 1762 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>(); 1763 1764 // Separate the items that are on the current screen, and all the other remaining items 1765 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems, 1766 otherWorkspaceItems); 1767 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets, 1768 otherAppWidgets); 1769 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders, 1770 otherFolders); 1771 sortWorkspaceItemsSpatially(currentWorkspaceItems); 1772 sortWorkspaceItemsSpatially(otherWorkspaceItems); 1773 1774 // Tell the workspace that we're about to start binding items 1775 r = new Runnable() { 1776 public void run() { 1777 Callbacks callbacks = tryGetCallbacks(oldCallbacks); 1778 if (callbacks != null) { 1779 callbacks.startBinding(); 1780 } 1781 } 1782 }; 1783 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); 1784 1785 // Load items on the current page 1786 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, 1787 currentFolders, null); 1788 if (isLoadingSynchronously) { 1789 r = new Runnable() { 1790 public void run() { 1791 Callbacks callbacks = tryGetCallbacks(oldCallbacks); 1792 if (callbacks != null) { 1793 callbacks.onPageBoundSynchronously(currentScreen); 1794 } 1795 } 1796 }; 1797 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); 1798 } 1799 1800 // Load all the remaining pages (if we are loading synchronously, we want to defer this 1801 // work until after the first render) 1802 mDeferredBindRunnables.clear(); 1803 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders, 1804 (isLoadingSynchronously ? mDeferredBindRunnables : null)); 1805 1806 // Tell the workspace that we're done binding items 1807 r = new Runnable() { 1808 public void run() { 1809 Callbacks callbacks = tryGetCallbacks(oldCallbacks); 1810 if (callbacks != null) { 1811 callbacks.finishBindingItems(); 1812 } 1813 1814 // If we're profiling, ensure this is the last thing in the queue. 1815 if (DEBUG_LOADERS) { 1816 Log.d(TAG, "bound workspace in " 1817 + (SystemClock.uptimeMillis()-t) + "ms"); 1818 } 1819 1820 mIsLoadingAndBindingWorkspace = false; 1821 } 1822 }; 1823 if (isLoadingSynchronously) { 1824 mDeferredBindRunnables.add(r); 1825 } else { 1826 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); 1827 } 1828 } 1829 1830 private void loadAndBindAllApps() { 1831 if (DEBUG_LOADERS) { 1832 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded); 1833 } 1834 if (!mAllAppsLoaded) { 1835 loadAllAppsByBatch(); 1836 synchronized (LoaderTask.this) { 1837 if (mStopped) { 1838 return; 1839 } 1840 mAllAppsLoaded = true; 1841 } 1842 } else { 1843 onlyBindAllApps(); 1844 } 1845 } 1846 1847 private void onlyBindAllApps() { 1848 final Callbacks oldCallbacks = mCallbacks.get(); 1849 if (oldCallbacks == null) { 1850 // This launcher has exited and nobody bothered to tell us. Just bail. 1851 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)"); 1852 return; 1853 } 1854 1855 // shallow copy 1856 @SuppressWarnings("unchecked") 1857 final ArrayList<ApplicationInfo> list 1858 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone(); 1859 Runnable r = new Runnable() { 1860 public void run() { 1861 final long t = SystemClock.uptimeMillis(); 1862 final Callbacks callbacks = tryGetCallbacks(oldCallbacks); 1863 if (callbacks != null) { 1864 callbacks.bindAllApplications(list); 1865 } 1866 if (DEBUG_LOADERS) { 1867 Log.d(TAG, "bound all " + list.size() + " apps from cache in " 1868 + (SystemClock.uptimeMillis()-t) + "ms"); 1869 } 1870 } 1871 }; 1872 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid()); 1873 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) { 1874 r.run(); 1875 } else { 1876 mHandler.post(r); 1877 } 1878 } 1879 1880 private void loadAllAppsByBatch() { 1881 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; 1882 1883 // Don't use these two variables in any of the callback runnables. 1884 // Otherwise we hold a reference to them. 1885 final Callbacks oldCallbacks = mCallbacks.get(); 1886 if (oldCallbacks == null) { 1887 // This launcher has exited and nobody bothered to tell us. Just bail. 1888 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)"); 1889 return; 1890 } 1891 1892 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 1893 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 1894 1895 final PackageManager packageManager = mContext.getPackageManager(); 1896 List<ResolveInfo> apps = null; 1897 1898 int N = Integer.MAX_VALUE; 1899 1900 int startIndex; 1901 int i=0; 1902 int batchSize = -1; 1903 while (i < N && !mStopped) { 1904 if (i == 0) { 1905 mBgAllAppsList.clear(); 1906 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; 1907 apps = packageManager.queryIntentActivities(mainIntent, 0); 1908 if (DEBUG_LOADERS) { 1909 Log.d(TAG, "queryIntentActivities took " 1910 + (SystemClock.uptimeMillis()-qiaTime) + "ms"); 1911 } 1912 if (apps == null) { 1913 return; 1914 } 1915 N = apps.size(); 1916 if (DEBUG_LOADERS) { 1917 Log.d(TAG, "queryIntentActivities got " + N + " apps"); 1918 } 1919 if (N == 0) { 1920 // There are no apps?!? 1921 return; 1922 } 1923 if (mBatchSize == 0) { 1924 batchSize = N; 1925 } else { 1926 batchSize = mBatchSize; 1927 } 1928 1929 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; 1930 Collections.sort(apps, 1931 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache)); 1932 if (DEBUG_LOADERS) { 1933 Log.d(TAG, "sort took " 1934 + (SystemClock.uptimeMillis()-sortTime) + "ms"); 1935 } 1936 } 1937 1938 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; 1939 1940 startIndex = i; 1941 for (int j=0; i<N && j<batchSize; j++) { 1942 // This builds the icon bitmaps. 1943 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), 1944 mIconCache, mLabelCache)); 1945 i++; 1946 } 1947 1948 final boolean first = i <= batchSize; 1949 final Callbacks callbacks = tryGetCallbacks(oldCallbacks); 1950 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added; 1951 mBgAllAppsList.added = new ArrayList<ApplicationInfo>(); 1952 1953 mHandler.post(new Runnable() { 1954 public void run() { 1955 final long t = SystemClock.uptimeMillis(); 1956 if (callbacks != null) { 1957 if (first) { 1958 callbacks.bindAllApplications(added); 1959 } else { 1960 callbacks.bindAppsAdded(added); 1961 } 1962 if (DEBUG_LOADERS) { 1963 Log.d(TAG, "bound " + added.size() + " apps in " 1964 + (SystemClock.uptimeMillis() - t) + "ms"); 1965 } 1966 } else { 1967 Log.i(TAG, "not binding apps: no Launcher activity"); 1968 } 1969 } 1970 }); 1971 1972 if (DEBUG_LOADERS) { 1973 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in " 1974 + (SystemClock.uptimeMillis()-t2) + "ms"); 1975 } 1976 1977 if (mAllAppsLoadDelay > 0 && i < N) { 1978 try { 1979 if (DEBUG_LOADERS) { 1980 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms"); 1981 } 1982 Thread.sleep(mAllAppsLoadDelay); 1983 } catch (InterruptedException exc) { } 1984 } 1985 } 1986 1987 if (DEBUG_LOADERS) { 1988 Log.d(TAG, "cached all " + N + " apps in " 1989 + (SystemClock.uptimeMillis()-t) + "ms" 1990 + (mAllAppsLoadDelay > 0 ? " (including delay)" : "")); 1991 } 1992 } 1993 1994 public void dumpState() { 1995 synchronized (sBgLock) { 1996 Log.d(TAG, "mLoaderTask.mContext=" + mContext); 1997 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching); 1998 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped); 1999 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished); 2000 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size()); 2001 } 2002 } 2003 } 2004 2005 void enqueuePackageUpdated(PackageUpdatedTask task) { 2006 sWorker.post(task); 2007 } 2008 2009 private class PackageUpdatedTask implements Runnable { 2010 int mOp; 2011 String[] mPackages; 2012 2013 public static final int OP_NONE = 0; 2014 public static final int OP_ADD = 1; 2015 public static final int OP_UPDATE = 2; 2016 public static final int OP_REMOVE = 3; // uninstlled 2017 public static final int OP_UNAVAILABLE = 4; // external media unmounted 2018 2019 2020 public PackageUpdatedTask(int op, String[] packages) { 2021 mOp = op; 2022 mPackages = packages; 2023 } 2024 2025 public void run() { 2026 final Context context = mApp; 2027 2028 final String[] packages = mPackages; 2029 final int N = packages.length; 2030 switch (mOp) { 2031 case OP_ADD: 2032 for (int i=0; i<N; i++) { 2033 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]); 2034 mBgAllAppsList.addPackage(context, packages[i]); 2035 } 2036 break; 2037 case OP_UPDATE: 2038 for (int i=0; i<N; i++) { 2039 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]); 2040 mBgAllAppsList.updatePackage(context, packages[i]); 2041 LauncherApplication app = 2042 (LauncherApplication) context.getApplicationContext(); 2043 WidgetPreviewLoader.removeFromDb( 2044 app.getWidgetPreviewCacheDb(), packages[i]); 2045 } 2046 break; 2047 case OP_REMOVE: 2048 case OP_UNAVAILABLE: 2049 for (int i=0; i<N; i++) { 2050 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]); 2051 mBgAllAppsList.removePackage(packages[i]); 2052 LauncherApplication app = 2053 (LauncherApplication) context.getApplicationContext(); 2054 WidgetPreviewLoader.removeFromDb( 2055 app.getWidgetPreviewCacheDb(), packages[i]); 2056 } 2057 break; 2058 } 2059 2060 ArrayList<ApplicationInfo> added = null; 2061 ArrayList<ApplicationInfo> modified = null; 2062 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>(); 2063 2064 if (mBgAllAppsList.added.size() > 0) { 2065 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added); 2066 mBgAllAppsList.added.clear(); 2067 } 2068 if (mBgAllAppsList.modified.size() > 0) { 2069 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified); 2070 mBgAllAppsList.modified.clear(); 2071 } 2072 if (mBgAllAppsList.removed.size() > 0) { 2073 removedApps.addAll(mBgAllAppsList.removed); 2074 mBgAllAppsList.removed.clear(); 2075 } 2076 2077 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null; 2078 if (callbacks == null) { 2079 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading."); 2080 return; 2081 } 2082 2083 if (added != null) { 2084 final ArrayList<ApplicationInfo> addedFinal = added; 2085 mHandler.post(new Runnable() { 2086 public void run() { 2087 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null; 2088 if (callbacks == cb && cb != null) { 2089 callbacks.bindAppsAdded(addedFinal); 2090 } 2091 } 2092 }); 2093 } 2094 if (modified != null) { 2095 final ArrayList<ApplicationInfo> modifiedFinal = modified; 2096 mHandler.post(new Runnable() { 2097 public void run() { 2098 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null; 2099 if (callbacks == cb && cb != null) { 2100 callbacks.bindAppsUpdated(modifiedFinal); 2101 } 2102 } 2103 }); 2104 } 2105 // If a package has been removed, or an app has been removed as a result of 2106 // an update (for example), make the removed callback. 2107 if (mOp == OP_REMOVE || !removedApps.isEmpty()) { 2108 final boolean permanent = (mOp == OP_REMOVE); 2109 final ArrayList<String> removedPackageNames = 2110 new ArrayList<String>(Arrays.asList(packages)); 2111 2112 mHandler.post(new Runnable() { 2113 public void run() { 2114 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null; 2115 if (callbacks == cb && cb != null) { 2116 callbacks.bindComponentsRemoved(removedPackageNames, 2117 removedApps, permanent); 2118 } 2119 } 2120 }); 2121 } 2122 2123 final ArrayList<Object> widgetsAndShortcuts = 2124 getSortedWidgetsAndShortcuts(context); 2125 mHandler.post(new Runnable() { 2126 @Override 2127 public void run() { 2128 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null; 2129 if (callbacks == cb && cb != null) { 2130 callbacks.bindPackagesUpdated(widgetsAndShortcuts); 2131 } 2132 } 2133 }); 2134 } 2135 } 2136 2137 // Returns a list of ResolveInfos/AppWindowInfos in sorted order 2138 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) { 2139 PackageManager packageManager = context.getPackageManager(); 2140 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>(); 2141 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders()); 2142 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); 2143 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0)); 2144 Collections.sort(widgetsAndShortcuts, 2145 new LauncherModel.WidgetAndShortcutNameComparator(packageManager)); 2146 return widgetsAndShortcuts; 2147 } 2148 2149 /** 2150 * This is called from the code that adds shortcuts from the intent receiver. This 2151 * doesn't have a Cursor, but 2152 */ 2153 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) { 2154 return getShortcutInfo(manager, intent, context, null, -1, -1, null); 2155 } 2156 2157 /** 2158 * Make an ShortcutInfo object for a shortcut that is an application. 2159 * 2160 * If c is not null, then it will be used to fill in missing data like the title and icon. 2161 */ 2162 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context, 2163 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) { 2164 Bitmap icon = null; 2165 final ShortcutInfo info = new ShortcutInfo(); 2166 2167 ComponentName componentName = intent.getComponent(); 2168 if (componentName == null) { 2169 return null; 2170 } 2171 2172 try { 2173 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0); 2174 if (!pi.applicationInfo.enabled) { 2175 // If we return null here, the corresponding item will be removed from the launcher 2176 // db and will not appear in the workspace. 2177 return null; 2178 } 2179 } catch (NameNotFoundException e) { 2180 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName()); 2181 } 2182 2183 // TODO: See if the PackageManager knows about this case. If it doesn't 2184 // then return null & delete this. 2185 2186 // the resource -- This may implicitly give us back the fallback icon, 2187 // but don't worry about that. All we're doing with usingFallbackIcon is 2188 // to avoid saving lots of copies of that in the database, and most apps 2189 // have icons anyway. 2190 2191 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and 2192 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info 2193 // via resolveActivity(). 2194 ResolveInfo resolveInfo = null; 2195 ComponentName oldComponent = intent.getComponent(); 2196 Intent newIntent = new Intent(intent.getAction(), null); 2197 newIntent.addCategory(Intent.CATEGORY_LAUNCHER); 2198 newIntent.setPackage(oldComponent.getPackageName()); 2199 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0); 2200 for (ResolveInfo i : infos) { 2201 ComponentName cn = new ComponentName(i.activityInfo.packageName, 2202 i.activityInfo.name); 2203 if (cn.equals(oldComponent)) { 2204 resolveInfo = i; 2205 } 2206 } 2207 if (resolveInfo == null) { 2208 resolveInfo = manager.resolveActivity(intent, 0); 2209 } 2210 if (resolveInfo != null) { 2211 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache); 2212 } 2213 // the db 2214 if (icon == null) { 2215 if (c != null) { 2216 icon = getIconFromCursor(c, iconIndex, context); 2217 } 2218 } 2219 // the fallback icon 2220 if (icon == null) { 2221 icon = getFallbackIcon(); 2222 info.usingFallbackIcon = true; 2223 } 2224 info.setIcon(icon); 2225 2226 // from the resource 2227 if (resolveInfo != null) { 2228 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo); 2229 if (labelCache != null && labelCache.containsKey(key)) { 2230 info.title = labelCache.get(key); 2231 } else { 2232 info.title = resolveInfo.activityInfo.loadLabel(manager); 2233 if (labelCache != null) { 2234 labelCache.put(key, info.title); 2235 } 2236 } 2237 } 2238 // from the db 2239 if (info.title == null) { 2240 if (c != null) { 2241 info.title = c.getString(titleIndex); 2242 } 2243 } 2244 // fall back to the class name of the activity 2245 if (info.title == null) { 2246 info.title = componentName.getClassName(); 2247 } 2248 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; 2249 return info; 2250 } 2251 2252 /** 2253 * Returns the set of workspace ShortcutInfos with the specified intent. 2254 */ 2255 static ArrayList<ItemInfo> getWorkspaceShortcutItemInfosWithIntent(Intent intent) { 2256 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>(); 2257 synchronized (sBgLock) { 2258 for (ItemInfo info : sBgWorkspaceItems) { 2259 if (info instanceof ShortcutInfo) { 2260 ShortcutInfo shortcut = (ShortcutInfo) info; 2261 if (shortcut.intent.toUri(0).equals(intent.toUri(0))) { 2262 items.add(shortcut); 2263 } 2264 } 2265 } 2266 } 2267 return items; 2268 } 2269 2270 /** 2271 * Make an ShortcutInfo object for a shortcut that isn't an application. 2272 */ 2273 private ShortcutInfo getShortcutInfo(Cursor c, Context context, 2274 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex, 2275 int titleIndex) { 2276 2277 Bitmap icon = null; 2278 final ShortcutInfo info = new ShortcutInfo(); 2279 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; 2280 2281 // TODO: If there's an explicit component and we can't install that, delete it. 2282 2283 info.title = c.getString(titleIndex); 2284 2285 int iconType = c.getInt(iconTypeIndex); 2286 switch (iconType) { 2287 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE: 2288 String packageName = c.getString(iconPackageIndex); 2289 String resourceName = c.getString(iconResourceIndex); 2290 PackageManager packageManager = context.getPackageManager(); 2291 info.customIcon = false; 2292 // the resource 2293 try { 2294 Resources resources = packageManager.getResourcesForApplication(packageName); 2295 if (resources != null) { 2296 final int id = resources.getIdentifier(resourceName, null, null); 2297 icon = Utilities.createIconBitmap( 2298 mIconCache.getFullResIcon(resources, id), context); 2299 } 2300 } catch (Exception e) { 2301 // drop this. we have other places to look for icons 2302 } 2303 // the db 2304 if (icon == null) { 2305 icon = getIconFromCursor(c, iconIndex, context); 2306 } 2307 // the fallback icon 2308 if (icon == null) { 2309 icon = getFallbackIcon(); 2310 info.usingFallbackIcon = true; 2311 } 2312 break; 2313 case LauncherSettings.Favorites.ICON_TYPE_BITMAP: 2314 icon = getIconFromCursor(c, iconIndex, context); 2315 if (icon == null) { 2316 icon = getFallbackIcon(); 2317 info.customIcon = false; 2318 info.usingFallbackIcon = true; 2319 } else { 2320 info.customIcon = true; 2321 } 2322 break; 2323 default: 2324 icon = getFallbackIcon(); 2325 info.usingFallbackIcon = true; 2326 info.customIcon = false; 2327 break; 2328 } 2329 info.setIcon(icon); 2330 return info; 2331 } 2332 2333 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) { 2334 @SuppressWarnings("all") // suppress dead code warning 2335 final boolean debug = false; 2336 if (debug) { 2337 Log.d(TAG, "getIconFromCursor app=" 2338 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE))); 2339 } 2340 byte[] data = c.getBlob(iconIndex); 2341 try { 2342 return Utilities.createIconBitmap( 2343 BitmapFactory.decodeByteArray(data, 0, data.length), context); 2344 } catch (Exception e) { 2345 return null; 2346 } 2347 } 2348 2349 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen, 2350 int cellX, int cellY, boolean notify) { 2351 final ShortcutInfo info = infoFromShortcutIntent(context, data, null); 2352 if (info == null) { 2353 return null; 2354 } 2355 addItemToDatabase(context, info, container, screen, cellX, cellY, notify); 2356 2357 return info; 2358 } 2359 2360 /** 2361 * Attempts to find an AppWidgetProviderInfo that matches the given component. 2362 */ 2363 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context, 2364 ComponentName component) { 2365 List<AppWidgetProviderInfo> widgets = 2366 AppWidgetManager.getInstance(context).getInstalledProviders(); 2367 for (AppWidgetProviderInfo info : widgets) { 2368 if (info.provider.equals(component)) { 2369 return info; 2370 } 2371 } 2372 return null; 2373 } 2374 2375 /** 2376 * Returns a list of all the widgets that can handle configuration with a particular mimeType. 2377 */ 2378 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) { 2379 final PackageManager packageManager = context.getPackageManager(); 2380 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities = 2381 new ArrayList<WidgetMimeTypeHandlerData>(); 2382 2383 final Intent supportsIntent = 2384 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE); 2385 supportsIntent.setType(mimeType); 2386 2387 // Create a set of widget configuration components that we can test against 2388 final List<AppWidgetProviderInfo> widgets = 2389 AppWidgetManager.getInstance(context).getInstalledProviders(); 2390 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget = 2391 new HashMap<ComponentName, AppWidgetProviderInfo>(); 2392 for (AppWidgetProviderInfo info : widgets) { 2393 configurationComponentToWidget.put(info.configure, info); 2394 } 2395 2396 // Run through each of the intents that can handle this type of clip data, and cross 2397 // reference them with the components that are actual configuration components 2398 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent, 2399 PackageManager.MATCH_DEFAULT_ONLY); 2400 for (ResolveInfo info : activities) { 2401 final ActivityInfo activityInfo = info.activityInfo; 2402 final ComponentName infoComponent = new ComponentName(activityInfo.packageName, 2403 activityInfo.name); 2404 if (configurationComponentToWidget.containsKey(infoComponent)) { 2405 supportedConfigurationActivities.add( 2406 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info, 2407 configurationComponentToWidget.get(infoComponent))); 2408 } 2409 } 2410 return supportedConfigurationActivities; 2411 } 2412 2413 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) { 2414 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); 2415 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); 2416 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); 2417 2418 if (intent == null) { 2419 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null 2420 Log.e(TAG, "Can't construct ShorcutInfo with null intent"); 2421 return null; 2422 } 2423 2424 Bitmap icon = null; 2425 boolean customIcon = false; 2426 ShortcutIconResource iconResource = null; 2427 2428 if (bitmap != null && bitmap instanceof Bitmap) { 2429 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context); 2430 customIcon = true; 2431 } else { 2432 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); 2433 if (extra != null && extra instanceof ShortcutIconResource) { 2434 try { 2435 iconResource = (ShortcutIconResource) extra; 2436 final PackageManager packageManager = context.getPackageManager(); 2437 Resources resources = packageManager.getResourcesForApplication( 2438 iconResource.packageName); 2439 final int id = resources.getIdentifier(iconResource.resourceName, null, null); 2440 icon = Utilities.createIconBitmap( 2441 mIconCache.getFullResIcon(resources, id), context); 2442 } catch (Exception e) { 2443 Log.w(TAG, "Could not load shortcut icon: " + extra); 2444 } 2445 } 2446 } 2447 2448 final ShortcutInfo info = new ShortcutInfo(); 2449 2450 if (icon == null) { 2451 if (fallbackIcon != null) { 2452 icon = fallbackIcon; 2453 } else { 2454 icon = getFallbackIcon(); 2455 info.usingFallbackIcon = true; 2456 } 2457 } 2458 info.setIcon(icon); 2459 2460 info.title = name; 2461 info.intent = intent; 2462 info.customIcon = customIcon; 2463 info.iconResource = iconResource; 2464 2465 return info; 2466 } 2467 2468 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c, 2469 int iconIndex) { 2470 // If apps can't be on SD, don't even bother. 2471 if (!mAppsCanBeOnRemoveableStorage) { 2472 return false; 2473 } 2474 // If this icon doesn't have a custom icon, check to see 2475 // what's stored in the DB, and if it doesn't match what 2476 // we're going to show, store what we are going to show back 2477 // into the DB. We do this so when we're loading, if the 2478 // package manager can't find an icon (for example because 2479 // the app is on SD) then we can use that instead. 2480 if (!info.customIcon && !info.usingFallbackIcon) { 2481 cache.put(info, c.getBlob(iconIndex)); 2482 return true; 2483 } 2484 return false; 2485 } 2486 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) { 2487 boolean needSave = false; 2488 try { 2489 if (data != null) { 2490 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length); 2491 Bitmap loaded = info.getIcon(mIconCache); 2492 needSave = !saved.sameAs(loaded); 2493 } else { 2494 needSave = true; 2495 } 2496 } catch (Exception e) { 2497 needSave = true; 2498 } 2499 if (needSave) { 2500 Log.d(TAG, "going to save icon bitmap for info=" + info); 2501 // This is slower than is ideal, but this only happens once 2502 // or when the app is updated with a new icon. 2503 updateItemInDatabase(context, info); 2504 } 2505 } 2506 2507 /** 2508 * Return an existing FolderInfo object if we have encountered this ID previously, 2509 * or make a new one. 2510 */ 2511 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) { 2512 // See if a placeholder was created for us already 2513 FolderInfo folderInfo = folders.get(id); 2514 if (folderInfo == null) { 2515 // No placeholder -- create a new instance 2516 folderInfo = new FolderInfo(); 2517 folders.put(id, folderInfo); 2518 } 2519 return folderInfo; 2520 } 2521 2522 public static final Comparator<ApplicationInfo> getAppNameComparator() { 2523 final Collator collator = Collator.getInstance(); 2524 return new Comparator<ApplicationInfo>() { 2525 public final int compare(ApplicationInfo a, ApplicationInfo b) { 2526 int result = collator.compare(a.title.toString(), b.title.toString()); 2527 if (result == 0) { 2528 result = a.componentName.compareTo(b.componentName); 2529 } 2530 return result; 2531 } 2532 }; 2533 } 2534 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR 2535 = new Comparator<ApplicationInfo>() { 2536 public final int compare(ApplicationInfo a, ApplicationInfo b) { 2537 if (a.firstInstallTime < b.firstInstallTime) return 1; 2538 if (a.firstInstallTime > b.firstInstallTime) return -1; 2539 return 0; 2540 } 2541 }; 2542 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() { 2543 final Collator collator = Collator.getInstance(); 2544 return new Comparator<AppWidgetProviderInfo>() { 2545 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) { 2546 return collator.compare(a.label.toString(), b.label.toString()); 2547 } 2548 }; 2549 } 2550 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) { 2551 if (info.activityInfo != null) { 2552 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name); 2553 } else { 2554 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name); 2555 } 2556 } 2557 public static class ShortcutNameComparator implements Comparator<ResolveInfo> { 2558 private Collator mCollator; 2559 private PackageManager mPackageManager; 2560 private HashMap<Object, CharSequence> mLabelCache; 2561 ShortcutNameComparator(PackageManager pm) { 2562 mPackageManager = pm; 2563 mLabelCache = new HashMap<Object, CharSequence>(); 2564 mCollator = Collator.getInstance(); 2565 } 2566 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) { 2567 mPackageManager = pm; 2568 mLabelCache = labelCache; 2569 mCollator = Collator.getInstance(); 2570 } 2571 public final int compare(ResolveInfo a, ResolveInfo b) { 2572 CharSequence labelA, labelB; 2573 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a); 2574 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b); 2575 if (mLabelCache.containsKey(keyA)) { 2576 labelA = mLabelCache.get(keyA); 2577 } else { 2578 labelA = a.loadLabel(mPackageManager).toString(); 2579 2580 mLabelCache.put(keyA, labelA); 2581 } 2582 if (mLabelCache.containsKey(keyB)) { 2583 labelB = mLabelCache.get(keyB); 2584 } else { 2585 labelB = b.loadLabel(mPackageManager).toString(); 2586 2587 mLabelCache.put(keyB, labelB); 2588 } 2589 return mCollator.compare(labelA, labelB); 2590 } 2591 }; 2592 public static class WidgetAndShortcutNameComparator implements Comparator<Object> { 2593 private Collator mCollator; 2594 private PackageManager mPackageManager; 2595 private HashMap<Object, String> mLabelCache; 2596 WidgetAndShortcutNameComparator(PackageManager pm) { 2597 mPackageManager = pm; 2598 mLabelCache = new HashMap<Object, String>(); 2599 mCollator = Collator.getInstance(); 2600 } 2601 public final int compare(Object a, Object b) { 2602 String labelA, labelB; 2603 if (mLabelCache.containsKey(a)) { 2604 labelA = mLabelCache.get(a); 2605 } else { 2606 labelA = (a instanceof AppWidgetProviderInfo) ? 2607 ((AppWidgetProviderInfo) a).label : 2608 ((ResolveInfo) a).loadLabel(mPackageManager).toString(); 2609 mLabelCache.put(a, labelA); 2610 } 2611 if (mLabelCache.containsKey(b)) { 2612 labelB = mLabelCache.get(b); 2613 } else { 2614 labelB = (b instanceof AppWidgetProviderInfo) ? 2615 ((AppWidgetProviderInfo) b).label : 2616 ((ResolveInfo) b).loadLabel(mPackageManager).toString(); 2617 mLabelCache.put(b, labelB); 2618 } 2619 return mCollator.compare(labelA, labelB); 2620 } 2621 }; 2622 2623 public void dumpState() { 2624 Log.d(TAG, "mCallbacks=" + mCallbacks); 2625 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data); 2626 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added); 2627 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed); 2628 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified); 2629 if (mLoaderTask != null) { 2630 mLoaderTask.dumpState(); 2631 } else { 2632 Log.d(TAG, "mLoaderTask=null"); 2633 } 2634 } 2635 } 2636