blob: d0988054201de3470c538c66df0ddab4068fe815 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070043import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040044import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080045
Michael Jurkac2f801e2011-07-12 14:19:46 -070046import java.lang.ref.WeakReference;
47import java.net.URISyntaxException;
48import java.text.Collator;
49import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070050import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070051import java.util.Collections;
52import java.util.Comparator;
53import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070054import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070055import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070056import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070057import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070058import java.util.TreeMap;
Michael Jurkac2f801e2011-07-12 14:19:46 -070059
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060/**
61 * Maintains in-memory state of the Launcher. It is expected that there should be only one
62 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070063 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040065public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080066 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040067 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070068
Joe Onorato36115782010-06-17 13:28:48 -040069 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Joe Onorato17a89222011-02-08 17:26:11 -080070 private final boolean mAppsCanBeOnExternalStorage;
Joe Onoratod65d08e2010-04-20 15:43:37 -040071 private int mBatchSize; // 0 is all apps at once
Daniel Sandler2ff10b32010-04-16 15:06:06 -040072 private int mAllAppsLoadDelay; // milliseconds between batches
Daniel Sandlerdca66122010-04-13 16:23:58 -040073
Daniel Sandlercc8befa2013-06-11 14:45:48 -040074 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040075 private final Object mLock = new Object();
76 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040077 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070078 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020079 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
Winson Chung81b52252012-08-27 15:34:29 -070081 // Specific runnable types that are run on the main thread deferred handler, this allows us to
82 // clear all queued binding runnables when the Launcher activity is destroyed.
83 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
84 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
85
86
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070087 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
88 static {
89 sWorkerThread.start();
90 }
91 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
92
Joe Onoratocc67f472010-06-08 10:54:30 -070093 // We start off with everything not loaded. After that, we assume that
94 // our monitoring of the package manager provides all updates and we never
95 // need to do a requery. These are only ever touched from the loader thread.
96 private boolean mWorkspaceLoaded;
97 private boolean mAllAppsLoaded;
98
Winson Chungb8b2a5a2012-07-12 17:55:31 -070099 // When we are loading pages synchronously, we can't just post the binding of items on the side
100 // pages as this delays the rotation process. Instead, we wait for a callback from the first
101 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
102 // a normal load, we also clear this set of Runnables.
103 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
104
Joe Onorato9c1289c2009-08-17 11:03:03 -0400105 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700107 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700108 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800109
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700110 // The lock that must be acquired before referencing any static bg data structures. Unlike
111 // other locks, this one can generally be held long-term because we never expect any of these
112 // static data structures to be referenced outside of the worker thread except on the first
113 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700114 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700115
Adam Cohen487f7dd2012-06-28 18:12:10 -0700116 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700117 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700118 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700119
Adam Cohen487f7dd2012-06-28 18:12:10 -0700120 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
121 // created by LauncherModel that are directly on the home screen (however, no widgets or
122 // shortcuts within folders).
123 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700124
Adam Cohen487f7dd2012-06-28 18:12:10 -0700125 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
126 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700127 new ArrayList<LauncherAppWidgetInfo>();
128
Adam Cohen487f7dd2012-06-28 18:12:10 -0700129 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
130 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700131
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
133 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700134
135 // sBgWorkspaceScreens is the ordered set of workspace screens.
136 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
137
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138 // </ only access in worker thread >
139
140 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800141 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Adam Cohend22015c2010-07-26 22:02:18 -0700143 private static int mCellCountX;
144 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700145
Reena Lee99a73f32011-10-24 17:27:37 -0700146 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700147
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700149 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public int getCurrentWorkspaceScreen();
151 public void startBinding();
152 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
Adam Cohendcd297f2013-06-18 13:13:40 -0700153 public void bindScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500154 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700155 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 public void bindAppWidget(LauncherAppWidgetInfo info);
157 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Joe Onorato64e6be72010-03-05 15:05:52 -0500158 public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
159 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700160 public void bindComponentsRemoved(ArrayList<String> packageNames,
161 ArrayList<ApplicationInfo> appInfos,
162 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200163 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Daniel Sandler843e8602010-06-07 14:59:01 -0400164 public boolean isAllAppsVisible();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800165 public boolean isAllAppsButtonRank(int rank);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100166 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700167 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Daniel Sandlere4f98912013-06-25 15:13:26 -0400170 LauncherModel(LauncherAppState app, IconCache iconCache) {
171 final Context context = app.getContext();
172
Joe Onorato17a89222011-02-08 17:26:11 -0800173 mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400174 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700175 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800176 mIconCache = iconCache;
177
178 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400179 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400180
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400181 final Resources res = context.getResources();
Reena Lee93f824a2011-09-23 17:20:28 -0700182 mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay);
183 mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize);
Reena Lee99a73f32011-10-24 17:27:37 -0700184 Configuration config = res.getConfiguration();
185 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800186 }
187
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700188 /** 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) {
Winson Chung81b52252012-08-27 15:34:29 -0700191 runOnMainThread(r, 0);
192 }
193 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700194 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
Winson Chungc9168342013-06-26 14:54:55 -0700213 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
214 long screen) {
215 final int xCount = LauncherModel.getCellCountX();
216 final int yCount = LauncherModel.getCellCountY();
217 boolean[][] occupied = new boolean[xCount][yCount];
218
219 int cellX, cellY, spanX, spanY;
220 for (int i = 0; i < items.size(); ++i) {
221 final ItemInfo item = items.get(i);
222 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
223 if (item.screenId == screen) {
224 cellX = item.cellX;
225 cellY = item.cellY;
226 spanX = item.spanX;
227 spanY = item.spanY;
228 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
229 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
230 occupied[x][y] = true;
231 }
232 }
233 }
234 }
235 }
236
237 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
238 }
239 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
240 Intent launchIntent) {
241 // Lock on the app so that we don't try and get the items while apps are being added
242 LauncherAppState app = LauncherAppState.getInstance();
243 LauncherModel model = app.getModel();
244 boolean found = false;
245 synchronized (app) {
246 // Flush the LauncherModel worker thread, so that if we just did another
247 // processInstallShortcut, we give it time for its shortcut to get added to the
248 // database (getItemsInLocalCoordinates reads the database)
249 model.flushWorkerThread();
250 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
251 final boolean shortcutExists = LauncherModel.shortcutExists(context, name, launchIntent);
252
253 // Try adding to the workspace screens incrementally, starting at the default or center
254 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
255 for (int screen = 0; screen < sBgWorkspaceScreens.size() && !found; screen++) {
256 int[] tmpCoordinates = new int[2];
257 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
258 sBgWorkspaceScreens.get(screen))) {
259 // Update the Launcher db
260 return new Pair<Long, int[]>(sBgWorkspaceScreens.get(screen), tmpCoordinates);
261 }
262 }
263 }
264 // XXX: Create a new page and add it to the first spot
265 return null;
266 }
267
Joe Onorato56d82912010-03-07 14:32:10 -0500268 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800269 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271
Winson Chung81b52252012-08-27 15:34:29 -0700272 public void unbindItemInfosAndClearQueuedBindRunnables() {
273 if (sWorkerThread.getThreadId() == Process.myTid()) {
274 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
275 "main thread");
276 }
277
278 // Clear any deferred bind runnables
279 mDeferredBindRunnables.clear();
280 // Remove any queued bind runnables
281 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
282 // Unbind all the workspace items
283 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700284 }
285
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700286 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700287 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700288 // Ensure that we don't use the same workspace items data structure on the main thread
289 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700290 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
291 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700292 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700293 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
294 tmpAppWidgets.addAll(sBgAppWidgets);
295 }
296 Runnable r = new Runnable() {
297 @Override
298 public void run() {
299 for (ItemInfo item : tmpWorkspaceItems) {
300 item.unbind();
301 }
302 for (ItemInfo item : tmpAppWidgets) {
303 item.unbind();
304 }
305 }
306 };
307 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700308 }
309
Joe Onorato9c1289c2009-08-17 11:03:03 -0400310 /**
311 * Adds an item to the DB if it was not created previously, or move it to a new
312 * <container, screen, cellX, cellY>
313 */
314 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700315 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316 if (item.container == ItemInfo.NO_ID) {
317 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700318 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400319 } else {
320 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700321 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323 }
324
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700325 static void checkItemInfoLocked(
326 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
327 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
328 if (modelItem != null && item != modelItem) {
329 // check all the data is consistent
330 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
331 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
332 ShortcutInfo shortcut = (ShortcutInfo) item;
333 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
334 modelShortcut.intent.filterEquals(shortcut.intent) &&
335 modelShortcut.id == shortcut.id &&
336 modelShortcut.itemType == shortcut.itemType &&
337 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700338 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700339 modelShortcut.cellX == shortcut.cellX &&
340 modelShortcut.cellY == shortcut.cellY &&
341 modelShortcut.spanX == shortcut.spanX &&
342 modelShortcut.spanY == shortcut.spanY &&
343 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
344 (modelShortcut.dropPos != null &&
345 shortcut.dropPos != null &&
346 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
347 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
348 // For all intents and purposes, this is the same object
349 return;
350 }
351 }
352
353 // the modelItem needs to match up perfectly with item if our model is
354 // to be consistent with the database-- for now, just require
355 // modelItem == item or the equality check above
356 String msg = "item: " + ((item != null) ? item.toString() : "null") +
357 "modelItem: " +
358 ((modelItem != null) ? modelItem.toString() : "null") +
359 "Error: ItemInfo passed to checkItemInfo doesn't match original";
360 RuntimeException e = new RuntimeException(msg);
361 if (stackTrace != null) {
362 e.setStackTrace(stackTrace);
363 }
Adam Cohene25af792013-06-06 23:08:25 -0700364 // TODO: something breaks this in the upgrade path
365 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700366 }
367 }
368
Michael Jurka816474f2012-06-25 14:49:02 -0700369 static void checkItemInfo(final ItemInfo item) {
370 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
371 final long itemId = item.id;
372 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700373 public void run() {
374 synchronized (sBgLock) {
375 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700376 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700377 }
378 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700379 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700380 }
381
Michael Jurkac9d95c52011-08-29 14:03:34 -0700382 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
383 final ItemInfo item, final String callingFunction) {
384 final long itemId = item.id;
385 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
386 final ContentResolver cr = context.getContentResolver();
387
Adam Cohen487f7dd2012-06-28 18:12:10 -0700388 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700389 Runnable r = new Runnable() {
390 public void run() {
391 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700392 updateItemArrays(item, itemId, stackTrace);
393 }
394 };
395 runOnWorkerThread(r);
396 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700397
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700398 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
399 final ArrayList<ItemInfo> items, final String callingFunction) {
400 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700401
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700402 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
403 Runnable r = new Runnable() {
404 public void run() {
405 ArrayList<ContentProviderOperation> ops =
406 new ArrayList<ContentProviderOperation>();
407 int count = items.size();
408 for (int i = 0; i < count; i++) {
409 ItemInfo item = items.get(i);
410 final long itemId = item.id;
411 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
412 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700413
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700414 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
415 updateItemArrays(item, itemId, stackTrace);
416
417 }
418 try {
419 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
420 } catch (Exception e) {
421 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700422 }
423 }
424 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700426 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700427
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700428 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
429 // Lock on mBgLock *after* the db operation
430 synchronized (sBgLock) {
431 checkItemInfoLocked(itemId, item, stackTrace);
432
433 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
434 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
435 // Item is in a folder, make sure this folder exists
436 if (!sBgFolders.containsKey(item.container)) {
437 // An items container is being set to a that of an item which is not in
438 // the list of Folders.
439 String msg = "item: " + item + " container being set to: " +
440 item.container + ", not in the list of folders";
441 Log.e(TAG, msg);
442 Launcher.dumpDebugLogsToConsole();
443 }
444 }
445
446 // Items are added/removed from the corresponding FolderInfo elsewhere, such
447 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
448 // that are on the desktop, as appropriate
449 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
450 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
451 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
452 switch (modelItem.itemType) {
453 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
454 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
455 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
456 if (!sBgWorkspaceItems.contains(modelItem)) {
457 sBgWorkspaceItems.add(modelItem);
458 }
459 break;
460 default:
461 break;
462 }
463 } else {
464 sBgWorkspaceItems.remove(modelItem);
465 }
466 }
467 }
468
Michael Jurkac7700af2013-05-14 20:17:58 +0200469 public void flushWorkerThread() {
470 mFlushingWorkerThread = true;
471 Runnable waiter = new Runnable() {
472 public void run() {
473 synchronized (this) {
474 notifyAll();
475 mFlushingWorkerThread = false;
476 }
477 }
478 };
479
480 synchronized(waiter) {
481 runOnWorkerThread(waiter);
482 if (mLoaderTask != null) {
483 synchronized(mLoaderTask) {
484 mLoaderTask.notify();
485 }
486 }
487 boolean success = false;
488 while (!success) {
489 try {
490 waiter.wait();
491 success = true;
492 } catch (InterruptedException e) {
493 }
494 }
495 }
496 }
497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400499 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700500 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700501 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700502 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700503 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700504 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
505 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700506 Launcher.sDumpLogs.add(transaction);
507 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400508 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400509 item.cellX = cellX;
510 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700511
Winson Chung3d503fb2011-07-13 17:25:49 -0700512 // We store hotseat items in canonical form which is this orientation invariant position
513 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700514 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700515 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700516 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700517 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700518 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700519 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400520
521 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400522 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700523 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
524 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700525 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526
Michael Jurkac9d95c52011-08-29 14:03:34 -0700527 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700528 }
529
530 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700531 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
532 * cellX, cellY have already been updated on the ItemInfos.
533 */
534 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
535 final long container, final int screen) {
536
537 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
538 int count = items.size();
539
540 for (int i = 0; i < count; i++) {
541 ItemInfo item = items.get(i);
542 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700543 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700544 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
545 + item.cellX + ", " + item.cellY + ")";
546 Launcher.sDumpLogs.add(transaction);
547 item.container = container;
548
549 // We store hotseat items in canonical form which is this orientation invariant position
550 // in the hotseat
551 if (context instanceof Launcher && screen < 0 &&
552 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700553 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700554 item.cellY);
555 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700556 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700557 }
558
559 final ContentValues values = new ContentValues();
560 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
561 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
562 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700563 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700564
565 contentValues.add(values);
566 }
567 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
568 }
569
570 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700571 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800572 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700573 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700574 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700575 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700576 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
577 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700578 Launcher.sDumpLogs.add(transaction);
579 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800580 item.cellX = cellX;
581 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700582 item.spanX = spanX;
583 item.spanY = spanY;
584
585 // We store hotseat items in canonical form which is this orientation invariant position
586 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700587 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700588 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700589 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700590 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700591 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700592 }
Adam Cohend4844c32011-02-18 19:25:06 -0800593
Adam Cohend4844c32011-02-18 19:25:06 -0800594 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800595 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700596 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
597 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
598 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
599 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700600 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800601
Michael Jurka816474f2012-06-25 14:49:02 -0700602 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700603 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700604
605 /**
606 * Update an item to the database in a specified container.
607 */
608 static void updateItemInDatabase(Context context, final ItemInfo item) {
609 final ContentValues values = new ContentValues();
610 item.onAddToDatabase(values);
611 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
612 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800613 }
614
615 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 * Returns true if the shortcuts already exists in the database.
617 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 static boolean shortcutExists(Context context, String title, Intent intent) {
620 final ContentResolver cr = context.getContentResolver();
621 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
622 new String[] { "title", "intent" }, "title=? and intent=?",
623 new String[] { title, intent.toUri(0) }, null);
624 boolean result = false;
625 try {
626 result = c.moveToFirst();
627 } finally {
628 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700631 }
632
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 * Returns an ItemInfo array containing all the items in the LauncherModel.
635 * The ItemInfo.id is not set through this function.
636 */
637 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
638 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
639 final ContentResolver cr = context.getContentResolver();
640 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
641 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
642 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
643 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
644
645 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
646 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
647 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
648 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
649 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
650 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
651 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
652
653 try {
654 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700655 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700656 item.cellX = c.getInt(cellXIndex);
657 item.cellY = c.getInt(cellYIndex);
658 item.spanX = c.getInt(spanXIndex);
659 item.spanY = c.getInt(spanYIndex);
660 item.container = c.getInt(containerIndex);
661 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700662 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700663
664 items.add(item);
665 }
666 } catch (Exception e) {
667 items.clear();
668 } finally {
669 c.close();
670 }
671
672 return items;
673 }
674
675 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
677 */
678 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
679 final ContentResolver cr = context.getContentResolver();
680 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
681 "_id=? and (itemType=? or itemType=?)",
682 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700683 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700684
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 try {
686 if (c.moveToFirst()) {
687 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
688 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
689 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
690 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
691 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
692 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694 FolderInfo folderInfo = null;
695 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700696 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
697 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700699 }
700
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 folderInfo.title = c.getString(titleIndex);
702 folderInfo.id = id;
703 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700705 folderInfo.cellX = c.getInt(cellXIndex);
706 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707
708 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700709 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 } finally {
711 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700712 }
713
714 return null;
715 }
716
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 /**
718 * Add an item to the database in a specified container. Sets the container, screen, cellX and
719 * cellY fields of the item. Also assigns an ID to the item.
720 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700721 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 item.cellX = cellX;
725 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700726 // We store hotseat items in canonical form which is this orientation invariant position
727 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700729 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700732 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734
735 final ContentValues values = new ContentValues();
736 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 item.onAddToDatabase(values);
738
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400739 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohendcd297f2013-06-18 13:13:40 -0700740 item.id = app.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700741 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700742 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700743
Michael Jurkac9d95c52011-08-29 14:03:34 -0700744 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700745 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700746 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700747 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700748 + cellY + ")";
749 Launcher.sDumpLogs.add(transaction);
750 Log.d(TAG, transaction);
751
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700752 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
753 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700755 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700756 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700757 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700758 sBgItemsIdMap.put(item.id, item);
759 switch (item.itemType) {
760 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
761 sBgFolders.put(item.id, (FolderInfo) item);
762 // Fall through
763 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
764 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
765 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
766 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
767 sBgWorkspaceItems.add(item);
768 } else {
769 if (!sBgFolders.containsKey(item.container)) {
770 // Adding an item to a folder that doesn't exist.
771 String msg = "adding item: " + item + " to a folder that " +
772 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700773 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700774 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700775 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700776 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700777 break;
778 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
779 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
780 break;
781 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700782 }
783 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700784 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700785 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700786 }
787
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700789 * Creates a new unique child id, for a given cell span across all layouts.
790 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700791 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700792 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700795 }
796
Adam Cohend22015c2010-07-26 22:02:18 -0700797 static int getCellCountX() {
798 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700799 }
800
Adam Cohend22015c2010-07-26 22:02:18 -0700801 static int getCellCountY() {
802 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700803 }
804
805 /**
806 * Updates the model orientation helper to take into account the current layout dimensions
807 * when performing local/canonical coordinate transformations.
808 */
809 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700810 mCellCountX = shortAxisCellCount;
811 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700812 }
813
814 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700815 * Removes the specified item from the database
816 * @param context
817 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700819 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700821 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700822
Michael Jurka83df1882011-08-31 20:59:26 -0700823 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700824 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700825 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700826 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700827 ", " + item.cellY + ")";
828 Launcher.sDumpLogs.add(transaction);
829 Log.d(TAG, transaction);
830
Michael Jurkac9d95c52011-08-29 14:03:34 -0700831 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700832
833 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700834 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700835 switch (item.itemType) {
836 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
837 sBgFolders.remove(item.id);
838 for (ItemInfo info: sBgItemsIdMap.values()) {
839 if (info.container == item.id) {
840 // We are deleting a folder which still contains items that
841 // think they are contained by that folder.
842 String msg = "deleting a folder (" + item + ") which still " +
843 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700844 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700845 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700846 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700847 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700848 sBgWorkspaceItems.remove(item);
849 break;
850 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
851 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
852 sBgWorkspaceItems.remove(item);
853 break;
854 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
855 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
856 break;
857 }
858 sBgItemsIdMap.remove(item.id);
859 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700860 }
861 }
Michael Jurka83df1882011-08-31 20:59:26 -0700862 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700863 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864 }
865
866 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700867 * Update the order of the workspace screens in the database. The array list contains
868 * a list of screen ids in the order that they should appear.
869 */
Winson Chungc9168342013-06-26 14:54:55 -0700870 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
871 updateWorkspaceScreenOrder(context, screens, null);
872 }
873 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens, final Runnable mainThreadCb) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700874 final ContentResolver cr = context.getContentResolver();
875 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
876
877 // Remove any negative screen ids -- these aren't persisted
878 Iterator<Long> iter = screens.iterator();
879 while (iter.hasNext()) {
880 long id = iter.next();
881 if (id < 0) {
882 iter.remove();
883 }
884 }
885
886 Runnable r = new Runnable() {
887 @Override
888 public void run() {
889 final ArrayList<Long> screensCopy = new ArrayList<Long>();
890
891 // Clear the table
892 cr.delete(uri, null, null);
893 int count = screens.size();
894 ContentValues[] values = new ContentValues[count];
895 for (int i = 0; i < count; i++) {
896 ContentValues v = new ContentValues();
897 long screenId = screens.get(i);
898 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
899 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
900 screensCopy.add(screenId);
901 values[i] = v;
902 }
903 cr.bulkInsert(uri, values);
904 sBgWorkspaceScreens.clear();
905 sBgWorkspaceScreens.addAll(screensCopy);
906 }
907 };
908 runOnWorkerThread(r);
Winson Chungc9168342013-06-26 14:54:55 -0700909 if (mainThreadCb != null) {
910 runOnWorkerThread(new Runnable() {
911 @Override
912 public void run() {
913 runOnMainThread(mainThreadCb);
914 }
915 });
916 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700917 }
918
919 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 * Remove the contents of the specified folder from the database
921 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700922 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 final ContentResolver cr = context.getContentResolver();
924
Michael Jurkac9d95c52011-08-29 14:03:34 -0700925 Runnable r = new Runnable() {
926 public void run() {
927 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700928 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700929 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700930 sBgItemsIdMap.remove(info.id);
931 sBgFolders.remove(info.id);
932 sBgDbIconCache.remove(info);
933 sBgWorkspaceItems.remove(info);
934 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700935
Michael Jurkac9d95c52011-08-29 14:03:34 -0700936 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
937 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700938 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700939 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700940 for (ItemInfo childInfo : info.contents) {
941 sBgItemsIdMap.remove(childInfo.id);
942 sBgDbIconCache.remove(childInfo);
943 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700944 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700945 }
946 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700947 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 }
949
950 /**
951 * Set this as the current Launcher activity object for the loader.
952 */
953 public void initialize(Callbacks callbacks) {
954 synchronized (mLock) {
955 mCallbacks = new WeakReference<Callbacks>(callbacks);
956 }
957 }
958
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700959 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
961 * ACTION_PACKAGE_CHANGED.
962 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100963 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400964 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -0400965 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700966
Joe Onorato36115782010-06-17 13:28:48 -0400967 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -0400968
Joe Onorato36115782010-06-17 13:28:48 -0400969 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
970 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
971 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
972 final String packageName = intent.getData().getSchemeSpecificPart();
973 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974
Joe Onorato36115782010-06-17 13:28:48 -0400975 int op = PackageUpdatedTask.OP_NONE;
976
977 if (packageName == null || packageName.length() == 0) {
978 // they sent us a bad intent
979 return;
980 }
981
982 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
983 op = PackageUpdatedTask.OP_UPDATE;
984 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
985 if (!replacing) {
986 op = PackageUpdatedTask.OP_REMOVE;
987 }
988 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
989 // later, we will update the package at this time
990 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
991 if (!replacing) {
992 op = PackageUpdatedTask.OP_ADD;
993 } else {
994 op = PackageUpdatedTask.OP_UPDATE;
995 }
996 }
997
998 if (op != PackageUpdatedTask.OP_NONE) {
999 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1000 }
1001
1002 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001003 // First, schedule to add these apps back in.
1004 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1005 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1006 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001007 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001008 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1009 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1010 enqueuePackageUpdated(new PackageUpdatedTask(
1011 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001012 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001013 // If we have changed locale we need to clear out the labels in all apps/workspace.
1014 forceReload();
1015 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1016 // Check if configuration change was an mcc/mnc change which would affect app resources
1017 // and we would need to clear out the labels in all apps/workspace. Same handling as
1018 // above for ACTION_LOCALE_CHANGED
1019 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001020 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001021 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001022 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001023 forceReload();
1024 }
1025 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001026 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001027 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1028 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001029 if (mCallbacks != null) {
1030 Callbacks callbacks = mCallbacks.get();
1031 if (callbacks != null) {
1032 callbacks.bindSearchablesChanged();
1033 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001034 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001035 }
1036 }
1037
Reena Lee93f824a2011-09-23 17:20:28 -07001038 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001039 resetLoadedState(true, true);
1040
Reena Lee93f824a2011-09-23 17:20:28 -07001041 // Do this here because if the launcher activity is running it will be restarted.
1042 // If it's not running startLoaderFromBackground will merely tell it that it needs
1043 // to reload.
1044 startLoaderFromBackground();
1045 }
1046
Winson Chungf0c6ae02012-03-21 16:10:31 -07001047 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1048 synchronized (mLock) {
1049 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1050 // mWorkspaceLoaded to true later
1051 stopLoaderLocked();
1052 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1053 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1054 }
1055 }
1056
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001057 /**
1058 * When the launcher is in the background, it's possible for it to miss paired
1059 * configuration changes. So whenever we trigger the loader from the background
1060 * tell the launcher that it needs to re-run the loader when it comes back instead
1061 * of doing it now.
1062 */
1063 public void startLoaderFromBackground() {
1064 boolean runLoader = false;
1065 if (mCallbacks != null) {
1066 Callbacks callbacks = mCallbacks.get();
1067 if (callbacks != null) {
1068 // Only actually run the loader if they're not paused.
1069 if (!callbacks.setLoadOnResume()) {
1070 runLoader = true;
1071 }
1072 }
1073 }
1074 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001075 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001076 }
Joe Onorato36115782010-06-17 13:28:48 -04001077 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001078
Reena Lee93f824a2011-09-23 17:20:28 -07001079 // If there is already a loader task running, tell it to stop.
1080 // returns true if isLaunching() was true on the old task
1081 private boolean stopLoaderLocked() {
1082 boolean isLaunching = false;
1083 LoaderTask oldTask = mLoaderTask;
1084 if (oldTask != null) {
1085 if (oldTask.isLaunching()) {
1086 isLaunching = true;
1087 }
1088 oldTask.stopLocked();
1089 }
1090 return isLaunching;
1091 }
1092
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001093 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001094 synchronized (mLock) {
1095 if (DEBUG_LOADERS) {
1096 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1097 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001098
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099 // Clear any deferred bind-runnables from the synchronized load process
1100 // We must do this before any loading/binding is scheduled below.
1101 mDeferredBindRunnables.clear();
1102
Joe Onorato36115782010-06-17 13:28:48 -04001103 // Don't bother to start the thread if we know it's not going to do anything
1104 if (mCallbacks != null && mCallbacks.get() != null) {
1105 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001106 // also, don't downgrade isLaunching if we're already running
1107 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001108 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001109 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1110 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1111 } else {
1112 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1113 sWorker.post(mLoaderTask);
1114 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001116 }
1117 }
1118
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 void bindRemainingSynchronousPages() {
1120 // Post the remaining side pages to be loaded
1121 if (!mDeferredBindRunnables.isEmpty()) {
1122 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001123 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 }
1125 mDeferredBindRunnables.clear();
1126 }
1127 }
1128
Joe Onorato36115782010-06-17 13:28:48 -04001129 public void stopLoader() {
1130 synchronized (mLock) {
1131 if (mLoaderTask != null) {
1132 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 }
1134 }
Joe Onorato36115782010-06-17 13:28:48 -04001135 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136
Michael Jurkac57b7a82011-08-09 22:02:20 -07001137 public boolean isAllAppsLoaded() {
1138 return mAllAppsLoaded;
1139 }
1140
Winson Chung36a62fe2012-05-06 18:04:42 -07001141 boolean isLoadingWorkspace() {
1142 synchronized (mLock) {
1143 if (mLoaderTask != null) {
1144 return mLoaderTask.isLoadingWorkspace();
1145 }
1146 }
1147 return false;
1148 }
1149
Joe Onorato36115782010-06-17 13:28:48 -04001150 /**
1151 * Runnable for the thread that loads the contents of the launcher:
1152 * - workspace icons
1153 * - widgets
1154 * - all apps icons
1155 */
1156 private class LoaderTask implements Runnable {
1157 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001158 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001159 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001160 private boolean mStopped;
1161 private boolean mLoadAndBindStepFinished;
Adam Cohene25af792013-06-06 23:08:25 -07001162 private boolean mIsUpgradePath;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001163
Winson Chungc3eecff2011-07-11 17:44:15 -07001164 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001165
1166 LoaderTask(Context context, boolean isLaunching) {
1167 mContext = context;
1168 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001169 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 }
1171
Joe Onorato36115782010-06-17 13:28:48 -04001172 boolean isLaunching() {
1173 return mIsLaunching;
1174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175
Winson Chung36a62fe2012-05-06 18:04:42 -07001176 boolean isLoadingWorkspace() {
1177 return mIsLoadingAndBindingWorkspace;
1178 }
1179
Joe Onorato36115782010-06-17 13:28:48 -04001180 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001181 mIsLoadingAndBindingWorkspace = true;
1182
Joe Onorato36115782010-06-17 13:28:48 -04001183 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001184 if (DEBUG_LOADERS) {
1185 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001187
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001188 if (!mWorkspaceLoaded) {
Joe Onorato36115782010-06-17 13:28:48 -04001189 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001190 synchronized (LoaderTask.this) {
1191 if (mStopped) {
1192 return;
1193 }
1194 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 }
1196 }
1197
Joe Onorato36115782010-06-17 13:28:48 -04001198 // Bind the workspace
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001200 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001201
Joe Onorato36115782010-06-17 13:28:48 -04001202 private void waitForIdle() {
1203 // Wait until the either we're stopped or the other threads are done.
1204 // This way we don't start loading all apps until the workspace has settled
1205 // down.
1206 synchronized (LoaderTask.this) {
1207 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001208
Joe Onorato36115782010-06-17 13:28:48 -04001209 mHandler.postIdle(new Runnable() {
1210 public void run() {
1211 synchronized (LoaderTask.this) {
1212 mLoadAndBindStepFinished = true;
1213 if (DEBUG_LOADERS) {
1214 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001215 }
Joe Onorato36115782010-06-17 13:28:48 -04001216 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001217 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001218 }
Joe Onorato36115782010-06-17 13:28:48 -04001219 });
1220
Michael Jurkac7700af2013-05-14 20:17:58 +02001221 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001222 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001223 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1224 // wait no longer than 1sec at a time
1225 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001226 } catch (InterruptedException ex) {
1227 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001228 }
1229 }
Joe Onorato36115782010-06-17 13:28:48 -04001230 if (DEBUG_LOADERS) {
1231 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001232 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001233 + "ms for previous step to finish binding");
1234 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001235 }
Joe Onorato36115782010-06-17 13:28:48 -04001236 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001237
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001238 void runBindSynchronousPage(int synchronousBindPage) {
1239 if (synchronousBindPage < 0) {
1240 // Ensure that we have a valid page index to load synchronously
1241 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1242 "valid page index");
1243 }
1244 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1245 // Ensure that we don't try and bind a specified page when the pages have not been
1246 // loaded already (we should load everything asynchronously in that case)
1247 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1248 }
1249 synchronized (mLock) {
1250 if (mIsLoaderTaskRunning) {
1251 // Ensure that we are never running the background loading at this point since
1252 // we also touch the background collections
1253 throw new RuntimeException("Error! Background loading is already running");
1254 }
1255 }
1256
1257 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1258 // data structures, we can't allow any other thread to touch that data, but because
1259 // this call is synchronous, we can get away with not locking).
1260
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001261 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001262 // operations from the previous activity. We need to ensure that all queued operations
1263 // are executed before any synchronous binding work is done.
1264 mHandler.flush();
1265
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001266 // Divide the set of loaded items into those that we are binding synchronously, and
1267 // everything else that is to be bound normally (asynchronously).
1268 bindWorkspace(synchronousBindPage);
1269 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1270 // arise from that.
1271 onlyBindAllApps();
1272 }
1273
Joe Onorato36115782010-06-17 13:28:48 -04001274 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001275 synchronized (mLock) {
1276 mIsLoaderTaskRunning = true;
1277 }
Joe Onorato36115782010-06-17 13:28:48 -04001278 // Optimize for end-user experience: if the Launcher is up and // running with the
1279 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1280 // workspace first (default).
1281 final Callbacks cbk = mCallbacks.get();
1282 final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true;
Daniel Sandler843e8602010-06-07 14:59:01 -04001283
Joe Onorato36115782010-06-17 13:28:48 -04001284 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001285 // Elevate priority when Home launches for the first time to avoid
1286 // starving at boot time. Staring at a blank home is not cool.
1287 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001288 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1289 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001290 android.os.Process.setThreadPriority(mIsLaunching
1291 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1292 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001293 if (loadWorkspaceFirst) {
1294 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
1295 loadAndBindWorkspace();
1296 } else {
1297 if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001298 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001299 }
1300
Joe Onorato36115782010-06-17 13:28:48 -04001301 if (mStopped) {
1302 break keep_running;
1303 }
1304
1305 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1306 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001307 synchronized (mLock) {
1308 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001309 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001310 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1311 }
1312 }
Joe Onorato36115782010-06-17 13:28:48 -04001313 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001314
1315 // second step
1316 if (loadWorkspaceFirst) {
1317 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
Joe Onoratocc67f472010-06-08 10:54:30 -07001318 loadAndBindAllApps();
Daniel Sandler843e8602010-06-07 14:59:01 -04001319 } else {
1320 if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace");
1321 loadAndBindWorkspace();
1322 }
Winson Chung7ed37742011-09-08 15:45:51 -07001323
1324 // Restore the default thread priority after we are done loading items
1325 synchronized (mLock) {
1326 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1327 }
Joe Onorato36115782010-06-17 13:28:48 -04001328 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329
Winson Chungaac01e12011-08-17 10:37:13 -07001330 // Update the saved icons if necessary
1331 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001332 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001333 for (Object key : sBgDbIconCache.keySet()) {
1334 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1335 }
1336 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001337 }
Winson Chungaac01e12011-08-17 10:37:13 -07001338
Joe Onorato36115782010-06-17 13:28:48 -04001339 // Clear out this reference, otherwise we end up holding it until all of the
1340 // callback runnables are done.
1341 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001342
Joe Onorato36115782010-06-17 13:28:48 -04001343 synchronized (mLock) {
1344 // If we are still the last one to be scheduled, remove ourselves.
1345 if (mLoaderTask == this) {
1346 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001348 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001349 }
Joe Onorato36115782010-06-17 13:28:48 -04001350 }
1351
1352 public void stopLocked() {
1353 synchronized (LoaderTask.this) {
1354 mStopped = true;
1355 this.notify();
1356 }
1357 }
1358
1359 /**
1360 * Gets the callbacks object. If we've been stopped, or if the launcher object
1361 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1362 * object that was around when the deferred message was scheduled, and if there's
1363 * a new Callbacks object around then also return null. This will save us from
1364 * calling onto it with data that will be ignored.
1365 */
1366 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1367 synchronized (mLock) {
1368 if (mStopped) {
1369 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001370 }
Joe Onorato36115782010-06-17 13:28:48 -04001371
1372 if (mCallbacks == null) {
1373 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001374 }
Joe Onorato36115782010-06-17 13:28:48 -04001375
1376 final Callbacks callbacks = mCallbacks.get();
1377 if (callbacks != oldCallbacks) {
1378 return null;
1379 }
1380 if (callbacks == null) {
1381 Log.w(TAG, "no mCallbacks");
1382 return null;
1383 }
1384
1385 return callbacks;
1386 }
1387 }
1388
1389 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001390 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
1391 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001392 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001393 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1394 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1395 [(int) item.screenId][0] != null) {
1396 Log.e(TAG, "Error loading shortcut into hotseat " + item
1397 + " into position (" + item.screenId + ":" + item.cellX + ","
1398 + item.cellY + ") occupied by "
1399 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1400 [(int) item.screenId][0]);
1401 return false;
1402 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001403 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001404 ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1405 items[(int) item.screenId][0] = item;
1406 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001407 return true;
1408 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001409 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1410 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001411 return true;
1412 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001413
Adam Cohendcd297f2013-06-18 13:13:40 -07001414 if (!occupied.containsKey(item.screenId)) {
1415 ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1416 occupied.put(item.screenId, items);
1417 }
1418
1419 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001420 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001421 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1422 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001423 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001424 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001425 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001426 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001427 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001428 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001429 return false;
1430 }
1431 }
1432 }
1433 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1434 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001435 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001436 }
1437 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001438
Joe Onorato36115782010-06-17 13:28:48 -04001439 return true;
1440 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441
Joe Onorato36115782010-06-17 13:28:48 -04001442 private void loadWorkspace() {
1443 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001444
Joe Onorato36115782010-06-17 13:28:48 -04001445 final Context context = mContext;
1446 final ContentResolver contentResolver = context.getContentResolver();
1447 final PackageManager manager = context.getPackageManager();
1448 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1449 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001450
Michael Jurkab85f8a42012-04-25 15:48:32 -07001451 // Make sure the default workspace is loaded, if needed
Adam Cohene25af792013-06-06 23:08:25 -07001452 boolean loadOldDb = mApp.getLauncherProvider().shouldLoadOldDb();
1453 Uri contentUri = loadOldDb ? LauncherSettings.Favorites.OLD_CONTENT_URI :
1454 LauncherSettings.Favorites.CONTENT_URI;
1455
1456 mIsUpgradePath = loadOldDb;
Michael Jurkab85f8a42012-04-25 15:48:32 -07001457
Winson Chung2abf94d2012-07-18 18:16:38 -07001458 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001459 sBgWorkspaceItems.clear();
1460 sBgAppWidgets.clear();
1461 sBgFolders.clear();
1462 sBgItemsIdMap.clear();
1463 sBgDbIconCache.clear();
Adam Cohendcd297f2013-06-18 13:13:40 -07001464 sBgWorkspaceScreens.clear();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001465
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467
Adam Cohene25af792013-06-06 23:08:25 -07001468 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001469
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001470 // +1 for the hotseat (it can be larger than the workspace)
1471 // Load workspace in reverse order to ensure that latest items are loaded first (and
1472 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001473 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001475 try {
1476 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1477 final int intentIndex = c.getColumnIndexOrThrow
1478 (LauncherSettings.Favorites.INTENT);
1479 final int titleIndex = c.getColumnIndexOrThrow
1480 (LauncherSettings.Favorites.TITLE);
1481 final int iconTypeIndex = c.getColumnIndexOrThrow(
1482 LauncherSettings.Favorites.ICON_TYPE);
1483 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1484 final int iconPackageIndex = c.getColumnIndexOrThrow(
1485 LauncherSettings.Favorites.ICON_PACKAGE);
1486 final int iconResourceIndex = c.getColumnIndexOrThrow(
1487 LauncherSettings.Favorites.ICON_RESOURCE);
1488 final int containerIndex = c.getColumnIndexOrThrow(
1489 LauncherSettings.Favorites.CONTAINER);
1490 final int itemTypeIndex = c.getColumnIndexOrThrow(
1491 LauncherSettings.Favorites.ITEM_TYPE);
1492 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1493 LauncherSettings.Favorites.APPWIDGET_ID);
1494 final int screenIndex = c.getColumnIndexOrThrow(
1495 LauncherSettings.Favorites.SCREEN);
1496 final int cellXIndex = c.getColumnIndexOrThrow
1497 (LauncherSettings.Favorites.CELLX);
1498 final int cellYIndex = c.getColumnIndexOrThrow
1499 (LauncherSettings.Favorites.CELLY);
1500 final int spanXIndex = c.getColumnIndexOrThrow
1501 (LauncherSettings.Favorites.SPANX);
1502 final int spanYIndex = c.getColumnIndexOrThrow(
1503 LauncherSettings.Favorites.SPANY);
1504 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1505 //final int displayModeIndex = c.getColumnIndexOrThrow(
1506 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 ShortcutInfo info;
1509 String intentDescription;
1510 LauncherAppWidgetInfo appWidgetInfo;
1511 int container;
1512 long id;
1513 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001514
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001515 while (!mStopped && c.moveToNext()) {
1516 try {
1517 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001518
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001519 switch (itemType) {
1520 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1521 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1522 intentDescription = c.getString(intentIndex);
1523 try {
1524 intent = Intent.parseUri(intentDescription, 0);
1525 } catch (URISyntaxException e) {
1526 continue;
1527 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001528
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001529 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1530 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1531 titleIndex, mLabelCache);
1532 } else {
1533 info = getShortcutInfo(c, context, iconTypeIndex,
1534 iconPackageIndex, iconResourceIndex, iconIndex,
1535 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001536
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001537 // App shortcuts that used to be automatically added to Launcher
1538 // didn't always have the correct intent flags set, so do that
1539 // here
1540 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001541 intent.getCategories() != null &&
1542 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001543 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001544 intent.addFlags(
1545 Intent.FLAG_ACTIVITY_NEW_TASK |
1546 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1547 }
Michael Jurka96879562012-03-22 05:54:33 -07001548 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001549
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550 if (info != null) {
1551 info.intent = intent;
1552 info.id = c.getLong(idIndex);
1553 container = c.getInt(containerIndex);
1554 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001555 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001556 info.cellX = c.getInt(cellXIndex);
1557 info.cellY = c.getInt(cellYIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001558 // check & update map of what's occupied
1559 if (!checkItemPlacement(occupied, info)) {
1560 break;
1561 }
1562
1563 switch (container) {
1564 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1565 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1566 sBgWorkspaceItems.add(info);
1567 break;
1568 default:
1569 // Item is in a user folder
1570 FolderInfo folderInfo =
1571 findOrMakeFolder(sBgFolders, container);
1572 folderInfo.add(info);
1573 break;
1574 }
Adam Cohene25af792013-06-06 23:08:25 -07001575 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1576 loadOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 info.screenId = permuteScreens(info.screenId);
Adam Cohene25af792013-06-06 23:08:25 -07001578 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001579 sBgItemsIdMap.put(info.id, info);
1580
1581 // now that we've loaded everthing re-save it with the
1582 // icon in case it disappears somehow.
1583 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
1584 } else {
1585 // Failed to load the shortcut, probably because the
1586 // activity manager couldn't resolve it (maybe the app
1587 // was uninstalled), or the db row was somehow screwed up.
1588 // Delete it.
1589 id = c.getLong(idIndex);
1590 Log.e(TAG, "Error loading shortcut " + id + ", removing it");
1591 contentResolver.delete(LauncherSettings.Favorites.getContentUri(
1592 id, false), null, null);
1593 }
1594 break;
1595
1596 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1597 id = c.getLong(idIndex);
1598 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1599
1600 folderInfo.title = c.getString(titleIndex);
1601 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001602 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001603 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001604 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001605 folderInfo.cellX = c.getInt(cellXIndex);
1606 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607
Daniel Sandler8802e962010-05-26 16:28:16 -04001608 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001609 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001610 break;
1611 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001612 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001613 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1614 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1615 sBgWorkspaceItems.add(folderInfo);
1616 break;
Joe Onorato36115782010-06-17 13:28:48 -04001617 }
Adam Cohene25af792013-06-06 23:08:25 -07001618 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1619 loadOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001620 folderInfo.screenId = permuteScreens(folderInfo.screenId);
Adam Cohene25af792013-06-06 23:08:25 -07001621 }
Joe Onorato17a89222011-02-08 17:26:11 -08001622
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001623 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1624 sBgFolders.put(folderInfo.id, folderInfo);
1625 break;
1626
1627 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1628 // Read all Launcher-specific widget details
1629 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001630 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001631
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001632 final AppWidgetProviderInfo provider =
1633 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001634
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001635 if (!isSafeMode && (provider == null || provider.provider == null ||
1636 provider.provider.getPackageName() == null)) {
1637 String log = "Deleting widget that isn't installed anymore: id="
1638 + id + " appWidgetId=" + appWidgetId;
1639 Log.e(TAG, log);
1640 Launcher.sDumpLogs.add(log);
1641 itemsToRemove.add(id);
1642 } else {
1643 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1644 provider.provider);
1645 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001646 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001647 appWidgetInfo.cellX = c.getInt(cellXIndex);
1648 appWidgetInfo.cellY = c.getInt(cellYIndex);
1649 appWidgetInfo.spanX = c.getInt(spanXIndex);
1650 appWidgetInfo.spanY = c.getInt(spanYIndex);
1651 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1652 appWidgetInfo.minSpanX = minSpan[0];
1653 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001654
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001655 container = c.getInt(containerIndex);
1656 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1657 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1658 Log.e(TAG, "Widget found where container != " +
1659 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1660 continue;
1661 }
Adam Cohene25af792013-06-06 23:08:25 -07001662 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
1663 loadOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001664 appWidgetInfo.screenId =
1665 permuteScreens(appWidgetInfo.screenId);
Adam Cohene25af792013-06-06 23:08:25 -07001666 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001667
Adam Cohene25af792013-06-06 23:08:25 -07001668 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669 // check & update map of what's occupied
1670 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1671 break;
1672 }
1673 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1674 sBgAppWidgets.add(appWidgetInfo);
1675 }
Joe Onorato36115782010-06-17 13:28:48 -04001676 break;
1677 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001678 } catch (Exception e) {
1679 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001680 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001681 }
1682 } finally {
1683 c.close();
1684 }
1685
1686 if (itemsToRemove.size() > 0) {
1687 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1688 LauncherSettings.Favorites.CONTENT_URI);
1689 // Remove dead items
1690 for (long id : itemsToRemove) {
1691 if (DEBUG_LOADERS) {
1692 Log.d(TAG, "Removed id = " + id);
1693 }
1694 // Don't notify content observers
1695 try {
1696 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1697 null, null);
1698 } catch (RemoteException e) {
1699 Log.w(TAG, "Could not remove id = " + id);
1700 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001701 }
1702 }
1703
Adam Cohendcd297f2013-06-18 13:13:40 -07001704 if (loadOldDb) {
1705 long maxScreenId = 0;
1706 // If we're importing we use the old screen order.
1707 for (ItemInfo item: sBgItemsIdMap.values()) {
1708 long screenId = item.screenId;
1709 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1710 !sBgWorkspaceScreens.contains(screenId)) {
1711 sBgWorkspaceScreens.add(screenId);
1712 if (screenId > maxScreenId) {
1713 maxScreenId = screenId;
1714 }
1715 }
1716 }
1717 Collections.sort(sBgWorkspaceScreens);
1718 mApp.getLauncherProvider().updateMaxScreenId(maxScreenId);
1719 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1720 } else {
1721 Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1722 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1723 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1724
1725 try {
1726 final int idIndex = sc.getColumnIndexOrThrow(
1727 LauncherSettings.WorkspaceScreens._ID);
1728 final int rankIndex = sc.getColumnIndexOrThrow(
1729 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1730 while (sc.moveToNext()) {
1731 try {
1732 long screenId = sc.getLong(idIndex);
1733 int rank = sc.getInt(rankIndex);
1734
1735 orderedScreens.put(rank, screenId);
1736 } catch (Exception e) {
1737 Log.w(TAG, "Desktop items loading interrupted:", e);
1738 }
1739 }
1740 } finally {
1741 sc.close();
1742 }
1743
1744 Iterator<Integer> iter = orderedScreens.keySet().iterator();
1745 while (iter.hasNext()) {
1746 sBgWorkspaceScreens.add(orderedScreens.get(iter.next()));
1747 }
1748
1749 // Remove any empty screens
1750 ArrayList<Long> unusedScreens = new ArrayList<Long>();
1751 unusedScreens.addAll(sBgWorkspaceScreens);
1752
1753 for (ItemInfo item: sBgItemsIdMap.values()) {
1754 long screenId = item.screenId;
1755
1756 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1757 unusedScreens.contains(screenId)) {
1758 unusedScreens.remove(screenId);
1759 }
1760 }
1761
1762 // If there are any empty screens remove them, and update.
1763 if (unusedScreens.size() != 0) {
1764 sBgWorkspaceScreens.removeAll(unusedScreens);
1765 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1766 }
1767 }
1768
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001769 if (DEBUG_LOADERS) {
1770 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1771 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001772 int nScreens = occupied.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 for (int y = 0; y < mCellCountY; y++) {
1774 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001775
Daniel Sandler566da102013-06-25 23:43:45 -04001776 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001777 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001778 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001779 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 line += " | ";
1781 }
1782 for (int x = 0; x < mCellCountX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001783 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 }
Joe Onorato36115782010-06-17 13:28:48 -04001785 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788 }
Joe Onorato36115782010-06-17 13:28:48 -04001789 }
1790 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791
Adam Cohene25af792013-06-06 23:08:25 -07001792 // We rearrange the screens from the old launcher
1793 // 12345 -> 34512
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 private long permuteScreens(long screen) {
Adam Cohene25af792013-06-06 23:08:25 -07001795 if (screen >= 2) {
1796 return screen - 2;
1797 } else {
1798 return screen + 3;
1799 }
1800 }
1801
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001802 /** Filters the set of items who are directly or indirectly (via another container) on the
1803 * specified screen. */
1804 private void filterCurrentWorkspaceItems(int currentScreen,
1805 ArrayList<ItemInfo> allWorkspaceItems,
1806 ArrayList<ItemInfo> currentScreenItems,
1807 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001808 // Purge any null ItemInfos
1809 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1810 while (iter.hasNext()) {
1811 ItemInfo i = iter.next();
1812 if (i == null) {
1813 iter.remove();
1814 }
1815 }
1816
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001817 // If we aren't filtering on a screen, then the set of items to load is the full set of
1818 // items given.
1819 if (currentScreen < 0) {
1820 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001821 }
1822
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 // Order the set of items by their containers first, this allows use to walk through the
1824 // list sequentially, build up a list of containers that are in the specified screen,
1825 // as well as all items in those containers.
1826 Set<Long> itemsOnScreen = new HashSet<Long>();
1827 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1828 @Override
1829 public int compare(ItemInfo lhs, ItemInfo rhs) {
1830 return (int) (lhs.container - rhs.container);
1831 }
1832 });
1833 for (ItemInfo info : allWorkspaceItems) {
1834 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001835 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 currentScreenItems.add(info);
1837 itemsOnScreen.add(info.id);
1838 } else {
1839 otherScreenItems.add(info);
1840 }
1841 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1842 currentScreenItems.add(info);
1843 itemsOnScreen.add(info.id);
1844 } else {
1845 if (itemsOnScreen.contains(info.container)) {
1846 currentScreenItems.add(info);
1847 itemsOnScreen.add(info.id);
1848 } else {
1849 otherScreenItems.add(info);
1850 }
1851 }
1852 }
1853 }
1854
1855 /** Filters the set of widgets which are on the specified screen. */
1856 private void filterCurrentAppWidgets(int currentScreen,
1857 ArrayList<LauncherAppWidgetInfo> appWidgets,
1858 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1859 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1860 // If we aren't filtering on a screen, then the set of items to load is the full set of
1861 // widgets given.
1862 if (currentScreen < 0) {
1863 currentScreenWidgets.addAll(appWidgets);
1864 }
1865
1866 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001867 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07001869 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 currentScreenWidgets.add(widget);
1871 } else {
1872 otherScreenWidgets.add(widget);
1873 }
1874 }
1875 }
1876
1877 /** Filters the set of folders which are on the specified screen. */
1878 private void filterCurrentFolders(int currentScreen,
1879 HashMap<Long, ItemInfo> itemsIdMap,
1880 HashMap<Long, FolderInfo> folders,
1881 HashMap<Long, FolderInfo> currentScreenFolders,
1882 HashMap<Long, FolderInfo> otherScreenFolders) {
1883 // If we aren't filtering on a screen, then the set of items to load is the full set of
1884 // widgets given.
1885 if (currentScreen < 0) {
1886 currentScreenFolders.putAll(folders);
1887 }
1888
1889 for (long id : folders.keySet()) {
1890 ItemInfo info = itemsIdMap.get(id);
1891 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07001892 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001893 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07001894 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001895 currentScreenFolders.put(id, folder);
1896 } else {
1897 otherScreenFolders.put(id, folder);
1898 }
1899 }
1900 }
1901
1902 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1903 * right) */
1904 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1905 // XXX: review this
1906 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001907 @Override
1908 public int compare(ItemInfo lhs, ItemInfo rhs) {
1909 int cellCountX = LauncherModel.getCellCountX();
1910 int cellCountY = LauncherModel.getCellCountY();
1911 int screenOffset = cellCountX * cellCountY;
1912 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001913 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07001914 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07001915 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07001916 rhs.cellY * cellCountX + rhs.cellX);
1917 return (int) (lr - rr);
1918 }
1919 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001920 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001921
Adam Cohendcd297f2013-06-18 13:13:40 -07001922 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1923 final ArrayList<Long> orderedScreens) {
1924
1925 final Runnable r = new Runnable() {
1926 @Override
1927 public void run() {
1928 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1929 if (callbacks != null) {
1930 callbacks.bindScreens(orderedScreens);
1931 }
1932 }
1933 };
1934 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
1935 }
1936
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001937 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1938 final ArrayList<ItemInfo> workspaceItems,
1939 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1940 final HashMap<Long, FolderInfo> folders,
1941 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07001942
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001943 final boolean postOnMainThread = (deferredBindRunnables != null);
1944
1945 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001946 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001948 final int start = i;
1949 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 final Runnable r = new Runnable() {
1951 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001953 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954 if (callbacks != null) {
Winson Chung603bcb92011-09-02 11:45:39 -07001955 callbacks.bindItems(workspaceItems, start, start+chunkSize);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001956 }
1957 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001958 };
1959 if (postOnMainThread) {
1960 deferredBindRunnables.add(r);
1961 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001962 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001963 }
Joe Onorato36115782010-06-17 13:28:48 -04001964 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965
1966 // Bind the folders
1967 if (!folders.isEmpty()) {
1968 final Runnable r = new Runnable() {
1969 public void run() {
1970 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1971 if (callbacks != null) {
1972 callbacks.bindFolders(folders);
1973 }
1974 }
1975 };
1976 if (postOnMainThread) {
1977 deferredBindRunnables.add(r);
1978 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001979 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001980 }
1981 }
1982
1983 // Bind the widgets, one at a time
1984 N = appWidgets.size();
1985 for (int i = 0; i < N; i++) {
1986 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1987 final Runnable r = new Runnable() {
1988 public void run() {
1989 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1990 if (callbacks != null) {
1991 callbacks.bindAppWidget(widget);
1992 }
1993 }
1994 };
1995 if (postOnMainThread) {
1996 deferredBindRunnables.add(r);
1997 } else {
Winson Chung81b52252012-08-27 15:34:29 -07001998 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999 }
2000 }
2001 }
2002
2003 /**
2004 * Binds all loaded data to actual views on the main thread.
2005 */
2006 private void bindWorkspace(int synchronizeBindPage) {
2007 final long t = SystemClock.uptimeMillis();
2008 Runnable r;
2009
2010 // Don't use these two variables in any of the callback runnables.
2011 // Otherwise we hold a reference to them.
2012 final Callbacks oldCallbacks = mCallbacks.get();
2013 if (oldCallbacks == null) {
2014 // This launcher has exited and nobody bothered to tell us. Just bail.
2015 Log.w(TAG, "LoaderTask running with no launcher");
2016 return;
2017 }
2018
Winson Chung4a2afa32012-07-19 14:53:05 -07002019 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2020 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 oldCallbacks.getCurrentWorkspaceScreen();
2022
2023 // Load all the items that are on the current page first (and in the process, unbind
2024 // all the existing workspace items before we call startBinding() below.
2025 unbindWorkspaceItemsOnMainThread();
2026 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2027 ArrayList<LauncherAppWidgetInfo> appWidgets =
2028 new ArrayList<LauncherAppWidgetInfo>();
2029 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2030 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002031 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002032 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 workspaceItems.addAll(sBgWorkspaceItems);
2034 appWidgets.addAll(sBgAppWidgets);
2035 folders.putAll(sBgFolders);
2036 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002037 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 }
2039
2040 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2041 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2042 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2043 new ArrayList<LauncherAppWidgetInfo>();
2044 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2045 new ArrayList<LauncherAppWidgetInfo>();
2046 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2047 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2048
2049 // Separate the items that are on the current screen, and all the other remaining items
2050 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2051 otherWorkspaceItems);
2052 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2053 otherAppWidgets);
2054 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2055 otherFolders);
2056 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2057 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2058
2059 // Tell the workspace that we're about to start binding items
2060 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002061 public void run() {
2062 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2063 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002065 }
2066 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 };
Winson Chung81b52252012-08-27 15:34:29 -07002068 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069
Adam Cohendcd297f2013-06-18 13:13:40 -07002070 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2071
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 // Load items on the current page
2073 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2074 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002075 if (isLoadingSynchronously) {
2076 r = new Runnable() {
2077 public void run() {
2078 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2079 if (callbacks != null) {
2080 callbacks.onPageBoundSynchronously(currentScreen);
2081 }
2082 }
2083 };
Winson Chung81b52252012-08-27 15:34:29 -07002084 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002085 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086
Winson Chung4a2afa32012-07-19 14:53:05 -07002087 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2088 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 mDeferredBindRunnables.clear();
2090 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002091 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092
2093 // Tell the workspace that we're done binding items
2094 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002095 public void run() {
2096 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2097 if (callbacks != null) {
Adam Cohene25af792013-06-06 23:08:25 -07002098 callbacks.finishBindingItems(mIsUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002099 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100
Winson Chung98e030b2012-05-07 16:01:11 -07002101 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002102 if (DEBUG_LOADERS) {
2103 Log.d(TAG, "bound workspace in "
2104 + (SystemClock.uptimeMillis()-t) + "ms");
2105 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002106
2107 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002108 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002109 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002110 if (isLoadingSynchronously) {
2111 mDeferredBindRunnables.add(r);
2112 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002113 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002114 }
Joe Onorato36115782010-06-17 13:28:48 -04002115 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002116
Joe Onorato36115782010-06-17 13:28:48 -04002117 private void loadAndBindAllApps() {
2118 if (DEBUG_LOADERS) {
2119 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2120 }
2121 if (!mAllAppsLoaded) {
2122 loadAllAppsByBatch();
Reena Lee93f824a2011-09-23 17:20:28 -07002123 synchronized (LoaderTask.this) {
2124 if (mStopped) {
2125 return;
2126 }
2127 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002128 }
Joe Onorato36115782010-06-17 13:28:48 -04002129 } else {
2130 onlyBindAllApps();
2131 }
2132 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002133
Joe Onorato36115782010-06-17 13:28:48 -04002134 private void onlyBindAllApps() {
2135 final Callbacks oldCallbacks = mCallbacks.get();
2136 if (oldCallbacks == null) {
2137 // This launcher has exited and nobody bothered to tell us. Just bail.
2138 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2139 return;
2140 }
2141
2142 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002143 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04002144 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07002145 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002146 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002147 public void run() {
2148 final long t = SystemClock.uptimeMillis();
2149 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2150 if (callbacks != null) {
2151 callbacks.bindAllApplications(list);
2152 }
2153 if (DEBUG_LOADERS) {
2154 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2155 + (SystemClock.uptimeMillis()-t) + "ms");
2156 }
2157 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002158 };
2159 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
2160 if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) {
2161 r.run();
2162 } else {
2163 mHandler.post(r);
2164 }
Joe Onorato36115782010-06-17 13:28:48 -04002165 }
2166
2167 private void loadAllAppsByBatch() {
2168 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2169
2170 // Don't use these two variables in any of the callback runnables.
2171 // Otherwise we hold a reference to them.
2172 final Callbacks oldCallbacks = mCallbacks.get();
2173 if (oldCallbacks == null) {
2174 // This launcher has exited and nobody bothered to tell us. Just bail.
2175 Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)");
2176 return;
2177 }
2178
2179 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2180 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2181
2182 final PackageManager packageManager = mContext.getPackageManager();
2183 List<ResolveInfo> apps = null;
2184
2185 int N = Integer.MAX_VALUE;
2186
2187 int startIndex;
2188 int i=0;
2189 int batchSize = -1;
2190 while (i < N && !mStopped) {
2191 if (i == 0) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002192 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002193 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2194 apps = packageManager.queryIntentActivities(mainIntent, 0);
2195 if (DEBUG_LOADERS) {
2196 Log.d(TAG, "queryIntentActivities took "
2197 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2198 }
2199 if (apps == null) {
2200 return;
2201 }
2202 N = apps.size();
2203 if (DEBUG_LOADERS) {
2204 Log.d(TAG, "queryIntentActivities got " + N + " apps");
2205 }
2206 if (N == 0) {
2207 // There are no apps?!?
2208 return;
2209 }
2210 if (mBatchSize == 0) {
2211 batchSize = N;
2212 } else {
2213 batchSize = mBatchSize;
2214 }
2215
2216 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2217 Collections.sort(apps,
Winson Chungc3eecff2011-07-11 17:44:15 -07002218 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04002219 if (DEBUG_LOADERS) {
2220 Log.d(TAG, "sort took "
2221 + (SystemClock.uptimeMillis()-sortTime) + "ms");
2222 }
2223 }
2224
2225 final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2226
2227 startIndex = i;
2228 for (int j=0; i<N && j<batchSize; j++) {
2229 // This builds the icon bitmaps.
Adam Cohen487f7dd2012-06-28 18:12:10 -07002230 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
Michael Jurkac9d95c52011-08-29 14:03:34 -07002231 mIconCache, mLabelCache));
Joe Onorato36115782010-06-17 13:28:48 -04002232 i++;
2233 }
2234
2235 final boolean first = i <= batchSize;
2236 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002237 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
2238 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002239
Joe Onoratocc67f472010-06-08 10:54:30 -07002240 mHandler.post(new Runnable() {
2241 public void run() {
2242 final long t = SystemClock.uptimeMillis();
Joe Onoratocc67f472010-06-08 10:54:30 -07002243 if (callbacks != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002244 if (first) {
2245 callbacks.bindAllApplications(added);
2246 } else {
2247 callbacks.bindAppsAdded(added);
2248 }
2249 if (DEBUG_LOADERS) {
2250 Log.d(TAG, "bound " + added.size() + " apps in "
2251 + (SystemClock.uptimeMillis() - t) + "ms");
2252 }
2253 } else {
2254 Log.i(TAG, "not binding apps: no Launcher activity");
Joe Onoratocc67f472010-06-08 10:54:30 -07002255 }
2256 }
2257 });
2258
Daniel Sandlerdca66122010-04-13 16:23:58 -04002259 if (DEBUG_LOADERS) {
Joe Onorato36115782010-06-17 13:28:48 -04002260 Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in "
2261 + (SystemClock.uptimeMillis()-t2) + "ms");
2262 }
2263
2264 if (mAllAppsLoadDelay > 0 && i < N) {
2265 try {
2266 if (DEBUG_LOADERS) {
2267 Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms");
2268 }
2269 Thread.sleep(mAllAppsLoadDelay);
2270 } catch (InterruptedException exc) { }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002271 }
2272 }
2273
Joe Onorato36115782010-06-17 13:28:48 -04002274 if (DEBUG_LOADERS) {
2275 Log.d(TAG, "cached all " + N + " apps in "
2276 + (SystemClock.uptimeMillis()-t) + "ms"
2277 + (mAllAppsLoadDelay > 0 ? " (including delay)" : ""));
Joe Onoratobe386092009-11-17 17:32:16 -08002278 }
2279 }
2280
2281 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002282 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2284 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2285 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2286 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2287 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2288 }
Joe Onorato36115782010-06-17 13:28:48 -04002289 }
2290 }
2291
2292 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002293 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002294 }
2295
2296 private class PackageUpdatedTask implements Runnable {
2297 int mOp;
2298 String[] mPackages;
2299
2300 public static final int OP_NONE = 0;
2301 public static final int OP_ADD = 1;
2302 public static final int OP_UPDATE = 2;
2303 public static final int OP_REMOVE = 3; // uninstlled
2304 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2305
2306
2307 public PackageUpdatedTask(int op, String[] packages) {
2308 mOp = op;
2309 mPackages = packages;
2310 }
2311
2312 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002313 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002314
2315 final String[] packages = mPackages;
2316 final int N = packages.length;
2317 switch (mOp) {
2318 case OP_ADD:
2319 for (int i=0; i<N; i++) {
2320 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002321 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002322 }
2323 break;
2324 case OP_UPDATE:
2325 for (int i=0; i<N; i++) {
2326 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002327 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002328 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002329 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002330 }
2331 break;
2332 case OP_REMOVE:
2333 case OP_UNAVAILABLE:
2334 for (int i=0; i<N; i++) {
2335 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002336 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002337 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002338 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002339 }
2340 break;
2341 }
2342
2343 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002344 ArrayList<ApplicationInfo> modified = null;
Winson Chung83892cc2013-05-01 16:53:33 -07002345 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002346
Adam Cohen487f7dd2012-06-28 18:12:10 -07002347 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002348 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2349 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002350 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002351 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002352 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2353 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002354 }
Winson Chung5d55f332012-07-16 20:45:03 -07002355 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002356 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002357 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002358 }
2359
Joe Onorato36115782010-06-17 13:28:48 -04002360 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2361 if (callbacks == null) {
2362 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2363 return;
2364 }
2365
2366 if (added != null) {
2367 final ArrayList<ApplicationInfo> addedFinal = added;
2368 mHandler.post(new Runnable() {
2369 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002370 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2371 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002372 callbacks.bindAppsAdded(addedFinal);
2373 }
2374 }
2375 });
2376 }
2377 if (modified != null) {
2378 final ArrayList<ApplicationInfo> modifiedFinal = modified;
2379 mHandler.post(new Runnable() {
2380 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002381 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2382 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002383 callbacks.bindAppsUpdated(modifiedFinal);
2384 }
2385 }
2386 });
2387 }
Winson Chung83892cc2013-05-01 16:53:33 -07002388 // If a package has been removed, or an app has been removed as a result of
2389 // an update (for example), make the removed callback.
2390 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
2391 final boolean permanent = (mOp == OP_REMOVE);
2392 final ArrayList<String> removedPackageNames =
2393 new ArrayList<String>(Arrays.asList(packages));
2394
Joe Onorato36115782010-06-17 13:28:48 -04002395 mHandler.post(new Runnable() {
2396 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002397 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2398 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002399 callbacks.bindComponentsRemoved(removedPackageNames,
2400 removedApps, permanent);
Joe Onorato36115782010-06-17 13:28:48 -04002401 }
2402 }
2403 });
Joe Onoratobe386092009-11-17 17:32:16 -08002404 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002405
Michael Jurkac402cd92013-05-20 15:49:32 +02002406 final ArrayList<Object> widgetsAndShortcuts =
2407 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002408 mHandler.post(new Runnable() {
2409 @Override
2410 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002411 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2412 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002413 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002414 }
2415 }
2416 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002417 }
2418 }
2419
Michael Jurkac402cd92013-05-20 15:49:32 +02002420 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2421 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2422 PackageManager packageManager = context.getPackageManager();
2423 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2424 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2425 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2426 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2427 Collections.sort(widgetsAndShortcuts,
2428 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2429 return widgetsAndShortcuts;
2430 }
2431
Joe Onorato9c1289c2009-08-17 11:03:03 -04002432 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002433 * This is called from the code that adds shortcuts from the intent receiver. This
2434 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002435 */
Joe Onorato56d82912010-03-07 14:32:10 -05002436 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002437 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002438 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002439
Joe Onorato56d82912010-03-07 14:32:10 -05002440 /**
2441 * Make an ShortcutInfo object for a shortcut that is an application.
2442 *
2443 * If c is not null, then it will be used to fill in missing data like the title and icon.
2444 */
2445 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002446 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002447 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002448 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002449
2450 ComponentName componentName = intent.getComponent();
2451 if (componentName == null) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002452 return null;
2453 }
2454
Adam Cohen00fcb492011-11-02 21:53:47 -07002455 try {
2456 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2457 if (!pi.applicationInfo.enabled) {
2458 // If we return null here, the corresponding item will be removed from the launcher
2459 // db and will not appear in the workspace.
2460 return null;
2461 }
2462 } catch (NameNotFoundException e) {
2463 Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName());
2464 }
2465
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002466 // TODO: See if the PackageManager knows about this case. If it doesn't
2467 // then return null & delete this.
2468
Joe Onorato56d82912010-03-07 14:32:10 -05002469 // the resource -- This may implicitly give us back the fallback icon,
2470 // but don't worry about that. All we're doing with usingFallbackIcon is
2471 // to avoid saving lots of copies of that in the database, and most apps
2472 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002473
2474 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2475 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2476 // via resolveActivity().
2477 ResolveInfo resolveInfo = null;
2478 ComponentName oldComponent = intent.getComponent();
2479 Intent newIntent = new Intent(intent.getAction(), null);
2480 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2481 newIntent.setPackage(oldComponent.getPackageName());
2482 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2483 for (ResolveInfo i : infos) {
2484 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2485 i.activityInfo.name);
2486 if (cn.equals(oldComponent)) {
2487 resolveInfo = i;
2488 }
2489 }
2490 if (resolveInfo == null) {
2491 resolveInfo = manager.resolveActivity(intent, 0);
2492 }
Joe Onorato56d82912010-03-07 14:32:10 -05002493 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002494 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002495 }
Joe Onorato56d82912010-03-07 14:32:10 -05002496 // the db
2497 if (icon == null) {
2498 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002499 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002500 }
2501 }
2502 // the fallback icon
2503 if (icon == null) {
2504 icon = getFallbackIcon();
2505 info.usingFallbackIcon = true;
2506 }
2507 info.setIcon(icon);
2508
2509 // from the resource
2510 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002511 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2512 if (labelCache != null && labelCache.containsKey(key)) {
2513 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002514 } else {
2515 info.title = resolveInfo.activityInfo.loadLabel(manager);
2516 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002517 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002518 }
2519 }
Joe Onorato56d82912010-03-07 14:32:10 -05002520 }
2521 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002522 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002523 if (c != null) {
2524 info.title = c.getString(titleIndex);
2525 }
2526 }
2527 // fall back to the class name of the activity
2528 if (info.title == null) {
2529 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002530 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002531 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2532 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 /**
Winson Chung8a435102012-08-30 17:16:53 -07002536 * Returns the set of workspace ShortcutInfos with the specified intent.
2537 */
2538 static ArrayList<ItemInfo> getWorkspaceShortcutItemInfosWithIntent(Intent intent) {
2539 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
2540 synchronized (sBgLock) {
2541 for (ItemInfo info : sBgWorkspaceItems) {
2542 if (info instanceof ShortcutInfo) {
2543 ShortcutInfo shortcut = (ShortcutInfo) info;
2544 if (shortcut.intent.toUri(0).equals(intent.toUri(0))) {
2545 items.add(shortcut);
2546 }
2547 }
2548 }
2549 }
2550 return items;
2551 }
2552
2553 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002554 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002556 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002557 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2558 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559
Joe Onorato56d82912010-03-07 14:32:10 -05002560 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002561 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002562 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002564 // TODO: If there's an explicit component and we can't install that, delete it.
2565
Joe Onorato56d82912010-03-07 14:32:10 -05002566 info.title = c.getString(titleIndex);
2567
Joe Onorato9c1289c2009-08-17 11:03:03 -04002568 int iconType = c.getInt(iconTypeIndex);
2569 switch (iconType) {
2570 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2571 String packageName = c.getString(iconPackageIndex);
2572 String resourceName = c.getString(iconResourceIndex);
2573 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002574 info.customIcon = false;
2575 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002577 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002578 if (resources != null) {
2579 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002580 icon = Utilities.createIconBitmap(
2581 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002582 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002583 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002584 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 }
Joe Onorato56d82912010-03-07 14:32:10 -05002586 // the db
2587 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002588 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002589 }
2590 // the fallback icon
2591 if (icon == null) {
2592 icon = getFallbackIcon();
2593 info.usingFallbackIcon = true;
2594 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002595 break;
2596 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002597 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002598 if (icon == null) {
2599 icon = getFallbackIcon();
2600 info.customIcon = false;
2601 info.usingFallbackIcon = true;
2602 } else {
2603 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002604 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002605 break;
2606 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002607 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002608 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002609 info.customIcon = false;
2610 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002611 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002612 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002613 return info;
2614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615
Michael Jurka931dc972011-08-05 15:08:15 -07002616 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002617 @SuppressWarnings("all") // suppress dead code warning
2618 final boolean debug = false;
2619 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002620 Log.d(TAG, "getIconFromCursor app="
2621 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2622 }
2623 byte[] data = c.getBlob(iconIndex);
2624 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002625 return Utilities.createIconBitmap(
2626 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002627 } catch (Exception e) {
2628 return null;
2629 }
2630 }
2631
Winson Chung3d503fb2011-07-13 17:25:49 -07002632 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2633 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002634 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002635 if (info == null) {
2636 return null;
2637 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002638 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002639
2640 return info;
2641 }
2642
Winson Chunga9abd0e2010-10-27 17:18:37 -07002643 /**
Winson Chung55cef262010-10-28 14:14:18 -07002644 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2645 */
2646 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2647 ComponentName component) {
2648 List<AppWidgetProviderInfo> widgets =
2649 AppWidgetManager.getInstance(context).getInstalledProviders();
2650 for (AppWidgetProviderInfo info : widgets) {
2651 if (info.provider.equals(component)) {
2652 return info;
2653 }
2654 }
2655 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002656 }
2657
Winson Chung68846fd2010-10-29 11:00:27 -07002658 /**
2659 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2660 */
2661 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2662 final PackageManager packageManager = context.getPackageManager();
2663 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2664 new ArrayList<WidgetMimeTypeHandlerData>();
2665
2666 final Intent supportsIntent =
2667 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2668 supportsIntent.setType(mimeType);
2669
2670 // Create a set of widget configuration components that we can test against
2671 final List<AppWidgetProviderInfo> widgets =
2672 AppWidgetManager.getInstance(context).getInstalledProviders();
2673 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2674 new HashMap<ComponentName, AppWidgetProviderInfo>();
2675 for (AppWidgetProviderInfo info : widgets) {
2676 configurationComponentToWidget.put(info.configure, info);
2677 }
2678
2679 // Run through each of the intents that can handle this type of clip data, and cross
2680 // reference them with the components that are actual configuration components
2681 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2682 PackageManager.MATCH_DEFAULT_ONLY);
2683 for (ResolveInfo info : activities) {
2684 final ActivityInfo activityInfo = info.activityInfo;
2685 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2686 activityInfo.name);
2687 if (configurationComponentToWidget.containsKey(infoComponent)) {
2688 supportedConfigurationActivities.add(
2689 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2690 configurationComponentToWidget.get(infoComponent)));
2691 }
2692 }
2693 return supportedConfigurationActivities;
2694 }
2695
Winson Chunga9abd0e2010-10-27 17:18:37 -07002696 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002697 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2698 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2699 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2700
Adam Cohend9198822011-11-22 16:42:47 -08002701 if (intent == null) {
2702 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2703 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2704 return null;
2705 }
2706
Joe Onorato0589f0f2010-02-08 13:44:00 -08002707 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002708 boolean customIcon = false;
2709 ShortcutIconResource iconResource = null;
2710
2711 if (bitmap != null && bitmap instanceof Bitmap) {
2712 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002713 customIcon = true;
2714 } else {
2715 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2716 if (extra != null && extra instanceof ShortcutIconResource) {
2717 try {
2718 iconResource = (ShortcutIconResource) extra;
2719 final PackageManager packageManager = context.getPackageManager();
2720 Resources resources = packageManager.getResourcesForApplication(
2721 iconResource.packageName);
2722 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002723 icon = Utilities.createIconBitmap(
2724 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002725 } catch (Exception e) {
2726 Log.w(TAG, "Could not load shortcut icon: " + extra);
2727 }
2728 }
2729 }
2730
Michael Jurkac9d95c52011-08-29 14:03:34 -07002731 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002732
2733 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002734 if (fallbackIcon != null) {
2735 icon = fallbackIcon;
2736 } else {
2737 icon = getFallbackIcon();
2738 info.usingFallbackIcon = true;
2739 }
Joe Onorato56d82912010-03-07 14:32:10 -05002740 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002741 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002742
Joe Onorato0589f0f2010-02-08 13:44:00 -08002743 info.title = name;
2744 info.intent = intent;
2745 info.customIcon = customIcon;
2746 info.iconResource = iconResource;
2747
2748 return info;
2749 }
2750
Winson Chungaac01e12011-08-17 10:37:13 -07002751 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2752 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002753 // If apps can't be on SD, don't even bother.
2754 if (!mAppsCanBeOnExternalStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002755 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002756 }
Joe Onorato56d82912010-03-07 14:32:10 -05002757 // If this icon doesn't have a custom icon, check to see
2758 // what's stored in the DB, and if it doesn't match what
2759 // we're going to show, store what we are going to show back
2760 // into the DB. We do this so when we're loading, if the
2761 // package manager can't find an icon (for example because
2762 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002763 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002764 cache.put(info, c.getBlob(iconIndex));
2765 return true;
2766 }
2767 return false;
2768 }
2769 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2770 boolean needSave = false;
2771 try {
2772 if (data != null) {
2773 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2774 Bitmap loaded = info.getIcon(mIconCache);
2775 needSave = !saved.sameAs(loaded);
2776 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05002777 needSave = true;
2778 }
Winson Chungaac01e12011-08-17 10:37:13 -07002779 } catch (Exception e) {
2780 needSave = true;
2781 }
2782 if (needSave) {
2783 Log.d(TAG, "going to save icon bitmap for info=" + info);
2784 // This is slower than is ideal, but this only happens once
2785 // or when the app is updated with a new icon.
2786 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05002787 }
2788 }
2789
Joe Onorato9c1289c2009-08-17 11:03:03 -04002790 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07002791 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002792 * or make a new one.
2793 */
Adam Cohendf2cc412011-04-27 16:56:57 -07002794 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795 // See if a placeholder was created for us already
2796 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07002797 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002798 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07002799 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002800 folders.put(id, folderInfo);
2801 }
Adam Cohendf2cc412011-04-27 16:56:57 -07002802 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 }
2804
Winson Chung11904872012-09-17 16:58:46 -07002805 public static final Comparator<ApplicationInfo> getAppNameComparator() {
2806 final Collator collator = Collator.getInstance();
2807 return new Comparator<ApplicationInfo>() {
2808 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2809 int result = collator.compare(a.title.toString(), b.title.toString());
2810 if (result == 0) {
2811 result = a.componentName.compareTo(b.componentName);
2812 }
2813 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07002814 }
Winson Chung11904872012-09-17 16:58:46 -07002815 };
2816 }
Winson Chung78403fe2011-01-21 15:38:02 -08002817 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
2818 = new Comparator<ApplicationInfo>() {
2819 public final int compare(ApplicationInfo a, ApplicationInfo b) {
2820 if (a.firstInstallTime < b.firstInstallTime) return 1;
2821 if (a.firstInstallTime > b.firstInstallTime) return -1;
2822 return 0;
2823 }
2824 };
Winson Chung11904872012-09-17 16:58:46 -07002825 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
2826 final Collator collator = Collator.getInstance();
2827 return new Comparator<AppWidgetProviderInfo>() {
2828 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
2829 return collator.compare(a.label.toString(), b.label.toString());
2830 }
2831 };
2832 }
Winson Chung5308f242011-08-18 12:12:41 -07002833 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
2834 if (info.activityInfo != null) {
2835 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
2836 } else {
2837 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
2838 }
2839 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002840 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07002841 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07002842 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07002843 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07002844 ShortcutNameComparator(PackageManager pm) {
2845 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07002846 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07002847 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07002848 }
2849 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
2850 mPackageManager = pm;
2851 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07002852 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07002853 }
2854 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002855 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07002856 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
2857 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
2858 if (mLabelCache.containsKey(keyA)) {
2859 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002860 } else {
2861 labelA = a.loadLabel(mPackageManager).toString();
2862
Winson Chung5308f242011-08-18 12:12:41 -07002863 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07002864 }
Winson Chung5308f242011-08-18 12:12:41 -07002865 if (mLabelCache.containsKey(keyB)) {
2866 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002867 } else {
2868 labelB = b.loadLabel(mPackageManager).toString();
2869
Winson Chung5308f242011-08-18 12:12:41 -07002870 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07002871 }
Winson Chung11904872012-09-17 16:58:46 -07002872 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07002873 }
2874 };
Winson Chung1ed747a2011-05-03 16:18:34 -07002875 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07002876 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07002877 private PackageManager mPackageManager;
2878 private HashMap<Object, String> mLabelCache;
2879 WidgetAndShortcutNameComparator(PackageManager pm) {
2880 mPackageManager = pm;
2881 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07002882 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07002883 }
2884 public final int compare(Object a, Object b) {
2885 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07002886 if (mLabelCache.containsKey(a)) {
2887 labelA = mLabelCache.get(a);
2888 } else {
2889 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002890 ((AppWidgetProviderInfo) a).label :
2891 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002892 mLabelCache.put(a, labelA);
2893 }
2894 if (mLabelCache.containsKey(b)) {
2895 labelB = mLabelCache.get(b);
2896 } else {
2897 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07002898 ((AppWidgetProviderInfo) b).label :
2899 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07002900 mLabelCache.put(b, labelB);
2901 }
Winson Chung11904872012-09-17 16:58:46 -07002902 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07002903 }
2904 };
Joe Onoratobe386092009-11-17 17:32:16 -08002905
2906 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002907 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002908 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2909 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2910 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2911 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002912 if (mLoaderTask != null) {
2913 mLoaderTask.dumpState();
2914 } else {
2915 Log.d(TAG, "mLoaderTask=null");
2916 }
Joe Onoratobe386092009-11-17 17:32:16 -08002917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918}