blob: 0efd1728fbe4fa1e17af179cb4a64184d50d73c6 [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;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070073
Daniel Sandler8707e0f2013-08-15 15:54:18 -070074 // true = use a "More Apps" folder for non-workspace apps on upgrade
75 // false = strew non-workspace apps across the workspace on upgrade
76 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
77
Dan Sandlerd5024042014-01-09 15:01:33 -050078 public static final int LOADER_FLAG_NONE = 0;
79 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
80 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
81
Joe Onorato36115782010-06-17 13:28:48 -040082 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050083 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080084
Winson Chungee055712013-07-30 14:46:24 -070085 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -080086 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -040087
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private final Object mLock = new Object();
90 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040091 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020093 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Winson Chung81b52252012-08-27 15:34:29 -070095 // Specific runnable types that are run on the main thread deferred handler, this allows us to
96 // clear all queued binding runnables when the Launcher activity is destroyed.
97 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
98 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
99
100
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700101 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
102 static {
103 sWorkerThread.start();
104 }
105 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
106
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 // We start off with everything not loaded. After that, we assume that
108 // our monitoring of the package manager provides all updates and we never
109 // need to do a requery. These are only ever touched from the loader thread.
110 private boolean mWorkspaceLoaded;
111 private boolean mAllAppsLoaded;
112
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113 // When we are loading pages synchronously, we can't just post the binding of items on the side
114 // pages as this delays the rotation process. Instead, we wait for a callback from the first
115 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
116 // a normal load, we also clear this set of Runnables.
117 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700121 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700122 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800123
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700124 // The lock that must be acquired before referencing any static bg data structures. Unlike
125 // other locks, this one can generally be held long-term because we never expect any of these
126 // static data structures to be referenced outside of the worker thread except on the first
127 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700128 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133
Adam Cohen487f7dd2012-06-28 18:12:10 -0700134 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
135 // created by LauncherModel that are directly on the home screen (however, no widgets or
136 // shortcuts within folders).
137 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138
Adam Cohen487f7dd2012-06-28 18:12:10 -0700139 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
140 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 new ArrayList<LauncherAppWidgetInfo>();
142
Adam Cohen487f7dd2012-06-28 18:12:10 -0700143 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
144 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700145
Adam Cohen487f7dd2012-06-28 18:12:10 -0700146 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
147 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700148
149 // sBgWorkspaceScreens is the ordered set of workspace screens.
150 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
151
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 // </ only access in worker thread >
153
154 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800155 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Reena Lee99a73f32011-10-24 17:27:37 -0700157 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700160 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public int getCurrentWorkspaceScreen();
162 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700163 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
164 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700166 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500167 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700168 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200170 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700171 public void bindAppsAdded(ArrayList<Long> newScreens,
172 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700173 ArrayList<ItemInfo> addAnimated,
174 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200175 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500176 public void updatePackageState(String pkgName, int state);
Winson Chung83892cc2013-05-01 16:53:33 -0700177 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700178 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200179 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100180 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700181 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700182 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700183 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Winson Chung64359a52013-07-08 17:17:08 -0700186 public interface ItemInfoFilter {
187 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
188 }
189
Bjorn Bringert1307f632013-10-03 22:31:03 +0100190 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800191 Context context = app.getContext();
192 ContentResolver contentResolver = context.getContentResolver();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400193
Winson Chungee055712013-07-30 14:46:24 -0700194 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Winson Chungeb23cb22014-03-06 10:39:43 -0800195 ContentProviderClient client = contentResolver.acquireContentProviderClient(
196 LauncherSettings.Favorites.OLD_CONTENT_URI);
197 mOldContentProviderExists = (client != null);
198 if (client != null) {
199 client.release();
200 }
Daniel Sandlere4f98912013-06-25 15:13:26 -0400201 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100202 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 mIconCache = iconCache;
204
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400205 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700206 Configuration config = res.getConfiguration();
207 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800208 }
209
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700210 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
211 * posted on the main thread handler. */
212 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700213 runOnMainThread(r, 0);
214 }
215 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700216 if (sWorkerThread.getThreadId() == Process.myTid()) {
217 // If we are on the worker thread, post onto the main handler
218 mHandler.post(r);
219 } else {
220 r.run();
221 }
222 }
223
224 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
225 * posted on the worker thread handler. */
226 private static void runOnWorkerThread(Runnable r) {
227 if (sWorkerThread.getThreadId() == Process.myTid()) {
228 r.run();
229 } else {
230 // If we are not on the worker thread, then post to the worker handler
231 sWorker.post(r);
232 }
233 }
234
Winson Chunge43a1e72014-01-15 10:33:02 -0800235 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
236 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800237 }
238
Winson Chungc9168342013-06-26 14:54:55 -0700239 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
240 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700241 LauncherAppState app = LauncherAppState.getInstance();
242 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
243 final int xCount = (int) grid.numColumns;
244 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700245 boolean[][] occupied = new boolean[xCount][yCount];
246
247 int cellX, cellY, spanX, spanY;
248 for (int i = 0; i < items.size(); ++i) {
249 final ItemInfo item = items.get(i);
250 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
251 if (item.screenId == screen) {
252 cellX = item.cellX;
253 cellY = item.cellY;
254 spanX = item.spanX;
255 spanY = item.spanY;
256 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
257 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
258 occupied[x][y] = true;
259 }
260 }
261 }
262 }
263 }
264
265 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
266 }
267 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700268 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700269 int firstScreenIndex,
270 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 // Lock on the app so that we don't try and get the items while apps are being added
272 LauncherAppState app = LauncherAppState.getInstance();
273 LauncherModel model = app.getModel();
274 boolean found = false;
275 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700276 if (sWorkerThread.getThreadId() != Process.myTid()) {
277 // Flush the LauncherModel worker thread, so that if we just did another
278 // processInstallShortcut, we give it time for its shortcut to get added to the
279 // database (getItemsInLocalCoordinates reads the database)
280 model.flushWorkerThread();
281 }
Winson Chungc9168342013-06-26 14:54:55 -0700282 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700283
284 // Try adding to the workspace screens incrementally, starting at the default or center
285 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700286 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
287 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700288 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700289 int[] tmpCoordinates = new int[2];
290 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700291 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700292 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700293 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700294 }
295 }
296 }
Winson Chungc9168342013-06-26 14:54:55 -0700297 return null;
298 }
299
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500300 public void setPackageState(final String pkgName, final int state) {
301 // Process the updated package state
302 Runnable r = new Runnable() {
303 public void run() {
304 Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
305 if (callbacks != null) {
306 callbacks.updatePackageState(pkgName, state);
307 }
308 }
309 };
310 mHandler.post(r);
311 }
312
Adam Cohen76a47a12014-02-05 11:47:43 -0800313 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
314 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
315
316 if (allAppsApps == null) {
317 throw new RuntimeException("allAppsApps must not be null");
318 }
319 if (allAppsApps.isEmpty()) {
320 return;
321 }
322
Chris Wrenb6d4c282014-01-27 14:17:08 -0500323 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
324 Iterator<AppInfo> iter = allAppsApps.iterator();
325 while (iter.hasNext()) {
326 ItemInfo a = iter.next();
327 if (LauncherModel.appWasRestored(ctx, a.getIntent())) {
328 restoredAppsFinal.add((AppInfo) a);
329 }
330 }
331
Adam Cohen76a47a12014-02-05 11:47:43 -0800332 // Process the newly added applications and add them to the database first
333 Runnable r = new Runnable() {
334 public void run() {
335 runOnMainThread(new Runnable() {
336 public void run() {
337 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
338 if (callbacks == cb && cb != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500339 if (!restoredAppsFinal.isEmpty()) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500340 for (AppInfo info : restoredAppsFinal) {
341 final Intent intent = info.getIntent();
342 if (intent != null) {
343 mIconCache.deletePreloadedIcon(intent.getComponent());
344 }
345 }
Chris Wrenb6d4c282014-01-27 14:17:08 -0500346 callbacks.bindAppsUpdated(restoredAppsFinal);
347 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500348 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800349 }
350 }
351 });
352 }
353 };
354 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700355 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800356
357 public void addAndBindAddedWorkspaceApps(final Context context,
358 final ArrayList<ItemInfo> workspaceApps) {
359 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
360
361 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800362 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
363 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800364 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700365 return;
Winson Chung997a9232013-07-24 15:33:46 -0700366 }
Winson Chung64359a52013-07-08 17:17:08 -0700367 // Process the newly added applications and add them to the database first
368 Runnable r = new Runnable() {
369 public void run() {
370 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
371 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500372 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700373
Winson Chung76828c82013-08-19 15:43:29 -0700374 // Get the list of workspace screens. We need to append to this list and
375 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
376 // called.
377 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
378 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
379 for (Integer i : orderedScreens.keySet()) {
380 long screenId = orderedScreens.get(i);
381 workspaceScreens.add(screenId);
382 }
383
Winson Chung64359a52013-07-08 17:17:08 -0700384 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700385 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700386 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700387 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700388 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700389 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700390
391 // Short-circuit this logic if the icon exists somewhere on the workspace
392 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500393 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
394 if (a instanceof AppInfo &&
395 LauncherModel.appWasRestored(context, launchIntent)) {
396 restoredAppsFinal.add((AppInfo) a);
397 }
Winson Chung64359a52013-07-08 17:17:08 -0700398 continue;
399 }
400
Winson Chung87412982013-10-03 18:34:14 -0700401 // Add this icon to the db, creating a new page if necessary. If there
402 // is only the empty page then we just add items to the first page.
403 // Otherwise, we add them to the next pages.
404 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700405 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700406 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700407 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200408 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700409
410 // If we can't find a valid position, then just add a new screen.
411 // This takes time so we need to re-queue the add until the new
412 // page is added. Create as many screens as necessary to satisfy
413 // the startSearchPageIndex.
414 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700415 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700416 while (numPagesToAdd > 0) {
417 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700418 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700419 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700420 addedWorkspaceScreensFinal.add(screenId);
421 numPagesToAdd--;
422 }
Winson Chung76828c82013-08-19 15:43:29 -0700423
Winson Chung64359a52013-07-08 17:17:08 -0700424 // Find the coordinate again
425 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700426 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700427 }
428 if (coords == null) {
429 throw new RuntimeException("Coordinates should not be null");
430 }
431
Winson Chung997a9232013-07-24 15:33:46 -0700432 ShortcutInfo shortcutInfo;
433 if (a instanceof ShortcutInfo) {
434 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200435 } else if (a instanceof AppInfo) {
436 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700437 } else {
438 throw new RuntimeException("Unexpected info type");
439 }
Winson Chung94d67682013-09-25 16:29:40 -0700440
Winson Chung64359a52013-07-08 17:17:08 -0700441 // Add the shortcut to the db
442 addItemToDatabase(context, shortcutInfo,
443 LauncherSettings.Favorites.CONTAINER_DESKTOP,
444 coords.first, coords.second[0], coords.second[1], false);
445 // Save the ShortcutInfo for binding in the workspace
446 addedShortcutsFinal.add(shortcutInfo);
447 }
448 }
449
Winson Chung76828c82013-08-19 15:43:29 -0700450 // Update the workspace screens
451 updateWorkspaceScreenOrder(context, workspaceScreens);
452
Adam Cohen76a47a12014-02-05 11:47:43 -0800453 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700454 runOnMainThread(new Runnable() {
455 public void run() {
456 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
457 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700458 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
459 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700460 if (!addedShortcutsFinal.isEmpty()) {
461 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
462 long lastScreenId = info.screenId;
463 for (ItemInfo i : addedShortcutsFinal) {
464 if (i.screenId == lastScreenId) {
465 addAnimated.add(i);
466 } else {
467 addNotAnimated.add(i);
468 }
Winson Chung997a9232013-07-24 15:33:46 -0700469 }
470 }
Winson Chungd64d1762013-08-20 14:37:16 -0700471 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800472 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500473 if (!restoredAppsFinal.isEmpty()) {
474 callbacks.bindAppsUpdated(restoredAppsFinal);
475 }
Winson Chung997a9232013-07-24 15:33:46 -0700476 }
Winson Chung64359a52013-07-08 17:17:08 -0700477 }
Winson Chung997a9232013-07-24 15:33:46 -0700478 });
479 }
Winson Chung64359a52013-07-08 17:17:08 -0700480 }
481 };
482 runOnWorkerThread(r);
483 }
484
Joe Onorato56d82912010-03-07 14:32:10 -0500485 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700486 if (mDefaultIcon == null) {
487 final Context context = LauncherAppState.getInstance().getContext();
488 mDefaultIcon = Utilities.createIconBitmap(
489 mIconCache.getFullResDefaultActivityIcon(), context);
490 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800491 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493
Winson Chung81b52252012-08-27 15:34:29 -0700494 public void unbindItemInfosAndClearQueuedBindRunnables() {
495 if (sWorkerThread.getThreadId() == Process.myTid()) {
496 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
497 "main thread");
498 }
499
500 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400501 synchronized (mDeferredBindRunnables) {
502 mDeferredBindRunnables.clear();
503 }
Winson Chung81b52252012-08-27 15:34:29 -0700504 // Remove any queued bind runnables
505 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
506 // Unbind all the workspace items
507 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700508 }
509
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700510 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700511 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700512 // Ensure that we don't use the same workspace items data structure on the main thread
513 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700514 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
515 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700516 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700517 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
518 tmpAppWidgets.addAll(sBgAppWidgets);
519 }
520 Runnable r = new Runnable() {
521 @Override
522 public void run() {
523 for (ItemInfo item : tmpWorkspaceItems) {
524 item.unbind();
525 }
526 for (ItemInfo item : tmpAppWidgets) {
527 item.unbind();
528 }
529 }
530 };
531 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700532 }
533
Joe Onorato9c1289c2009-08-17 11:03:03 -0400534 /**
535 * Adds an item to the DB if it was not created previously, or move it to a new
536 * <container, screen, cellX, cellY>
537 */
538 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700539 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400540 if (item.container == ItemInfo.NO_ID) {
541 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700542 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400543 } else {
544 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700545 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 }
547 }
548
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700549 static void checkItemInfoLocked(
550 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
551 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
552 if (modelItem != null && item != modelItem) {
553 // check all the data is consistent
554 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
555 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
556 ShortcutInfo shortcut = (ShortcutInfo) item;
557 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
558 modelShortcut.intent.filterEquals(shortcut.intent) &&
559 modelShortcut.id == shortcut.id &&
560 modelShortcut.itemType == shortcut.itemType &&
561 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700562 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700563 modelShortcut.cellX == shortcut.cellX &&
564 modelShortcut.cellY == shortcut.cellY &&
565 modelShortcut.spanX == shortcut.spanX &&
566 modelShortcut.spanY == shortcut.spanY &&
567 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
568 (modelShortcut.dropPos != null &&
569 shortcut.dropPos != null &&
570 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
571 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
572 // For all intents and purposes, this is the same object
573 return;
574 }
575 }
576
577 // the modelItem needs to match up perfectly with item if our model is
578 // to be consistent with the database-- for now, just require
579 // modelItem == item or the equality check above
580 String msg = "item: " + ((item != null) ? item.toString() : "null") +
581 "modelItem: " +
582 ((modelItem != null) ? modelItem.toString() : "null") +
583 "Error: ItemInfo passed to checkItemInfo doesn't match original";
584 RuntimeException e = new RuntimeException(msg);
585 if (stackTrace != null) {
586 e.setStackTrace(stackTrace);
587 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800588 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700589 }
590 }
591
Michael Jurka816474f2012-06-25 14:49:02 -0700592 static void checkItemInfo(final ItemInfo item) {
593 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
594 final long itemId = item.id;
595 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700596 public void run() {
597 synchronized (sBgLock) {
598 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700599 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700600 }
601 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700602 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700603 }
604
Michael Jurkac9d95c52011-08-29 14:03:34 -0700605 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
606 final ItemInfo item, final String callingFunction) {
607 final long itemId = item.id;
608 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
609 final ContentResolver cr = context.getContentResolver();
610
Adam Cohen487f7dd2012-06-28 18:12:10 -0700611 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700612 Runnable r = new Runnable() {
613 public void run() {
614 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700615 updateItemArrays(item, itemId, stackTrace);
616 }
617 };
618 runOnWorkerThread(r);
619 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700620
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700621 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
622 final ArrayList<ItemInfo> items, final String callingFunction) {
623 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700624
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700625 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
626 Runnable r = new Runnable() {
627 public void run() {
628 ArrayList<ContentProviderOperation> ops =
629 new ArrayList<ContentProviderOperation>();
630 int count = items.size();
631 for (int i = 0; i < count; i++) {
632 ItemInfo item = items.get(i);
633 final long itemId = item.id;
634 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
635 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700636
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700637 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
638 updateItemArrays(item, itemId, stackTrace);
639
640 }
641 try {
642 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
643 } catch (Exception e) {
644 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700645 }
646 }
647 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700648 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700649 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700650
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700651 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
652 // Lock on mBgLock *after* the db operation
653 synchronized (sBgLock) {
654 checkItemInfoLocked(itemId, item, stackTrace);
655
656 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
657 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
658 // Item is in a folder, make sure this folder exists
659 if (!sBgFolders.containsKey(item.container)) {
660 // An items container is being set to a that of an item which is not in
661 // the list of Folders.
662 String msg = "item: " + item + " container being set to: " +
663 item.container + ", not in the list of folders";
664 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700665 }
666 }
667
668 // Items are added/removed from the corresponding FolderInfo elsewhere, such
669 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
670 // that are on the desktop, as appropriate
671 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800672 if (modelItem != null &&
673 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
674 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675 switch (modelItem.itemType) {
676 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
677 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
678 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
679 if (!sBgWorkspaceItems.contains(modelItem)) {
680 sBgWorkspaceItems.add(modelItem);
681 }
682 break;
683 default:
684 break;
685 }
686 } else {
687 sBgWorkspaceItems.remove(modelItem);
688 }
689 }
690 }
691
Michael Jurkac7700af2013-05-14 20:17:58 +0200692 public void flushWorkerThread() {
693 mFlushingWorkerThread = true;
694 Runnable waiter = new Runnable() {
695 public void run() {
696 synchronized (this) {
697 notifyAll();
698 mFlushingWorkerThread = false;
699 }
700 }
701 };
702
703 synchronized(waiter) {
704 runOnWorkerThread(waiter);
705 if (mLoaderTask != null) {
706 synchronized(mLoaderTask) {
707 mLoaderTask.notify();
708 }
709 }
710 boolean success = false;
711 while (!success) {
712 try {
713 waiter.wait();
714 success = true;
715 } catch (InterruptedException e) {
716 }
717 }
718 }
719 }
720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700723 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700724 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700725 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400726 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727 item.cellX = cellX;
728 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700729
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 // We store hotseat items in canonical form which is this orientation invariant position
731 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700732 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700734 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700736 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738
739 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700741 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
742 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700743 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744
Michael Jurkac9d95c52011-08-29 14:03:34 -0700745 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700746 }
747
748 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700749 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
750 * cellX, cellY have already been updated on the ItemInfos.
751 */
752 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
753 final long container, final int screen) {
754
755 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
756 int count = items.size();
757
758 for (int i = 0; i < count; i++) {
759 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700760 item.container = container;
761
762 // We store hotseat items in canonical form which is this orientation invariant position
763 // in the hotseat
764 if (context instanceof Launcher && screen < 0 &&
765 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700766 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700767 item.cellY);
768 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700769 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700770 }
771
772 final ContentValues values = new ContentValues();
773 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
774 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
775 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700777
778 contentValues.add(values);
779 }
780 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
781 }
782
783 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800785 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700788 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800789 item.cellX = cellX;
790 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700791 item.spanX = spanX;
792 item.spanY = spanY;
793
794 // We store hotseat items in canonical form which is this orientation invariant position
795 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 }
Adam Cohend4844c32011-02-18 19:25:06 -0800802
Adam Cohend4844c32011-02-18 19:25:06 -0800803 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800804 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700805 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
806 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
807 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
808 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700809 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800810
Michael Jurka816474f2012-06-25 14:49:02 -0700811 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700812 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700813
814 /**
815 * Update an item to the database in a specified container.
816 */
817 static void updateItemInDatabase(Context context, final ItemInfo item) {
818 final ContentValues values = new ContentValues();
819 item.onAddToDatabase(values);
820 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
821 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800822 }
823
824 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400825 * Returns true if the shortcuts already exists in the database.
826 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828 static boolean shortcutExists(Context context, String title, Intent intent) {
829 final ContentResolver cr = context.getContentResolver();
830 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
831 new String[] { "title", "intent" }, "title=? and intent=?",
832 new String[] { title, intent.toUri(0) }, null);
833 boolean result = false;
834 try {
835 result = c.moveToFirst();
836 } finally {
837 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700840 }
841
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500843 * Returns true if the shortcuts already exists in the database.
844 * we identify a shortcut by the component name of the intent.
845 */
846 static boolean appWasRestored(Context context, Intent intent) {
847 final ContentResolver cr = context.getContentResolver();
848 final ComponentName component = intent.getComponent();
849 if (component == null) {
850 return false;
851 }
852 String componentName = component.flattenToString();
853 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
854 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
855 new String[]{"intent", "restored"}, where, null, null);
856 boolean result = false;
857 try {
858 result = c.moveToFirst();
859 } finally {
860 c.close();
861 }
862 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
863 return result;
864 }
865
866 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700867 * Returns an ItemInfo array containing all the items in the LauncherModel.
868 * The ItemInfo.id is not set through this function.
869 */
870 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
871 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
872 final ContentResolver cr = context.getContentResolver();
873 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
874 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
875 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
876 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
877
878 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
879 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
880 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
881 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
882 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
883 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
884 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
885
886 try {
887 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700888 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700889 item.cellX = c.getInt(cellXIndex);
890 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700891 item.spanX = Math.max(1, c.getInt(spanXIndex));
892 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700893 item.container = c.getInt(containerIndex);
894 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700895 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700896
897 items.add(item);
898 }
899 } catch (Exception e) {
900 items.clear();
901 } finally {
902 c.close();
903 }
904
905 return items;
906 }
907
908 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
910 */
911 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
912 final ContentResolver cr = context.getContentResolver();
913 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
914 "_id=? and (itemType=? or itemType=?)",
915 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700916 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700917
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 try {
919 if (c.moveToFirst()) {
920 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
921 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
922 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
923 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
924 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
925 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 FolderInfo folderInfo = null;
928 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700929 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
930 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700932 }
933
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 folderInfo.title = c.getString(titleIndex);
935 folderInfo.id = id;
936 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700938 folderInfo.cellX = c.getInt(cellXIndex);
939 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940
941 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700942 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943 } finally {
944 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700945 }
946
947 return null;
948 }
949
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 /**
951 * Add an item to the database in a specified container. Sets the container, screen, cellX and
952 * cellY fields of the item. Also assigns an ID to the item.
953 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700954 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700955 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 item.cellX = cellX;
958 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 // We store hotseat items in canonical form which is this orientation invariant position
960 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700963 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967
968 final ContentValues values = new ContentValues();
969 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 item.onAddToDatabase(values);
971
Michael Jurka414300a2013-08-27 15:42:35 +0200972 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700973 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700974 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700975
Jason Monk8e19cf22014-03-20 15:06:57 -0400976 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700977 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700978 public void run() {
979 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
980 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700982 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700983 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400984 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985 sBgItemsIdMap.put(item.id, item);
986 switch (item.itemType) {
987 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
988 sBgFolders.put(item.id, (FolderInfo) item);
989 // Fall through
990 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
991 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
992 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
993 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
994 sBgWorkspaceItems.add(item);
995 } else {
996 if (!sBgFolders.containsKey(item.container)) {
997 // Adding an item to a folder that doesn't exist.
998 String msg = "adding item: " + item + " to a folder that " +
999 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001000 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001001 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001002 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001003 break;
1004 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1005 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1006 break;
1007 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001008 }
1009 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001010 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001012 }
1013
Joe Onorato9c1289c2009-08-17 11:03:03 -04001014 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001015 * Creates a new unique child id, for a given cell span across all layouts.
1016 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001017 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001018 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001019 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001020 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001021 }
1022
Winson Chungaafa03c2010-06-11 17:34:16 -07001023 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001024 * Removes the specified item from the database
1025 * @param context
1026 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001028 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001029 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001030 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001031
Michael Jurka83df1882011-08-31 20:59:26 -07001032 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001033 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001034 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001035
1036 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001037 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 switch (item.itemType) {
1039 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1040 sBgFolders.remove(item.id);
1041 for (ItemInfo info: sBgItemsIdMap.values()) {
1042 if (info.container == item.id) {
1043 // We are deleting a folder which still contains items that
1044 // think they are contained by that folder.
1045 String msg = "deleting a folder (" + item + ") which still " +
1046 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001047 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001049 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050 sBgWorkspaceItems.remove(item);
1051 break;
1052 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1053 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1054 sBgWorkspaceItems.remove(item);
1055 break;
1056 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1057 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1058 break;
1059 }
1060 sBgItemsIdMap.remove(item.id);
1061 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001062 }
1063 }
Michael Jurka83df1882011-08-31 20:59:26 -07001064 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001065 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001066 }
1067
1068 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 * Update the order of the workspace screens in the database. The array list contains
1070 * a list of screen ids in the order that they should appear.
1071 */
Winson Chungc9168342013-06-26 14:54:55 -07001072 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001073 // Log to disk
1074 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1075 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1076
Winson Chung64359a52013-07-08 17:17:08 -07001077 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001078 final ContentResolver cr = context.getContentResolver();
1079 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1080
1081 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001082 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 while (iter.hasNext()) {
1084 long id = iter.next();
1085 if (id < 0) {
1086 iter.remove();
1087 }
1088 }
1089
1090 Runnable r = new Runnable() {
1091 @Override
1092 public void run() {
Yura085c8532014-02-11 15:15:29 +00001093 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001094 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001095 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001096 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001097 for (int i = 0; i < count; i++) {
1098 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001099 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001100 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1101 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001102 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001103 }
Yura085c8532014-02-11 15:15:29 +00001104
1105 try {
1106 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1107 } catch (Exception ex) {
1108 throw new RuntimeException(ex);
1109 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001110
Winson Chungba9c37f2013-08-30 14:11:37 -07001111 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001112 sBgWorkspaceScreens.clear();
1113 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001114 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 }
1116 };
1117 runOnWorkerThread(r);
1118 }
1119
1120 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001121 * Remove the contents of the specified folder from the database
1122 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001123 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124 final ContentResolver cr = context.getContentResolver();
1125
Michael Jurkac9d95c52011-08-29 14:03:34 -07001126 Runnable r = new Runnable() {
1127 public void run() {
1128 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001130 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 sBgItemsIdMap.remove(info.id);
1132 sBgFolders.remove(info.id);
1133 sBgDbIconCache.remove(info);
1134 sBgWorkspaceItems.remove(info);
1135 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001136
Michael Jurkac9d95c52011-08-29 14:03:34 -07001137 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1138 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001139 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001140 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001141 for (ItemInfo childInfo : info.contents) {
1142 sBgItemsIdMap.remove(childInfo.id);
1143 sBgDbIconCache.remove(childInfo);
1144 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001145 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001146 }
1147 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001148 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001149 }
1150
1151 /**
1152 * Set this as the current Launcher activity object for the loader.
1153 */
1154 public void initialize(Callbacks callbacks) {
1155 synchronized (mLock) {
1156 mCallbacks = new WeakReference<Callbacks>(callbacks);
1157 }
1158 }
1159
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001160 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1162 * ACTION_PACKAGE_CHANGED.
1163 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001164 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001165 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001166 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001167
Joe Onorato36115782010-06-17 13:28:48 -04001168 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001169
Joe Onorato36115782010-06-17 13:28:48 -04001170 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1171 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1172 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1173 final String packageName = intent.getData().getSchemeSpecificPart();
1174 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175
Joe Onorato36115782010-06-17 13:28:48 -04001176 int op = PackageUpdatedTask.OP_NONE;
1177
1178 if (packageName == null || packageName.length() == 0) {
1179 // they sent us a bad intent
1180 return;
1181 }
1182
1183 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1184 op = PackageUpdatedTask.OP_UPDATE;
1185 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1186 if (!replacing) {
1187 op = PackageUpdatedTask.OP_REMOVE;
1188 }
1189 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1190 // later, we will update the package at this time
1191 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1192 if (!replacing) {
1193 op = PackageUpdatedTask.OP_ADD;
1194 } else {
1195 op = PackageUpdatedTask.OP_UPDATE;
1196 }
1197 }
1198
1199 if (op != PackageUpdatedTask.OP_NONE) {
1200 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1201 }
1202
1203 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001204 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001205 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001206 if (!replacing) {
1207 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1208 if (mAppsCanBeOnRemoveableStorage) {
1209 // Only rebind if we support removable storage. It catches the case where
1210 // apps on the external sd card need to be reloaded
1211 startLoaderFromBackground();
1212 }
1213 } else {
1214 // If we are replacing then just update the packages in the list
1215 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1216 packages));
1217 }
Joe Onorato36115782010-06-17 13:28:48 -04001218 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001219 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1220 if (!replacing) {
1221 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1222 enqueuePackageUpdated(new PackageUpdatedTask(
1223 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1224 }
1225 // else, we are replacing the packages, so ignore this event and wait for
1226 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001227 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001228 // If we have changed locale we need to clear out the labels in all apps/workspace.
1229 forceReload();
1230 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1231 // Check if configuration change was an mcc/mnc change which would affect app resources
1232 // and we would need to clear out the labels in all apps/workspace. Same handling as
1233 // above for ACTION_LOCALE_CHANGED
1234 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001235 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001236 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001237 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001238 forceReload();
1239 }
1240 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001241 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001242 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1243 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001244 if (mCallbacks != null) {
1245 Callbacks callbacks = mCallbacks.get();
1246 if (callbacks != null) {
1247 callbacks.bindSearchablesChanged();
1248 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001249 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001250 }
1251 }
1252
Reena Lee93f824a2011-09-23 17:20:28 -07001253 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001254 resetLoadedState(true, true);
1255
Reena Lee93f824a2011-09-23 17:20:28 -07001256 // Do this here because if the launcher activity is running it will be restarted.
1257 // If it's not running startLoaderFromBackground will merely tell it that it needs
1258 // to reload.
1259 startLoaderFromBackground();
1260 }
1261
Winson Chungf0c6ae02012-03-21 16:10:31 -07001262 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1263 synchronized (mLock) {
1264 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1265 // mWorkspaceLoaded to true later
1266 stopLoaderLocked();
1267 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1268 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1269 }
1270 }
1271
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001272 /**
1273 * When the launcher is in the background, it's possible for it to miss paired
1274 * configuration changes. So whenever we trigger the loader from the background
1275 * tell the launcher that it needs to re-run the loader when it comes back instead
1276 * of doing it now.
1277 */
1278 public void startLoaderFromBackground() {
1279 boolean runLoader = false;
1280 if (mCallbacks != null) {
1281 Callbacks callbacks = mCallbacks.get();
1282 if (callbacks != null) {
1283 // Only actually run the loader if they're not paused.
1284 if (!callbacks.setLoadOnResume()) {
1285 runLoader = true;
1286 }
1287 }
1288 }
1289 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001290 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001291 }
Joe Onorato36115782010-06-17 13:28:48 -04001292 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001293
Reena Lee93f824a2011-09-23 17:20:28 -07001294 // If there is already a loader task running, tell it to stop.
1295 // returns true if isLaunching() was true on the old task
1296 private boolean stopLoaderLocked() {
1297 boolean isLaunching = false;
1298 LoaderTask oldTask = mLoaderTask;
1299 if (oldTask != null) {
1300 if (oldTask.isLaunching()) {
1301 isLaunching = true;
1302 }
1303 oldTask.stopLocked();
1304 }
1305 return isLaunching;
1306 }
1307
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001308 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001309 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1310 }
1311
1312 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001313 synchronized (mLock) {
1314 if (DEBUG_LOADERS) {
1315 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001318 // Clear any deferred bind-runnables from the synchronized load process
1319 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001320 synchronized (mDeferredBindRunnables) {
1321 mDeferredBindRunnables.clear();
1322 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323
Joe Onorato36115782010-06-17 13:28:48 -04001324 // Don't bother to start the thread if we know it's not going to do anything
1325 if (mCallbacks != null && mCallbacks.get() != null) {
1326 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001327 // also, don't downgrade isLaunching if we're already running
1328 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001329 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001330 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1331 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001332 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1333 } else {
1334 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1335 sWorker.post(mLoaderTask);
1336 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001337 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338 }
1339 }
1340
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001341 void bindRemainingSynchronousPages() {
1342 // Post the remaining side pages to be loaded
1343 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001344 Runnable[] deferredBindRunnables = null;
1345 synchronized (mDeferredBindRunnables) {
1346 deferredBindRunnables = mDeferredBindRunnables.toArray(
1347 new Runnable[mDeferredBindRunnables.size()]);
1348 mDeferredBindRunnables.clear();
1349 }
1350 for (final Runnable r : deferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001351 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001352 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001353 }
1354 }
1355
Joe Onorato36115782010-06-17 13:28:48 -04001356 public void stopLoader() {
1357 synchronized (mLock) {
1358 if (mLoaderTask != null) {
1359 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 }
1361 }
Joe Onorato36115782010-06-17 13:28:48 -04001362 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363
Winson Chung76828c82013-08-19 15:43:29 -07001364 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1365 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1366 final ContentResolver contentResolver = context.getContentResolver();
1367 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1368 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1369 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1370
1371 try {
1372 final int idIndex = sc.getColumnIndexOrThrow(
1373 LauncherSettings.WorkspaceScreens._ID);
1374 final int rankIndex = sc.getColumnIndexOrThrow(
1375 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1376 while (sc.moveToNext()) {
1377 try {
1378 long screenId = sc.getLong(idIndex);
1379 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001380 orderedScreens.put(rank, screenId);
1381 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001382 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001383 }
1384 }
1385 } finally {
1386 sc.close();
1387 }
Winson Chunga90303b2013-11-15 13:05:06 -08001388
1389 // Log to disk
1390 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1391 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1392 for (Integer i : orderedScreens.keySet()) {
1393 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1394 }
1395 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1396 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001397 return orderedScreens;
1398 }
1399
Michael Jurkac57b7a82011-08-09 22:02:20 -07001400 public boolean isAllAppsLoaded() {
1401 return mAllAppsLoaded;
1402 }
1403
Winson Chung36a62fe2012-05-06 18:04:42 -07001404 boolean isLoadingWorkspace() {
1405 synchronized (mLock) {
1406 if (mLoaderTask != null) {
1407 return mLoaderTask.isLoadingWorkspace();
1408 }
1409 }
1410 return false;
1411 }
1412
Joe Onorato36115782010-06-17 13:28:48 -04001413 /**
1414 * Runnable for the thread that loads the contents of the launcher:
1415 * - workspace icons
1416 * - widgets
1417 * - all apps icons
1418 */
1419 private class LoaderTask implements Runnable {
1420 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001421 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001422 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001423 private boolean mStopped;
1424 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001425 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001426
Winson Chungc3eecff2011-07-11 17:44:15 -07001427 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001428
Dan Sandlerd5024042014-01-09 15:01:33 -05001429 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001430 mContext = context;
1431 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001432 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001433 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 }
1435
Joe Onorato36115782010-06-17 13:28:48 -04001436 boolean isLaunching() {
1437 return mIsLaunching;
1438 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001439
Winson Chung36a62fe2012-05-06 18:04:42 -07001440 boolean isLoadingWorkspace() {
1441 return mIsLoadingAndBindingWorkspace;
1442 }
1443
Winson Chungc763c4e2013-07-19 13:49:06 -07001444 /** Returns whether this is an upgrade path */
1445 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001446 mIsLoadingAndBindingWorkspace = true;
1447
Joe Onorato36115782010-06-17 13:28:48 -04001448 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001449 if (DEBUG_LOADERS) {
1450 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001452
Winson Chungc763c4e2013-07-19 13:49:06 -07001453 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001454 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001455 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001456 synchronized (LoaderTask.this) {
1457 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001458 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001459 }
1460 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001461 }
1462 }
1463
Joe Onorato36115782010-06-17 13:28:48 -04001464 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001465 bindWorkspace(-1, isUpgradePath);
1466 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001467 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001468
Joe Onorato36115782010-06-17 13:28:48 -04001469 private void waitForIdle() {
1470 // Wait until the either we're stopped or the other threads are done.
1471 // This way we don't start loading all apps until the workspace has settled
1472 // down.
1473 synchronized (LoaderTask.this) {
1474 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001475
Joe Onorato36115782010-06-17 13:28:48 -04001476 mHandler.postIdle(new Runnable() {
1477 public void run() {
1478 synchronized (LoaderTask.this) {
1479 mLoadAndBindStepFinished = true;
1480 if (DEBUG_LOADERS) {
1481 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001482 }
Joe Onorato36115782010-06-17 13:28:48 -04001483 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001484 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001485 }
Joe Onorato36115782010-06-17 13:28:48 -04001486 });
1487
Michael Jurkac7700af2013-05-14 20:17:58 +02001488 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001489 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001490 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1491 // wait no longer than 1sec at a time
1492 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001493 } catch (InterruptedException ex) {
1494 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001495 }
1496 }
Joe Onorato36115782010-06-17 13:28:48 -04001497 if (DEBUG_LOADERS) {
1498 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001499 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001500 + "ms for previous step to finish binding");
1501 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001502 }
Joe Onorato36115782010-06-17 13:28:48 -04001503 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001505 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001506 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 // Ensure that we have a valid page index to load synchronously
1508 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1509 "valid page index");
1510 }
1511 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1512 // Ensure that we don't try and bind a specified page when the pages have not been
1513 // loaded already (we should load everything asynchronously in that case)
1514 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1515 }
1516 synchronized (mLock) {
1517 if (mIsLoaderTaskRunning) {
1518 // Ensure that we are never running the background loading at this point since
1519 // we also touch the background collections
1520 throw new RuntimeException("Error! Background loading is already running");
1521 }
1522 }
1523
1524 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1525 // data structures, we can't allow any other thread to touch that data, but because
1526 // this call is synchronous, we can get away with not locking).
1527
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001528 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001529 // operations from the previous activity. We need to ensure that all queued operations
1530 // are executed before any synchronous binding work is done.
1531 mHandler.flush();
1532
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 // Divide the set of loaded items into those that we are binding synchronously, and
1534 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001535 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001536 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1537 // arise from that.
1538 onlyBindAllApps();
1539 }
1540
Joe Onorato36115782010-06-17 13:28:48 -04001541 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001542 boolean isUpgrade = false;
1543
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001544 synchronized (mLock) {
1545 mIsLoaderTaskRunning = true;
1546 }
Joe Onorato36115782010-06-17 13:28:48 -04001547 // Optimize for end-user experience: if the Launcher is up and // running with the
1548 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1549 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001550 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001551 // Elevate priority when Home launches for the first time to avoid
1552 // starving at boot time. Staring at a blank home is not cool.
1553 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001554 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1555 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001556 android.os.Process.setThreadPriority(mIsLaunching
1557 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1558 }
Winson Chung64359a52013-07-08 17:17:08 -07001559 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001560 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001561
Joe Onorato36115782010-06-17 13:28:48 -04001562 if (mStopped) {
1563 break keep_running;
1564 }
1565
1566 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1567 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001568 synchronized (mLock) {
1569 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001570 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001571 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1572 }
1573 }
Joe Onorato36115782010-06-17 13:28:48 -04001574 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001575
1576 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001577 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1578 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001579
1580 // Restore the default thread priority after we are done loading items
1581 synchronized (mLock) {
1582 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1583 }
Joe Onorato36115782010-06-17 13:28:48 -04001584 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585
Winson Chungaac01e12011-08-17 10:37:13 -07001586 // Update the saved icons if necessary
1587 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001588 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001589 for (Object key : sBgDbIconCache.keySet()) {
1590 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1591 }
1592 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001593 }
Winson Chungaac01e12011-08-17 10:37:13 -07001594
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001595 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001596 // Ensure that all the applications that are in the system are
1597 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001598 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001599 verifyApplications();
1600 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001601 }
1602
Joe Onorato36115782010-06-17 13:28:48 -04001603 // Clear out this reference, otherwise we end up holding it until all of the
1604 // callback runnables are done.
1605 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001606
Joe Onorato36115782010-06-17 13:28:48 -04001607 synchronized (mLock) {
1608 // If we are still the last one to be scheduled, remove ourselves.
1609 if (mLoaderTask == this) {
1610 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001611 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001612 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001613 }
Joe Onorato36115782010-06-17 13:28:48 -04001614 }
1615
1616 public void stopLocked() {
1617 synchronized (LoaderTask.this) {
1618 mStopped = true;
1619 this.notify();
1620 }
1621 }
1622
1623 /**
1624 * Gets the callbacks object. If we've been stopped, or if the launcher object
1625 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1626 * object that was around when the deferred message was scheduled, and if there's
1627 * a new Callbacks object around then also return null. This will save us from
1628 * calling onto it with data that will be ignored.
1629 */
1630 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1631 synchronized (mLock) {
1632 if (mStopped) {
1633 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001634 }
Joe Onorato36115782010-06-17 13:28:48 -04001635
1636 if (mCallbacks == null) {
1637 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001638 }
Joe Onorato36115782010-06-17 13:28:48 -04001639
1640 final Callbacks callbacks = mCallbacks.get();
1641 if (callbacks != oldCallbacks) {
1642 return null;
1643 }
1644 if (callbacks == null) {
1645 Log.w(TAG, "no mCallbacks");
1646 return null;
1647 }
1648
1649 return callbacks;
1650 }
1651 }
1652
Winson Chungc763c4e2013-07-19 13:49:06 -07001653 private void verifyApplications() {
1654 final Context context = mApp.getContext();
1655
1656 // Cross reference all the applications in our apps list with items in the workspace
1657 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001658 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001659 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001660 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001661 tmpInfos = getItemInfoForComponentName(app.componentName);
1662 if (tmpInfos.isEmpty()) {
1663 // We are missing an application icon, so add this to the workspace
1664 added.add(app);
1665 // This is a rare event, so lets log it
1666 Log.e(TAG, "Missing Application on load: " + app);
1667 }
1668 }
1669 }
1670 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001671 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001672 }
1673 }
1674
Joe Onorato36115782010-06-17 13:28:48 -04001675 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001676 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001677 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001678 LauncherAppState app = LauncherAppState.getInstance();
1679 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001680 final int countX = (int) grid.numColumns;
1681 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001682
Adam Cohendcd297f2013-06-18 13:13:40 -07001683 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001684 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001685 // Return early if we detect that an item is under the hotseat button
1686 if (mCallbacks == null ||
1687 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001688 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001689 Log.e(TAG, "Error loading shortcut into hotseat " + item
1690 + " into position (" + item.screenId + ":" + item.cellX + ","
1691 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001692 return false;
1693 }
1694
Dan Sandler295ae182013-12-10 16:05:47 -05001695 final ItemInfo[][] hotseatItems =
1696 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1697
Adam Cohenae4409d2013-11-26 10:34:59 -08001698 if (item.screenId >= grid.numHotseatIcons) {
1699 Log.e(TAG, "Error loading shortcut " + item
1700 + " into hotseat position " + item.screenId
1701 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1702 + ")");
1703 return false;
1704 }
1705
Dan Sandler295ae182013-12-10 16:05:47 -05001706 if (hotseatItems != null) {
1707 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001708 Log.e(TAG, "Error loading shortcut into hotseat " + item
1709 + " into position (" + item.screenId + ":" + item.cellX + ","
1710 + item.cellY + ") occupied by "
1711 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1712 [(int) item.screenId][0]);
1713 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001714 } else {
1715 hotseatItems[(int) item.screenId][0] = item;
1716 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001717 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001718 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001719 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001720 items[(int) item.screenId][0] = item;
1721 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001722 return true;
1723 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001724 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1725 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001726 return true;
1727 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001728
Adam Cohendcd297f2013-06-18 13:13:40 -07001729 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001730 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001731 occupied.put(item.screenId, items);
1732 }
1733
Dan Sandler295ae182013-12-10 16:05:47 -05001734 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001735 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1736 item.cellX < 0 || item.cellY < 0 ||
1737 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1738 Log.e(TAG, "Error loading shortcut " + item
1739 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1740 + item.cellX + "," + item.cellY
1741 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1742 return false;
1743 }
1744
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001745 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001746 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1747 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001748 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001749 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001750 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001751 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001752 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001753 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001754 return false;
1755 }
1756 }
1757 }
1758 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1759 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001760 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001761 }
1762 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001763
Joe Onorato36115782010-06-17 13:28:48 -04001764 return true;
1765 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766
Winson Chungba9c37f2013-08-30 14:11:37 -07001767 /** Clears all the sBg data structures */
1768 private void clearSBgDataStructures() {
1769 synchronized (sBgLock) {
1770 sBgWorkspaceItems.clear();
1771 sBgAppWidgets.clear();
1772 sBgFolders.clear();
1773 sBgItemsIdMap.clear();
1774 sBgDbIconCache.clear();
1775 sBgWorkspaceScreens.clear();
1776 }
1777 }
1778
Dan Sandlerd5024042014-01-09 15:01:33 -05001779 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001780 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001781 // Log to disk
1782 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1783
Joe Onorato36115782010-06-17 13:28:48 -04001784 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785
Joe Onorato36115782010-06-17 13:28:48 -04001786 final Context context = mContext;
1787 final ContentResolver contentResolver = context.getContentResolver();
1788 final PackageManager manager = context.getPackageManager();
1789 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1790 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001791
Winson Chung892c74d2013-08-22 16:15:50 -07001792 LauncherAppState app = LauncherAppState.getInstance();
1793 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1794 int countX = (int) grid.numColumns;
1795 int countY = (int) grid.numRows;
1796
Dan Sandlerd5024042014-01-09 15:01:33 -05001797 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1798 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1799 LauncherAppState.getLauncherProvider().deleteDatabase();
1800 }
1801
1802 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1803 // append the user's Launcher2 shortcuts
1804 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1805 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1806 } else {
1807 // Make sure the default workspace is loaded
1808 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1809 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1810 }
Adam Cohene25af792013-06-06 23:08:25 -07001811
Winson Chungc763c4e2013-07-19 13:49:06 -07001812 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001813 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001814 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001815
Winson Chung9f9f00b2013-11-15 13:27:00 -08001816 // Log to disk
1817 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001818
Winson Chung2abf94d2012-07-18 18:16:38 -07001819 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001820 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001821
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001822 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001823 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001824 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001825 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001826 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001827
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 // +1 for the hotseat (it can be larger than the workspace)
1829 // Load workspace in reverse order to ensure that latest items are loaded first (and
1830 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001831 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 try {
1834 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1835 final int intentIndex = c.getColumnIndexOrThrow
1836 (LauncherSettings.Favorites.INTENT);
1837 final int titleIndex = c.getColumnIndexOrThrow
1838 (LauncherSettings.Favorites.TITLE);
1839 final int iconTypeIndex = c.getColumnIndexOrThrow(
1840 LauncherSettings.Favorites.ICON_TYPE);
1841 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1842 final int iconPackageIndex = c.getColumnIndexOrThrow(
1843 LauncherSettings.Favorites.ICON_PACKAGE);
1844 final int iconResourceIndex = c.getColumnIndexOrThrow(
1845 LauncherSettings.Favorites.ICON_RESOURCE);
1846 final int containerIndex = c.getColumnIndexOrThrow(
1847 LauncherSettings.Favorites.CONTAINER);
1848 final int itemTypeIndex = c.getColumnIndexOrThrow(
1849 LauncherSettings.Favorites.ITEM_TYPE);
1850 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1851 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001852 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1853 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854 final int screenIndex = c.getColumnIndexOrThrow(
1855 LauncherSettings.Favorites.SCREEN);
1856 final int cellXIndex = c.getColumnIndexOrThrow
1857 (LauncherSettings.Favorites.CELLX);
1858 final int cellYIndex = c.getColumnIndexOrThrow
1859 (LauncherSettings.Favorites.CELLY);
1860 final int spanXIndex = c.getColumnIndexOrThrow
1861 (LauncherSettings.Favorites.SPANX);
1862 final int spanYIndex = c.getColumnIndexOrThrow(
1863 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001864 final int restoredIndex = c.getColumnIndexOrThrow(
1865 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1867 //final int displayModeIndex = c.getColumnIndexOrThrow(
1868 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001869
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 ShortcutInfo info;
1871 String intentDescription;
1872 LauncherAppWidgetInfo appWidgetInfo;
1873 int container;
1874 long id;
1875 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001878 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001879 try {
1880 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001881 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883 switch (itemType) {
1884 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1885 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001886 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001887 intentDescription = c.getString(intentIndex);
1888 try {
1889 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001890 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001891 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001892 if (restored) {
1893 // might be installed later
1894 Launcher.addDumpLog(TAG,
1895 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001896 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001897 if (!mAppsCanBeOnRemoveableStorage) {
1898 // Log the invalid package, and remove it
1899 Launcher.addDumpLog(TAG,
1900 "Invalid package removed: " + cn, true);
1901 itemsToRemove.add(id);
1902 } else {
1903 // If apps can be on external storage, then we just
1904 // leave them for the user to remove (maybe add
1905 // visual treatment to it)
1906 Launcher.addDumpLog(TAG,
1907 "Invalid package found: " + cn, true);
1908 }
1909 continue;
Winson Chungee055712013-07-30 14:46:24 -07001910 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001911 } else if (restored) {
1912 // no special handling necessary for this restored item
1913 restoredRows.add(id);
1914 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001915 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001917 Launcher.addDumpLog(TAG,
1918 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 continue;
1920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921
Chris Wrenf4d08112014-01-16 18:13:56 -05001922 if (restored) {
1923 Launcher.addDumpLog(TAG,
1924 "constructing info for partially restored package",
1925 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001926 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001927 intent = getRestoredItemIntent(c, context, intent);
1928 } else if (itemType ==
1929 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001930 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1931 titleIndex, mLabelCache);
1932 } else {
1933 info = getShortcutInfo(c, context, iconTypeIndex,
1934 iconPackageIndex, iconResourceIndex, iconIndex,
1935 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001936
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001937 // App shortcuts that used to be automatically added to Launcher
1938 // didn't always have the correct intent flags set, so do that
1939 // here
1940 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001941 intent.getCategories() != null &&
1942 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001943 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001944 intent.addFlags(
1945 Intent.FLAG_ACTIVITY_NEW_TASK |
1946 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1947 }
Michael Jurka96879562012-03-22 05:54:33 -07001948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001949
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001951 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001952 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001953 container = c.getInt(containerIndex);
1954 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001955 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001956 info.cellX = c.getInt(cellXIndex);
1957 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001958 info.spanX = 1;
1959 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001960
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001962 deleteOnInvalidPlacement.set(false);
1963 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1964 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001965 itemsToRemove.add(id);
1966 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001967 break;
1968 }
1969
1970 switch (container) {
1971 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1972 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1973 sBgWorkspaceItems.add(info);
1974 break;
1975 default:
1976 // Item is in a user folder
1977 FolderInfo folderInfo =
1978 findOrMakeFolder(sBgFolders, container);
1979 folderInfo.add(info);
1980 break;
1981 }
1982 sBgItemsIdMap.put(info.id, info);
1983
1984 // now that we've loaded everthing re-save it with the
1985 // icon in case it disappears somehow.
1986 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001987 } else {
1988 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 }
1990 break;
1991
1992 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1993 id = c.getLong(idIndex);
1994 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1995
1996 folderInfo.title = c.getString(titleIndex);
1997 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001998 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002000 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 folderInfo.cellX = c.getInt(cellXIndex);
2002 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002003 folderInfo.spanX = 1;
2004 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002005
Daniel Sandler8802e962010-05-26 16:28:16 -04002006 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002007 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002008 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002009 deleteOnInvalidPlacement)) {
2010 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002011 itemsToRemove.add(id);
2012 }
Daniel Sandler8802e962010-05-26 16:28:16 -04002013 break;
2014 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002015
Joe Onorato9c1289c2009-08-17 11:03:03 -04002016 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2018 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2019 sBgWorkspaceItems.add(folderInfo);
2020 break;
Joe Onorato36115782010-06-17 13:28:48 -04002021 }
Joe Onorato17a89222011-02-08 17:26:11 -08002022
Chris Wrenf4d08112014-01-16 18:13:56 -05002023 if (restored) {
2024 // no special handling required for restored folders
2025 restoredRows.add(id);
2026 }
2027
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2029 sBgFolders.put(folderInfo.id, folderInfo);
2030 break;
2031
2032 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2033 // Read all Launcher-specific widget details
2034 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002035 String savedProvider = c.getString(appWidgetProviderIndex);
2036
Joe Onorato36115782010-06-17 13:28:48 -04002037 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002038
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 final AppWidgetProviderInfo provider =
2040 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002041
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 if (!isSafeMode && (provider == null || provider.provider == null ||
2043 provider.provider.getPackageName() == null)) {
2044 String log = "Deleting widget that isn't installed anymore: id="
2045 + id + " appWidgetId=" + appWidgetId;
2046 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002047 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 itemsToRemove.add(id);
2049 } else {
2050 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2051 provider.provider);
2052 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002053 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 appWidgetInfo.cellX = c.getInt(cellXIndex);
2055 appWidgetInfo.cellY = c.getInt(cellYIndex);
2056 appWidgetInfo.spanX = c.getInt(spanXIndex);
2057 appWidgetInfo.spanY = c.getInt(spanYIndex);
2058 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2059 appWidgetInfo.minSpanX = minSpan[0];
2060 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002061
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 container = c.getInt(containerIndex);
2063 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2064 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2065 Log.e(TAG, "Widget found where container != " +
2066 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2067 continue;
2068 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069
Adam Cohene25af792013-06-06 23:08:25 -07002070 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002072 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002073 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002074 deleteOnInvalidPlacement)) {
2075 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002076 itemsToRemove.add(id);
2077 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 break;
2079 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002080 String providerName = provider.provider.flattenToString();
2081 if (!providerName.equals(savedProvider)) {
2082 ContentValues values = new ContentValues();
2083 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2084 providerName);
2085 String where = BaseColumns._ID + "= ?";
2086 String[] args = {Integer.toString(c.getInt(idIndex))};
2087 contentResolver.update(contentUri, values, where, args);
2088 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2090 sBgAppWidgets.add(appWidgetInfo);
2091 }
Joe Onorato36115782010-06-17 13:28:48 -04002092 break;
2093 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002095 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002096 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 }
2098 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002099 if (c != null) {
2100 c.close();
2101 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 }
2103
Winson Chungba9c37f2013-08-30 14:11:37 -07002104 // Break early if we've stopped loading
2105 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002106 clearSBgDataStructures();
2107 return false;
2108 }
2109
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110 if (itemsToRemove.size() > 0) {
2111 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002112 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 // Remove dead items
2114 for (long id : itemsToRemove) {
2115 if (DEBUG_LOADERS) {
2116 Log.d(TAG, "Removed id = " + id);
2117 }
2118 // Don't notify content observers
2119 try {
2120 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2121 null, null);
2122 } catch (RemoteException e) {
2123 Log.w(TAG, "Could not remove id = " + id);
2124 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002125 }
2126 }
2127
Chris Wrenf4d08112014-01-16 18:13:56 -05002128 if (restoredRows.size() > 0) {
2129 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2130 LauncherSettings.Favorites.CONTENT_URI);
2131 // Update restored items that no longer require special handling
2132 try {
2133 StringBuilder selectionBuilder = new StringBuilder();
2134 selectionBuilder.append(LauncherSettings.Favorites._ID);
2135 selectionBuilder.append(" IN (");
2136 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2137 selectionBuilder.append(")");
2138 ContentValues values = new ContentValues();
2139 values.put(LauncherSettings.Favorites.RESTORED, 0);
2140 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2141 values, selectionBuilder.toString(), null);
2142 } catch (RemoteException e) {
2143 Log.w(TAG, "Could not update restored rows");
2144 }
2145 }
2146
Winson Chungc763c4e2013-07-19 13:49:06 -07002147 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002148 long maxScreenId = 0;
2149 // If we're importing we use the old screen order.
2150 for (ItemInfo item: sBgItemsIdMap.values()) {
2151 long screenId = item.screenId;
2152 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2153 !sBgWorkspaceScreens.contains(screenId)) {
2154 sBgWorkspaceScreens.add(screenId);
2155 if (screenId > maxScreenId) {
2156 maxScreenId = screenId;
2157 }
2158 }
2159 }
2160 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002161 // Log to disk
2162 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2163 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2164 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002165
Michael Jurka414300a2013-08-27 15:42:35 +02002166 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002167 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002168
2169 // Update the max item id after we load an old db
2170 long maxItemId = 0;
2171 // If we're importing we use the old screen order.
2172 for (ItemInfo item: sBgItemsIdMap.values()) {
2173 maxItemId = Math.max(maxItemId, item.id);
2174 }
Michael Jurka414300a2013-08-27 15:42:35 +02002175 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002176 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002177 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2178 for (Integer i : orderedScreens.keySet()) {
2179 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002180 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002181 // Log to disk
2182 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2183 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002184
2185 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002186 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002187 for (ItemInfo item: sBgItemsIdMap.values()) {
2188 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002189 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2190 unusedScreens.contains(screenId)) {
2191 unusedScreens.remove(screenId);
2192 }
2193 }
2194
2195 // If there are any empty screens remove them, and update.
2196 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002197 // Log to disk
2198 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2199 TextUtils.join(", ", unusedScreens), true);
2200
Winson Chung933bae62013-08-29 11:42:30 -07002201 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002202 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2203 }
2204 }
2205
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 if (DEBUG_LOADERS) {
2207 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2208 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002209 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002210 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002212
Daniel Sandler566da102013-06-25 23:43:45 -04002213 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002214 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002216 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 line += " | ";
2218 }
Winson Chung892c74d2013-08-22 16:15:50 -07002219 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002220 ItemInfo[][] screen = occupied.get(screenId);
2221 if (x < screen.length && y < screen[x].length) {
2222 line += (screen[x][y] != null) ? "#" : ".";
2223 } else {
2224 line += "!";
2225 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 }
Joe Onorato36115782010-06-17 13:28:48 -04002227 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002229 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 }
Joe Onorato36115782010-06-17 13:28:48 -04002231 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002232 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002233 }
2234
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 /** Filters the set of items who are directly or indirectly (via another container) on the
2236 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002237 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002238 ArrayList<ItemInfo> allWorkspaceItems,
2239 ArrayList<ItemInfo> currentScreenItems,
2240 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002241 // Purge any null ItemInfos
2242 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2243 while (iter.hasNext()) {
2244 ItemInfo i = iter.next();
2245 if (i == null) {
2246 iter.remove();
2247 }
2248 }
2249
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 // Order the set of items by their containers first, this allows use to walk through the
2251 // list sequentially, build up a list of containers that are in the specified screen,
2252 // as well as all items in those containers.
2253 Set<Long> itemsOnScreen = new HashSet<Long>();
2254 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2255 @Override
2256 public int compare(ItemInfo lhs, ItemInfo rhs) {
2257 return (int) (lhs.container - rhs.container);
2258 }
2259 });
2260 for (ItemInfo info : allWorkspaceItems) {
2261 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002262 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 currentScreenItems.add(info);
2264 itemsOnScreen.add(info.id);
2265 } else {
2266 otherScreenItems.add(info);
2267 }
2268 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2269 currentScreenItems.add(info);
2270 itemsOnScreen.add(info.id);
2271 } else {
2272 if (itemsOnScreen.contains(info.container)) {
2273 currentScreenItems.add(info);
2274 itemsOnScreen.add(info.id);
2275 } else {
2276 otherScreenItems.add(info);
2277 }
2278 }
2279 }
2280 }
2281
2282 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002283 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 ArrayList<LauncherAppWidgetInfo> appWidgets,
2285 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2286 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287
2288 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002289 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002290 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002291 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 currentScreenWidgets.add(widget);
2293 } else {
2294 otherScreenWidgets.add(widget);
2295 }
2296 }
2297 }
2298
2299 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002300 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 HashMap<Long, ItemInfo> itemsIdMap,
2302 HashMap<Long, FolderInfo> folders,
2303 HashMap<Long, FolderInfo> currentScreenFolders,
2304 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305
2306 for (long id : folders.keySet()) {
2307 ItemInfo info = itemsIdMap.get(id);
2308 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002309 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002310 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002311 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312 currentScreenFolders.put(id, folder);
2313 } else {
2314 otherScreenFolders.put(id, folder);
2315 }
2316 }
2317 }
2318
2319 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2320 * right) */
2321 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002322 final LauncherAppState app = LauncherAppState.getInstance();
2323 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 // XXX: review this
2325 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002326 @Override
2327 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002328 int cellCountX = (int) grid.numColumns;
2329 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002330 int screenOffset = cellCountX * cellCountY;
2331 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002333 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002335 rhs.cellY * cellCountX + rhs.cellX);
2336 return (int) (lr - rr);
2337 }
2338 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002340
Adam Cohendcd297f2013-06-18 13:13:40 -07002341 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2342 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002343 final Runnable r = new Runnable() {
2344 @Override
2345 public void run() {
2346 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2347 if (callbacks != null) {
2348 callbacks.bindScreens(orderedScreens);
2349 }
2350 }
2351 };
2352 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2353 }
2354
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2356 final ArrayList<ItemInfo> workspaceItems,
2357 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2358 final HashMap<Long, FolderInfo> folders,
2359 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002360
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361 final boolean postOnMainThread = (deferredBindRunnables != null);
2362
2363 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002364 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002365 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002366 final int start = i;
2367 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368 final Runnable r = new Runnable() {
2369 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002370 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002371 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002372 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002373 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2374 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002375 }
2376 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377 };
2378 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002379 synchronized (deferredBindRunnables) {
2380 deferredBindRunnables.add(r);
2381 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002382 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002383 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 }
Joe Onorato36115782010-06-17 13:28:48 -04002385 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002386
2387 // Bind the folders
2388 if (!folders.isEmpty()) {
2389 final Runnable r = new Runnable() {
2390 public void run() {
2391 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2392 if (callbacks != null) {
2393 callbacks.bindFolders(folders);
2394 }
2395 }
2396 };
2397 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002398 synchronized (deferredBindRunnables) {
2399 deferredBindRunnables.add(r);
2400 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002401 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002402 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002403 }
2404 }
2405
2406 // Bind the widgets, one at a time
2407 N = appWidgets.size();
2408 for (int i = 0; i < N; i++) {
2409 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2410 final Runnable r = new Runnable() {
2411 public void run() {
2412 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2413 if (callbacks != null) {
2414 callbacks.bindAppWidget(widget);
2415 }
2416 }
2417 };
2418 if (postOnMainThread) {
2419 deferredBindRunnables.add(r);
2420 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002421 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002422 }
2423 }
2424 }
2425
2426 /**
2427 * Binds all loaded data to actual views on the main thread.
2428 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002429 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 final long t = SystemClock.uptimeMillis();
2431 Runnable r;
2432
2433 // Don't use these two variables in any of the callback runnables.
2434 // Otherwise we hold a reference to them.
2435 final Callbacks oldCallbacks = mCallbacks.get();
2436 if (oldCallbacks == null) {
2437 // This launcher has exited and nobody bothered to tell us. Just bail.
2438 Log.w(TAG, "LoaderTask running with no launcher");
2439 return;
2440 }
2441
Winson Chung9b9fb962013-11-15 15:39:34 -08002442 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2444 ArrayList<LauncherAppWidgetInfo> appWidgets =
2445 new ArrayList<LauncherAppWidgetInfo>();
2446 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2447 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002448 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002449 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450 workspaceItems.addAll(sBgWorkspaceItems);
2451 appWidgets.addAll(sBgAppWidgets);
2452 folders.putAll(sBgFolders);
2453 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002454 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 }
2456
Derek Prothro7aff3992013-12-10 14:00:37 -05002457 final boolean isLoadingSynchronously =
2458 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002459 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002460 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002461 if (currScreen >= orderedScreenIds.size()) {
2462 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002463 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002464 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002465 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002466 final long currentScreenId = currentScreen < 0
2467 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002468
2469 // Load all the items that are on the current page first (and in the process, unbind
2470 // all the existing workspace items before we call startBinding() below.
2471 unbindWorkspaceItemsOnMainThread();
2472
2473 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2475 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2476 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2477 new ArrayList<LauncherAppWidgetInfo>();
2478 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2479 new ArrayList<LauncherAppWidgetInfo>();
2480 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2481 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2482
Winson Chung9b9fb962013-11-15 15:39:34 -08002483 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002485 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002487 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 otherFolders);
2489 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2490 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2491
2492 // Tell the workspace that we're about to start binding items
2493 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002494 public void run() {
2495 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2496 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002498 }
2499 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500 };
Winson Chung81b52252012-08-27 15:34:29 -07002501 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502
Adam Cohendcd297f2013-06-18 13:13:40 -07002503 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 // Load items on the current page
2506 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2507 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002508 if (isLoadingSynchronously) {
2509 r = new Runnable() {
2510 public void run() {
2511 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002512 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002513 callbacks.onPageBoundSynchronously(currentScreen);
2514 }
2515 }
2516 };
Winson Chung81b52252012-08-27 15:34:29 -07002517 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002518 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519
Winson Chung4a2afa32012-07-19 14:53:05 -07002520 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2521 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002522 synchronized (mDeferredBindRunnables) {
2523 mDeferredBindRunnables.clear();
2524 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002526 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002527
2528 // Tell the workspace that we're done binding items
2529 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002530 public void run() {
2531 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2532 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002533 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002534 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535
Winson Chung98e030b2012-05-07 16:01:11 -07002536 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002537 if (DEBUG_LOADERS) {
2538 Log.d(TAG, "bound workspace in "
2539 + (SystemClock.uptimeMillis()-t) + "ms");
2540 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002541
2542 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002543 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002545 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002546 synchronized (mDeferredBindRunnables) {
2547 mDeferredBindRunnables.add(r);
2548 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002549 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002550 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002551 }
Joe Onorato36115782010-06-17 13:28:48 -04002552 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002553
Joe Onorato36115782010-06-17 13:28:48 -04002554 private void loadAndBindAllApps() {
2555 if (DEBUG_LOADERS) {
2556 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2557 }
2558 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002559 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002560 synchronized (LoaderTask.this) {
2561 if (mStopped) {
2562 return;
2563 }
2564 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002565 }
Joe Onorato36115782010-06-17 13:28:48 -04002566 } else {
2567 onlyBindAllApps();
2568 }
2569 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002570
Joe Onorato36115782010-06-17 13:28:48 -04002571 private void onlyBindAllApps() {
2572 final Callbacks oldCallbacks = mCallbacks.get();
2573 if (oldCallbacks == null) {
2574 // This launcher has exited and nobody bothered to tell us. Just bail.
2575 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2576 return;
2577 }
2578
2579 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002580 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002581 final ArrayList<AppInfo> list
2582 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002583 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002584 public void run() {
2585 final long t = SystemClock.uptimeMillis();
2586 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2587 if (callbacks != null) {
2588 callbacks.bindAllApplications(list);
2589 }
2590 if (DEBUG_LOADERS) {
2591 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2592 + (SystemClock.uptimeMillis()-t) + "ms");
2593 }
2594 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002595 };
2596 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002597 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002598 r.run();
2599 } else {
2600 mHandler.post(r);
2601 }
Joe Onorato36115782010-06-17 13:28:48 -04002602 }
2603
Winson Chung64359a52013-07-08 17:17:08 -07002604 private void loadAllApps() {
2605 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002606
Joe Onorato36115782010-06-17 13:28:48 -04002607 final Callbacks oldCallbacks = mCallbacks.get();
2608 if (oldCallbacks == null) {
2609 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002610 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002611 return;
2612 }
2613
Winson Chung64359a52013-07-08 17:17:08 -07002614 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002615 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2616 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2617
Winson Chung64359a52013-07-08 17:17:08 -07002618 // Clear the list of apps
2619 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002620
Winson Chung64359a52013-07-08 17:17:08 -07002621 // Query for the set of apps
2622 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2623 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2624 if (DEBUG_LOADERS) {
2625 Log.d(TAG, "queryIntentActivities took "
2626 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2627 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2628 }
2629 // Fail if we don't have any apps
2630 if (apps == null || apps.isEmpty()) {
2631 return;
2632 }
2633 // Sort the applications by name
2634 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2635 Collections.sort(apps,
2636 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2637 if (DEBUG_LOADERS) {
2638 Log.d(TAG, "sort took "
2639 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002640 }
2641
Winson Chung64359a52013-07-08 17:17:08 -07002642 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002643 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002644 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002645 // This builds the icon bitmaps.
2646 mBgAllAppsList.add(new AppInfo(packageManager, app,
2647 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002648 }
2649
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002650 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002651 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2652 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002653
2654 // Post callback on main thread
2655 mHandler.post(new Runnable() {
2656 public void run() {
2657 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002658 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002659 if (callbacks != null) {
2660 callbacks.bindAllApplications(added);
2661 if (DEBUG_LOADERS) {
2662 Log.d(TAG, "bound " + added.size() + " apps in "
2663 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2664 }
2665 } else {
2666 Log.i(TAG, "not binding apps: no Launcher activity");
2667 }
2668 }
2669 });
2670
Joe Onorato36115782010-06-17 13:28:48 -04002671 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002672 Log.d(TAG, "Icons processed in "
2673 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002674 }
2675 }
2676
2677 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002678 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002679 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2680 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2681 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2682 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2683 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2684 }
Joe Onorato36115782010-06-17 13:28:48 -04002685 }
2686 }
2687
2688 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002689 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002690 }
2691
2692 private class PackageUpdatedTask implements Runnable {
2693 int mOp;
2694 String[] mPackages;
2695
2696 public static final int OP_NONE = 0;
2697 public static final int OP_ADD = 1;
2698 public static final int OP_UPDATE = 2;
2699 public static final int OP_REMOVE = 3; // uninstlled
2700 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2701
2702
2703 public PackageUpdatedTask(int op, String[] packages) {
2704 mOp = op;
2705 mPackages = packages;
2706 }
2707
2708 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002709 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002710
2711 final String[] packages = mPackages;
2712 final int N = packages.length;
2713 switch (mOp) {
2714 case OP_ADD:
2715 for (int i=0; i<N; i++) {
2716 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Chris Wren6d0dde02014-02-10 12:16:54 -05002717 mIconCache.remove(packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002718 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002719 }
2720 break;
2721 case OP_UPDATE:
2722 for (int i=0; i<N; i++) {
2723 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002724 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002725 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002726 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002727 }
2728 break;
2729 case OP_REMOVE:
2730 case OP_UNAVAILABLE:
2731 for (int i=0; i<N; i++) {
2732 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002733 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002734 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002735 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002736 }
2737 break;
2738 }
2739
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002740 ArrayList<AppInfo> added = null;
2741 ArrayList<AppInfo> modified = null;
2742 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002743
Adam Cohen487f7dd2012-06-28 18:12:10 -07002744 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002745 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002746 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002747 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002748 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002749 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002750 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002751 }
Winson Chung5d55f332012-07-16 20:45:03 -07002752 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002753 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002754 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002755 }
2756
Joe Onorato36115782010-06-17 13:28:48 -04002757 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2758 if (callbacks == null) {
2759 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2760 return;
2761 }
2762
2763 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002764 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002765 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002766 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002767 addAndBindAddedWorkspaceApps(context, addedInfos);
2768 } else {
2769 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002770 }
Joe Onorato36115782010-06-17 13:28:48 -04002771 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002772
Joe Onorato36115782010-06-17 13:28:48 -04002773 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002774 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002775
2776 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002777 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002778 ArrayList<ItemInfo> infos =
2779 getItemInfoForComponentName(a.componentName);
2780 for (ItemInfo i : infos) {
2781 if (isShortcutInfoUpdateable(i)) {
2782 ShortcutInfo info = (ShortcutInfo) i;
2783 info.title = a.title.toString();
2784 updateItemInDatabase(context, info);
2785 }
2786 }
2787 }
2788
Joe Onorato36115782010-06-17 13:28:48 -04002789 mHandler.post(new Runnable() {
2790 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002791 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2792 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002793 callbacks.bindAppsUpdated(modifiedFinal);
2794 }
2795 }
2796 });
2797 }
Winson Chung83892cc2013-05-01 16:53:33 -07002798
Winson Chungdf95eb12013-10-16 14:57:07 -07002799 final ArrayList<String> removedPackageNames =
2800 new ArrayList<String>();
2801 if (mOp == OP_REMOVE) {
2802 // Mark all packages in the broadcast to be removed
2803 removedPackageNames.addAll(Arrays.asList(packages));
2804 } else if (mOp == OP_UPDATE) {
2805 // Mark disabled packages in the broadcast to be removed
2806 final PackageManager pm = context.getPackageManager();
2807 for (int i=0; i<N; i++) {
2808 if (isPackageDisabled(pm, packages[i])) {
2809 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002810 }
2811 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002812 }
2813 // Remove all the components associated with this package
2814 for (String pn : removedPackageNames) {
2815 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2816 for (ItemInfo i : infos) {
2817 deleteItemFromDatabase(context, i);
2818 }
2819 }
2820 // Remove all the specific components
2821 for (AppInfo a : removedApps) {
2822 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2823 for (ItemInfo i : infos) {
2824 deleteItemFromDatabase(context, i);
2825 }
2826 }
2827 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2828 // Remove any queued items from the install queue
2829 String spKey = LauncherAppState.getSharedPreferencesKey();
2830 SharedPreferences sp =
2831 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2832 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2833 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002834 mHandler.post(new Runnable() {
2835 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002836 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2837 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002838 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002839 }
2840 }
2841 });
Joe Onoratobe386092009-11-17 17:32:16 -08002842 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002843
Michael Jurkac402cd92013-05-20 15:49:32 +02002844 final ArrayList<Object> widgetsAndShortcuts =
2845 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002846 mHandler.post(new Runnable() {
2847 @Override
2848 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002849 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2850 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002851 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002852 }
2853 }
2854 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002855
2856 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002857 mHandler.post(new Runnable() {
2858 public void run() {
2859 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2860 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002861 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002862 }
2863 }
2864 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 }
2866 }
2867
Michael Jurkac402cd92013-05-20 15:49:32 +02002868 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2869 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2870 PackageManager packageManager = context.getPackageManager();
2871 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2872 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2873 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2874 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2875 Collections.sort(widgetsAndShortcuts,
2876 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2877 return widgetsAndShortcuts;
2878 }
2879
Adam Cohen556f6132014-01-15 15:18:08 -08002880 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002881 try {
2882 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2883 return !pi.applicationInfo.enabled;
2884 } catch (NameNotFoundException e) {
2885 // Fall through
2886 }
2887 return false;
2888 }
Adam Cohen556f6132014-01-15 15:18:08 -08002889
2890 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002891 if (cn == null) {
2892 return false;
2893 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002894 if (isPackageDisabled(pm, cn.getPackageName())) {
2895 return false;
2896 }
Winson Chungee055712013-07-30 14:46:24 -07002897
2898 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002899 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002900 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002901 return (pm.getActivityInfo(cn, 0) != null);
2902 } catch (NameNotFoundException e) {
2903 return false;
2904 }
2905 }
2906
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002908 * Make an ShortcutInfo object for a restored application or shortcut item that points
2909 * to a package that is not yet installed on the system.
2910 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002911 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002912 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002913 if (cursor != null) {
2914 info.title = cursor.getString(titleIndex);
2915 } else {
2916 info.title = "";
2917 }
Chris Wren6d0dde02014-02-10 12:16:54 -05002918 info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
Chris Wrenf4d08112014-01-16 18:13:56 -05002919 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002920 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002921 return info;
2922 }
2923
2924 /**
2925 * Make an Intent object for a restored application or shortcut item that points
2926 * to the market page for the item.
2927 */
2928 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002929 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002930 ComponentName componentName = intent.getComponent();
2931 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2932 Uri marketUri = new Uri.Builder()
2933 .scheme("market")
2934 .authority("details")
2935 .appendQueryParameter("id", componentName.getPackageName())
2936 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002937 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002938 marketIntent.setData(marketUri);
2939 return marketIntent;
2940 }
2941
2942 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002943 * This is called from the code that adds shortcuts from the intent receiver. This
2944 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 */
Joe Onorato56d82912010-03-07 14:32:10 -05002946 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002947 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949
Joe Onorato56d82912010-03-07 14:32:10 -05002950 /**
2951 * Make an ShortcutInfo object for a shortcut that is an application.
2952 *
2953 * If c is not null, then it will be used to fill in missing data like the title and icon.
2954 */
2955 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002956 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002957 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002958 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002959 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002960 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002961 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002962 } else {
2963 try {
2964 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2965 info.initFlagsAndFirstInstallTime(pi);
2966 } catch (NameNotFoundException e) {
2967 Log.d(TAG, "getPackInfo failed for package " +
2968 componentName.getPackageName());
2969 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002970 }
2971
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002972 // TODO: See if the PackageManager knows about this case. If it doesn't
2973 // then return null & delete this.
2974
Joe Onorato56d82912010-03-07 14:32:10 -05002975 // the resource -- This may implicitly give us back the fallback icon,
2976 // but don't worry about that. All we're doing with usingFallbackIcon is
2977 // to avoid saving lots of copies of that in the database, and most apps
2978 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002979
2980 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2981 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2982 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002983 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002984 ResolveInfo resolveInfo = null;
2985 ComponentName oldComponent = intent.getComponent();
2986 Intent newIntent = new Intent(intent.getAction(), null);
2987 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2988 newIntent.setPackage(oldComponent.getPackageName());
2989 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2990 for (ResolveInfo i : infos) {
2991 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2992 i.activityInfo.name);
2993 if (cn.equals(oldComponent)) {
2994 resolveInfo = i;
2995 }
2996 }
2997 if (resolveInfo == null) {
2998 resolveInfo = manager.resolveActivity(intent, 0);
2999 }
Joe Onorato56d82912010-03-07 14:32:10 -05003000 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07003001 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003002 }
Joe Onorato56d82912010-03-07 14:32:10 -05003003 // the db
3004 if (icon == null) {
3005 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003006 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003007 }
3008 }
3009 // the fallback icon
3010 if (icon == null) {
3011 icon = getFallbackIcon();
3012 info.usingFallbackIcon = true;
3013 }
3014 info.setIcon(icon);
3015
3016 // from the resource
3017 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003018 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
3019 if (labelCache != null && labelCache.containsKey(key)) {
3020 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07003021 } else {
3022 info.title = resolveInfo.activityInfo.loadLabel(manager);
3023 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003024 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003025 }
3026 }
Joe Onorato56d82912010-03-07 14:32:10 -05003027 }
3028 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003030 if (c != null) {
3031 info.title = c.getString(titleIndex);
3032 }
3033 }
3034 // fall back to the class name of the activity
3035 if (info.title == null) {
3036 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
3039 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003041
Winson Chung64359a52013-07-08 17:17:08 -07003042 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3043 ItemInfoFilter f) {
3044 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3045 for (ItemInfo i : infos) {
3046 if (i instanceof ShortcutInfo) {
3047 ShortcutInfo info = (ShortcutInfo) i;
3048 ComponentName cn = info.intent.getComponent();
3049 if (cn != null && f.filterItem(null, info, cn)) {
3050 filtered.add(info);
3051 }
3052 } else if (i instanceof FolderInfo) {
3053 FolderInfo info = (FolderInfo) i;
3054 for (ShortcutInfo s : info.contents) {
3055 ComponentName cn = s.intent.getComponent();
3056 if (cn != null && f.filterItem(info, s, cn)) {
3057 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003058 }
3059 }
Winson Chung64359a52013-07-08 17:17:08 -07003060 } else if (i instanceof LauncherAppWidgetInfo) {
3061 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3062 ComponentName cn = info.providerName;
3063 if (cn != null && f.filterItem(null, info, cn)) {
3064 filtered.add(info);
3065 }
Winson Chung8a435102012-08-30 17:16:53 -07003066 }
3067 }
Winson Chung64359a52013-07-08 17:17:08 -07003068 return new ArrayList<ItemInfo>(filtered);
3069 }
3070
3071 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07003072 ItemInfoFilter filter = new ItemInfoFilter() {
3073 @Override
3074 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3075 return cn.getPackageName().equals(pn);
3076 }
3077 };
3078 return filterItemInfos(sBgItemsIdMap.values(), filter);
3079 }
3080
3081 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003082 ItemInfoFilter filter = new ItemInfoFilter() {
3083 @Override
3084 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3085 return cn.equals(cname);
3086 }
3087 };
3088 return filterItemInfos(sBgItemsIdMap.values(), filter);
3089 }
3090
3091 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3092 if (i instanceof ShortcutInfo) {
3093 ShortcutInfo info = (ShortcutInfo) i;
3094 // We need to check for ACTION_MAIN otherwise getComponent() might
3095 // return null for some shortcuts (for instance, for shortcuts to
3096 // web pages.)
3097 Intent intent = info.intent;
3098 ComponentName name = intent.getComponent();
3099 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3100 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3101 return true;
3102 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003103 // placeholder shortcuts get special treatment, let them through too.
3104 if (info.getRestoredIntent() != null) {
3105 return true;
3106 }
Winson Chung64359a52013-07-08 17:17:08 -07003107 }
3108 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003109 }
3110
3111 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003112 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003114 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003115 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3116 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117
Joe Onorato56d82912010-03-07 14:32:10 -05003118 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003119 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003122 // TODO: If there's an explicit component and we can't install that, delete it.
3123
Joe Onorato56d82912010-03-07 14:32:10 -05003124 info.title = c.getString(titleIndex);
3125
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126 int iconType = c.getInt(iconTypeIndex);
3127 switch (iconType) {
3128 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3129 String packageName = c.getString(iconPackageIndex);
3130 String resourceName = c.getString(iconResourceIndex);
3131 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003132 info.customIcon = false;
3133 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003135 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003136 if (resources != null) {
3137 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003138 icon = Utilities.createIconBitmap(
3139 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003140 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003141 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003142 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 }
Joe Onorato56d82912010-03-07 14:32:10 -05003144 // the db
3145 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003146 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003147 }
3148 // the fallback icon
3149 if (icon == null) {
3150 icon = getFallbackIcon();
3151 info.usingFallbackIcon = true;
3152 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 break;
3154 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003155 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003156 if (icon == null) {
3157 icon = getFallbackIcon();
3158 info.customIcon = false;
3159 info.usingFallbackIcon = true;
3160 } else {
3161 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003162 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163 break;
3164 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003165 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003166 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003167 info.customIcon = false;
3168 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003170 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 return info;
3172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173
Michael Jurka931dc972011-08-05 15:08:15 -07003174 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003175 @SuppressWarnings("all") // suppress dead code warning
3176 final boolean debug = false;
3177 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003178 Log.d(TAG, "getIconFromCursor app="
3179 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3180 }
3181 byte[] data = c.getBlob(iconIndex);
3182 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003183 return Utilities.createIconBitmap(
3184 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003185 } catch (Exception e) {
3186 return null;
3187 }
3188 }
3189
Winson Chung3d503fb2011-07-13 17:25:49 -07003190 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3191 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003192 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003193 if (info == null) {
3194 return null;
3195 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003197
3198 return info;
3199 }
3200
Winson Chunga9abd0e2010-10-27 17:18:37 -07003201 /**
Winson Chung55cef262010-10-28 14:14:18 -07003202 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3203 */
3204 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3205 ComponentName component) {
3206 List<AppWidgetProviderInfo> widgets =
3207 AppWidgetManager.getInstance(context).getInstalledProviders();
3208 for (AppWidgetProviderInfo info : widgets) {
3209 if (info.provider.equals(component)) {
3210 return info;
3211 }
3212 }
3213 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003214 }
3215
Winson Chung68846fd2010-10-29 11:00:27 -07003216 /**
3217 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3218 */
3219 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3220 final PackageManager packageManager = context.getPackageManager();
3221 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3222 new ArrayList<WidgetMimeTypeHandlerData>();
3223
3224 final Intent supportsIntent =
3225 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3226 supportsIntent.setType(mimeType);
3227
3228 // Create a set of widget configuration components that we can test against
3229 final List<AppWidgetProviderInfo> widgets =
3230 AppWidgetManager.getInstance(context).getInstalledProviders();
3231 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3232 new HashMap<ComponentName, AppWidgetProviderInfo>();
3233 for (AppWidgetProviderInfo info : widgets) {
3234 configurationComponentToWidget.put(info.configure, info);
3235 }
3236
3237 // Run through each of the intents that can handle this type of clip data, and cross
3238 // reference them with the components that are actual configuration components
3239 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3240 PackageManager.MATCH_DEFAULT_ONLY);
3241 for (ResolveInfo info : activities) {
3242 final ActivityInfo activityInfo = info.activityInfo;
3243 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3244 activityInfo.name);
3245 if (configurationComponentToWidget.containsKey(infoComponent)) {
3246 supportedConfigurationActivities.add(
3247 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3248 configurationComponentToWidget.get(infoComponent)));
3249 }
3250 }
3251 return supportedConfigurationActivities;
3252 }
3253
Winson Chunga9abd0e2010-10-27 17:18:37 -07003254 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003255 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3256 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3257 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3258
Adam Cohend9198822011-11-22 16:42:47 -08003259 if (intent == null) {
3260 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3261 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3262 return null;
3263 }
3264
Joe Onorato0589f0f2010-02-08 13:44:00 -08003265 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003266 boolean customIcon = false;
3267 ShortcutIconResource iconResource = null;
3268
3269 if (bitmap != null && bitmap instanceof Bitmap) {
3270 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003271 customIcon = true;
3272 } else {
3273 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3274 if (extra != null && extra instanceof ShortcutIconResource) {
3275 try {
3276 iconResource = (ShortcutIconResource) extra;
3277 final PackageManager packageManager = context.getPackageManager();
3278 Resources resources = packageManager.getResourcesForApplication(
3279 iconResource.packageName);
3280 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003281 icon = Utilities.createIconBitmap(
3282 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003283 } catch (Exception e) {
3284 Log.w(TAG, "Could not load shortcut icon: " + extra);
3285 }
3286 }
3287 }
3288
Michael Jurkac9d95c52011-08-29 14:03:34 -07003289 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003290
3291 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003292 if (fallbackIcon != null) {
3293 icon = fallbackIcon;
3294 } else {
3295 icon = getFallbackIcon();
3296 info.usingFallbackIcon = true;
3297 }
Joe Onorato56d82912010-03-07 14:32:10 -05003298 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003299 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003300
Joe Onorato0589f0f2010-02-08 13:44:00 -08003301 info.title = name;
3302 info.intent = intent;
3303 info.customIcon = customIcon;
3304 info.iconResource = iconResource;
3305
3306 return info;
3307 }
3308
Winson Chungaac01e12011-08-17 10:37:13 -07003309 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3310 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003311 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003312 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003313 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003314 }
Joe Onorato56d82912010-03-07 14:32:10 -05003315 // If this icon doesn't have a custom icon, check to see
3316 // what's stored in the DB, and if it doesn't match what
3317 // we're going to show, store what we are going to show back
3318 // into the DB. We do this so when we're loading, if the
3319 // package manager can't find an icon (for example because
3320 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003321 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003322 cache.put(info, c.getBlob(iconIndex));
3323 return true;
3324 }
3325 return false;
3326 }
3327 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3328 boolean needSave = false;
3329 try {
3330 if (data != null) {
3331 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3332 Bitmap loaded = info.getIcon(mIconCache);
3333 needSave = !saved.sameAs(loaded);
3334 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003335 needSave = true;
3336 }
Winson Chungaac01e12011-08-17 10:37:13 -07003337 } catch (Exception e) {
3338 needSave = true;
3339 }
3340 if (needSave) {
3341 Log.d(TAG, "going to save icon bitmap for info=" + info);
3342 // This is slower than is ideal, but this only happens once
3343 // or when the app is updated with a new icon.
3344 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003345 }
3346 }
3347
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003349 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350 * or make a new one.
3351 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003352 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003353 // See if a placeholder was created for us already
3354 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003355 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003357 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 folders.put(id, folderInfo);
3359 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003360 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003361 }
3362
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003363 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003364 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003365 return new Comparator<AppInfo>() {
3366 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003367 int result = collator.compare(a.title.toString().trim(),
3368 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003369 if (result == 0) {
3370 result = a.componentName.compareTo(b.componentName);
3371 }
3372 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003373 }
Winson Chung11904872012-09-17 16:58:46 -07003374 };
3375 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003376 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3377 = new Comparator<AppInfo>() {
3378 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003379 if (a.firstInstallTime < b.firstInstallTime) return 1;
3380 if (a.firstInstallTime > b.firstInstallTime) return -1;
3381 return 0;
3382 }
3383 };
Winson Chung11904872012-09-17 16:58:46 -07003384 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3385 final Collator collator = Collator.getInstance();
3386 return new Comparator<AppWidgetProviderInfo>() {
3387 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003388 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003389 }
3390 };
3391 }
Winson Chung5308f242011-08-18 12:12:41 -07003392 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3393 if (info.activityInfo != null) {
3394 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3395 } else {
3396 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3397 }
3398 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003399 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003400 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003401 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003402 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003403 ShortcutNameComparator(PackageManager pm) {
3404 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003405 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003406 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003407 }
3408 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3409 mPackageManager = pm;
3410 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003411 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003412 }
3413 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003414 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003415 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3416 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3417 if (mLabelCache.containsKey(keyA)) {
3418 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003419 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003420 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003421
Winson Chung5308f242011-08-18 12:12:41 -07003422 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003423 }
Winson Chung5308f242011-08-18 12:12:41 -07003424 if (mLabelCache.containsKey(keyB)) {
3425 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003426 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003427 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003428
Winson Chung5308f242011-08-18 12:12:41 -07003429 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003430 }
Winson Chung11904872012-09-17 16:58:46 -07003431 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003432 }
3433 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003434 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003435 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003436 private PackageManager mPackageManager;
3437 private HashMap<Object, String> mLabelCache;
3438 WidgetAndShortcutNameComparator(PackageManager pm) {
3439 mPackageManager = pm;
3440 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003441 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003442 }
3443 public final int compare(Object a, Object b) {
3444 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003445 if (mLabelCache.containsKey(a)) {
3446 labelA = mLabelCache.get(a);
3447 } else {
3448 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003449 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003450 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003451 mLabelCache.put(a, labelA);
3452 }
3453 if (mLabelCache.containsKey(b)) {
3454 labelB = mLabelCache.get(b);
3455 } else {
3456 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003457 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003458 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003459 mLabelCache.put(b, labelB);
3460 }
Winson Chung11904872012-09-17 16:58:46 -07003461 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003462 }
3463 };
Joe Onoratobe386092009-11-17 17:32:16 -08003464
3465 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003466 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003467 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3468 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3469 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3470 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003471 if (mLoaderTask != null) {
3472 mLoaderTask.dumpState();
3473 } else {
3474 Log.d(TAG, "mLoaderTask=null");
3475 }
Joe Onoratobe386092009-11-17 17:32:16 -08003476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003477}