blob: 9311723ae62a192b448e59992aeeada23af402c1 [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;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Daniel Sandler325dc232013-06-05 22:57:57 -040047import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080048
Michael Jurkac2f801e2011-07-12 14:19:46 -070049import java.lang.ref.WeakReference;
50import java.net.URISyntaxException;
51import java.text.Collator;
52import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070053import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070054import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070055import java.util.Collections;
56import java.util.Comparator;
57import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070059import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070061import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070062import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070063import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065/**
66 * Maintains in-memory state of the Launcher. It is expected that there should be only one
67 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070068 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040070public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080071 static final boolean DEBUG_LOADERS = false;
Chris Wrenb358f812014-04-16 13:37:00 -040072 private static final boolean DEBUG_RECEIVER = true; // STOPSHIP(cwren) temporary for debugging
73
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070075
Daniel Sandler8707e0f2013-08-15 15:54:18 -070076 // true = use a "More Apps" folder for non-workspace apps on upgrade
77 // false = strew non-workspace apps across the workspace on upgrade
78 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
Dan Sandlerd5024042014-01-09 15:01:33 -050079 public static final int LOADER_FLAG_NONE = 0;
80 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
81 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
82
Joe Onorato36115782010-06-17 13:28:48 -040083 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050084 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080085
Winson Chungee055712013-07-30 14:46:24 -070086 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -080087 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -040088
Daniel Sandlercc8befa2013-06-11 14:45:48 -040089 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 private final Object mLock = new Object();
91 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040092 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070093 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020094 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095
Winson Chung81b52252012-08-27 15:34:29 -070096 // Specific runnable types that are run on the main thread deferred handler, this allows us to
97 // clear all queued binding runnables when the Launcher activity is destroyed.
98 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
99 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
100
101
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700102 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
103 static {
104 sWorkerThread.start();
105 }
106 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
107
Joe Onoratocc67f472010-06-08 10:54:30 -0700108 // We start off with everything not loaded. After that, we assume that
109 // our monitoring of the package manager provides all updates and we never
110 // need to do a requery. These are only ever touched from the loader thread.
111 private boolean mWorkspaceLoaded;
112 private boolean mAllAppsLoaded;
113
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700114 // When we are loading pages synchronously, we can't just post the binding of items on the side
115 // pages as this delays the rotation process. Instead, we wait for a callback from the first
116 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
117 // a normal load, we also clear this set of Runnables.
118 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700123 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800124
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700125 // The lock that must be acquired before referencing any static bg data structures. Unlike
126 // other locks, this one can generally be held long-term because we never expect any of these
127 // static data structures to be referenced outside of the worker thread except on the first
128 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700129 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700130
Adam Cohen487f7dd2012-06-28 18:12:10 -0700131 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700133 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
136 // created by LauncherModel that are directly on the home screen (however, no widgets or
137 // shortcuts within folders).
138 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139
Adam Cohen487f7dd2012-06-28 18:12:10 -0700140 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
141 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 new ArrayList<LauncherAppWidgetInfo>();
143
Adam Cohen487f7dd2012-06-28 18:12:10 -0700144 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
145 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700146
Adam Cohen487f7dd2012-06-28 18:12:10 -0700147 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
148 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700149
150 // sBgWorkspaceScreens is the ordered set of workspace screens.
151 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
152
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 // </ only access in worker thread >
154
155 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800156 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Reena Lee99a73f32011-10-24 17:27:37 -0700158 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700159
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700161 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400162 public int getCurrentWorkspaceScreen();
163 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700164 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
165 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700166 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700167 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500168 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700169 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200171 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700172 public void bindAppsAdded(ArrayList<Long> newScreens,
173 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700174 ArrayList<ItemInfo> addAnimated,
175 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200176 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500177 public void updatePackageState(String pkgName, int state);
Winson Chung83892cc2013-05-01 16:53:33 -0700178 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700179 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200180 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100181 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700182 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700183 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700184 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Winson Chung64359a52013-07-08 17:17:08 -0700187 public interface ItemInfoFilter {
188 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
189 }
190
Bjorn Bringert1307f632013-10-03 22:31:03 +0100191 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800192 Context context = app.getContext();
193 ContentResolver contentResolver = context.getContentResolver();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400194
Winson Chungee055712013-07-30 14:46:24 -0700195 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Winson Chungeb23cb22014-03-06 10:39:43 -0800196 ContentProviderClient client = contentResolver.acquireContentProviderClient(
197 LauncherSettings.Favorites.OLD_CONTENT_URI);
198 mOldContentProviderExists = (client != null);
199 if (client != null) {
200 client.release();
201 }
Daniel Sandlere4f98912013-06-25 15:13:26 -0400202 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100203 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800204 mIconCache = iconCache;
205
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400206 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700207 Configuration config = res.getConfiguration();
208 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 }
210
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700211 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
212 * posted on the main thread handler. */
213 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700214 runOnMainThread(r, 0);
215 }
216 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700217 if (sWorkerThread.getThreadId() == Process.myTid()) {
218 // If we are on the worker thread, post onto the main handler
219 mHandler.post(r);
220 } else {
221 r.run();
222 }
223 }
224
225 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
226 * posted on the worker thread handler. */
227 private static void runOnWorkerThread(Runnable r) {
228 if (sWorkerThread.getThreadId() == Process.myTid()) {
229 r.run();
230 } else {
231 // If we are not on the worker thread, then post to the worker handler
232 sWorker.post(r);
233 }
234 }
235
Winson Chunge43a1e72014-01-15 10:33:02 -0800236 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
237 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800238 }
239
Winson Chungc9168342013-06-26 14:54:55 -0700240 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
241 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700242 LauncherAppState app = LauncherAppState.getInstance();
243 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
244 final int xCount = (int) grid.numColumns;
245 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700246 boolean[][] occupied = new boolean[xCount][yCount];
247
248 int cellX, cellY, spanX, spanY;
249 for (int i = 0; i < items.size(); ++i) {
250 final ItemInfo item = items.get(i);
251 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
252 if (item.screenId == screen) {
253 cellX = item.cellX;
254 cellY = item.cellY;
255 spanX = item.spanX;
256 spanY = item.spanY;
257 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
258 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
259 occupied[x][y] = true;
260 }
261 }
262 }
263 }
264 }
265
266 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
267 }
268 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700269 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700270 int firstScreenIndex,
271 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700272 // Lock on the app so that we don't try and get the items while apps are being added
273 LauncherAppState app = LauncherAppState.getInstance();
274 LauncherModel model = app.getModel();
275 boolean found = false;
276 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700277 if (sWorkerThread.getThreadId() != Process.myTid()) {
278 // Flush the LauncherModel worker thread, so that if we just did another
279 // processInstallShortcut, we give it time for its shortcut to get added to the
280 // database (getItemsInLocalCoordinates reads the database)
281 model.flushWorkerThread();
282 }
Winson Chungc9168342013-06-26 14:54:55 -0700283 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700284
285 // Try adding to the workspace screens incrementally, starting at the default or center
286 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700287 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
288 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700289 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700290 int[] tmpCoordinates = new int[2];
291 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700292 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700293 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700294 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700295 }
296 }
297 }
Winson Chungc9168342013-06-26 14:54:55 -0700298 return null;
299 }
300
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500301 public void setPackageState(final String pkgName, final int state) {
302 // Process the updated package state
303 Runnable r = new Runnable() {
304 public void run() {
305 Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
306 if (callbacks != null) {
307 callbacks.updatePackageState(pkgName, state);
308 }
309 }
310 };
311 mHandler.post(r);
312 }
313
Adam Cohen76a47a12014-02-05 11:47:43 -0800314 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
315 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
316
317 if (allAppsApps == null) {
318 throw new RuntimeException("allAppsApps must not be null");
319 }
320 if (allAppsApps.isEmpty()) {
321 return;
322 }
323
Chris Wrenb6d4c282014-01-27 14:17:08 -0500324 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
325 Iterator<AppInfo> iter = allAppsApps.iterator();
326 while (iter.hasNext()) {
327 ItemInfo a = iter.next();
328 if (LauncherModel.appWasRestored(ctx, a.getIntent())) {
329 restoredAppsFinal.add((AppInfo) a);
330 }
331 }
332
Adam Cohen76a47a12014-02-05 11:47:43 -0800333 // Process the newly added applications and add them to the database first
334 Runnable r = new Runnable() {
335 public void run() {
336 runOnMainThread(new Runnable() {
337 public void run() {
338 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
339 if (callbacks == cb && cb != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500340 if (!restoredAppsFinal.isEmpty()) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500341 for (AppInfo info : restoredAppsFinal) {
342 final Intent intent = info.getIntent();
343 if (intent != null) {
344 mIconCache.deletePreloadedIcon(intent.getComponent());
345 }
346 }
Chris Wrenb6d4c282014-01-27 14:17:08 -0500347 callbacks.bindAppsUpdated(restoredAppsFinal);
348 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500349 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800350 }
351 }
352 });
353 }
354 };
355 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700356 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800357
358 public void addAndBindAddedWorkspaceApps(final Context context,
359 final ArrayList<ItemInfo> workspaceApps) {
360 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
361
362 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800363 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
364 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800365 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700366 return;
Winson Chung997a9232013-07-24 15:33:46 -0700367 }
Winson Chung64359a52013-07-08 17:17:08 -0700368 // Process the newly added applications and add them to the database first
369 Runnable r = new Runnable() {
370 public void run() {
371 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
372 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500373 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700374
Winson Chung76828c82013-08-19 15:43:29 -0700375 // Get the list of workspace screens. We need to append to this list and
376 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
377 // called.
378 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
379 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
380 for (Integer i : orderedScreens.keySet()) {
381 long screenId = orderedScreens.get(i);
382 workspaceScreens.add(screenId);
383 }
384
Winson Chung64359a52013-07-08 17:17:08 -0700385 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700386 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700387 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700388 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700389 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700390 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700391
392 // Short-circuit this logic if the icon exists somewhere on the workspace
393 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500394 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
395 if (a instanceof AppInfo &&
396 LauncherModel.appWasRestored(context, launchIntent)) {
397 restoredAppsFinal.add((AppInfo) a);
398 }
Winson Chung64359a52013-07-08 17:17:08 -0700399 continue;
400 }
401
Winson Chung87412982013-10-03 18:34:14 -0700402 // Add this icon to the db, creating a new page if necessary. If there
403 // is only the empty page then we just add items to the first page.
404 // Otherwise, we add them to the next pages.
405 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700406 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700407 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700408 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200409 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700410
411 // If we can't find a valid position, then just add a new screen.
412 // This takes time so we need to re-queue the add until the new
413 // page is added. Create as many screens as necessary to satisfy
414 // the startSearchPageIndex.
415 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700416 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700417 while (numPagesToAdd > 0) {
418 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700419 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700420 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700421 addedWorkspaceScreensFinal.add(screenId);
422 numPagesToAdd--;
423 }
Winson Chung76828c82013-08-19 15:43:29 -0700424
Winson Chung64359a52013-07-08 17:17:08 -0700425 // Find the coordinate again
426 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700427 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700428 }
429 if (coords == null) {
430 throw new RuntimeException("Coordinates should not be null");
431 }
432
Winson Chung997a9232013-07-24 15:33:46 -0700433 ShortcutInfo shortcutInfo;
434 if (a instanceof ShortcutInfo) {
435 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200436 } else if (a instanceof AppInfo) {
437 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700438 } else {
439 throw new RuntimeException("Unexpected info type");
440 }
Winson Chung94d67682013-09-25 16:29:40 -0700441
Winson Chung64359a52013-07-08 17:17:08 -0700442 // Add the shortcut to the db
443 addItemToDatabase(context, shortcutInfo,
444 LauncherSettings.Favorites.CONTAINER_DESKTOP,
445 coords.first, coords.second[0], coords.second[1], false);
446 // Save the ShortcutInfo for binding in the workspace
447 addedShortcutsFinal.add(shortcutInfo);
448 }
449 }
450
Winson Chung76828c82013-08-19 15:43:29 -0700451 // Update the workspace screens
452 updateWorkspaceScreenOrder(context, workspaceScreens);
453
Adam Cohen76a47a12014-02-05 11:47:43 -0800454 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700455 runOnMainThread(new Runnable() {
456 public void run() {
457 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
458 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700459 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
460 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700461 if (!addedShortcutsFinal.isEmpty()) {
462 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
463 long lastScreenId = info.screenId;
464 for (ItemInfo i : addedShortcutsFinal) {
465 if (i.screenId == lastScreenId) {
466 addAnimated.add(i);
467 } else {
468 addNotAnimated.add(i);
469 }
Winson Chung997a9232013-07-24 15:33:46 -0700470 }
471 }
Winson Chungd64d1762013-08-20 14:37:16 -0700472 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800473 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500474 if (!restoredAppsFinal.isEmpty()) {
475 callbacks.bindAppsUpdated(restoredAppsFinal);
476 }
Winson Chung997a9232013-07-24 15:33:46 -0700477 }
Winson Chung64359a52013-07-08 17:17:08 -0700478 }
Winson Chung997a9232013-07-24 15:33:46 -0700479 });
480 }
Winson Chung64359a52013-07-08 17:17:08 -0700481 }
482 };
483 runOnWorkerThread(r);
484 }
485
Joe Onorato56d82912010-03-07 14:32:10 -0500486 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700487 if (mDefaultIcon == null) {
488 final Context context = LauncherAppState.getInstance().getContext();
489 mDefaultIcon = Utilities.createIconBitmap(
490 mIconCache.getFullResDefaultActivityIcon(), context);
491 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800492 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400493 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494
Winson Chung81b52252012-08-27 15:34:29 -0700495 public void unbindItemInfosAndClearQueuedBindRunnables() {
496 if (sWorkerThread.getThreadId() == Process.myTid()) {
497 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
498 "main thread");
499 }
500
501 // Clear any deferred bind runnables
502 mDeferredBindRunnables.clear();
503 // Remove any queued bind runnables
504 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
505 // Unbind all the workspace items
506 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700507 }
508
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700509 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700510 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700511 // Ensure that we don't use the same workspace items data structure on the main thread
512 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700513 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
514 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700515 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700516 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
517 tmpAppWidgets.addAll(sBgAppWidgets);
518 }
519 Runnable r = new Runnable() {
520 @Override
521 public void run() {
522 for (ItemInfo item : tmpWorkspaceItems) {
523 item.unbind();
524 }
525 for (ItemInfo item : tmpAppWidgets) {
526 item.unbind();
527 }
528 }
529 };
530 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700531 }
532
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 /**
534 * Adds an item to the DB if it was not created previously, or move it to a new
535 * <container, screen, cellX, cellY>
536 */
537 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700538 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400539 if (item.container == ItemInfo.NO_ID) {
540 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700541 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 } else {
543 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700544 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 }
546 }
547
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700548 static void checkItemInfoLocked(
549 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
550 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
551 if (modelItem != null && item != modelItem) {
552 // check all the data is consistent
553 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
554 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
555 ShortcutInfo shortcut = (ShortcutInfo) item;
556 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
557 modelShortcut.intent.filterEquals(shortcut.intent) &&
558 modelShortcut.id == shortcut.id &&
559 modelShortcut.itemType == shortcut.itemType &&
560 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700561 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700562 modelShortcut.cellX == shortcut.cellX &&
563 modelShortcut.cellY == shortcut.cellY &&
564 modelShortcut.spanX == shortcut.spanX &&
565 modelShortcut.spanY == shortcut.spanY &&
566 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
567 (modelShortcut.dropPos != null &&
568 shortcut.dropPos != null &&
569 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
570 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
571 // For all intents and purposes, this is the same object
572 return;
573 }
574 }
575
576 // the modelItem needs to match up perfectly with item if our model is
577 // to be consistent with the database-- for now, just require
578 // modelItem == item or the equality check above
579 String msg = "item: " + ((item != null) ? item.toString() : "null") +
580 "modelItem: " +
581 ((modelItem != null) ? modelItem.toString() : "null") +
582 "Error: ItemInfo passed to checkItemInfo doesn't match original";
583 RuntimeException e = new RuntimeException(msg);
584 if (stackTrace != null) {
585 e.setStackTrace(stackTrace);
586 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800587 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700588 }
589 }
590
Michael Jurka816474f2012-06-25 14:49:02 -0700591 static void checkItemInfo(final ItemInfo item) {
592 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
593 final long itemId = item.id;
594 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700595 public void run() {
596 synchronized (sBgLock) {
597 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700598 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700599 }
600 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700601 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700602 }
603
Michael Jurkac9d95c52011-08-29 14:03:34 -0700604 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
605 final ItemInfo item, final String callingFunction) {
606 final long itemId = item.id;
607 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
608 final ContentResolver cr = context.getContentResolver();
609
Adam Cohen487f7dd2012-06-28 18:12:10 -0700610 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700611 Runnable r = new Runnable() {
612 public void run() {
613 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700614 updateItemArrays(item, itemId, stackTrace);
615 }
616 };
617 runOnWorkerThread(r);
618 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700619
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700620 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
621 final ArrayList<ItemInfo> items, final String callingFunction) {
622 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700623
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700624 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
625 Runnable r = new Runnable() {
626 public void run() {
627 ArrayList<ContentProviderOperation> ops =
628 new ArrayList<ContentProviderOperation>();
629 int count = items.size();
630 for (int i = 0; i < count; i++) {
631 ItemInfo item = items.get(i);
632 final long itemId = item.id;
633 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
634 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700635
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700636 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
637 updateItemArrays(item, itemId, stackTrace);
638
639 }
640 try {
641 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
642 } catch (Exception e) {
643 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700644 }
645 }
646 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700647 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700648 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700649
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700650 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
651 // Lock on mBgLock *after* the db operation
652 synchronized (sBgLock) {
653 checkItemInfoLocked(itemId, item, stackTrace);
654
655 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
656 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
657 // Item is in a folder, make sure this folder exists
658 if (!sBgFolders.containsKey(item.container)) {
659 // An items container is being set to a that of an item which is not in
660 // the list of Folders.
661 String msg = "item: " + item + " container being set to: " +
662 item.container + ", not in the list of folders";
663 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700664 }
665 }
666
667 // Items are added/removed from the corresponding FolderInfo elsewhere, such
668 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
669 // that are on the desktop, as appropriate
670 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800671 if (modelItem != null &&
672 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
673 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 switch (modelItem.itemType) {
675 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
676 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
677 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
678 if (!sBgWorkspaceItems.contains(modelItem)) {
679 sBgWorkspaceItems.add(modelItem);
680 }
681 break;
682 default:
683 break;
684 }
685 } else {
686 sBgWorkspaceItems.remove(modelItem);
687 }
688 }
689 }
690
Michael Jurkac7700af2013-05-14 20:17:58 +0200691 public void flushWorkerThread() {
692 mFlushingWorkerThread = true;
693 Runnable waiter = new Runnable() {
694 public void run() {
695 synchronized (this) {
696 notifyAll();
697 mFlushingWorkerThread = false;
698 }
699 }
700 };
701
702 synchronized(waiter) {
703 runOnWorkerThread(waiter);
704 if (mLoaderTask != null) {
705 synchronized(mLoaderTask) {
706 mLoaderTask.notify();
707 }
708 }
709 boolean success = false;
710 while (!success) {
711 try {
712 waiter.wait();
713 success = true;
714 } catch (InterruptedException e) {
715 }
716 }
717 }
718 }
719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700722 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700723 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700724 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400726 item.cellX = cellX;
727 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700728
Winson Chung3d503fb2011-07-13 17:25:49 -0700729 // We store hotseat items in canonical form which is this orientation invariant position
730 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700731 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700733 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700735 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700736 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737
738 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700740 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
741 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743
Michael Jurkac9d95c52011-08-29 14:03:34 -0700744 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700745 }
746
747 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
749 * cellX, cellY have already been updated on the ItemInfos.
750 */
751 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
752 final long container, final int screen) {
753
754 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
755 int count = items.size();
756
757 for (int i = 0; i < count; i++) {
758 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700759 item.container = container;
760
761 // We store hotseat items in canonical form which is this orientation invariant position
762 // in the hotseat
763 if (context instanceof Launcher && screen < 0 &&
764 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 item.cellY);
767 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700768 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700769 }
770
771 final ContentValues values = new ContentValues();
772 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
773 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
774 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700776
777 contentValues.add(values);
778 }
779 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
780 }
781
782 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800784 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700787 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800788 item.cellX = cellX;
789 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700790 item.spanX = spanX;
791 item.spanY = spanY;
792
793 // We store hotseat items in canonical form which is this orientation invariant position
794 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700798 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700800 }
Adam Cohend4844c32011-02-18 19:25:06 -0800801
Adam Cohend4844c32011-02-18 19:25:06 -0800802 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800803 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700804 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
805 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
806 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
807 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700808 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800809
Michael Jurka816474f2012-06-25 14:49:02 -0700810 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700811 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700812
813 /**
814 * Update an item to the database in a specified container.
815 */
816 static void updateItemInDatabase(Context context, final ItemInfo item) {
817 final ContentValues values = new ContentValues();
818 item.onAddToDatabase(values);
819 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
820 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800821 }
822
823 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824 * Returns true if the shortcuts already exists in the database.
825 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 static boolean shortcutExists(Context context, String title, Intent intent) {
828 final ContentResolver cr = context.getContentResolver();
829 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
830 new String[] { "title", "intent" }, "title=? and intent=?",
831 new String[] { title, intent.toUri(0) }, null);
832 boolean result = false;
833 try {
834 result = c.moveToFirst();
835 } finally {
836 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700839 }
840
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500842 * Returns true if the shortcuts already exists in the database.
843 * we identify a shortcut by the component name of the intent.
844 */
845 static boolean appWasRestored(Context context, Intent intent) {
846 final ContentResolver cr = context.getContentResolver();
847 final ComponentName component = intent.getComponent();
848 if (component == null) {
849 return false;
850 }
851 String componentName = component.flattenToString();
852 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
853 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
854 new String[]{"intent", "restored"}, where, null, null);
855 boolean result = false;
856 try {
857 result = c.moveToFirst();
858 } finally {
859 c.close();
860 }
861 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
862 return result;
863 }
864
865 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700866 * Returns an ItemInfo array containing all the items in the LauncherModel.
867 * The ItemInfo.id is not set through this function.
868 */
869 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
870 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
871 final ContentResolver cr = context.getContentResolver();
872 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
873 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
874 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
875 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
876
877 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
878 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
879 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
880 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
881 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
882 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
883 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
884
885 try {
886 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700887 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700888 item.cellX = c.getInt(cellXIndex);
889 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700890 item.spanX = Math.max(1, c.getInt(spanXIndex));
891 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700892 item.container = c.getInt(containerIndex);
893 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700894 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700895
896 items.add(item);
897 }
898 } catch (Exception e) {
899 items.clear();
900 } finally {
901 c.close();
902 }
903
904 return items;
905 }
906
907 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
909 */
910 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
911 final ContentResolver cr = context.getContentResolver();
912 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
913 "_id=? and (itemType=? or itemType=?)",
914 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700915 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700916
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 try {
918 if (c.moveToFirst()) {
919 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
920 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
921 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
922 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
923 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
924 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 FolderInfo folderInfo = null;
927 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700928 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
929 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700931 }
932
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 folderInfo.title = c.getString(titleIndex);
934 folderInfo.id = id;
935 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700937 folderInfo.cellX = c.getInt(cellXIndex);
938 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939
940 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 } finally {
943 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700944 }
945
946 return null;
947 }
948
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 /**
950 * Add an item to the database in a specified container. Sets the container, screen, cellX and
951 * cellY fields of the item. Also assigns an ID to the item.
952 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700953 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700954 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 item.cellX = cellX;
957 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700958 // We store hotseat items in canonical form which is this orientation invariant position
959 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700960 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700961 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700962 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966
967 final ContentValues values = new ContentValues();
968 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 item.onAddToDatabase(values);
970
Michael Jurka414300a2013-08-27 15:42:35 +0200971 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700972 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700973 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700974
Jason Monk8e19cf22014-03-20 15:06:57 -0400975 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700976 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700977 public void run() {
978 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
979 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700981 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700982 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400983 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700984 sBgItemsIdMap.put(item.id, item);
985 switch (item.itemType) {
986 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
987 sBgFolders.put(item.id, (FolderInfo) item);
988 // Fall through
989 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
990 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
991 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
992 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
993 sBgWorkspaceItems.add(item);
994 } else {
995 if (!sBgFolders.containsKey(item.container)) {
996 // Adding an item to a folder that doesn't exist.
997 String msg = "adding item: " + item + " to a folder that " +
998 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700999 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001000 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001001 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001002 break;
1003 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1004 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1005 break;
1006 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001007 }
1008 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001009 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001010 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001011 }
1012
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001014 * Creates a new unique child id, for a given cell span across all layouts.
1015 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001016 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001017 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001019 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001020 }
1021
Winson Chungaafa03c2010-06-11 17:34:16 -07001022 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001023 * Removes the specified item from the database
1024 * @param context
1025 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001027 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001029 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001030
Michael Jurka83df1882011-08-31 20:59:26 -07001031 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001032 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001033 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001034
1035 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001036 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001037 switch (item.itemType) {
1038 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1039 sBgFolders.remove(item.id);
1040 for (ItemInfo info: sBgItemsIdMap.values()) {
1041 if (info.container == item.id) {
1042 // We are deleting a folder which still contains items that
1043 // think they are contained by that folder.
1044 String msg = "deleting a folder (" + item + ") which still " +
1045 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001046 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001047 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001048 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001049 sBgWorkspaceItems.remove(item);
1050 break;
1051 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1052 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1053 sBgWorkspaceItems.remove(item);
1054 break;
1055 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1056 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1057 break;
1058 }
1059 sBgItemsIdMap.remove(item.id);
1060 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001061 }
1062 }
Michael Jurka83df1882011-08-31 20:59:26 -07001063 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001064 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 }
1066
1067 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 * Update the order of the workspace screens in the database. The array list contains
1069 * a list of screen ids in the order that they should appear.
1070 */
Winson Chungc9168342013-06-26 14:54:55 -07001071 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001072 // Log to disk
1073 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1074 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1075
Winson Chung64359a52013-07-08 17:17:08 -07001076 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001077 final ContentResolver cr = context.getContentResolver();
1078 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1079
1080 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001081 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001082 while (iter.hasNext()) {
1083 long id = iter.next();
1084 if (id < 0) {
1085 iter.remove();
1086 }
1087 }
1088
1089 Runnable r = new Runnable() {
1090 @Override
1091 public void run() {
Yura085c8532014-02-11 15:15:29 +00001092 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001093 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001094 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001095 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 for (int i = 0; i < count; i++) {
1097 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001098 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001099 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1100 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001101 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001102 }
Yura085c8532014-02-11 15:15:29 +00001103
1104 try {
1105 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1106 } catch (Exception ex) {
1107 throw new RuntimeException(ex);
1108 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001109
Winson Chungba9c37f2013-08-30 14:11:37 -07001110 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001111 sBgWorkspaceScreens.clear();
1112 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001113 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 }
1115 };
1116 runOnWorkerThread(r);
1117 }
1118
1119 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001120 * Remove the contents of the specified folder from the database
1121 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001122 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 final ContentResolver cr = context.getContentResolver();
1124
Michael Jurkac9d95c52011-08-29 14:03:34 -07001125 Runnable r = new Runnable() {
1126 public void run() {
1127 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001129 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 sBgItemsIdMap.remove(info.id);
1131 sBgFolders.remove(info.id);
1132 sBgDbIconCache.remove(info);
1133 sBgWorkspaceItems.remove(info);
1134 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001135
Michael Jurkac9d95c52011-08-29 14:03:34 -07001136 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1137 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001138 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001139 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001140 for (ItemInfo childInfo : info.contents) {
1141 sBgItemsIdMap.remove(childInfo.id);
1142 sBgDbIconCache.remove(childInfo);
1143 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001144 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001145 }
1146 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001147 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148 }
1149
1150 /**
1151 * Set this as the current Launcher activity object for the loader.
1152 */
1153 public void initialize(Callbacks callbacks) {
1154 synchronized (mLock) {
1155 mCallbacks = new WeakReference<Callbacks>(callbacks);
1156 }
1157 }
1158
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001159 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001160 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1161 * ACTION_PACKAGE_CHANGED.
1162 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001163 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001164 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001165 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001166
Joe Onorato36115782010-06-17 13:28:48 -04001167 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001168
Joe Onorato36115782010-06-17 13:28:48 -04001169 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1170 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1171 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1172 final String packageName = intent.getData().getSchemeSpecificPart();
1173 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001174
Joe Onorato36115782010-06-17 13:28:48 -04001175 int op = PackageUpdatedTask.OP_NONE;
1176
1177 if (packageName == null || packageName.length() == 0) {
1178 // they sent us a bad intent
1179 return;
1180 }
1181
1182 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1183 op = PackageUpdatedTask.OP_UPDATE;
1184 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1185 if (!replacing) {
1186 op = PackageUpdatedTask.OP_REMOVE;
1187 }
1188 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1189 // later, we will update the package at this time
1190 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1191 if (!replacing) {
1192 op = PackageUpdatedTask.OP_ADD;
1193 } else {
1194 op = PackageUpdatedTask.OP_UPDATE;
1195 }
1196 }
1197
1198 if (op != PackageUpdatedTask.OP_NONE) {
1199 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1200 }
1201
1202 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001203 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001204 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001205 if (!replacing) {
1206 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1207 if (mAppsCanBeOnRemoveableStorage) {
1208 // Only rebind if we support removable storage. It catches the case where
1209 // apps on the external sd card need to be reloaded
1210 startLoaderFromBackground();
1211 }
1212 } else {
1213 // If we are replacing then just update the packages in the list
1214 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1215 packages));
1216 }
Joe Onorato36115782010-06-17 13:28:48 -04001217 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001218 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1219 if (!replacing) {
1220 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1221 enqueuePackageUpdated(new PackageUpdatedTask(
1222 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1223 }
1224 // else, we are replacing the packages, so ignore this event and wait for
1225 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001226 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001227 // If we have changed locale we need to clear out the labels in all apps/workspace.
1228 forceReload();
1229 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1230 // Check if configuration change was an mcc/mnc change which would affect app resources
1231 // and we would need to clear out the labels in all apps/workspace. Same handling as
1232 // above for ACTION_LOCALE_CHANGED
1233 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001234 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001235 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001236 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001237 forceReload();
1238 }
1239 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001240 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001241 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1242 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001243 if (mCallbacks != null) {
1244 Callbacks callbacks = mCallbacks.get();
1245 if (callbacks != null) {
1246 callbacks.bindSearchablesChanged();
1247 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001248 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001249 }
1250 }
1251
Reena Lee93f824a2011-09-23 17:20:28 -07001252 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001253 resetLoadedState(true, true);
1254
Reena Lee93f824a2011-09-23 17:20:28 -07001255 // Do this here because if the launcher activity is running it will be restarted.
1256 // If it's not running startLoaderFromBackground will merely tell it that it needs
1257 // to reload.
1258 startLoaderFromBackground();
1259 }
1260
Winson Chungf0c6ae02012-03-21 16:10:31 -07001261 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1262 synchronized (mLock) {
1263 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1264 // mWorkspaceLoaded to true later
1265 stopLoaderLocked();
1266 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1267 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1268 }
1269 }
1270
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001271 /**
1272 * When the launcher is in the background, it's possible for it to miss paired
1273 * configuration changes. So whenever we trigger the loader from the background
1274 * tell the launcher that it needs to re-run the loader when it comes back instead
1275 * of doing it now.
1276 */
1277 public void startLoaderFromBackground() {
1278 boolean runLoader = false;
1279 if (mCallbacks != null) {
1280 Callbacks callbacks = mCallbacks.get();
1281 if (callbacks != null) {
1282 // Only actually run the loader if they're not paused.
1283 if (!callbacks.setLoadOnResume()) {
1284 runLoader = true;
1285 }
1286 }
1287 }
1288 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001289 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001290 }
Joe Onorato36115782010-06-17 13:28:48 -04001291 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001292
Reena Lee93f824a2011-09-23 17:20:28 -07001293 // If there is already a loader task running, tell it to stop.
1294 // returns true if isLaunching() was true on the old task
1295 private boolean stopLoaderLocked() {
1296 boolean isLaunching = false;
1297 LoaderTask oldTask = mLoaderTask;
1298 if (oldTask != null) {
1299 if (oldTask.isLaunching()) {
1300 isLaunching = true;
1301 }
1302 oldTask.stopLocked();
1303 }
1304 return isLaunching;
1305 }
1306
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001307 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001308 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1309 }
1310
1311 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001312 synchronized (mLock) {
1313 if (DEBUG_LOADERS) {
1314 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1315 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001317 // Clear any deferred bind-runnables from the synchronized load process
1318 // We must do this before any loading/binding is scheduled below.
1319 mDeferredBindRunnables.clear();
1320
Joe Onorato36115782010-06-17 13:28:48 -04001321 // Don't bother to start the thread if we know it's not going to do anything
1322 if (mCallbacks != null && mCallbacks.get() != null) {
1323 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001324 // also, don't downgrade isLaunching if we're already running
1325 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001326 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001327 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1328 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001329 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1330 } else {
1331 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1332 sWorker.post(mLoaderTask);
1333 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001334 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001335 }
1336 }
1337
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001338 void bindRemainingSynchronousPages() {
1339 // Post the remaining side pages to be loaded
1340 if (!mDeferredBindRunnables.isEmpty()) {
1341 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001342 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001343 }
1344 mDeferredBindRunnables.clear();
1345 }
1346 }
1347
Joe Onorato36115782010-06-17 13:28:48 -04001348 public void stopLoader() {
1349 synchronized (mLock) {
1350 if (mLoaderTask != null) {
1351 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001352 }
1353 }
Joe Onorato36115782010-06-17 13:28:48 -04001354 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355
Winson Chung76828c82013-08-19 15:43:29 -07001356 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1357 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1358 final ContentResolver contentResolver = context.getContentResolver();
1359 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1360 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1361 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1362
1363 try {
1364 final int idIndex = sc.getColumnIndexOrThrow(
1365 LauncherSettings.WorkspaceScreens._ID);
1366 final int rankIndex = sc.getColumnIndexOrThrow(
1367 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1368 while (sc.moveToNext()) {
1369 try {
1370 long screenId = sc.getLong(idIndex);
1371 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001372 orderedScreens.put(rank, screenId);
1373 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001374 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001375 }
1376 }
1377 } finally {
1378 sc.close();
1379 }
Winson Chunga90303b2013-11-15 13:05:06 -08001380
1381 // Log to disk
1382 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1383 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1384 for (Integer i : orderedScreens.keySet()) {
1385 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1386 }
1387 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1388 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001389 return orderedScreens;
1390 }
1391
Michael Jurkac57b7a82011-08-09 22:02:20 -07001392 public boolean isAllAppsLoaded() {
1393 return mAllAppsLoaded;
1394 }
1395
Winson Chung36a62fe2012-05-06 18:04:42 -07001396 boolean isLoadingWorkspace() {
1397 synchronized (mLock) {
1398 if (mLoaderTask != null) {
1399 return mLoaderTask.isLoadingWorkspace();
1400 }
1401 }
1402 return false;
1403 }
1404
Joe Onorato36115782010-06-17 13:28:48 -04001405 /**
1406 * Runnable for the thread that loads the contents of the launcher:
1407 * - workspace icons
1408 * - widgets
1409 * - all apps icons
1410 */
1411 private class LoaderTask implements Runnable {
1412 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001413 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001414 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001415 private boolean mStopped;
1416 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001417 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418
Winson Chungc3eecff2011-07-11 17:44:15 -07001419 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001420
Dan Sandlerd5024042014-01-09 15:01:33 -05001421 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001422 mContext = context;
1423 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001424 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001425 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 }
1427
Joe Onorato36115782010-06-17 13:28:48 -04001428 boolean isLaunching() {
1429 return mIsLaunching;
1430 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431
Winson Chung36a62fe2012-05-06 18:04:42 -07001432 boolean isLoadingWorkspace() {
1433 return mIsLoadingAndBindingWorkspace;
1434 }
1435
Winson Chungc763c4e2013-07-19 13:49:06 -07001436 /** Returns whether this is an upgrade path */
1437 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001438 mIsLoadingAndBindingWorkspace = true;
1439
Joe Onorato36115782010-06-17 13:28:48 -04001440 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001441 if (DEBUG_LOADERS) {
1442 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001444
Winson Chungc763c4e2013-07-19 13:49:06 -07001445 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001446 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001447 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001448 synchronized (LoaderTask.this) {
1449 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001450 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001451 }
1452 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 }
1454 }
1455
Joe Onorato36115782010-06-17 13:28:48 -04001456 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001457 bindWorkspace(-1, isUpgradePath);
1458 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001459 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001460
Joe Onorato36115782010-06-17 13:28:48 -04001461 private void waitForIdle() {
1462 // Wait until the either we're stopped or the other threads are done.
1463 // This way we don't start loading all apps until the workspace has settled
1464 // down.
1465 synchronized (LoaderTask.this) {
1466 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001467
Joe Onorato36115782010-06-17 13:28:48 -04001468 mHandler.postIdle(new Runnable() {
1469 public void run() {
1470 synchronized (LoaderTask.this) {
1471 mLoadAndBindStepFinished = true;
1472 if (DEBUG_LOADERS) {
1473 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001476 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001477 }
Joe Onorato36115782010-06-17 13:28:48 -04001478 });
1479
Michael Jurkac7700af2013-05-14 20:17:58 +02001480 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001481 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001482 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1483 // wait no longer than 1sec at a time
1484 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001485 } catch (InterruptedException ex) {
1486 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001487 }
1488 }
Joe Onorato36115782010-06-17 13:28:48 -04001489 if (DEBUG_LOADERS) {
1490 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001491 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001492 + "ms for previous step to finish binding");
1493 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001494 }
Joe Onorato36115782010-06-17 13:28:48 -04001495 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001496
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001497 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001498 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001499 // Ensure that we have a valid page index to load synchronously
1500 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1501 "valid page index");
1502 }
1503 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1504 // Ensure that we don't try and bind a specified page when the pages have not been
1505 // loaded already (we should load everything asynchronously in that case)
1506 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1507 }
1508 synchronized (mLock) {
1509 if (mIsLoaderTaskRunning) {
1510 // Ensure that we are never running the background loading at this point since
1511 // we also touch the background collections
1512 throw new RuntimeException("Error! Background loading is already running");
1513 }
1514 }
1515
1516 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1517 // data structures, we can't allow any other thread to touch that data, but because
1518 // this call is synchronous, we can get away with not locking).
1519
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001520 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001521 // operations from the previous activity. We need to ensure that all queued operations
1522 // are executed before any synchronous binding work is done.
1523 mHandler.flush();
1524
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001525 // Divide the set of loaded items into those that we are binding synchronously, and
1526 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001527 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1529 // arise from that.
1530 onlyBindAllApps();
1531 }
1532
Joe Onorato36115782010-06-17 13:28:48 -04001533 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001534 boolean isUpgrade = false;
1535
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001536 synchronized (mLock) {
1537 mIsLoaderTaskRunning = true;
1538 }
Joe Onorato36115782010-06-17 13:28:48 -04001539 // Optimize for end-user experience: if the Launcher is up and // running with the
1540 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1541 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001542 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001543 // Elevate priority when Home launches for the first time to avoid
1544 // starving at boot time. Staring at a blank home is not cool.
1545 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001546 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1547 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001548 android.os.Process.setThreadPriority(mIsLaunching
1549 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1550 }
Winson Chung64359a52013-07-08 17:17:08 -07001551 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001552 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001553
Joe Onorato36115782010-06-17 13:28:48 -04001554 if (mStopped) {
1555 break keep_running;
1556 }
1557
1558 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1559 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001560 synchronized (mLock) {
1561 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001562 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001563 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1564 }
1565 }
Joe Onorato36115782010-06-17 13:28:48 -04001566 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001567
1568 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001569 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1570 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001571
1572 // Restore the default thread priority after we are done loading items
1573 synchronized (mLock) {
1574 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1575 }
Joe Onorato36115782010-06-17 13:28:48 -04001576 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001577
Winson Chungaac01e12011-08-17 10:37:13 -07001578 // Update the saved icons if necessary
1579 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001580 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001581 for (Object key : sBgDbIconCache.keySet()) {
1582 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1583 }
1584 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001585 }
Winson Chungaac01e12011-08-17 10:37:13 -07001586
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001587 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001588 // Ensure that all the applications that are in the system are
1589 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001590 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001591 verifyApplications();
1592 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001593 }
1594
Joe Onorato36115782010-06-17 13:28:48 -04001595 // Clear out this reference, otherwise we end up holding it until all of the
1596 // callback runnables are done.
1597 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001598
Joe Onorato36115782010-06-17 13:28:48 -04001599 synchronized (mLock) {
1600 // If we are still the last one to be scheduled, remove ourselves.
1601 if (mLoaderTask == this) {
1602 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001604 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001605 }
Joe Onorato36115782010-06-17 13:28:48 -04001606 }
1607
1608 public void stopLocked() {
1609 synchronized (LoaderTask.this) {
1610 mStopped = true;
1611 this.notify();
1612 }
1613 }
1614
1615 /**
1616 * Gets the callbacks object. If we've been stopped, or if the launcher object
1617 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1618 * object that was around when the deferred message was scheduled, and if there's
1619 * a new Callbacks object around then also return null. This will save us from
1620 * calling onto it with data that will be ignored.
1621 */
1622 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1623 synchronized (mLock) {
1624 if (mStopped) {
1625 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001626 }
Joe Onorato36115782010-06-17 13:28:48 -04001627
1628 if (mCallbacks == null) {
1629 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001630 }
Joe Onorato36115782010-06-17 13:28:48 -04001631
1632 final Callbacks callbacks = mCallbacks.get();
1633 if (callbacks != oldCallbacks) {
1634 return null;
1635 }
1636 if (callbacks == null) {
1637 Log.w(TAG, "no mCallbacks");
1638 return null;
1639 }
1640
1641 return callbacks;
1642 }
1643 }
1644
Winson Chungc763c4e2013-07-19 13:49:06 -07001645 private void verifyApplications() {
1646 final Context context = mApp.getContext();
1647
1648 // Cross reference all the applications in our apps list with items in the workspace
1649 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001650 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001651 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001652 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001653 tmpInfos = getItemInfoForComponentName(app.componentName);
1654 if (tmpInfos.isEmpty()) {
1655 // We are missing an application icon, so add this to the workspace
1656 added.add(app);
1657 // This is a rare event, so lets log it
1658 Log.e(TAG, "Missing Application on load: " + app);
1659 }
1660 }
1661 }
1662 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001663 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001664 }
1665 }
1666
Joe Onorato36115782010-06-17 13:28:48 -04001667 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001668 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001669 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001670 LauncherAppState app = LauncherAppState.getInstance();
1671 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001672 final int countX = (int) grid.numColumns;
1673 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001674
Adam Cohendcd297f2013-06-18 13:13:40 -07001675 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001676 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001677 // Return early if we detect that an item is under the hotseat button
1678 if (mCallbacks == null ||
1679 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001680 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001681 Log.e(TAG, "Error loading shortcut into hotseat " + item
1682 + " into position (" + item.screenId + ":" + item.cellX + ","
1683 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001684 return false;
1685 }
1686
Dan Sandler295ae182013-12-10 16:05:47 -05001687 final ItemInfo[][] hotseatItems =
1688 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1689
Adam Cohenae4409d2013-11-26 10:34:59 -08001690 if (item.screenId >= grid.numHotseatIcons) {
1691 Log.e(TAG, "Error loading shortcut " + item
1692 + " into hotseat position " + item.screenId
1693 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1694 + ")");
1695 return false;
1696 }
1697
Dan Sandler295ae182013-12-10 16:05:47 -05001698 if (hotseatItems != null) {
1699 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001700 Log.e(TAG, "Error loading shortcut into hotseat " + item
1701 + " into position (" + item.screenId + ":" + item.cellX + ","
1702 + item.cellY + ") occupied by "
1703 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1704 [(int) item.screenId][0]);
1705 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001706 } else {
1707 hotseatItems[(int) item.screenId][0] = item;
1708 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001709 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001710 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001711 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001712 items[(int) item.screenId][0] = item;
1713 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001714 return true;
1715 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001716 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1717 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001718 return true;
1719 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001720
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001722 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001723 occupied.put(item.screenId, items);
1724 }
1725
Dan Sandler295ae182013-12-10 16:05:47 -05001726 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001727 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1728 item.cellX < 0 || item.cellY < 0 ||
1729 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1730 Log.e(TAG, "Error loading shortcut " + item
1731 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1732 + item.cellX + "," + item.cellY
1733 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1734 return false;
1735 }
1736
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001737 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001738 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1739 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001740 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001741 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001742 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001743 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001744 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001745 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001746 return false;
1747 }
1748 }
1749 }
1750 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1751 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001752 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001753 }
1754 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001755
Joe Onorato36115782010-06-17 13:28:48 -04001756 return true;
1757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001758
Winson Chungba9c37f2013-08-30 14:11:37 -07001759 /** Clears all the sBg data structures */
1760 private void clearSBgDataStructures() {
1761 synchronized (sBgLock) {
1762 sBgWorkspaceItems.clear();
1763 sBgAppWidgets.clear();
1764 sBgFolders.clear();
1765 sBgItemsIdMap.clear();
1766 sBgDbIconCache.clear();
1767 sBgWorkspaceScreens.clear();
1768 }
1769 }
1770
Dan Sandlerd5024042014-01-09 15:01:33 -05001771 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001772 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001773 // Log to disk
1774 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1775
Joe Onorato36115782010-06-17 13:28:48 -04001776 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777
Joe Onorato36115782010-06-17 13:28:48 -04001778 final Context context = mContext;
1779 final ContentResolver contentResolver = context.getContentResolver();
1780 final PackageManager manager = context.getPackageManager();
1781 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1782 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001783
Winson Chung892c74d2013-08-22 16:15:50 -07001784 LauncherAppState app = LauncherAppState.getInstance();
1785 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1786 int countX = (int) grid.numColumns;
1787 int countY = (int) grid.numRows;
1788
Dan Sandlerd5024042014-01-09 15:01:33 -05001789 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1790 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1791 LauncherAppState.getLauncherProvider().deleteDatabase();
1792 }
1793
1794 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1795 // append the user's Launcher2 shortcuts
1796 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1797 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1798 } else {
1799 // Make sure the default workspace is loaded
1800 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1801 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1802 }
Adam Cohene25af792013-06-06 23:08:25 -07001803
Winson Chungc763c4e2013-07-19 13:49:06 -07001804 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001805 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001806 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001807
Winson Chung9f9f00b2013-11-15 13:27:00 -08001808 // Log to disk
1809 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001810
Winson Chung2abf94d2012-07-18 18:16:38 -07001811 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001812 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001813
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001814 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001815 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001816 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001817 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001818 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001819
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001820 // +1 for the hotseat (it can be larger than the workspace)
1821 // Load workspace in reverse order to ensure that latest items are loaded first (and
1822 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001823 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001825 try {
1826 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1827 final int intentIndex = c.getColumnIndexOrThrow
1828 (LauncherSettings.Favorites.INTENT);
1829 final int titleIndex = c.getColumnIndexOrThrow
1830 (LauncherSettings.Favorites.TITLE);
1831 final int iconTypeIndex = c.getColumnIndexOrThrow(
1832 LauncherSettings.Favorites.ICON_TYPE);
1833 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1834 final int iconPackageIndex = c.getColumnIndexOrThrow(
1835 LauncherSettings.Favorites.ICON_PACKAGE);
1836 final int iconResourceIndex = c.getColumnIndexOrThrow(
1837 LauncherSettings.Favorites.ICON_RESOURCE);
1838 final int containerIndex = c.getColumnIndexOrThrow(
1839 LauncherSettings.Favorites.CONTAINER);
1840 final int itemTypeIndex = c.getColumnIndexOrThrow(
1841 LauncherSettings.Favorites.ITEM_TYPE);
1842 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1843 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001844 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1845 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 final int screenIndex = c.getColumnIndexOrThrow(
1847 LauncherSettings.Favorites.SCREEN);
1848 final int cellXIndex = c.getColumnIndexOrThrow
1849 (LauncherSettings.Favorites.CELLX);
1850 final int cellYIndex = c.getColumnIndexOrThrow
1851 (LauncherSettings.Favorites.CELLY);
1852 final int spanXIndex = c.getColumnIndexOrThrow
1853 (LauncherSettings.Favorites.SPANX);
1854 final int spanYIndex = c.getColumnIndexOrThrow(
1855 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001856 final int restoredIndex = c.getColumnIndexOrThrow(
1857 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1859 //final int displayModeIndex = c.getColumnIndexOrThrow(
1860 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 ShortcutInfo info;
1863 String intentDescription;
1864 LauncherAppWidgetInfo appWidgetInfo;
1865 int container;
1866 long id;
1867 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001868
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001870 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001871 try {
1872 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001873 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001874
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001875 switch (itemType) {
1876 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1877 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001878 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001879 intentDescription = c.getString(intentIndex);
1880 try {
1881 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001882 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001883 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001884 if (restored) {
1885 // might be installed later
1886 Launcher.addDumpLog(TAG,
1887 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001888 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001889 if (!mAppsCanBeOnRemoveableStorage) {
1890 // Log the invalid package, and remove it
1891 Launcher.addDumpLog(TAG,
1892 "Invalid package removed: " + cn, true);
1893 itemsToRemove.add(id);
1894 } else {
1895 // If apps can be on external storage, then we just
1896 // leave them for the user to remove (maybe add
1897 // visual treatment to it)
1898 Launcher.addDumpLog(TAG,
1899 "Invalid package found: " + cn, true);
1900 }
1901 continue;
Winson Chungee055712013-07-30 14:46:24 -07001902 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001903 } else if (restored) {
1904 // no special handling necessary for this restored item
1905 restoredRows.add(id);
1906 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001907 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001908 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001909 Launcher.addDumpLog(TAG,
1910 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001911 continue;
1912 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001913
Chris Wrenf4d08112014-01-16 18:13:56 -05001914 if (restored) {
1915 Launcher.addDumpLog(TAG,
1916 "constructing info for partially restored package",
1917 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001918 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001919 intent = getRestoredItemIntent(c, context, intent);
1920 } else if (itemType ==
1921 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1923 titleIndex, mLabelCache);
1924 } else {
1925 info = getShortcutInfo(c, context, iconTypeIndex,
1926 iconPackageIndex, iconResourceIndex, iconIndex,
1927 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001928
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001929 // App shortcuts that used to be automatically added to Launcher
1930 // didn't always have the correct intent flags set, so do that
1931 // here
1932 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001933 intent.getCategories() != null &&
1934 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001935 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001936 intent.addFlags(
1937 Intent.FLAG_ACTIVITY_NEW_TASK |
1938 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1939 }
Michael Jurka96879562012-03-22 05:54:33 -07001940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001942 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001943 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001944 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001945 container = c.getInt(containerIndex);
1946 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001948 info.cellX = c.getInt(cellXIndex);
1949 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001950 info.spanX = 1;
1951 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001952
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001953 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001954 deleteOnInvalidPlacement.set(false);
1955 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1956 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001957 itemsToRemove.add(id);
1958 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001959 break;
1960 }
1961
1962 switch (container) {
1963 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1964 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1965 sBgWorkspaceItems.add(info);
1966 break;
1967 default:
1968 // Item is in a user folder
1969 FolderInfo folderInfo =
1970 findOrMakeFolder(sBgFolders, container);
1971 folderInfo.add(info);
1972 break;
1973 }
1974 sBgItemsIdMap.put(info.id, info);
1975
1976 // now that we've loaded everthing re-save it with the
1977 // icon in case it disappears somehow.
1978 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001979 } else {
1980 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 }
1982 break;
1983
1984 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1985 id = c.getLong(idIndex);
1986 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1987
1988 folderInfo.title = c.getString(titleIndex);
1989 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001992 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 folderInfo.cellX = c.getInt(cellXIndex);
1994 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001995 folderInfo.spanX = 1;
1996 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997
Daniel Sandler8802e962010-05-26 16:28:16 -04001998 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001999 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002000 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002001 deleteOnInvalidPlacement)) {
2002 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002003 itemsToRemove.add(id);
2004 }
Daniel Sandler8802e962010-05-26 16:28:16 -04002005 break;
2006 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002007
Joe Onorato9c1289c2009-08-17 11:03:03 -04002008 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2010 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2011 sBgWorkspaceItems.add(folderInfo);
2012 break;
Joe Onorato36115782010-06-17 13:28:48 -04002013 }
Joe Onorato17a89222011-02-08 17:26:11 -08002014
Chris Wrenf4d08112014-01-16 18:13:56 -05002015 if (restored) {
2016 // no special handling required for restored folders
2017 restoredRows.add(id);
2018 }
2019
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2021 sBgFolders.put(folderInfo.id, folderInfo);
2022 break;
2023
2024 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2025 // Read all Launcher-specific widget details
2026 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002027 String savedProvider = c.getString(appWidgetProviderIndex);
2028
Joe Onorato36115782010-06-17 13:28:48 -04002029 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002030
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 final AppWidgetProviderInfo provider =
2032 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002033
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002034 if (!isSafeMode && (provider == null || provider.provider == null ||
2035 provider.provider.getPackageName() == null)) {
2036 String log = "Deleting widget that isn't installed anymore: id="
2037 + id + " appWidgetId=" + appWidgetId;
2038 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002039 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 itemsToRemove.add(id);
2041 } else {
2042 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2043 provider.provider);
2044 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002045 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046 appWidgetInfo.cellX = c.getInt(cellXIndex);
2047 appWidgetInfo.cellY = c.getInt(cellYIndex);
2048 appWidgetInfo.spanX = c.getInt(spanXIndex);
2049 appWidgetInfo.spanY = c.getInt(spanYIndex);
2050 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2051 appWidgetInfo.minSpanX = minSpan[0];
2052 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002053
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 container = c.getInt(containerIndex);
2055 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2056 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2057 Log.e(TAG, "Widget found where container != " +
2058 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2059 continue;
2060 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061
Adam Cohene25af792013-06-06 23:08:25 -07002062 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002064 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002065 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002066 deleteOnInvalidPlacement)) {
2067 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002068 itemsToRemove.add(id);
2069 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070 break;
2071 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002072 String providerName = provider.provider.flattenToString();
2073 if (!providerName.equals(savedProvider)) {
2074 ContentValues values = new ContentValues();
2075 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2076 providerName);
2077 String where = BaseColumns._ID + "= ?";
2078 String[] args = {Integer.toString(c.getInt(idIndex))};
2079 contentResolver.update(contentUri, values, where, args);
2080 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2082 sBgAppWidgets.add(appWidgetInfo);
2083 }
Joe Onorato36115782010-06-17 13:28:48 -04002084 break;
2085 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002087 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002088 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 }
2090 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002091 if (c != null) {
2092 c.close();
2093 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 }
2095
Winson Chungba9c37f2013-08-30 14:11:37 -07002096 // Break early if we've stopped loading
2097 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002098 clearSBgDataStructures();
2099 return false;
2100 }
2101
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 if (itemsToRemove.size() > 0) {
2103 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002104 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002105 // Remove dead items
2106 for (long id : itemsToRemove) {
2107 if (DEBUG_LOADERS) {
2108 Log.d(TAG, "Removed id = " + id);
2109 }
2110 // Don't notify content observers
2111 try {
2112 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2113 null, null);
2114 } catch (RemoteException e) {
2115 Log.w(TAG, "Could not remove id = " + id);
2116 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002117 }
2118 }
2119
Chris Wrenf4d08112014-01-16 18:13:56 -05002120 if (restoredRows.size() > 0) {
2121 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2122 LauncherSettings.Favorites.CONTENT_URI);
2123 // Update restored items that no longer require special handling
2124 try {
2125 StringBuilder selectionBuilder = new StringBuilder();
2126 selectionBuilder.append(LauncherSettings.Favorites._ID);
2127 selectionBuilder.append(" IN (");
2128 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2129 selectionBuilder.append(")");
2130 ContentValues values = new ContentValues();
2131 values.put(LauncherSettings.Favorites.RESTORED, 0);
2132 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2133 values, selectionBuilder.toString(), null);
2134 } catch (RemoteException e) {
2135 Log.w(TAG, "Could not update restored rows");
2136 }
2137 }
2138
Winson Chungc763c4e2013-07-19 13:49:06 -07002139 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002140 long maxScreenId = 0;
2141 // If we're importing we use the old screen order.
2142 for (ItemInfo item: sBgItemsIdMap.values()) {
2143 long screenId = item.screenId;
2144 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2145 !sBgWorkspaceScreens.contains(screenId)) {
2146 sBgWorkspaceScreens.add(screenId);
2147 if (screenId > maxScreenId) {
2148 maxScreenId = screenId;
2149 }
2150 }
2151 }
2152 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002153 // Log to disk
2154 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2155 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2156 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002157
Michael Jurka414300a2013-08-27 15:42:35 +02002158 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002159 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002160
2161 // Update the max item id after we load an old db
2162 long maxItemId = 0;
2163 // If we're importing we use the old screen order.
2164 for (ItemInfo item: sBgItemsIdMap.values()) {
2165 maxItemId = Math.max(maxItemId, item.id);
2166 }
Michael Jurka414300a2013-08-27 15:42:35 +02002167 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002168 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002169 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2170 for (Integer i : orderedScreens.keySet()) {
2171 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002172 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002173 // Log to disk
2174 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2175 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002176
2177 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002178 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 for (ItemInfo item: sBgItemsIdMap.values()) {
2180 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2182 unusedScreens.contains(screenId)) {
2183 unusedScreens.remove(screenId);
2184 }
2185 }
2186
2187 // If there are any empty screens remove them, and update.
2188 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002189 // Log to disk
2190 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2191 TextUtils.join(", ", unusedScreens), true);
2192
Winson Chung933bae62013-08-29 11:42:30 -07002193 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002194 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2195 }
2196 }
2197
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002198 if (DEBUG_LOADERS) {
2199 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2200 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002201 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002202 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002204
Daniel Sandler566da102013-06-25 23:43:45 -04002205 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002206 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002207 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002208 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 line += " | ";
2210 }
Winson Chung892c74d2013-08-22 16:15:50 -07002211 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002212 ItemInfo[][] screen = occupied.get(screenId);
2213 if (x < screen.length && y < screen[x].length) {
2214 line += (screen[x][y] != null) ? "#" : ".";
2215 } else {
2216 line += "!";
2217 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 }
Joe Onorato36115782010-06-17 13:28:48 -04002219 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002221 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002222 }
Joe Onorato36115782010-06-17 13:28:48 -04002223 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002224 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002225 }
2226
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 /** Filters the set of items who are directly or indirectly (via another container) on the
2228 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002229 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 ArrayList<ItemInfo> allWorkspaceItems,
2231 ArrayList<ItemInfo> currentScreenItems,
2232 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002233 // Purge any null ItemInfos
2234 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2235 while (iter.hasNext()) {
2236 ItemInfo i = iter.next();
2237 if (i == null) {
2238 iter.remove();
2239 }
2240 }
2241
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 // Order the set of items by their containers first, this allows use to walk through the
2243 // list sequentially, build up a list of containers that are in the specified screen,
2244 // as well as all items in those containers.
2245 Set<Long> itemsOnScreen = new HashSet<Long>();
2246 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2247 @Override
2248 public int compare(ItemInfo lhs, ItemInfo rhs) {
2249 return (int) (lhs.container - rhs.container);
2250 }
2251 });
2252 for (ItemInfo info : allWorkspaceItems) {
2253 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002254 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002255 currentScreenItems.add(info);
2256 itemsOnScreen.add(info.id);
2257 } else {
2258 otherScreenItems.add(info);
2259 }
2260 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2261 currentScreenItems.add(info);
2262 itemsOnScreen.add(info.id);
2263 } else {
2264 if (itemsOnScreen.contains(info.container)) {
2265 currentScreenItems.add(info);
2266 itemsOnScreen.add(info.id);
2267 } else {
2268 otherScreenItems.add(info);
2269 }
2270 }
2271 }
2272 }
2273
2274 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002275 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 ArrayList<LauncherAppWidgetInfo> appWidgets,
2277 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2278 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002279
2280 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002281 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002283 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 currentScreenWidgets.add(widget);
2285 } else {
2286 otherScreenWidgets.add(widget);
2287 }
2288 }
2289 }
2290
2291 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002292 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002293 HashMap<Long, ItemInfo> itemsIdMap,
2294 HashMap<Long, FolderInfo> folders,
2295 HashMap<Long, FolderInfo> currentScreenFolders,
2296 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297
2298 for (long id : folders.keySet()) {
2299 ItemInfo info = itemsIdMap.get(id);
2300 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002301 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002303 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002304 currentScreenFolders.put(id, folder);
2305 } else {
2306 otherScreenFolders.put(id, folder);
2307 }
2308 }
2309 }
2310
2311 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2312 * right) */
2313 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002314 final LauncherAppState app = LauncherAppState.getInstance();
2315 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316 // XXX: review this
2317 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002318 @Override
2319 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002320 int cellCountX = (int) grid.numColumns;
2321 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002322 int screenOffset = cellCountX * cellCountY;
2323 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002325 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002327 rhs.cellY * cellCountX + rhs.cellX);
2328 return (int) (lr - rr);
2329 }
2330 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002332
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2334 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002335 final Runnable r = new Runnable() {
2336 @Override
2337 public void run() {
2338 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2339 if (callbacks != null) {
2340 callbacks.bindScreens(orderedScreens);
2341 }
2342 }
2343 };
2344 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2345 }
2346
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2348 final ArrayList<ItemInfo> workspaceItems,
2349 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2350 final HashMap<Long, FolderInfo> folders,
2351 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002352
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 final boolean postOnMainThread = (deferredBindRunnables != null);
2354
2355 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002356 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002357 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002358 final int start = i;
2359 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002360 final Runnable r = new Runnable() {
2361 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002362 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002363 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002364 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002365 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2366 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002367 }
2368 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 };
2370 if (postOnMainThread) {
2371 deferredBindRunnables.add(r);
2372 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002373 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 }
Joe Onorato36115782010-06-17 13:28:48 -04002375 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376
2377 // Bind the folders
2378 if (!folders.isEmpty()) {
2379 final Runnable r = new Runnable() {
2380 public void run() {
2381 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2382 if (callbacks != null) {
2383 callbacks.bindFolders(folders);
2384 }
2385 }
2386 };
2387 if (postOnMainThread) {
2388 deferredBindRunnables.add(r);
2389 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002390 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 }
2392 }
2393
2394 // Bind the widgets, one at a time
2395 N = appWidgets.size();
2396 for (int i = 0; i < N; i++) {
2397 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2398 final Runnable r = new Runnable() {
2399 public void run() {
2400 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2401 if (callbacks != null) {
2402 callbacks.bindAppWidget(widget);
2403 }
2404 }
2405 };
2406 if (postOnMainThread) {
2407 deferredBindRunnables.add(r);
2408 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002409 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002410 }
2411 }
2412 }
2413
2414 /**
2415 * Binds all loaded data to actual views on the main thread.
2416 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002417 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 final long t = SystemClock.uptimeMillis();
2419 Runnable r;
2420
2421 // Don't use these two variables in any of the callback runnables.
2422 // Otherwise we hold a reference to them.
2423 final Callbacks oldCallbacks = mCallbacks.get();
2424 if (oldCallbacks == null) {
2425 // This launcher has exited and nobody bothered to tell us. Just bail.
2426 Log.w(TAG, "LoaderTask running with no launcher");
2427 return;
2428 }
2429
Winson Chung9b9fb962013-11-15 15:39:34 -08002430 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2432 ArrayList<LauncherAppWidgetInfo> appWidgets =
2433 new ArrayList<LauncherAppWidgetInfo>();
2434 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2435 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002436 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002437 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438 workspaceItems.addAll(sBgWorkspaceItems);
2439 appWidgets.addAll(sBgAppWidgets);
2440 folders.putAll(sBgFolders);
2441 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002442 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443 }
2444
Derek Prothro7aff3992013-12-10 14:00:37 -05002445 final boolean isLoadingSynchronously =
2446 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002447 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002448 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002449 if (currScreen >= orderedScreenIds.size()) {
2450 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002451 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002452 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002453 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002454 final long currentScreenId = currentScreen < 0
2455 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002456
2457 // Load all the items that are on the current page first (and in the process, unbind
2458 // all the existing workspace items before we call startBinding() below.
2459 unbindWorkspaceItemsOnMainThread();
2460
2461 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002462 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2463 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2464 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2465 new ArrayList<LauncherAppWidgetInfo>();
2466 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2467 new ArrayList<LauncherAppWidgetInfo>();
2468 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2469 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2470
Winson Chung9b9fb962013-11-15 15:39:34 -08002471 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002473 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002475 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 otherFolders);
2477 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2478 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2479
2480 // Tell the workspace that we're about to start binding items
2481 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002482 public void run() {
2483 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2484 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002486 }
2487 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 };
Winson Chung81b52252012-08-27 15:34:29 -07002489 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002490
Adam Cohendcd297f2013-06-18 13:13:40 -07002491 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2492
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 // Load items on the current page
2494 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2495 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002496 if (isLoadingSynchronously) {
2497 r = new Runnable() {
2498 public void run() {
2499 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002500 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002501 callbacks.onPageBoundSynchronously(currentScreen);
2502 }
2503 }
2504 };
Winson Chung81b52252012-08-27 15:34:29 -07002505 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002506 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507
Winson Chung4a2afa32012-07-19 14:53:05 -07002508 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2509 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510 mDeferredBindRunnables.clear();
2511 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002512 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513
2514 // Tell the workspace that we're done binding items
2515 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002516 public void run() {
2517 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2518 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002519 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002520 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002521
Winson Chung98e030b2012-05-07 16:01:11 -07002522 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002523 if (DEBUG_LOADERS) {
2524 Log.d(TAG, "bound workspace in "
2525 + (SystemClock.uptimeMillis()-t) + "ms");
2526 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002527
2528 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002529 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002530 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002531 if (isLoadingSynchronously) {
2532 mDeferredBindRunnables.add(r);
2533 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002534 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002535 }
Joe Onorato36115782010-06-17 13:28:48 -04002536 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002537
Joe Onorato36115782010-06-17 13:28:48 -04002538 private void loadAndBindAllApps() {
2539 if (DEBUG_LOADERS) {
2540 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2541 }
2542 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002543 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002544 synchronized (LoaderTask.this) {
2545 if (mStopped) {
2546 return;
2547 }
2548 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002549 }
Joe Onorato36115782010-06-17 13:28:48 -04002550 } else {
2551 onlyBindAllApps();
2552 }
2553 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002554
Joe Onorato36115782010-06-17 13:28:48 -04002555 private void onlyBindAllApps() {
2556 final Callbacks oldCallbacks = mCallbacks.get();
2557 if (oldCallbacks == null) {
2558 // This launcher has exited and nobody bothered to tell us. Just bail.
2559 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2560 return;
2561 }
2562
2563 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002564 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002565 final ArrayList<AppInfo> list
2566 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002567 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002568 public void run() {
2569 final long t = SystemClock.uptimeMillis();
2570 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2571 if (callbacks != null) {
2572 callbacks.bindAllApplications(list);
2573 }
2574 if (DEBUG_LOADERS) {
2575 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2576 + (SystemClock.uptimeMillis()-t) + "ms");
2577 }
2578 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002579 };
2580 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002581 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002582 r.run();
2583 } else {
2584 mHandler.post(r);
2585 }
Joe Onorato36115782010-06-17 13:28:48 -04002586 }
2587
Winson Chung64359a52013-07-08 17:17:08 -07002588 private void loadAllApps() {
2589 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002590
Joe Onorato36115782010-06-17 13:28:48 -04002591 final Callbacks oldCallbacks = mCallbacks.get();
2592 if (oldCallbacks == null) {
2593 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002594 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002595 return;
2596 }
2597
Winson Chung64359a52013-07-08 17:17:08 -07002598 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002599 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2600 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2601
Winson Chung64359a52013-07-08 17:17:08 -07002602 // Clear the list of apps
2603 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002604
Winson Chung64359a52013-07-08 17:17:08 -07002605 // Query for the set of apps
2606 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2607 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2608 if (DEBUG_LOADERS) {
2609 Log.d(TAG, "queryIntentActivities took "
2610 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2611 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2612 }
2613 // Fail if we don't have any apps
2614 if (apps == null || apps.isEmpty()) {
2615 return;
2616 }
2617 // Sort the applications by name
2618 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2619 Collections.sort(apps,
2620 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2621 if (DEBUG_LOADERS) {
2622 Log.d(TAG, "sort took "
2623 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002624 }
2625
Winson Chung64359a52013-07-08 17:17:08 -07002626 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002627 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002628 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002629 // This builds the icon bitmaps.
2630 mBgAllAppsList.add(new AppInfo(packageManager, app,
2631 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002632 }
2633
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002634 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002635 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2636 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002637
2638 // Post callback on main thread
2639 mHandler.post(new Runnable() {
2640 public void run() {
2641 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002642 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002643 if (callbacks != null) {
2644 callbacks.bindAllApplications(added);
2645 if (DEBUG_LOADERS) {
2646 Log.d(TAG, "bound " + added.size() + " apps in "
2647 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2648 }
2649 } else {
2650 Log.i(TAG, "not binding apps: no Launcher activity");
2651 }
2652 }
2653 });
2654
Joe Onorato36115782010-06-17 13:28:48 -04002655 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002656 Log.d(TAG, "Icons processed in "
2657 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002658 }
2659 }
2660
2661 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002662 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002663 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2664 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2665 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2666 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2667 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2668 }
Joe Onorato36115782010-06-17 13:28:48 -04002669 }
2670 }
2671
2672 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002673 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002674 }
2675
2676 private class PackageUpdatedTask implements Runnable {
2677 int mOp;
2678 String[] mPackages;
2679
2680 public static final int OP_NONE = 0;
2681 public static final int OP_ADD = 1;
2682 public static final int OP_UPDATE = 2;
2683 public static final int OP_REMOVE = 3; // uninstlled
2684 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2685
2686
2687 public PackageUpdatedTask(int op, String[] packages) {
2688 mOp = op;
2689 mPackages = packages;
2690 }
2691
2692 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002693 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002694
2695 final String[] packages = mPackages;
2696 final int N = packages.length;
2697 switch (mOp) {
2698 case OP_ADD:
2699 for (int i=0; i<N; i++) {
2700 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Chris Wren6d0dde02014-02-10 12:16:54 -05002701 mIconCache.remove(packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002702 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002703 }
2704 break;
2705 case OP_UPDATE:
2706 for (int i=0; i<N; i++) {
2707 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002708 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002709 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002710 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002711 }
2712 break;
2713 case OP_REMOVE:
2714 case OP_UNAVAILABLE:
2715 for (int i=0; i<N; i++) {
2716 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002717 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002718 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002719 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002720 }
2721 break;
2722 }
2723
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002724 ArrayList<AppInfo> added = null;
2725 ArrayList<AppInfo> modified = null;
2726 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002727
Adam Cohen487f7dd2012-06-28 18:12:10 -07002728 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002729 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002730 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002731 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002732 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002733 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002734 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002735 }
Winson Chung5d55f332012-07-16 20:45:03 -07002736 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002737 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002738 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002739 }
2740
Joe Onorato36115782010-06-17 13:28:48 -04002741 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2742 if (callbacks == null) {
2743 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2744 return;
2745 }
2746
2747 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002748 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002749 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002750 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002751 addAndBindAddedWorkspaceApps(context, addedInfos);
2752 } else {
2753 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002754 }
Joe Onorato36115782010-06-17 13:28:48 -04002755 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002756
Joe Onorato36115782010-06-17 13:28:48 -04002757 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002758 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002759
2760 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002761 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002762 ArrayList<ItemInfo> infos =
2763 getItemInfoForComponentName(a.componentName);
2764 for (ItemInfo i : infos) {
2765 if (isShortcutInfoUpdateable(i)) {
2766 ShortcutInfo info = (ShortcutInfo) i;
2767 info.title = a.title.toString();
2768 updateItemInDatabase(context, info);
2769 }
2770 }
2771 }
2772
Joe Onorato36115782010-06-17 13:28:48 -04002773 mHandler.post(new Runnable() {
2774 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002775 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2776 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002777 callbacks.bindAppsUpdated(modifiedFinal);
2778 }
2779 }
2780 });
2781 }
Winson Chung83892cc2013-05-01 16:53:33 -07002782
Winson Chungdf95eb12013-10-16 14:57:07 -07002783 final ArrayList<String> removedPackageNames =
2784 new ArrayList<String>();
2785 if (mOp == OP_REMOVE) {
2786 // Mark all packages in the broadcast to be removed
2787 removedPackageNames.addAll(Arrays.asList(packages));
2788 } else if (mOp == OP_UPDATE) {
2789 // Mark disabled packages in the broadcast to be removed
2790 final PackageManager pm = context.getPackageManager();
2791 for (int i=0; i<N; i++) {
2792 if (isPackageDisabled(pm, packages[i])) {
2793 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002794 }
2795 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002796 }
2797 // Remove all the components associated with this package
2798 for (String pn : removedPackageNames) {
2799 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2800 for (ItemInfo i : infos) {
2801 deleteItemFromDatabase(context, i);
2802 }
2803 }
2804 // Remove all the specific components
2805 for (AppInfo a : removedApps) {
2806 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2807 for (ItemInfo i : infos) {
2808 deleteItemFromDatabase(context, i);
2809 }
2810 }
2811 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2812 // Remove any queued items from the install queue
2813 String spKey = LauncherAppState.getSharedPreferencesKey();
2814 SharedPreferences sp =
2815 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2816 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2817 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002818 mHandler.post(new Runnable() {
2819 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002820 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2821 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002822 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002823 }
2824 }
2825 });
Joe Onoratobe386092009-11-17 17:32:16 -08002826 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002827
Michael Jurkac402cd92013-05-20 15:49:32 +02002828 final ArrayList<Object> widgetsAndShortcuts =
2829 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002830 mHandler.post(new Runnable() {
2831 @Override
2832 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002833 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2834 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002835 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002836 }
2837 }
2838 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002839
2840 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002841 mHandler.post(new Runnable() {
2842 public void run() {
2843 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2844 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002845 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002846 }
2847 }
2848 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002849 }
2850 }
2851
Michael Jurkac402cd92013-05-20 15:49:32 +02002852 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2853 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2854 PackageManager packageManager = context.getPackageManager();
2855 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2856 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2857 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2858 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2859 Collections.sort(widgetsAndShortcuts,
2860 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2861 return widgetsAndShortcuts;
2862 }
2863
Adam Cohen556f6132014-01-15 15:18:08 -08002864 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002865 try {
2866 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2867 return !pi.applicationInfo.enabled;
2868 } catch (NameNotFoundException e) {
2869 // Fall through
2870 }
2871 return false;
2872 }
Adam Cohen556f6132014-01-15 15:18:08 -08002873
2874 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002875 if (cn == null) {
2876 return false;
2877 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002878 if (isPackageDisabled(pm, cn.getPackageName())) {
2879 return false;
2880 }
Winson Chungee055712013-07-30 14:46:24 -07002881
2882 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002883 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002884 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002885 return (pm.getActivityInfo(cn, 0) != null);
2886 } catch (NameNotFoundException e) {
2887 return false;
2888 }
2889 }
2890
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002892 * Make an ShortcutInfo object for a restored application or shortcut item that points
2893 * to a package that is not yet installed on the system.
2894 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002895 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002896 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002897 if (cursor != null) {
2898 info.title = cursor.getString(titleIndex);
2899 } else {
2900 info.title = "";
2901 }
Chris Wren6d0dde02014-02-10 12:16:54 -05002902 info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
Chris Wrenf4d08112014-01-16 18:13:56 -05002903 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002904 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002905 return info;
2906 }
2907
2908 /**
2909 * Make an Intent object for a restored application or shortcut item that points
2910 * to the market page for the item.
2911 */
2912 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002913 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002914 ComponentName componentName = intent.getComponent();
2915 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2916 Uri marketUri = new Uri.Builder()
2917 .scheme("market")
2918 .authority("details")
2919 .appendQueryParameter("id", componentName.getPackageName())
2920 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002921 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002922 marketIntent.setData(marketUri);
2923 return marketIntent;
2924 }
2925
2926 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002927 * This is called from the code that adds shortcuts from the intent receiver. This
2928 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002929 */
Joe Onorato56d82912010-03-07 14:32:10 -05002930 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002931 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002933
Joe Onorato56d82912010-03-07 14:32:10 -05002934 /**
2935 * Make an ShortcutInfo object for a shortcut that is an application.
2936 *
2937 * If c is not null, then it will be used to fill in missing data like the title and icon.
2938 */
2939 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002940 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002941 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002942 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002943 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002944 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002945 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002946 } else {
2947 try {
2948 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2949 info.initFlagsAndFirstInstallTime(pi);
2950 } catch (NameNotFoundException e) {
2951 Log.d(TAG, "getPackInfo failed for package " +
2952 componentName.getPackageName());
2953 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002954 }
2955
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002956 // TODO: See if the PackageManager knows about this case. If it doesn't
2957 // then return null & delete this.
2958
Joe Onorato56d82912010-03-07 14:32:10 -05002959 // the resource -- This may implicitly give us back the fallback icon,
2960 // but don't worry about that. All we're doing with usingFallbackIcon is
2961 // to avoid saving lots of copies of that in the database, and most apps
2962 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002963
2964 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2965 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2966 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002967 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002968 ResolveInfo resolveInfo = null;
2969 ComponentName oldComponent = intent.getComponent();
2970 Intent newIntent = new Intent(intent.getAction(), null);
2971 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2972 newIntent.setPackage(oldComponent.getPackageName());
2973 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2974 for (ResolveInfo i : infos) {
2975 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2976 i.activityInfo.name);
2977 if (cn.equals(oldComponent)) {
2978 resolveInfo = i;
2979 }
2980 }
2981 if (resolveInfo == null) {
2982 resolveInfo = manager.resolveActivity(intent, 0);
2983 }
Joe Onorato56d82912010-03-07 14:32:10 -05002984 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002985 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002986 }
Joe Onorato56d82912010-03-07 14:32:10 -05002987 // the db
2988 if (icon == null) {
2989 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002990 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002991 }
2992 }
2993 // the fallback icon
2994 if (icon == null) {
2995 icon = getFallbackIcon();
2996 info.usingFallbackIcon = true;
2997 }
2998 info.setIcon(icon);
2999
3000 // from the resource
3001 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003002 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
3003 if (labelCache != null && labelCache.containsKey(key)) {
3004 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07003005 } else {
3006 info.title = resolveInfo.activityInfo.loadLabel(manager);
3007 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003008 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003009 }
3010 }
Joe Onorato56d82912010-03-07 14:32:10 -05003011 }
3012 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003014 if (c != null) {
3015 info.title = c.getString(titleIndex);
3016 }
3017 }
3018 // fall back to the class name of the activity
3019 if (info.title == null) {
3020 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003021 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
3023 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003025
Winson Chung64359a52013-07-08 17:17:08 -07003026 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3027 ItemInfoFilter f) {
3028 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3029 for (ItemInfo i : infos) {
3030 if (i instanceof ShortcutInfo) {
3031 ShortcutInfo info = (ShortcutInfo) i;
3032 ComponentName cn = info.intent.getComponent();
3033 if (cn != null && f.filterItem(null, info, cn)) {
3034 filtered.add(info);
3035 }
3036 } else if (i instanceof FolderInfo) {
3037 FolderInfo info = (FolderInfo) i;
3038 for (ShortcutInfo s : info.contents) {
3039 ComponentName cn = s.intent.getComponent();
3040 if (cn != null && f.filterItem(info, s, cn)) {
3041 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003042 }
3043 }
Winson Chung64359a52013-07-08 17:17:08 -07003044 } else if (i instanceof LauncherAppWidgetInfo) {
3045 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3046 ComponentName cn = info.providerName;
3047 if (cn != null && f.filterItem(null, info, cn)) {
3048 filtered.add(info);
3049 }
Winson Chung8a435102012-08-30 17:16:53 -07003050 }
3051 }
Winson Chung64359a52013-07-08 17:17:08 -07003052 return new ArrayList<ItemInfo>(filtered);
3053 }
3054
3055 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07003056 ItemInfoFilter filter = new ItemInfoFilter() {
3057 @Override
3058 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3059 return cn.getPackageName().equals(pn);
3060 }
3061 };
3062 return filterItemInfos(sBgItemsIdMap.values(), filter);
3063 }
3064
3065 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003066 ItemInfoFilter filter = new ItemInfoFilter() {
3067 @Override
3068 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3069 return cn.equals(cname);
3070 }
3071 };
3072 return filterItemInfos(sBgItemsIdMap.values(), filter);
3073 }
3074
3075 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3076 if (i instanceof ShortcutInfo) {
3077 ShortcutInfo info = (ShortcutInfo) i;
3078 // We need to check for ACTION_MAIN otherwise getComponent() might
3079 // return null for some shortcuts (for instance, for shortcuts to
3080 // web pages.)
3081 Intent intent = info.intent;
3082 ComponentName name = intent.getComponent();
3083 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3084 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3085 return true;
3086 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003087 // placeholder shortcuts get special treatment, let them through too.
3088 if (info.getRestoredIntent() != null) {
3089 return true;
3090 }
Winson Chung64359a52013-07-08 17:17:08 -07003091 }
3092 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003093 }
3094
3095 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003096 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003098 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003099 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3100 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101
Joe Onorato56d82912010-03-07 14:32:10 -05003102 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003103 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003106 // TODO: If there's an explicit component and we can't install that, delete it.
3107
Joe Onorato56d82912010-03-07 14:32:10 -05003108 info.title = c.getString(titleIndex);
3109
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 int iconType = c.getInt(iconTypeIndex);
3111 switch (iconType) {
3112 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3113 String packageName = c.getString(iconPackageIndex);
3114 String resourceName = c.getString(iconResourceIndex);
3115 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003116 info.customIcon = false;
3117 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003119 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003120 if (resources != null) {
3121 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003122 icon = Utilities.createIconBitmap(
3123 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003124 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003125 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003126 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003127 }
Joe Onorato56d82912010-03-07 14:32:10 -05003128 // the db
3129 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003130 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003131 }
3132 // the fallback icon
3133 if (icon == null) {
3134 icon = getFallbackIcon();
3135 info.usingFallbackIcon = true;
3136 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003137 break;
3138 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003139 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003140 if (icon == null) {
3141 icon = getFallbackIcon();
3142 info.customIcon = false;
3143 info.usingFallbackIcon = true;
3144 } else {
3145 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 break;
3148 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003149 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003150 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003151 info.customIcon = false;
3152 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003154 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 return info;
3156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157
Michael Jurka931dc972011-08-05 15:08:15 -07003158 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003159 @SuppressWarnings("all") // suppress dead code warning
3160 final boolean debug = false;
3161 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003162 Log.d(TAG, "getIconFromCursor app="
3163 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3164 }
3165 byte[] data = c.getBlob(iconIndex);
3166 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003167 return Utilities.createIconBitmap(
3168 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003169 } catch (Exception e) {
3170 return null;
3171 }
3172 }
3173
Winson Chung3d503fb2011-07-13 17:25:49 -07003174 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3175 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003176 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003177 if (info == null) {
3178 return null;
3179 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003180 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003181
3182 return info;
3183 }
3184
Winson Chunga9abd0e2010-10-27 17:18:37 -07003185 /**
Winson Chung55cef262010-10-28 14:14:18 -07003186 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3187 */
3188 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3189 ComponentName component) {
3190 List<AppWidgetProviderInfo> widgets =
3191 AppWidgetManager.getInstance(context).getInstalledProviders();
3192 for (AppWidgetProviderInfo info : widgets) {
3193 if (info.provider.equals(component)) {
3194 return info;
3195 }
3196 }
3197 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003198 }
3199
Winson Chung68846fd2010-10-29 11:00:27 -07003200 /**
3201 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3202 */
3203 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3204 final PackageManager packageManager = context.getPackageManager();
3205 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3206 new ArrayList<WidgetMimeTypeHandlerData>();
3207
3208 final Intent supportsIntent =
3209 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3210 supportsIntent.setType(mimeType);
3211
3212 // Create a set of widget configuration components that we can test against
3213 final List<AppWidgetProviderInfo> widgets =
3214 AppWidgetManager.getInstance(context).getInstalledProviders();
3215 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3216 new HashMap<ComponentName, AppWidgetProviderInfo>();
3217 for (AppWidgetProviderInfo info : widgets) {
3218 configurationComponentToWidget.put(info.configure, info);
3219 }
3220
3221 // Run through each of the intents that can handle this type of clip data, and cross
3222 // reference them with the components that are actual configuration components
3223 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3224 PackageManager.MATCH_DEFAULT_ONLY);
3225 for (ResolveInfo info : activities) {
3226 final ActivityInfo activityInfo = info.activityInfo;
3227 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3228 activityInfo.name);
3229 if (configurationComponentToWidget.containsKey(infoComponent)) {
3230 supportedConfigurationActivities.add(
3231 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3232 configurationComponentToWidget.get(infoComponent)));
3233 }
3234 }
3235 return supportedConfigurationActivities;
3236 }
3237
Winson Chunga9abd0e2010-10-27 17:18:37 -07003238 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003239 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3240 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3241 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3242
Adam Cohend9198822011-11-22 16:42:47 -08003243 if (intent == null) {
3244 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3245 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3246 return null;
3247 }
3248
Joe Onorato0589f0f2010-02-08 13:44:00 -08003249 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003250 boolean customIcon = false;
3251 ShortcutIconResource iconResource = null;
3252
3253 if (bitmap != null && bitmap instanceof Bitmap) {
3254 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003255 customIcon = true;
3256 } else {
3257 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3258 if (extra != null && extra instanceof ShortcutIconResource) {
3259 try {
3260 iconResource = (ShortcutIconResource) extra;
3261 final PackageManager packageManager = context.getPackageManager();
3262 Resources resources = packageManager.getResourcesForApplication(
3263 iconResource.packageName);
3264 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003265 icon = Utilities.createIconBitmap(
3266 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003267 } catch (Exception e) {
3268 Log.w(TAG, "Could not load shortcut icon: " + extra);
3269 }
3270 }
3271 }
3272
Michael Jurkac9d95c52011-08-29 14:03:34 -07003273 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003274
3275 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003276 if (fallbackIcon != null) {
3277 icon = fallbackIcon;
3278 } else {
3279 icon = getFallbackIcon();
3280 info.usingFallbackIcon = true;
3281 }
Joe Onorato56d82912010-03-07 14:32:10 -05003282 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003283 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003284
Joe Onorato0589f0f2010-02-08 13:44:00 -08003285 info.title = name;
3286 info.intent = intent;
3287 info.customIcon = customIcon;
3288 info.iconResource = iconResource;
3289
3290 return info;
3291 }
3292
Winson Chungaac01e12011-08-17 10:37:13 -07003293 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3294 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003295 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003296 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003297 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003298 }
Joe Onorato56d82912010-03-07 14:32:10 -05003299 // If this icon doesn't have a custom icon, check to see
3300 // what's stored in the DB, and if it doesn't match what
3301 // we're going to show, store what we are going to show back
3302 // into the DB. We do this so when we're loading, if the
3303 // package manager can't find an icon (for example because
3304 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003305 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003306 cache.put(info, c.getBlob(iconIndex));
3307 return true;
3308 }
3309 return false;
3310 }
3311 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3312 boolean needSave = false;
3313 try {
3314 if (data != null) {
3315 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3316 Bitmap loaded = info.getIcon(mIconCache);
3317 needSave = !saved.sameAs(loaded);
3318 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003319 needSave = true;
3320 }
Winson Chungaac01e12011-08-17 10:37:13 -07003321 } catch (Exception e) {
3322 needSave = true;
3323 }
3324 if (needSave) {
3325 Log.d(TAG, "going to save icon bitmap for info=" + info);
3326 // This is slower than is ideal, but this only happens once
3327 // or when the app is updated with a new icon.
3328 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003329 }
3330 }
3331
Joe Onorato9c1289c2009-08-17 11:03:03 -04003332 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003333 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003334 * or make a new one.
3335 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003336 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003337 // See if a placeholder was created for us already
3338 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003339 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003340 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003341 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342 folders.put(id, folderInfo);
3343 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003344 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003345 }
3346
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003347 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003348 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003349 return new Comparator<AppInfo>() {
3350 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003351 int result = collator.compare(a.title.toString().trim(),
3352 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003353 if (result == 0) {
3354 result = a.componentName.compareTo(b.componentName);
3355 }
3356 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003357 }
Winson Chung11904872012-09-17 16:58:46 -07003358 };
3359 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003360 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3361 = new Comparator<AppInfo>() {
3362 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003363 if (a.firstInstallTime < b.firstInstallTime) return 1;
3364 if (a.firstInstallTime > b.firstInstallTime) return -1;
3365 return 0;
3366 }
3367 };
Winson Chung11904872012-09-17 16:58:46 -07003368 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3369 final Collator collator = Collator.getInstance();
3370 return new Comparator<AppWidgetProviderInfo>() {
3371 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003372 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003373 }
3374 };
3375 }
Winson Chung5308f242011-08-18 12:12:41 -07003376 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3377 if (info.activityInfo != null) {
3378 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3379 } else {
3380 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3381 }
3382 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003383 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003384 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003385 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003386 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003387 ShortcutNameComparator(PackageManager pm) {
3388 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003389 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003390 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003391 }
3392 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3393 mPackageManager = pm;
3394 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003395 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003396 }
3397 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003398 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003399 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3400 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3401 if (mLabelCache.containsKey(keyA)) {
3402 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003403 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003404 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003405
Winson Chung5308f242011-08-18 12:12:41 -07003406 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003407 }
Winson Chung5308f242011-08-18 12:12:41 -07003408 if (mLabelCache.containsKey(keyB)) {
3409 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003410 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003411 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003412
Winson Chung5308f242011-08-18 12:12:41 -07003413 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003414 }
Winson Chung11904872012-09-17 16:58:46 -07003415 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003416 }
3417 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003418 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003419 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003420 private PackageManager mPackageManager;
3421 private HashMap<Object, String> mLabelCache;
3422 WidgetAndShortcutNameComparator(PackageManager pm) {
3423 mPackageManager = pm;
3424 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003425 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003426 }
3427 public final int compare(Object a, Object b) {
3428 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003429 if (mLabelCache.containsKey(a)) {
3430 labelA = mLabelCache.get(a);
3431 } else {
3432 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003433 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003434 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003435 mLabelCache.put(a, labelA);
3436 }
3437 if (mLabelCache.containsKey(b)) {
3438 labelB = mLabelCache.get(b);
3439 } else {
3440 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003441 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003442 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003443 mLabelCache.put(b, labelB);
3444 }
Winson Chung11904872012-09-17 16:58:46 -07003445 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003446 }
3447 };
Joe Onoratobe386092009-11-17 17:32:16 -08003448
3449 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003450 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003451 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3452 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3453 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3454 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003455 if (mLoaderTask != null) {
3456 mLoaderTask.dumpState();
3457 } else {
3458 Log.d(TAG, "mLoaderTask=null");
3459 }
Joe Onoratobe386092009-11-17 17:32:16 -08003460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003461}