blob: 044ddbb44e463d9bfca9a38ddda6cc24a81f01a9 [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(
Jason Monk0bfcceb2014-03-21 15:42:06 -0400197 Uri.parse(context.getString(R.string.old_launcher_provider_uri)));
Winson Chungeb23cb22014-03-06 10:39:43 -0800198 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
Jason Monka0a7a742014-04-22 09:23:19 -0400502 synchronized (mDeferredBindRunnables) {
503 mDeferredBindRunnables.clear();
504 }
Winson Chung81b52252012-08-27 15:34:29 -0700505 // Remove any queued bind runnables
506 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
507 // Unbind all the workspace items
508 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700509 }
510
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700511 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700512 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700513 // Ensure that we don't use the same workspace items data structure on the main thread
514 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700515 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
516 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700517 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700518 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
519 tmpAppWidgets.addAll(sBgAppWidgets);
520 }
521 Runnable r = new Runnable() {
522 @Override
523 public void run() {
524 for (ItemInfo item : tmpWorkspaceItems) {
525 item.unbind();
526 }
527 for (ItemInfo item : tmpAppWidgets) {
528 item.unbind();
529 }
530 }
531 };
532 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700533 }
534
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 /**
536 * Adds an item to the DB if it was not created previously, or move it to a new
537 * <container, screen, cellX, cellY>
538 */
539 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700540 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541 if (item.container == ItemInfo.NO_ID) {
542 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700543 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 } else {
545 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700546 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
548 }
549
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700550 static void checkItemInfoLocked(
551 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
552 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
553 if (modelItem != null && item != modelItem) {
554 // check all the data is consistent
555 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
556 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
557 ShortcutInfo shortcut = (ShortcutInfo) item;
558 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
559 modelShortcut.intent.filterEquals(shortcut.intent) &&
560 modelShortcut.id == shortcut.id &&
561 modelShortcut.itemType == shortcut.itemType &&
562 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700563 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700564 modelShortcut.cellX == shortcut.cellX &&
565 modelShortcut.cellY == shortcut.cellY &&
566 modelShortcut.spanX == shortcut.spanX &&
567 modelShortcut.spanY == shortcut.spanY &&
568 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
569 (modelShortcut.dropPos != null &&
570 shortcut.dropPos != null &&
571 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
572 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
573 // For all intents and purposes, this is the same object
574 return;
575 }
576 }
577
578 // the modelItem needs to match up perfectly with item if our model is
579 // to be consistent with the database-- for now, just require
580 // modelItem == item or the equality check above
581 String msg = "item: " + ((item != null) ? item.toString() : "null") +
582 "modelItem: " +
583 ((modelItem != null) ? modelItem.toString() : "null") +
584 "Error: ItemInfo passed to checkItemInfo doesn't match original";
585 RuntimeException e = new RuntimeException(msg);
586 if (stackTrace != null) {
587 e.setStackTrace(stackTrace);
588 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800589 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700590 }
591 }
592
Michael Jurka816474f2012-06-25 14:49:02 -0700593 static void checkItemInfo(final ItemInfo item) {
594 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
595 final long itemId = item.id;
596 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700597 public void run() {
598 synchronized (sBgLock) {
599 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700600 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700601 }
602 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700603 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700604 }
605
Michael Jurkac9d95c52011-08-29 14:03:34 -0700606 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
607 final ItemInfo item, final String callingFunction) {
608 final long itemId = item.id;
609 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
610 final ContentResolver cr = context.getContentResolver();
611
Adam Cohen487f7dd2012-06-28 18:12:10 -0700612 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700613 Runnable r = new Runnable() {
614 public void run() {
615 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700616 updateItemArrays(item, itemId, stackTrace);
617 }
618 };
619 runOnWorkerThread(r);
620 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700621
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700622 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
623 final ArrayList<ItemInfo> items, final String callingFunction) {
624 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700625
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700626 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
627 Runnable r = new Runnable() {
628 public void run() {
629 ArrayList<ContentProviderOperation> ops =
630 new ArrayList<ContentProviderOperation>();
631 int count = items.size();
632 for (int i = 0; i < count; i++) {
633 ItemInfo item = items.get(i);
634 final long itemId = item.id;
635 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
636 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700637
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700638 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
639 updateItemArrays(item, itemId, stackTrace);
640
641 }
642 try {
643 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
644 } catch (Exception e) {
645 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700646 }
647 }
648 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700649 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700650 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700651
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700652 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
653 // Lock on mBgLock *after* the db operation
654 synchronized (sBgLock) {
655 checkItemInfoLocked(itemId, item, stackTrace);
656
657 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
658 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
659 // Item is in a folder, make sure this folder exists
660 if (!sBgFolders.containsKey(item.container)) {
661 // An items container is being set to a that of an item which is not in
662 // the list of Folders.
663 String msg = "item: " + item + " container being set to: " +
664 item.container + ", not in the list of folders";
665 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700666 }
667 }
668
669 // Items are added/removed from the corresponding FolderInfo elsewhere, such
670 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
671 // that are on the desktop, as appropriate
672 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800673 if (modelItem != null &&
674 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
675 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 switch (modelItem.itemType) {
677 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
678 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
679 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
680 if (!sBgWorkspaceItems.contains(modelItem)) {
681 sBgWorkspaceItems.add(modelItem);
682 }
683 break;
684 default:
685 break;
686 }
687 } else {
688 sBgWorkspaceItems.remove(modelItem);
689 }
690 }
691 }
692
Michael Jurkac7700af2013-05-14 20:17:58 +0200693 public void flushWorkerThread() {
694 mFlushingWorkerThread = true;
695 Runnable waiter = new Runnable() {
696 public void run() {
697 synchronized (this) {
698 notifyAll();
699 mFlushingWorkerThread = false;
700 }
701 }
702 };
703
704 synchronized(waiter) {
705 runOnWorkerThread(waiter);
706 if (mLoaderTask != null) {
707 synchronized(mLoaderTask) {
708 mLoaderTask.notify();
709 }
710 }
711 boolean success = false;
712 while (!success) {
713 try {
714 waiter.wait();
715 success = true;
716 } catch (InterruptedException e) {
717 }
718 }
719 }
720 }
721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700724 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700725 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700726 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728 item.cellX = cellX;
729 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 // We store hotseat items in canonical form which is this orientation invariant position
732 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700733 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700735 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700736 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700737 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739
740 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700742 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
743 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700744 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745
Michael Jurkac9d95c52011-08-29 14:03:34 -0700746 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700747 }
748
749 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
751 * cellX, cellY have already been updated on the ItemInfos.
752 */
753 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
754 final long container, final int screen) {
755
756 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
757 int count = items.size();
758
759 for (int i = 0; i < count; i++) {
760 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700761 item.container = container;
762
763 // We store hotseat items in canonical form which is this orientation invariant position
764 // in the hotseat
765 if (context instanceof Launcher && screen < 0 &&
766 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700767 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700768 item.cellY);
769 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700770 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700771 }
772
773 final ContentValues values = new ContentValues();
774 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
775 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
776 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700777 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700778
779 contentValues.add(values);
780 }
781 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
782 }
783
784 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800786 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700787 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700789 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800790 item.cellX = cellX;
791 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700792 item.spanX = spanX;
793 item.spanY = spanY;
794
795 // We store hotseat items in canonical form which is this orientation invariant position
796 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700798 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700800 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700801 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700802 }
Adam Cohend4844c32011-02-18 19:25:06 -0800803
Adam Cohend4844c32011-02-18 19:25:06 -0800804 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800805 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700806 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
807 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
808 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
809 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700810 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800811
Michael Jurka816474f2012-06-25 14:49:02 -0700812 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700813 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700814
815 /**
816 * Update an item to the database in a specified container.
817 */
818 static void updateItemInDatabase(Context context, final ItemInfo item) {
819 final ContentValues values = new ContentValues();
820 item.onAddToDatabase(values);
821 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
822 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800823 }
824
825 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 * Returns true if the shortcuts already exists in the database.
827 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 static boolean shortcutExists(Context context, String title, Intent intent) {
830 final ContentResolver cr = context.getContentResolver();
831 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
832 new String[] { "title", "intent" }, "title=? and intent=?",
833 new String[] { title, intent.toUri(0) }, null);
834 boolean result = false;
835 try {
836 result = c.moveToFirst();
837 } finally {
838 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700841 }
842
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500844 * Returns true if the shortcuts already exists in the database.
845 * we identify a shortcut by the component name of the intent.
846 */
847 static boolean appWasRestored(Context context, Intent intent) {
848 final ContentResolver cr = context.getContentResolver();
849 final ComponentName component = intent.getComponent();
850 if (component == null) {
851 return false;
852 }
853 String componentName = component.flattenToString();
854 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
855 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
856 new String[]{"intent", "restored"}, where, null, null);
857 boolean result = false;
858 try {
859 result = c.moveToFirst();
860 } finally {
861 c.close();
862 }
863 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
864 return result;
865 }
866
867 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700868 * Returns an ItemInfo array containing all the items in the LauncherModel.
869 * The ItemInfo.id is not set through this function.
870 */
871 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
872 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
873 final ContentResolver cr = context.getContentResolver();
874 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
875 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
876 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
877 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
878
879 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
880 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
881 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
882 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
883 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
884 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
885 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
886
887 try {
888 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700889 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700890 item.cellX = c.getInt(cellXIndex);
891 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700892 item.spanX = Math.max(1, c.getInt(spanXIndex));
893 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700894 item.container = c.getInt(containerIndex);
895 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700896 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700897
898 items.add(item);
899 }
900 } catch (Exception e) {
901 items.clear();
902 } finally {
903 c.close();
904 }
905
906 return items;
907 }
908
909 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
911 */
912 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
913 final ContentResolver cr = context.getContentResolver();
914 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
915 "_id=? and (itemType=? or itemType=?)",
916 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700917 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700918
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 try {
920 if (c.moveToFirst()) {
921 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
922 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
923 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
924 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
925 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
926 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 FolderInfo folderInfo = null;
929 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700930 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
931 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700933 }
934
Joe Onorato9c1289c2009-08-17 11:03:03 -0400935 folderInfo.title = c.getString(titleIndex);
936 folderInfo.id = id;
937 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700939 folderInfo.cellX = c.getInt(cellXIndex);
940 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941
942 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 } finally {
945 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700946 }
947
948 return null;
949 }
950
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 /**
952 * Add an item to the database in a specified container. Sets the container, screen, cellX and
953 * cellY fields of the item. Also assigns an ID to the item.
954 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700955 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700956 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 item.cellX = cellX;
959 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 // We store hotseat items in canonical form which is this orientation invariant position
961 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700962 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700965 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700966 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700967 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968
969 final ContentValues values = new ContentValues();
970 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971 item.onAddToDatabase(values);
972
Michael Jurka414300a2013-08-27 15:42:35 +0200973 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700974 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700975 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700976
Jason Monk8e19cf22014-03-20 15:06:57 -0400977 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700978 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700979 public void run() {
980 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
981 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700984 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400985 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700986 sBgItemsIdMap.put(item.id, item);
987 switch (item.itemType) {
988 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
989 sBgFolders.put(item.id, (FolderInfo) item);
990 // Fall through
991 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
992 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
993 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
994 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
995 sBgWorkspaceItems.add(item);
996 } else {
997 if (!sBgFolders.containsKey(item.container)) {
998 // Adding an item to a folder that doesn't exist.
999 String msg = "adding item: " + item + " to a folder that " +
1000 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001001 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001002 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001003 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001004 break;
1005 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1006 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1007 break;
1008 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001009 }
1010 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001011 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001013 }
1014
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001016 * Creates a new unique child id, for a given cell span across all layouts.
1017 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001018 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001019 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001020 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001021 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001022 }
1023
Winson Chungaafa03c2010-06-11 17:34:16 -07001024 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001025 * Removes the specified item from the database
1026 * @param context
1027 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001029 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001030 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001031 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001032
Michael Jurka83df1882011-08-31 20:59:26 -07001033 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001034 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001035 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001036
1037 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001038 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001039 switch (item.itemType) {
1040 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1041 sBgFolders.remove(item.id);
1042 for (ItemInfo info: sBgItemsIdMap.values()) {
1043 if (info.container == item.id) {
1044 // We are deleting a folder which still contains items that
1045 // think they are contained by that folder.
1046 String msg = "deleting a folder (" + item + ") which still " +
1047 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001048 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001049 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001050 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 sBgWorkspaceItems.remove(item);
1052 break;
1053 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1054 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1055 sBgWorkspaceItems.remove(item);
1056 break;
1057 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1058 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1059 break;
1060 }
1061 sBgItemsIdMap.remove(item.id);
1062 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001063 }
1064 }
Michael Jurka83df1882011-08-31 20:59:26 -07001065 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001066 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001067 }
1068
1069 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001070 * Update the order of the workspace screens in the database. The array list contains
1071 * a list of screen ids in the order that they should appear.
1072 */
Winson Chungc9168342013-06-26 14:54:55 -07001073 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001074 // Log to disk
1075 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1076 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1077
Winson Chung64359a52013-07-08 17:17:08 -07001078 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 final ContentResolver cr = context.getContentResolver();
1080 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1081
1082 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001083 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 while (iter.hasNext()) {
1085 long id = iter.next();
1086 if (id < 0) {
1087 iter.remove();
1088 }
1089 }
1090
1091 Runnable r = new Runnable() {
1092 @Override
1093 public void run() {
Yura085c8532014-02-11 15:15:29 +00001094 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001096 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001097 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 for (int i = 0; i < count; i++) {
1099 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001100 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001101 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1102 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001103 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001104 }
Yura085c8532014-02-11 15:15:29 +00001105
1106 try {
1107 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1108 } catch (Exception ex) {
1109 throw new RuntimeException(ex);
1110 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001111
Winson Chungba9c37f2013-08-30 14:11:37 -07001112 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001113 sBgWorkspaceScreens.clear();
1114 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001115 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001116 }
1117 };
1118 runOnWorkerThread(r);
1119 }
1120
1121 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 * Remove the contents of the specified folder from the database
1123 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001124 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 final ContentResolver cr = context.getContentResolver();
1126
Michael Jurkac9d95c52011-08-29 14:03:34 -07001127 Runnable r = new Runnable() {
1128 public void run() {
1129 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001131 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 sBgItemsIdMap.remove(info.id);
1133 sBgFolders.remove(info.id);
1134 sBgDbIconCache.remove(info);
1135 sBgWorkspaceItems.remove(info);
1136 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001137
Michael Jurkac9d95c52011-08-29 14:03:34 -07001138 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1139 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001140 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001141 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001142 for (ItemInfo childInfo : info.contents) {
1143 sBgItemsIdMap.remove(childInfo.id);
1144 sBgDbIconCache.remove(childInfo);
1145 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001146 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001147 }
1148 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001149 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 }
1151
1152 /**
1153 * Set this as the current Launcher activity object for the loader.
1154 */
1155 public void initialize(Callbacks callbacks) {
1156 synchronized (mLock) {
1157 mCallbacks = new WeakReference<Callbacks>(callbacks);
1158 }
1159 }
1160
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001161 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1163 * ACTION_PACKAGE_CHANGED.
1164 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001165 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001166 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001167 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001168
Joe Onorato36115782010-06-17 13:28:48 -04001169 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001170
Joe Onorato36115782010-06-17 13:28:48 -04001171 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1172 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1173 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1174 final String packageName = intent.getData().getSchemeSpecificPart();
1175 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176
Joe Onorato36115782010-06-17 13:28:48 -04001177 int op = PackageUpdatedTask.OP_NONE;
1178
1179 if (packageName == null || packageName.length() == 0) {
1180 // they sent us a bad intent
1181 return;
1182 }
1183
1184 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1185 op = PackageUpdatedTask.OP_UPDATE;
1186 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1187 if (!replacing) {
1188 op = PackageUpdatedTask.OP_REMOVE;
1189 }
1190 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1191 // later, we will update the package at this time
1192 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1193 if (!replacing) {
1194 op = PackageUpdatedTask.OP_ADD;
1195 } else {
1196 op = PackageUpdatedTask.OP_UPDATE;
1197 }
1198 }
1199
1200 if (op != PackageUpdatedTask.OP_NONE) {
1201 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1202 }
1203
1204 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001205 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001206 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001207 if (!replacing) {
1208 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1209 if (mAppsCanBeOnRemoveableStorage) {
1210 // Only rebind if we support removable storage. It catches the case where
1211 // apps on the external sd card need to be reloaded
1212 startLoaderFromBackground();
1213 }
1214 } else {
1215 // If we are replacing then just update the packages in the list
1216 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1217 packages));
1218 }
Joe Onorato36115782010-06-17 13:28:48 -04001219 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001220 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1221 if (!replacing) {
1222 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1223 enqueuePackageUpdated(new PackageUpdatedTask(
1224 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1225 }
1226 // else, we are replacing the packages, so ignore this event and wait for
1227 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001228 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001229 // If we have changed locale we need to clear out the labels in all apps/workspace.
1230 forceReload();
1231 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1232 // Check if configuration change was an mcc/mnc change which would affect app resources
1233 // and we would need to clear out the labels in all apps/workspace. Same handling as
1234 // above for ACTION_LOCALE_CHANGED
1235 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001236 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001237 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001238 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001239 forceReload();
1240 }
1241 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001242 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001243 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1244 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001245 if (mCallbacks != null) {
1246 Callbacks callbacks = mCallbacks.get();
1247 if (callbacks != null) {
1248 callbacks.bindSearchablesChanged();
1249 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001250 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001251 }
1252 }
1253
Reena Lee93f824a2011-09-23 17:20:28 -07001254 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001255 resetLoadedState(true, true);
1256
Reena Lee93f824a2011-09-23 17:20:28 -07001257 // Do this here because if the launcher activity is running it will be restarted.
1258 // If it's not running startLoaderFromBackground will merely tell it that it needs
1259 // to reload.
1260 startLoaderFromBackground();
1261 }
1262
Winson Chungf0c6ae02012-03-21 16:10:31 -07001263 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1264 synchronized (mLock) {
1265 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1266 // mWorkspaceLoaded to true later
1267 stopLoaderLocked();
1268 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1269 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1270 }
1271 }
1272
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001273 /**
1274 * When the launcher is in the background, it's possible for it to miss paired
1275 * configuration changes. So whenever we trigger the loader from the background
1276 * tell the launcher that it needs to re-run the loader when it comes back instead
1277 * of doing it now.
1278 */
1279 public void startLoaderFromBackground() {
1280 boolean runLoader = false;
1281 if (mCallbacks != null) {
1282 Callbacks callbacks = mCallbacks.get();
1283 if (callbacks != null) {
1284 // Only actually run the loader if they're not paused.
1285 if (!callbacks.setLoadOnResume()) {
1286 runLoader = true;
1287 }
1288 }
1289 }
1290 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001291 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001292 }
Joe Onorato36115782010-06-17 13:28:48 -04001293 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001294
Reena Lee93f824a2011-09-23 17:20:28 -07001295 // If there is already a loader task running, tell it to stop.
1296 // returns true if isLaunching() was true on the old task
1297 private boolean stopLoaderLocked() {
1298 boolean isLaunching = false;
1299 LoaderTask oldTask = mLoaderTask;
1300 if (oldTask != null) {
1301 if (oldTask.isLaunching()) {
1302 isLaunching = true;
1303 }
1304 oldTask.stopLocked();
1305 }
1306 return isLaunching;
1307 }
1308
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001309 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001310 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1311 }
1312
1313 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001314 synchronized (mLock) {
1315 if (DEBUG_LOADERS) {
1316 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1317 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001319 // Clear any deferred bind-runnables from the synchronized load process
1320 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001321 synchronized (mDeferredBindRunnables) {
1322 mDeferredBindRunnables.clear();
1323 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324
Joe Onorato36115782010-06-17 13:28:48 -04001325 // Don't bother to start the thread if we know it's not going to do anything
1326 if (mCallbacks != null && mCallbacks.get() != null) {
1327 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001328 // also, don't downgrade isLaunching if we're already running
1329 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001330 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001331 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1332 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001333 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1334 } else {
1335 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1336 sWorker.post(mLoaderTask);
1337 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 }
1340 }
1341
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001342 void bindRemainingSynchronousPages() {
1343 // Post the remaining side pages to be loaded
1344 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001345 Runnable[] deferredBindRunnables = null;
1346 synchronized (mDeferredBindRunnables) {
1347 deferredBindRunnables = mDeferredBindRunnables.toArray(
1348 new Runnable[mDeferredBindRunnables.size()]);
1349 mDeferredBindRunnables.clear();
1350 }
1351 for (final Runnable r : deferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001352 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001353 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001354 }
1355 }
1356
Joe Onorato36115782010-06-17 13:28:48 -04001357 public void stopLoader() {
1358 synchronized (mLock) {
1359 if (mLoaderTask != null) {
1360 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 }
1362 }
Joe Onorato36115782010-06-17 13:28:48 -04001363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001364
Winson Chung76828c82013-08-19 15:43:29 -07001365 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1366 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1367 final ContentResolver contentResolver = context.getContentResolver();
1368 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1369 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1370 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1371
1372 try {
1373 final int idIndex = sc.getColumnIndexOrThrow(
1374 LauncherSettings.WorkspaceScreens._ID);
1375 final int rankIndex = sc.getColumnIndexOrThrow(
1376 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1377 while (sc.moveToNext()) {
1378 try {
1379 long screenId = sc.getLong(idIndex);
1380 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001381 orderedScreens.put(rank, screenId);
1382 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001383 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001384 }
1385 }
1386 } finally {
1387 sc.close();
1388 }
Winson Chunga90303b2013-11-15 13:05:06 -08001389
1390 // Log to disk
1391 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1392 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1393 for (Integer i : orderedScreens.keySet()) {
1394 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1395 }
1396 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1397 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001398 return orderedScreens;
1399 }
1400
Michael Jurkac57b7a82011-08-09 22:02:20 -07001401 public boolean isAllAppsLoaded() {
1402 return mAllAppsLoaded;
1403 }
1404
Winson Chung36a62fe2012-05-06 18:04:42 -07001405 boolean isLoadingWorkspace() {
1406 synchronized (mLock) {
1407 if (mLoaderTask != null) {
1408 return mLoaderTask.isLoadingWorkspace();
1409 }
1410 }
1411 return false;
1412 }
1413
Joe Onorato36115782010-06-17 13:28:48 -04001414 /**
1415 * Runnable for the thread that loads the contents of the launcher:
1416 * - workspace icons
1417 * - widgets
1418 * - all apps icons
1419 */
1420 private class LoaderTask implements Runnable {
1421 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001422 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001423 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001424 private boolean mStopped;
1425 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001426 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001427
Winson Chungc3eecff2011-07-11 17:44:15 -07001428 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001429
Dan Sandlerd5024042014-01-09 15:01:33 -05001430 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001431 mContext = context;
1432 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001433 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001434 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001435 }
1436
Joe Onorato36115782010-06-17 13:28:48 -04001437 boolean isLaunching() {
1438 return mIsLaunching;
1439 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440
Winson Chung36a62fe2012-05-06 18:04:42 -07001441 boolean isLoadingWorkspace() {
1442 return mIsLoadingAndBindingWorkspace;
1443 }
1444
Winson Chungc763c4e2013-07-19 13:49:06 -07001445 /** Returns whether this is an upgrade path */
1446 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001447 mIsLoadingAndBindingWorkspace = true;
1448
Joe Onorato36115782010-06-17 13:28:48 -04001449 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001450 if (DEBUG_LOADERS) {
1451 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001453
Winson Chungc763c4e2013-07-19 13:49:06 -07001454 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001455 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001456 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001457 synchronized (LoaderTask.this) {
1458 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001459 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001460 }
1461 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 }
1463 }
1464
Joe Onorato36115782010-06-17 13:28:48 -04001465 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001466 bindWorkspace(-1, isUpgradePath);
1467 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001468 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001469
Joe Onorato36115782010-06-17 13:28:48 -04001470 private void waitForIdle() {
1471 // Wait until the either we're stopped or the other threads are done.
1472 // This way we don't start loading all apps until the workspace has settled
1473 // down.
1474 synchronized (LoaderTask.this) {
1475 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001476
Joe Onorato36115782010-06-17 13:28:48 -04001477 mHandler.postIdle(new Runnable() {
1478 public void run() {
1479 synchronized (LoaderTask.this) {
1480 mLoadAndBindStepFinished = true;
1481 if (DEBUG_LOADERS) {
1482 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001483 }
Joe Onorato36115782010-06-17 13:28:48 -04001484 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001485 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001486 }
Joe Onorato36115782010-06-17 13:28:48 -04001487 });
1488
Michael Jurkac7700af2013-05-14 20:17:58 +02001489 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001490 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001491 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1492 // wait no longer than 1sec at a time
1493 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001494 } catch (InterruptedException ex) {
1495 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001496 }
1497 }
Joe Onorato36115782010-06-17 13:28:48 -04001498 if (DEBUG_LOADERS) {
1499 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001500 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001501 + "ms for previous step to finish binding");
1502 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001503 }
Joe Onorato36115782010-06-17 13:28:48 -04001504 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001505
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001506 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001507 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 // Ensure that we have a valid page index to load synchronously
1509 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1510 "valid page index");
1511 }
1512 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1513 // Ensure that we don't try and bind a specified page when the pages have not been
1514 // loaded already (we should load everything asynchronously in that case)
1515 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1516 }
1517 synchronized (mLock) {
1518 if (mIsLoaderTaskRunning) {
1519 // Ensure that we are never running the background loading at this point since
1520 // we also touch the background collections
1521 throw new RuntimeException("Error! Background loading is already running");
1522 }
1523 }
1524
1525 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1526 // data structures, we can't allow any other thread to touch that data, but because
1527 // this call is synchronous, we can get away with not locking).
1528
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001529 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001530 // operations from the previous activity. We need to ensure that all queued operations
1531 // are executed before any synchronous binding work is done.
1532 mHandler.flush();
1533
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001534 // Divide the set of loaded items into those that we are binding synchronously, and
1535 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001536 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001537 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1538 // arise from that.
1539 onlyBindAllApps();
1540 }
1541
Joe Onorato36115782010-06-17 13:28:48 -04001542 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001543 boolean isUpgrade = false;
1544
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001545 synchronized (mLock) {
1546 mIsLoaderTaskRunning = true;
1547 }
Joe Onorato36115782010-06-17 13:28:48 -04001548 // Optimize for end-user experience: if the Launcher is up and // running with the
1549 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1550 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001551 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001552 // Elevate priority when Home launches for the first time to avoid
1553 // starving at boot time. Staring at a blank home is not cool.
1554 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001555 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1556 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001557 android.os.Process.setThreadPriority(mIsLaunching
1558 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1559 }
Winson Chung64359a52013-07-08 17:17:08 -07001560 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001561 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001562
Joe Onorato36115782010-06-17 13:28:48 -04001563 if (mStopped) {
1564 break keep_running;
1565 }
1566
1567 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1568 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001569 synchronized (mLock) {
1570 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001571 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001572 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1573 }
1574 }
Joe Onorato36115782010-06-17 13:28:48 -04001575 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001576
1577 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001578 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1579 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001580
1581 // Restore the default thread priority after we are done loading items
1582 synchronized (mLock) {
1583 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1584 }
Joe Onorato36115782010-06-17 13:28:48 -04001585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586
Winson Chungaac01e12011-08-17 10:37:13 -07001587 // Update the saved icons if necessary
1588 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001589 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001590 for (Object key : sBgDbIconCache.keySet()) {
1591 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1592 }
1593 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001594 }
Winson Chungaac01e12011-08-17 10:37:13 -07001595
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001596 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001597 // Ensure that all the applications that are in the system are
1598 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001599 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001600 verifyApplications();
1601 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001602 }
1603
Joe Onorato36115782010-06-17 13:28:48 -04001604 // Clear out this reference, otherwise we end up holding it until all of the
1605 // callback runnables are done.
1606 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607
Joe Onorato36115782010-06-17 13:28:48 -04001608 synchronized (mLock) {
1609 // If we are still the last one to be scheduled, remove ourselves.
1610 if (mLoaderTask == this) {
1611 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001612 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001613 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001614 }
Joe Onorato36115782010-06-17 13:28:48 -04001615 }
1616
1617 public void stopLocked() {
1618 synchronized (LoaderTask.this) {
1619 mStopped = true;
1620 this.notify();
1621 }
1622 }
1623
1624 /**
1625 * Gets the callbacks object. If we've been stopped, or if the launcher object
1626 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1627 * object that was around when the deferred message was scheduled, and if there's
1628 * a new Callbacks object around then also return null. This will save us from
1629 * calling onto it with data that will be ignored.
1630 */
1631 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1632 synchronized (mLock) {
1633 if (mStopped) {
1634 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001635 }
Joe Onorato36115782010-06-17 13:28:48 -04001636
1637 if (mCallbacks == null) {
1638 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001639 }
Joe Onorato36115782010-06-17 13:28:48 -04001640
1641 final Callbacks callbacks = mCallbacks.get();
1642 if (callbacks != oldCallbacks) {
1643 return null;
1644 }
1645 if (callbacks == null) {
1646 Log.w(TAG, "no mCallbacks");
1647 return null;
1648 }
1649
1650 return callbacks;
1651 }
1652 }
1653
Winson Chungc763c4e2013-07-19 13:49:06 -07001654 private void verifyApplications() {
1655 final Context context = mApp.getContext();
1656
1657 // Cross reference all the applications in our apps list with items in the workspace
1658 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001659 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001660 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001661 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001662 tmpInfos = getItemInfoForComponentName(app.componentName);
1663 if (tmpInfos.isEmpty()) {
1664 // We are missing an application icon, so add this to the workspace
1665 added.add(app);
1666 // This is a rare event, so lets log it
1667 Log.e(TAG, "Missing Application on load: " + app);
1668 }
1669 }
1670 }
1671 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001672 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001673 }
1674 }
1675
Joe Onorato36115782010-06-17 13:28:48 -04001676 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001677 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001678 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001679 LauncherAppState app = LauncherAppState.getInstance();
1680 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001681 final int countX = (int) grid.numColumns;
1682 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001683
Adam Cohendcd297f2013-06-18 13:13:40 -07001684 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001685 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001686 // Return early if we detect that an item is under the hotseat button
1687 if (mCallbacks == null ||
1688 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001689 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001690 Log.e(TAG, "Error loading shortcut into hotseat " + item
1691 + " into position (" + item.screenId + ":" + item.cellX + ","
1692 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001693 return false;
1694 }
1695
Dan Sandler295ae182013-12-10 16:05:47 -05001696 final ItemInfo[][] hotseatItems =
1697 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1698
Adam Cohenae4409d2013-11-26 10:34:59 -08001699 if (item.screenId >= grid.numHotseatIcons) {
1700 Log.e(TAG, "Error loading shortcut " + item
1701 + " into hotseat position " + item.screenId
1702 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1703 + ")");
1704 return false;
1705 }
1706
Dan Sandler295ae182013-12-10 16:05:47 -05001707 if (hotseatItems != null) {
1708 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001709 Log.e(TAG, "Error loading shortcut into hotseat " + item
1710 + " into position (" + item.screenId + ":" + item.cellX + ","
1711 + item.cellY + ") occupied by "
1712 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1713 [(int) item.screenId][0]);
1714 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001715 } else {
1716 hotseatItems[(int) item.screenId][0] = item;
1717 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001718 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001719 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001720 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 items[(int) item.screenId][0] = item;
1722 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001723 return true;
1724 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001725 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1726 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001727 return true;
1728 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001729
Adam Cohendcd297f2013-06-18 13:13:40 -07001730 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001731 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001732 occupied.put(item.screenId, items);
1733 }
1734
Dan Sandler295ae182013-12-10 16:05:47 -05001735 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001736 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1737 item.cellX < 0 || item.cellY < 0 ||
1738 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1739 Log.e(TAG, "Error loading shortcut " + item
1740 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1741 + item.cellX + "," + item.cellY
1742 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1743 return false;
1744 }
1745
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001746 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001747 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1748 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001749 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001750 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001751 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001752 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001753 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001754 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001755 return false;
1756 }
1757 }
1758 }
1759 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1760 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001761 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001762 }
1763 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001764
Joe Onorato36115782010-06-17 13:28:48 -04001765 return true;
1766 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001767
Winson Chungba9c37f2013-08-30 14:11:37 -07001768 /** Clears all the sBg data structures */
1769 private void clearSBgDataStructures() {
1770 synchronized (sBgLock) {
1771 sBgWorkspaceItems.clear();
1772 sBgAppWidgets.clear();
1773 sBgFolders.clear();
1774 sBgItemsIdMap.clear();
1775 sBgDbIconCache.clear();
1776 sBgWorkspaceScreens.clear();
1777 }
1778 }
1779
Dan Sandlerd5024042014-01-09 15:01:33 -05001780 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001781 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001782 // Log to disk
1783 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1784
Joe Onorato36115782010-06-17 13:28:48 -04001785 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786
Joe Onorato36115782010-06-17 13:28:48 -04001787 final Context context = mContext;
1788 final ContentResolver contentResolver = context.getContentResolver();
1789 final PackageManager manager = context.getPackageManager();
1790 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1791 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001792
Winson Chung892c74d2013-08-22 16:15:50 -07001793 LauncherAppState app = LauncherAppState.getInstance();
1794 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1795 int countX = (int) grid.numColumns;
1796 int countY = (int) grid.numRows;
1797
Dan Sandlerd5024042014-01-09 15:01:33 -05001798 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1799 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1800 LauncherAppState.getLauncherProvider().deleteDatabase();
1801 }
1802
1803 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1804 // append the user's Launcher2 shortcuts
1805 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1806 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1807 } else {
1808 // Make sure the default workspace is loaded
1809 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1810 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1811 }
Adam Cohene25af792013-06-06 23:08:25 -07001812
Winson Chungc763c4e2013-07-19 13:49:06 -07001813 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001814 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001815 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001816
Winson Chung9f9f00b2013-11-15 13:27:00 -08001817 // Log to disk
1818 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001819
Winson Chung2abf94d2012-07-18 18:16:38 -07001820 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001821 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001822
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001824 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001825 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001826 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001827 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001828
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 // +1 for the hotseat (it can be larger than the workspace)
1830 // Load workspace in reverse order to ensure that latest items are loaded first (and
1831 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001832 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 try {
1835 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1836 final int intentIndex = c.getColumnIndexOrThrow
1837 (LauncherSettings.Favorites.INTENT);
1838 final int titleIndex = c.getColumnIndexOrThrow
1839 (LauncherSettings.Favorites.TITLE);
1840 final int iconTypeIndex = c.getColumnIndexOrThrow(
1841 LauncherSettings.Favorites.ICON_TYPE);
1842 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1843 final int iconPackageIndex = c.getColumnIndexOrThrow(
1844 LauncherSettings.Favorites.ICON_PACKAGE);
1845 final int iconResourceIndex = c.getColumnIndexOrThrow(
1846 LauncherSettings.Favorites.ICON_RESOURCE);
1847 final int containerIndex = c.getColumnIndexOrThrow(
1848 LauncherSettings.Favorites.CONTAINER);
1849 final int itemTypeIndex = c.getColumnIndexOrThrow(
1850 LauncherSettings.Favorites.ITEM_TYPE);
1851 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1852 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001853 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1854 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001855 final int screenIndex = c.getColumnIndexOrThrow(
1856 LauncherSettings.Favorites.SCREEN);
1857 final int cellXIndex = c.getColumnIndexOrThrow
1858 (LauncherSettings.Favorites.CELLX);
1859 final int cellYIndex = c.getColumnIndexOrThrow
1860 (LauncherSettings.Favorites.CELLY);
1861 final int spanXIndex = c.getColumnIndexOrThrow
1862 (LauncherSettings.Favorites.SPANX);
1863 final int spanYIndex = c.getColumnIndexOrThrow(
1864 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001865 final int restoredIndex = c.getColumnIndexOrThrow(
1866 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001867 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1868 //final int displayModeIndex = c.getColumnIndexOrThrow(
1869 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001870
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001871 ShortcutInfo info;
1872 String intentDescription;
1873 LauncherAppWidgetInfo appWidgetInfo;
1874 int container;
1875 long id;
1876 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001878 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001879 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 try {
1881 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001882 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001883
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001884 switch (itemType) {
1885 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1886 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001887 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 intentDescription = c.getString(intentIndex);
1889 try {
1890 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001891 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001892 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001893 if (restored) {
1894 // might be installed later
1895 Launcher.addDumpLog(TAG,
1896 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001897 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001898 if (!mAppsCanBeOnRemoveableStorage) {
1899 // Log the invalid package, and remove it
1900 Launcher.addDumpLog(TAG,
1901 "Invalid package removed: " + cn, true);
1902 itemsToRemove.add(id);
1903 } else {
1904 // If apps can be on external storage, then we just
1905 // leave them for the user to remove (maybe add
1906 // visual treatment to it)
1907 Launcher.addDumpLog(TAG,
1908 "Invalid package found: " + cn, true);
1909 }
1910 continue;
Winson Chungee055712013-07-30 14:46:24 -07001911 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001912 } else if (restored) {
1913 // no special handling necessary for this restored item
1914 restoredRows.add(id);
1915 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001916 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001918 Launcher.addDumpLog(TAG,
1919 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001920 continue;
1921 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001922
Chris Wrenf4d08112014-01-16 18:13:56 -05001923 if (restored) {
1924 Launcher.addDumpLog(TAG,
1925 "constructing info for partially restored package",
1926 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001927 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001928 intent = getRestoredItemIntent(c, context, intent);
1929 } else if (itemType ==
1930 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1932 titleIndex, mLabelCache);
1933 } else {
1934 info = getShortcutInfo(c, context, iconTypeIndex,
1935 iconPackageIndex, iconResourceIndex, iconIndex,
1936 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001937
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001938 // App shortcuts that used to be automatically added to Launcher
1939 // didn't always have the correct intent flags set, so do that
1940 // here
1941 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001942 intent.getCategories() != null &&
1943 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001944 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001945 intent.addFlags(
1946 Intent.FLAG_ACTIVITY_NEW_TASK |
1947 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1948 }
Michael Jurka96879562012-03-22 05:54:33 -07001949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001950
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001951 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001952 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001953 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001954 container = c.getInt(containerIndex);
1955 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001956 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001957 info.cellX = c.getInt(cellXIndex);
1958 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001959 info.spanX = 1;
1960 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001961
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001962 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001963 deleteOnInvalidPlacement.set(false);
1964 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1965 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001966 itemsToRemove.add(id);
1967 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001968 break;
1969 }
1970
1971 switch (container) {
1972 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1973 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1974 sBgWorkspaceItems.add(info);
1975 break;
1976 default:
1977 // Item is in a user folder
1978 FolderInfo folderInfo =
1979 findOrMakeFolder(sBgFolders, container);
1980 folderInfo.add(info);
1981 break;
1982 }
1983 sBgItemsIdMap.put(info.id, info);
1984
1985 // now that we've loaded everthing re-save it with the
1986 // icon in case it disappears somehow.
1987 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001988 } else {
1989 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001990 }
1991 break;
1992
1993 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1994 id = c.getLong(idIndex);
1995 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1996
1997 folderInfo.title = c.getString(titleIndex);
1998 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001999 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 folderInfo.cellX = c.getInt(cellXIndex);
2003 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002004 folderInfo.spanX = 1;
2005 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006
Daniel Sandler8802e962010-05-26 16:28:16 -04002007 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002008 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002009 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002010 deleteOnInvalidPlacement)) {
2011 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002012 itemsToRemove.add(id);
2013 }
Daniel Sandler8802e962010-05-26 16:28:16 -04002014 break;
2015 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002016
Joe Onorato9c1289c2009-08-17 11:03:03 -04002017 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2019 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2020 sBgWorkspaceItems.add(folderInfo);
2021 break;
Joe Onorato36115782010-06-17 13:28:48 -04002022 }
Joe Onorato17a89222011-02-08 17:26:11 -08002023
Chris Wrenf4d08112014-01-16 18:13:56 -05002024 if (restored) {
2025 // no special handling required for restored folders
2026 restoredRows.add(id);
2027 }
2028
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2030 sBgFolders.put(folderInfo.id, folderInfo);
2031 break;
2032
2033 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2034 // Read all Launcher-specific widget details
2035 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002036 String savedProvider = c.getString(appWidgetProviderIndex);
2037
Joe Onorato36115782010-06-17 13:28:48 -04002038 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002039
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 final AppWidgetProviderInfo provider =
2041 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002042
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 if (!isSafeMode && (provider == null || provider.provider == null ||
2044 provider.provider.getPackageName() == null)) {
2045 String log = "Deleting widget that isn't installed anymore: id="
2046 + id + " appWidgetId=" + appWidgetId;
2047 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002048 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 itemsToRemove.add(id);
2050 } else {
2051 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2052 provider.provider);
2053 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002054 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 appWidgetInfo.cellX = c.getInt(cellXIndex);
2056 appWidgetInfo.cellY = c.getInt(cellYIndex);
2057 appWidgetInfo.spanX = c.getInt(spanXIndex);
2058 appWidgetInfo.spanY = c.getInt(spanYIndex);
2059 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2060 appWidgetInfo.minSpanX = minSpan[0];
2061 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002062
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 container = c.getInt(containerIndex);
2064 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2065 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2066 Log.e(TAG, "Widget found where container != " +
2067 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2068 continue;
2069 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070
Adam Cohene25af792013-06-06 23:08:25 -07002071 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002073 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002074 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002075 deleteOnInvalidPlacement)) {
2076 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002077 itemsToRemove.add(id);
2078 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 break;
2080 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002081 String providerName = provider.provider.flattenToString();
2082 if (!providerName.equals(savedProvider)) {
2083 ContentValues values = new ContentValues();
2084 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2085 providerName);
2086 String where = BaseColumns._ID + "= ?";
2087 String[] args = {Integer.toString(c.getInt(idIndex))};
2088 contentResolver.update(contentUri, values, where, args);
2089 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2091 sBgAppWidgets.add(appWidgetInfo);
2092 }
Joe Onorato36115782010-06-17 13:28:48 -04002093 break;
2094 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002096 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002097 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002098 }
2099 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002100 if (c != null) {
2101 c.close();
2102 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 }
2104
Winson Chungba9c37f2013-08-30 14:11:37 -07002105 // Break early if we've stopped loading
2106 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002107 clearSBgDataStructures();
2108 return false;
2109 }
2110
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002111 if (itemsToRemove.size() > 0) {
2112 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002113 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 // Remove dead items
2115 for (long id : itemsToRemove) {
2116 if (DEBUG_LOADERS) {
2117 Log.d(TAG, "Removed id = " + id);
2118 }
2119 // Don't notify content observers
2120 try {
2121 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2122 null, null);
2123 } catch (RemoteException e) {
2124 Log.w(TAG, "Could not remove id = " + id);
2125 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002126 }
2127 }
2128
Chris Wrenf4d08112014-01-16 18:13:56 -05002129 if (restoredRows.size() > 0) {
2130 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2131 LauncherSettings.Favorites.CONTENT_URI);
2132 // Update restored items that no longer require special handling
2133 try {
2134 StringBuilder selectionBuilder = new StringBuilder();
2135 selectionBuilder.append(LauncherSettings.Favorites._ID);
2136 selectionBuilder.append(" IN (");
2137 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2138 selectionBuilder.append(")");
2139 ContentValues values = new ContentValues();
2140 values.put(LauncherSettings.Favorites.RESTORED, 0);
2141 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2142 values, selectionBuilder.toString(), null);
2143 } catch (RemoteException e) {
2144 Log.w(TAG, "Could not update restored rows");
2145 }
2146 }
2147
Winson Chungc763c4e2013-07-19 13:49:06 -07002148 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002149 long maxScreenId = 0;
2150 // If we're importing we use the old screen order.
2151 for (ItemInfo item: sBgItemsIdMap.values()) {
2152 long screenId = item.screenId;
2153 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2154 !sBgWorkspaceScreens.contains(screenId)) {
2155 sBgWorkspaceScreens.add(screenId);
2156 if (screenId > maxScreenId) {
2157 maxScreenId = screenId;
2158 }
2159 }
2160 }
2161 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002162 // Log to disk
2163 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2164 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2165 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002166
Michael Jurka414300a2013-08-27 15:42:35 +02002167 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002168 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002169
2170 // Update the max item id after we load an old db
2171 long maxItemId = 0;
2172 // If we're importing we use the old screen order.
2173 for (ItemInfo item: sBgItemsIdMap.values()) {
2174 maxItemId = Math.max(maxItemId, item.id);
2175 }
Michael Jurka414300a2013-08-27 15:42:35 +02002176 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002177 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002178 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2179 for (Integer i : orderedScreens.keySet()) {
2180 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002182 // Log to disk
2183 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2184 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002185
2186 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002187 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002188 for (ItemInfo item: sBgItemsIdMap.values()) {
2189 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002190 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2191 unusedScreens.contains(screenId)) {
2192 unusedScreens.remove(screenId);
2193 }
2194 }
2195
2196 // If there are any empty screens remove them, and update.
2197 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002198 // Log to disk
2199 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2200 TextUtils.join(", ", unusedScreens), true);
2201
Winson Chung933bae62013-08-29 11:42:30 -07002202 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2204 }
2205 }
2206
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 if (DEBUG_LOADERS) {
2208 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2209 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002210 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002211 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002213
Daniel Sandler566da102013-06-25 23:43:45 -04002214 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002215 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002217 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 line += " | ";
2219 }
Winson Chung892c74d2013-08-22 16:15:50 -07002220 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002221 ItemInfo[][] screen = occupied.get(screenId);
2222 if (x < screen.length && y < screen[x].length) {
2223 line += (screen[x][y] != null) ? "#" : ".";
2224 } else {
2225 line += "!";
2226 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 }
Joe Onorato36115782010-06-17 13:28:48 -04002228 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002230 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002231 }
Joe Onorato36115782010-06-17 13:28:48 -04002232 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002233 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002234 }
2235
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 /** Filters the set of items who are directly or indirectly (via another container) on the
2237 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002238 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 ArrayList<ItemInfo> allWorkspaceItems,
2240 ArrayList<ItemInfo> currentScreenItems,
2241 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002242 // Purge any null ItemInfos
2243 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2244 while (iter.hasNext()) {
2245 ItemInfo i = iter.next();
2246 if (i == null) {
2247 iter.remove();
2248 }
2249 }
2250
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 // Order the set of items by their containers first, this allows use to walk through the
2252 // list sequentially, build up a list of containers that are in the specified screen,
2253 // as well as all items in those containers.
2254 Set<Long> itemsOnScreen = new HashSet<Long>();
2255 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2256 @Override
2257 public int compare(ItemInfo lhs, ItemInfo rhs) {
2258 return (int) (lhs.container - rhs.container);
2259 }
2260 });
2261 for (ItemInfo info : allWorkspaceItems) {
2262 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002263 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002264 currentScreenItems.add(info);
2265 itemsOnScreen.add(info.id);
2266 } else {
2267 otherScreenItems.add(info);
2268 }
2269 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2270 currentScreenItems.add(info);
2271 itemsOnScreen.add(info.id);
2272 } else {
2273 if (itemsOnScreen.contains(info.container)) {
2274 currentScreenItems.add(info);
2275 itemsOnScreen.add(info.id);
2276 } else {
2277 otherScreenItems.add(info);
2278 }
2279 }
2280 }
2281 }
2282
2283 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002284 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 ArrayList<LauncherAppWidgetInfo> appWidgets,
2286 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2287 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288
2289 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002290 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002292 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002293 currentScreenWidgets.add(widget);
2294 } else {
2295 otherScreenWidgets.add(widget);
2296 }
2297 }
2298 }
2299
2300 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002301 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 HashMap<Long, ItemInfo> itemsIdMap,
2303 HashMap<Long, FolderInfo> folders,
2304 HashMap<Long, FolderInfo> currentScreenFolders,
2305 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306
2307 for (long id : folders.keySet()) {
2308 ItemInfo info = itemsIdMap.get(id);
2309 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002310 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002312 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 currentScreenFolders.put(id, folder);
2314 } else {
2315 otherScreenFolders.put(id, folder);
2316 }
2317 }
2318 }
2319
2320 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2321 * right) */
2322 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002323 final LauncherAppState app = LauncherAppState.getInstance();
2324 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325 // XXX: review this
2326 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002327 @Override
2328 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002329 int cellCountX = (int) grid.numColumns;
2330 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002331 int screenOffset = cellCountX * cellCountY;
2332 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002334 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002335 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002336 rhs.cellY * cellCountX + rhs.cellX);
2337 return (int) (lr - rr);
2338 }
2339 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002341
Adam Cohendcd297f2013-06-18 13:13:40 -07002342 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2343 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 final Runnable r = new Runnable() {
2345 @Override
2346 public void run() {
2347 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2348 if (callbacks != null) {
2349 callbacks.bindScreens(orderedScreens);
2350 }
2351 }
2352 };
2353 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2354 }
2355
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2357 final ArrayList<ItemInfo> workspaceItems,
2358 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2359 final HashMap<Long, FolderInfo> folders,
2360 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002361
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002362 final boolean postOnMainThread = (deferredBindRunnables != null);
2363
2364 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002365 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002367 final int start = i;
2368 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 final Runnable r = new Runnable() {
2370 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002371 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002372 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002373 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002374 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2375 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002376 }
2377 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 };
2379 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002380 synchronized (deferredBindRunnables) {
2381 deferredBindRunnables.add(r);
2382 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002384 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 }
Joe Onorato36115782010-06-17 13:28:48 -04002386 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387
2388 // Bind the folders
2389 if (!folders.isEmpty()) {
2390 final Runnable r = new Runnable() {
2391 public void run() {
2392 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2393 if (callbacks != null) {
2394 callbacks.bindFolders(folders);
2395 }
2396 }
2397 };
2398 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002399 synchronized (deferredBindRunnables) {
2400 deferredBindRunnables.add(r);
2401 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002403 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 }
2405 }
2406
2407 // Bind the widgets, one at a time
2408 N = appWidgets.size();
2409 for (int i = 0; i < N; i++) {
2410 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2411 final Runnable r = new Runnable() {
2412 public void run() {
2413 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2414 if (callbacks != null) {
2415 callbacks.bindAppWidget(widget);
2416 }
2417 }
2418 };
2419 if (postOnMainThread) {
2420 deferredBindRunnables.add(r);
2421 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002422 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423 }
2424 }
2425 }
2426
2427 /**
2428 * Binds all loaded data to actual views on the main thread.
2429 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002430 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431 final long t = SystemClock.uptimeMillis();
2432 Runnable r;
2433
2434 // Don't use these two variables in any of the callback runnables.
2435 // Otherwise we hold a reference to them.
2436 final Callbacks oldCallbacks = mCallbacks.get();
2437 if (oldCallbacks == null) {
2438 // This launcher has exited and nobody bothered to tell us. Just bail.
2439 Log.w(TAG, "LoaderTask running with no launcher");
2440 return;
2441 }
2442
Winson Chung9b9fb962013-11-15 15:39:34 -08002443 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002444 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2445 ArrayList<LauncherAppWidgetInfo> appWidgets =
2446 new ArrayList<LauncherAppWidgetInfo>();
2447 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2448 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002449 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002450 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451 workspaceItems.addAll(sBgWorkspaceItems);
2452 appWidgets.addAll(sBgAppWidgets);
2453 folders.putAll(sBgFolders);
2454 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002455 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 }
2457
Derek Prothro7aff3992013-12-10 14:00:37 -05002458 final boolean isLoadingSynchronously =
2459 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002460 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002461 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002462 if (currScreen >= orderedScreenIds.size()) {
2463 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002464 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002465 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002466 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002467 final long currentScreenId = currentScreen < 0
2468 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002469
2470 // Load all the items that are on the current page first (and in the process, unbind
2471 // all the existing workspace items before we call startBinding() below.
2472 unbindWorkspaceItemsOnMainThread();
2473
2474 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2476 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2477 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2478 new ArrayList<LauncherAppWidgetInfo>();
2479 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2480 new ArrayList<LauncherAppWidgetInfo>();
2481 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2482 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2483
Winson Chung9b9fb962013-11-15 15:39:34 -08002484 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002486 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002487 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002488 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002489 otherFolders);
2490 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2491 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2492
2493 // Tell the workspace that we're about to start binding items
2494 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002495 public void run() {
2496 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2497 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002499 }
2500 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 };
Winson Chung81b52252012-08-27 15:34:29 -07002502 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002503
Adam Cohendcd297f2013-06-18 13:13:40 -07002504 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2505
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506 // Load items on the current page
2507 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2508 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002509 if (isLoadingSynchronously) {
2510 r = new Runnable() {
2511 public void run() {
2512 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002513 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002514 callbacks.onPageBoundSynchronously(currentScreen);
2515 }
2516 }
2517 };
Winson Chung81b52252012-08-27 15:34:29 -07002518 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002519 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520
Winson Chung4a2afa32012-07-19 14:53:05 -07002521 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2522 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002523 synchronized (mDeferredBindRunnables) {
2524 mDeferredBindRunnables.clear();
2525 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002526 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002527 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528
2529 // Tell the workspace that we're done binding items
2530 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002531 public void run() {
2532 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2533 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002534 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002535 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536
Winson Chung98e030b2012-05-07 16:01:11 -07002537 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002538 if (DEBUG_LOADERS) {
2539 Log.d(TAG, "bound workspace in "
2540 + (SystemClock.uptimeMillis()-t) + "ms");
2541 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002542
2543 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002544 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002546 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002547 synchronized (mDeferredBindRunnables) {
2548 mDeferredBindRunnables.add(r);
2549 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002550 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002551 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002552 }
Joe Onorato36115782010-06-17 13:28:48 -04002553 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002554
Joe Onorato36115782010-06-17 13:28:48 -04002555 private void loadAndBindAllApps() {
2556 if (DEBUG_LOADERS) {
2557 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2558 }
2559 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002560 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002561 synchronized (LoaderTask.this) {
2562 if (mStopped) {
2563 return;
2564 }
2565 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002566 }
Joe Onorato36115782010-06-17 13:28:48 -04002567 } else {
2568 onlyBindAllApps();
2569 }
2570 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002571
Joe Onorato36115782010-06-17 13:28:48 -04002572 private void onlyBindAllApps() {
2573 final Callbacks oldCallbacks = mCallbacks.get();
2574 if (oldCallbacks == null) {
2575 // This launcher has exited and nobody bothered to tell us. Just bail.
2576 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2577 return;
2578 }
2579
2580 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002581 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002582 final ArrayList<AppInfo> list
2583 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002584 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002585 public void run() {
2586 final long t = SystemClock.uptimeMillis();
2587 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2588 if (callbacks != null) {
2589 callbacks.bindAllApplications(list);
2590 }
2591 if (DEBUG_LOADERS) {
2592 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2593 + (SystemClock.uptimeMillis()-t) + "ms");
2594 }
2595 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002596 };
2597 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002598 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002599 r.run();
2600 } else {
2601 mHandler.post(r);
2602 }
Joe Onorato36115782010-06-17 13:28:48 -04002603 }
2604
Winson Chung64359a52013-07-08 17:17:08 -07002605 private void loadAllApps() {
2606 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002607
Joe Onorato36115782010-06-17 13:28:48 -04002608 final Callbacks oldCallbacks = mCallbacks.get();
2609 if (oldCallbacks == null) {
2610 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002611 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002612 return;
2613 }
2614
Winson Chung64359a52013-07-08 17:17:08 -07002615 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002616 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2617 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2618
Winson Chung64359a52013-07-08 17:17:08 -07002619 // Clear the list of apps
2620 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002621
Winson Chung64359a52013-07-08 17:17:08 -07002622 // Query for the set of apps
2623 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2624 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2625 if (DEBUG_LOADERS) {
2626 Log.d(TAG, "queryIntentActivities took "
2627 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2628 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2629 }
2630 // Fail if we don't have any apps
2631 if (apps == null || apps.isEmpty()) {
2632 return;
2633 }
2634 // Sort the applications by name
2635 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2636 Collections.sort(apps,
2637 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2638 if (DEBUG_LOADERS) {
2639 Log.d(TAG, "sort took "
2640 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002641 }
2642
Winson Chung64359a52013-07-08 17:17:08 -07002643 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002644 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002645 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002646 // This builds the icon bitmaps.
2647 mBgAllAppsList.add(new AppInfo(packageManager, app,
2648 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002649 }
2650
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002651 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002652 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2653 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002654
2655 // Post callback on main thread
2656 mHandler.post(new Runnable() {
2657 public void run() {
2658 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002659 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002660 if (callbacks != null) {
2661 callbacks.bindAllApplications(added);
2662 if (DEBUG_LOADERS) {
2663 Log.d(TAG, "bound " + added.size() + " apps in "
2664 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2665 }
2666 } else {
2667 Log.i(TAG, "not binding apps: no Launcher activity");
2668 }
2669 }
2670 });
2671
Joe Onorato36115782010-06-17 13:28:48 -04002672 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002673 Log.d(TAG, "Icons processed in "
2674 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002675 }
2676 }
2677
2678 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002679 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002680 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2681 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2682 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2683 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2684 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2685 }
Joe Onorato36115782010-06-17 13:28:48 -04002686 }
2687 }
2688
2689 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002690 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002691 }
2692
2693 private class PackageUpdatedTask implements Runnable {
2694 int mOp;
2695 String[] mPackages;
2696
2697 public static final int OP_NONE = 0;
2698 public static final int OP_ADD = 1;
2699 public static final int OP_UPDATE = 2;
2700 public static final int OP_REMOVE = 3; // uninstlled
2701 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2702
2703
2704 public PackageUpdatedTask(int op, String[] packages) {
2705 mOp = op;
2706 mPackages = packages;
2707 }
2708
2709 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002710 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002711
2712 final String[] packages = mPackages;
2713 final int N = packages.length;
2714 switch (mOp) {
2715 case OP_ADD:
2716 for (int i=0; i<N; i++) {
2717 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Chris Wren6d0dde02014-02-10 12:16:54 -05002718 mIconCache.remove(packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002719 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002720 }
2721 break;
2722 case OP_UPDATE:
2723 for (int i=0; i<N; i++) {
2724 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002725 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002726 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002727 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002728 }
2729 break;
2730 case OP_REMOVE:
2731 case OP_UNAVAILABLE:
2732 for (int i=0; i<N; i++) {
2733 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002734 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002735 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002736 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002737 }
2738 break;
2739 }
2740
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002741 ArrayList<AppInfo> added = null;
2742 ArrayList<AppInfo> modified = null;
2743 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002744
Adam Cohen487f7dd2012-06-28 18:12:10 -07002745 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002746 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002747 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002748 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002749 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002750 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002751 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002752 }
Winson Chung5d55f332012-07-16 20:45:03 -07002753 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002754 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002755 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002756 }
2757
Joe Onorato36115782010-06-17 13:28:48 -04002758 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2759 if (callbacks == null) {
2760 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2761 return;
2762 }
2763
2764 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002765 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002766 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002767 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002768 addAndBindAddedWorkspaceApps(context, addedInfos);
2769 } else {
2770 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002771 }
Joe Onorato36115782010-06-17 13:28:48 -04002772 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002773
Joe Onorato36115782010-06-17 13:28:48 -04002774 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002775 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002776
2777 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002778 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002779 ArrayList<ItemInfo> infos =
2780 getItemInfoForComponentName(a.componentName);
2781 for (ItemInfo i : infos) {
2782 if (isShortcutInfoUpdateable(i)) {
2783 ShortcutInfo info = (ShortcutInfo) i;
2784 info.title = a.title.toString();
2785 updateItemInDatabase(context, info);
2786 }
2787 }
2788 }
2789
Joe Onorato36115782010-06-17 13:28:48 -04002790 mHandler.post(new Runnable() {
2791 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002792 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2793 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002794 callbacks.bindAppsUpdated(modifiedFinal);
2795 }
2796 }
2797 });
2798 }
Winson Chung83892cc2013-05-01 16:53:33 -07002799
Winson Chungdf95eb12013-10-16 14:57:07 -07002800 final ArrayList<String> removedPackageNames =
2801 new ArrayList<String>();
2802 if (mOp == OP_REMOVE) {
2803 // Mark all packages in the broadcast to be removed
2804 removedPackageNames.addAll(Arrays.asList(packages));
2805 } else if (mOp == OP_UPDATE) {
2806 // Mark disabled packages in the broadcast to be removed
2807 final PackageManager pm = context.getPackageManager();
2808 for (int i=0; i<N; i++) {
2809 if (isPackageDisabled(pm, packages[i])) {
2810 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002811 }
2812 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002813 }
2814 // Remove all the components associated with this package
2815 for (String pn : removedPackageNames) {
2816 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2817 for (ItemInfo i : infos) {
2818 deleteItemFromDatabase(context, i);
2819 }
2820 }
2821 // Remove all the specific components
2822 for (AppInfo a : removedApps) {
2823 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2824 for (ItemInfo i : infos) {
2825 deleteItemFromDatabase(context, i);
2826 }
2827 }
2828 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2829 // Remove any queued items from the install queue
2830 String spKey = LauncherAppState.getSharedPreferencesKey();
2831 SharedPreferences sp =
2832 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2833 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2834 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002835 mHandler.post(new Runnable() {
2836 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002837 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2838 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002839 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002840 }
2841 }
2842 });
Joe Onoratobe386092009-11-17 17:32:16 -08002843 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002844
Michael Jurkac402cd92013-05-20 15:49:32 +02002845 final ArrayList<Object> widgetsAndShortcuts =
2846 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002847 mHandler.post(new Runnable() {
2848 @Override
2849 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002850 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2851 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002852 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002853 }
2854 }
2855 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002856
2857 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002858 mHandler.post(new Runnable() {
2859 public void run() {
2860 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2861 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002862 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002863 }
2864 }
2865 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 }
2867 }
2868
Michael Jurkac402cd92013-05-20 15:49:32 +02002869 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2870 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2871 PackageManager packageManager = context.getPackageManager();
2872 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2873 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2874 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2875 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2876 Collections.sort(widgetsAndShortcuts,
2877 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2878 return widgetsAndShortcuts;
2879 }
2880
Adam Cohen556f6132014-01-15 15:18:08 -08002881 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002882 try {
2883 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2884 return !pi.applicationInfo.enabled;
2885 } catch (NameNotFoundException e) {
2886 // Fall through
2887 }
2888 return false;
2889 }
Adam Cohen556f6132014-01-15 15:18:08 -08002890
2891 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002892 if (cn == null) {
2893 return false;
2894 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002895 if (isPackageDisabled(pm, cn.getPackageName())) {
2896 return false;
2897 }
Winson Chungee055712013-07-30 14:46:24 -07002898
2899 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002900 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002901 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002902 return (pm.getActivityInfo(cn, 0) != null);
2903 } catch (NameNotFoundException e) {
2904 return false;
2905 }
2906 }
2907
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002909 * Make an ShortcutInfo object for a restored application or shortcut item that points
2910 * to a package that is not yet installed on the system.
2911 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002912 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002913 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002914 if (cursor != null) {
2915 info.title = cursor.getString(titleIndex);
2916 } else {
2917 info.title = "";
2918 }
Chris Wren6d0dde02014-02-10 12:16:54 -05002919 info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
Chris Wrenf4d08112014-01-16 18:13:56 -05002920 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002921 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002922 return info;
2923 }
2924
2925 /**
2926 * Make an Intent object for a restored application or shortcut item that points
2927 * to the market page for the item.
2928 */
2929 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002930 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002931 ComponentName componentName = intent.getComponent();
2932 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2933 Uri marketUri = new Uri.Builder()
2934 .scheme("market")
2935 .authority("details")
2936 .appendQueryParameter("id", componentName.getPackageName())
2937 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002938 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002939 marketIntent.setData(marketUri);
2940 return marketIntent;
2941 }
2942
2943 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002944 * This is called from the code that adds shortcuts from the intent receiver. This
2945 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 */
Joe Onorato56d82912010-03-07 14:32:10 -05002947 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002948 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950
Joe Onorato56d82912010-03-07 14:32:10 -05002951 /**
2952 * Make an ShortcutInfo object for a shortcut that is an application.
2953 *
2954 * If c is not null, then it will be used to fill in missing data like the title and icon.
2955 */
2956 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002957 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002958 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002959 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002960 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002961 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002962 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002963 } else {
2964 try {
2965 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2966 info.initFlagsAndFirstInstallTime(pi);
2967 } catch (NameNotFoundException e) {
2968 Log.d(TAG, "getPackInfo failed for package " +
2969 componentName.getPackageName());
2970 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002971 }
2972
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002973 // TODO: See if the PackageManager knows about this case. If it doesn't
2974 // then return null & delete this.
2975
Joe Onorato56d82912010-03-07 14:32:10 -05002976 // the resource -- This may implicitly give us back the fallback icon,
2977 // but don't worry about that. All we're doing with usingFallbackIcon is
2978 // to avoid saving lots of copies of that in the database, and most apps
2979 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002980
2981 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2982 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2983 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002984 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002985 ResolveInfo resolveInfo = null;
2986 ComponentName oldComponent = intent.getComponent();
2987 Intent newIntent = new Intent(intent.getAction(), null);
2988 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2989 newIntent.setPackage(oldComponent.getPackageName());
2990 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2991 for (ResolveInfo i : infos) {
2992 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2993 i.activityInfo.name);
2994 if (cn.equals(oldComponent)) {
2995 resolveInfo = i;
2996 }
2997 }
2998 if (resolveInfo == null) {
2999 resolveInfo = manager.resolveActivity(intent, 0);
3000 }
Joe Onorato56d82912010-03-07 14:32:10 -05003001 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07003002 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003003 }
Joe Onorato56d82912010-03-07 14:32:10 -05003004 // the db
3005 if (icon == null) {
3006 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003007 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003008 }
3009 }
3010 // the fallback icon
3011 if (icon == null) {
3012 icon = getFallbackIcon();
3013 info.usingFallbackIcon = true;
3014 }
3015 info.setIcon(icon);
3016
3017 // from the resource
3018 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003019 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
3020 if (labelCache != null && labelCache.containsKey(key)) {
3021 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07003022 } else {
3023 info.title = resolveInfo.activityInfo.loadLabel(manager);
3024 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003025 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003026 }
3027 }
Joe Onorato56d82912010-03-07 14:32:10 -05003028 }
3029 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003031 if (c != null) {
3032 info.title = c.getString(titleIndex);
3033 }
3034 }
3035 // fall back to the class name of the activity
3036 if (info.title == null) {
3037 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003038 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
3040 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003042
Winson Chung64359a52013-07-08 17:17:08 -07003043 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3044 ItemInfoFilter f) {
3045 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3046 for (ItemInfo i : infos) {
3047 if (i instanceof ShortcutInfo) {
3048 ShortcutInfo info = (ShortcutInfo) i;
3049 ComponentName cn = info.intent.getComponent();
3050 if (cn != null && f.filterItem(null, info, cn)) {
3051 filtered.add(info);
3052 }
3053 } else if (i instanceof FolderInfo) {
3054 FolderInfo info = (FolderInfo) i;
3055 for (ShortcutInfo s : info.contents) {
3056 ComponentName cn = s.intent.getComponent();
3057 if (cn != null && f.filterItem(info, s, cn)) {
3058 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003059 }
3060 }
Winson Chung64359a52013-07-08 17:17:08 -07003061 } else if (i instanceof LauncherAppWidgetInfo) {
3062 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3063 ComponentName cn = info.providerName;
3064 if (cn != null && f.filterItem(null, info, cn)) {
3065 filtered.add(info);
3066 }
Winson Chung8a435102012-08-30 17:16:53 -07003067 }
3068 }
Winson Chung64359a52013-07-08 17:17:08 -07003069 return new ArrayList<ItemInfo>(filtered);
3070 }
3071
3072 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07003073 ItemInfoFilter filter = new ItemInfoFilter() {
3074 @Override
3075 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3076 return cn.getPackageName().equals(pn);
3077 }
3078 };
3079 return filterItemInfos(sBgItemsIdMap.values(), filter);
3080 }
3081
3082 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003083 ItemInfoFilter filter = new ItemInfoFilter() {
3084 @Override
3085 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3086 return cn.equals(cname);
3087 }
3088 };
3089 return filterItemInfos(sBgItemsIdMap.values(), filter);
3090 }
3091
3092 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3093 if (i instanceof ShortcutInfo) {
3094 ShortcutInfo info = (ShortcutInfo) i;
3095 // We need to check for ACTION_MAIN otherwise getComponent() might
3096 // return null for some shortcuts (for instance, for shortcuts to
3097 // web pages.)
3098 Intent intent = info.intent;
3099 ComponentName name = intent.getComponent();
3100 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3101 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3102 return true;
3103 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003104 // placeholder shortcuts get special treatment, let them through too.
3105 if (info.getRestoredIntent() != null) {
3106 return true;
3107 }
Winson Chung64359a52013-07-08 17:17:08 -07003108 }
3109 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003110 }
3111
3112 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003113 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003115 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003116 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3117 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118
Joe Onorato56d82912010-03-07 14:32:10 -05003119 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003120 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003123 // TODO: If there's an explicit component and we can't install that, delete it.
3124
Joe Onorato56d82912010-03-07 14:32:10 -05003125 info.title = c.getString(titleIndex);
3126
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 int iconType = c.getInt(iconTypeIndex);
3128 switch (iconType) {
3129 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3130 String packageName = c.getString(iconPackageIndex);
3131 String resourceName = c.getString(iconResourceIndex);
3132 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003133 info.customIcon = false;
3134 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003136 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003137 if (resources != null) {
3138 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003139 icon = Utilities.createIconBitmap(
3140 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003141 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003142 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003143 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 }
Joe Onorato56d82912010-03-07 14:32:10 -05003145 // the db
3146 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003147 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003148 }
3149 // the fallback icon
3150 if (icon == null) {
3151 icon = getFallbackIcon();
3152 info.usingFallbackIcon = true;
3153 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 break;
3155 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003156 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003157 if (icon == null) {
3158 icon = getFallbackIcon();
3159 info.customIcon = false;
3160 info.usingFallbackIcon = true;
3161 } else {
3162 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003164 break;
3165 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003166 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003167 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 info.customIcon = false;
3169 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003171 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 return info;
3173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174
Michael Jurka931dc972011-08-05 15:08:15 -07003175 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003176 @SuppressWarnings("all") // suppress dead code warning
3177 final boolean debug = false;
3178 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003179 Log.d(TAG, "getIconFromCursor app="
3180 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3181 }
3182 byte[] data = c.getBlob(iconIndex);
3183 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003184 return Utilities.createIconBitmap(
3185 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003186 } catch (Exception e) {
3187 return null;
3188 }
3189 }
3190
Winson Chung3d503fb2011-07-13 17:25:49 -07003191 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3192 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003193 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003194 if (info == null) {
3195 return null;
3196 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003197 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003198
3199 return info;
3200 }
3201
Winson Chunga9abd0e2010-10-27 17:18:37 -07003202 /**
Winson Chung55cef262010-10-28 14:14:18 -07003203 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3204 */
3205 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3206 ComponentName component) {
3207 List<AppWidgetProviderInfo> widgets =
3208 AppWidgetManager.getInstance(context).getInstalledProviders();
3209 for (AppWidgetProviderInfo info : widgets) {
3210 if (info.provider.equals(component)) {
3211 return info;
3212 }
3213 }
3214 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003215 }
3216
Winson Chung68846fd2010-10-29 11:00:27 -07003217 /**
3218 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3219 */
3220 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3221 final PackageManager packageManager = context.getPackageManager();
3222 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3223 new ArrayList<WidgetMimeTypeHandlerData>();
3224
3225 final Intent supportsIntent =
3226 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3227 supportsIntent.setType(mimeType);
3228
3229 // Create a set of widget configuration components that we can test against
3230 final List<AppWidgetProviderInfo> widgets =
3231 AppWidgetManager.getInstance(context).getInstalledProviders();
3232 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3233 new HashMap<ComponentName, AppWidgetProviderInfo>();
3234 for (AppWidgetProviderInfo info : widgets) {
3235 configurationComponentToWidget.put(info.configure, info);
3236 }
3237
3238 // Run through each of the intents that can handle this type of clip data, and cross
3239 // reference them with the components that are actual configuration components
3240 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3241 PackageManager.MATCH_DEFAULT_ONLY);
3242 for (ResolveInfo info : activities) {
3243 final ActivityInfo activityInfo = info.activityInfo;
3244 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3245 activityInfo.name);
3246 if (configurationComponentToWidget.containsKey(infoComponent)) {
3247 supportedConfigurationActivities.add(
3248 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3249 configurationComponentToWidget.get(infoComponent)));
3250 }
3251 }
3252 return supportedConfigurationActivities;
3253 }
3254
Winson Chunga9abd0e2010-10-27 17:18:37 -07003255 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003256 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3257 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3258 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3259
Adam Cohend9198822011-11-22 16:42:47 -08003260 if (intent == null) {
3261 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3262 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3263 return null;
3264 }
3265
Joe Onorato0589f0f2010-02-08 13:44:00 -08003266 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003267 boolean customIcon = false;
3268 ShortcutIconResource iconResource = null;
3269
3270 if (bitmap != null && bitmap instanceof Bitmap) {
3271 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003272 customIcon = true;
3273 } else {
3274 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3275 if (extra != null && extra instanceof ShortcutIconResource) {
3276 try {
3277 iconResource = (ShortcutIconResource) extra;
3278 final PackageManager packageManager = context.getPackageManager();
3279 Resources resources = packageManager.getResourcesForApplication(
3280 iconResource.packageName);
3281 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003282 icon = Utilities.createIconBitmap(
3283 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003284 } catch (Exception e) {
3285 Log.w(TAG, "Could not load shortcut icon: " + extra);
3286 }
3287 }
3288 }
3289
Michael Jurkac9d95c52011-08-29 14:03:34 -07003290 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003291
3292 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003293 if (fallbackIcon != null) {
3294 icon = fallbackIcon;
3295 } else {
3296 icon = getFallbackIcon();
3297 info.usingFallbackIcon = true;
3298 }
Joe Onorato56d82912010-03-07 14:32:10 -05003299 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003300 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003301
Joe Onorato0589f0f2010-02-08 13:44:00 -08003302 info.title = name;
3303 info.intent = intent;
3304 info.customIcon = customIcon;
3305 info.iconResource = iconResource;
3306
3307 return info;
3308 }
3309
Winson Chungaac01e12011-08-17 10:37:13 -07003310 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3311 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003312 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003313 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003314 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003315 }
Joe Onorato56d82912010-03-07 14:32:10 -05003316 // If this icon doesn't have a custom icon, check to see
3317 // what's stored in the DB, and if it doesn't match what
3318 // we're going to show, store what we are going to show back
3319 // into the DB. We do this so when we're loading, if the
3320 // package manager can't find an icon (for example because
3321 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003322 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003323 cache.put(info, c.getBlob(iconIndex));
3324 return true;
3325 }
3326 return false;
3327 }
3328 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3329 boolean needSave = false;
3330 try {
3331 if (data != null) {
3332 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3333 Bitmap loaded = info.getIcon(mIconCache);
3334 needSave = !saved.sameAs(loaded);
3335 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003336 needSave = true;
3337 }
Winson Chungaac01e12011-08-17 10:37:13 -07003338 } catch (Exception e) {
3339 needSave = true;
3340 }
3341 if (needSave) {
3342 Log.d(TAG, "going to save icon bitmap for info=" + info);
3343 // This is slower than is ideal, but this only happens once
3344 // or when the app is updated with a new icon.
3345 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003346 }
3347 }
3348
Joe Onorato9c1289c2009-08-17 11:03:03 -04003349 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003350 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003351 * or make a new one.
3352 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003353 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003354 // See if a placeholder was created for us already
3355 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003356 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003357 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003358 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 folders.put(id, folderInfo);
3360 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003361 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003362 }
3363
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003364 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003365 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003366 return new Comparator<AppInfo>() {
3367 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003368 int result = collator.compare(a.title.toString().trim(),
3369 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003370 if (result == 0) {
3371 result = a.componentName.compareTo(b.componentName);
3372 }
3373 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003374 }
Winson Chung11904872012-09-17 16:58:46 -07003375 };
3376 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003377 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3378 = new Comparator<AppInfo>() {
3379 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003380 if (a.firstInstallTime < b.firstInstallTime) return 1;
3381 if (a.firstInstallTime > b.firstInstallTime) return -1;
3382 return 0;
3383 }
3384 };
Winson Chung11904872012-09-17 16:58:46 -07003385 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3386 final Collator collator = Collator.getInstance();
3387 return new Comparator<AppWidgetProviderInfo>() {
3388 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003389 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003390 }
3391 };
3392 }
Winson Chung5308f242011-08-18 12:12:41 -07003393 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3394 if (info.activityInfo != null) {
3395 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3396 } else {
3397 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3398 }
3399 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003400 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003401 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003402 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003403 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003404 ShortcutNameComparator(PackageManager pm) {
3405 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003406 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003407 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003408 }
3409 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3410 mPackageManager = pm;
3411 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003412 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003413 }
3414 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003415 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003416 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3417 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3418 if (mLabelCache.containsKey(keyA)) {
3419 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003420 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003421 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003422
Winson Chung5308f242011-08-18 12:12:41 -07003423 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003424 }
Winson Chung5308f242011-08-18 12:12:41 -07003425 if (mLabelCache.containsKey(keyB)) {
3426 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003427 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003428 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003429
Winson Chung5308f242011-08-18 12:12:41 -07003430 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003431 }
Winson Chung11904872012-09-17 16:58:46 -07003432 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003433 }
3434 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003435 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003436 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003437 private PackageManager mPackageManager;
3438 private HashMap<Object, String> mLabelCache;
3439 WidgetAndShortcutNameComparator(PackageManager pm) {
3440 mPackageManager = pm;
3441 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003442 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003443 }
3444 public final int compare(Object a, Object b) {
3445 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003446 if (mLabelCache.containsKey(a)) {
3447 labelA = mLabelCache.get(a);
3448 } else {
3449 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003450 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003451 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003452 mLabelCache.put(a, labelA);
3453 }
3454 if (mLabelCache.containsKey(b)) {
3455 labelB = mLabelCache.get(b);
3456 } else {
3457 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003458 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003459 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003460 mLabelCache.put(b, labelB);
3461 }
Winson Chung11904872012-09-17 16:58:46 -07003462 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003463 }
3464 };
Joe Onoratobe386092009-11-17 17:32:16 -08003465
3466 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003467 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003468 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3469 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3470 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3471 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003472 if (mLoaderTask != null) {
3473 mLoaderTask.dumpState();
3474 } else {
3475 Log.d(TAG, "mLoaderTask=null");
3476 }
Joe Onoratobe386092009-11-17 17:32:16 -08003477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003478}