blob: 007fd7a4af029829f830d67d98d9c6a934cfaa77 [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);
Winson Chung83892cc2013-05-01 16:53:33 -0700176 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700177 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200178 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100179 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700180 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700181 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700182 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Winson Chung64359a52013-07-08 17:17:08 -0700185 public interface ItemInfoFilter {
186 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
187 }
188
Bjorn Bringert1307f632013-10-03 22:31:03 +0100189 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800190 Context context = app.getContext();
191 ContentResolver contentResolver = context.getContentResolver();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400192
Winson Chungee055712013-07-30 14:46:24 -0700193 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Winson Chunga6945242014-01-08 14:04:34 -0800194 mOldContentProviderExists = (contentResolver.acquireContentProviderClient(
195 LauncherSettings.Favorites.OLD_CONTENT_URI) != null);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400196 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100197 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800198 mIconCache = iconCache;
199
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400200 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700201 Configuration config = res.getConfiguration();
202 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 }
204
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700205 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
206 * posted on the main thread handler. */
207 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700208 runOnMainThread(r, 0);
209 }
210 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700211 if (sWorkerThread.getThreadId() == Process.myTid()) {
212 // If we are on the worker thread, post onto the main handler
213 mHandler.post(r);
214 } else {
215 r.run();
216 }
217 }
218
219 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
220 * posted on the worker thread handler. */
221 private static void runOnWorkerThread(Runnable r) {
222 if (sWorkerThread.getThreadId() == Process.myTid()) {
223 r.run();
224 } else {
225 // If we are not on the worker thread, then post to the worker handler
226 sWorker.post(r);
227 }
228 }
229
Winson Chunge43a1e72014-01-15 10:33:02 -0800230 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
231 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800232 }
233
Winson Chungc9168342013-06-26 14:54:55 -0700234 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
235 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700236 LauncherAppState app = LauncherAppState.getInstance();
237 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
238 final int xCount = (int) grid.numColumns;
239 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700240 boolean[][] occupied = new boolean[xCount][yCount];
241
242 int cellX, cellY, spanX, spanY;
243 for (int i = 0; i < items.size(); ++i) {
244 final ItemInfo item = items.get(i);
245 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
246 if (item.screenId == screen) {
247 cellX = item.cellX;
248 cellY = item.cellY;
249 spanX = item.spanX;
250 spanY = item.spanY;
251 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
252 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
253 occupied[x][y] = true;
254 }
255 }
256 }
257 }
258 }
259
260 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
261 }
262 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700263 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700264 int firstScreenIndex,
265 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700266 // Lock on the app so that we don't try and get the items while apps are being added
267 LauncherAppState app = LauncherAppState.getInstance();
268 LauncherModel model = app.getModel();
269 boolean found = false;
270 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700271 if (sWorkerThread.getThreadId() != Process.myTid()) {
272 // Flush the LauncherModel worker thread, so that if we just did another
273 // processInstallShortcut, we give it time for its shortcut to get added to the
274 // database (getItemsInLocalCoordinates reads the database)
275 model.flushWorkerThread();
276 }
Winson Chungc9168342013-06-26 14:54:55 -0700277 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700278
279 // Try adding to the workspace screens incrementally, starting at the default or center
280 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700281 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
282 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700283 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700284 int[] tmpCoordinates = new int[2];
285 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700286 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700287 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700288 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700289 }
290 }
291 }
Winson Chungc9168342013-06-26 14:54:55 -0700292 return null;
293 }
294
Adam Cohen76a47a12014-02-05 11:47:43 -0800295 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
296 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
297
298 if (allAppsApps == null) {
299 throw new RuntimeException("allAppsApps must not be null");
300 }
301 if (allAppsApps.isEmpty()) {
302 return;
303 }
304
Chris Wrenb6d4c282014-01-27 14:17:08 -0500305 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
306 Iterator<AppInfo> iter = allAppsApps.iterator();
307 while (iter.hasNext()) {
308 ItemInfo a = iter.next();
309 if (LauncherModel.appWasRestored(ctx, a.getIntent())) {
310 restoredAppsFinal.add((AppInfo) a);
311 }
312 }
313
Adam Cohen76a47a12014-02-05 11:47:43 -0800314 // Process the newly added applications and add them to the database first
315 Runnable r = new Runnable() {
316 public void run() {
317 runOnMainThread(new Runnable() {
318 public void run() {
319 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
320 if (callbacks == cb && cb != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500321 if (!restoredAppsFinal.isEmpty()) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500322 for (AppInfo info : restoredAppsFinal) {
323 final Intent intent = info.getIntent();
324 if (intent != null) {
325 mIconCache.deletePreloadedIcon(intent.getComponent());
326 }
327 }
Chris Wrenb6d4c282014-01-27 14:17:08 -0500328 callbacks.bindAppsUpdated(restoredAppsFinal);
329 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500330 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800331 }
332 }
333 });
334 }
335 };
336 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700337 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800338
339 public void addAndBindAddedWorkspaceApps(final Context context,
340 final ArrayList<ItemInfo> workspaceApps) {
341 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
342
343 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800344 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
345 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800346 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700347 return;
Winson Chung997a9232013-07-24 15:33:46 -0700348 }
Winson Chung64359a52013-07-08 17:17:08 -0700349 // Process the newly added applications and add them to the database first
350 Runnable r = new Runnable() {
351 public void run() {
352 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
353 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500354 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700355
Winson Chung76828c82013-08-19 15:43:29 -0700356 // Get the list of workspace screens. We need to append to this list and
357 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
358 // called.
359 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
360 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
361 for (Integer i : orderedScreens.keySet()) {
362 long screenId = orderedScreens.get(i);
363 workspaceScreens.add(screenId);
364 }
365
Winson Chung64359a52013-07-08 17:17:08 -0700366 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700367 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700368 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700369 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700370 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700371 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700372
373 // Short-circuit this logic if the icon exists somewhere on the workspace
374 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500375 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
376 if (a instanceof AppInfo &&
377 LauncherModel.appWasRestored(context, launchIntent)) {
378 restoredAppsFinal.add((AppInfo) a);
379 }
Winson Chung64359a52013-07-08 17:17:08 -0700380 continue;
381 }
382
Winson Chung87412982013-10-03 18:34:14 -0700383 // Add this icon to the db, creating a new page if necessary. If there
384 // is only the empty page then we just add items to the first page.
385 // Otherwise, we add them to the next pages.
386 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700387 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700388 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700389 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200390 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700391
392 // If we can't find a valid position, then just add a new screen.
393 // This takes time so we need to re-queue the add until the new
394 // page is added. Create as many screens as necessary to satisfy
395 // the startSearchPageIndex.
396 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700397 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700398 while (numPagesToAdd > 0) {
399 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700400 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700401 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700402 addedWorkspaceScreensFinal.add(screenId);
403 numPagesToAdd--;
404 }
Winson Chung76828c82013-08-19 15:43:29 -0700405
Winson Chung64359a52013-07-08 17:17:08 -0700406 // Find the coordinate again
407 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700408 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700409 }
410 if (coords == null) {
411 throw new RuntimeException("Coordinates should not be null");
412 }
413
Winson Chung997a9232013-07-24 15:33:46 -0700414 ShortcutInfo shortcutInfo;
415 if (a instanceof ShortcutInfo) {
416 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200417 } else if (a instanceof AppInfo) {
418 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700419 } else {
420 throw new RuntimeException("Unexpected info type");
421 }
Winson Chung94d67682013-09-25 16:29:40 -0700422
Winson Chung64359a52013-07-08 17:17:08 -0700423 // Add the shortcut to the db
424 addItemToDatabase(context, shortcutInfo,
425 LauncherSettings.Favorites.CONTAINER_DESKTOP,
426 coords.first, coords.second[0], coords.second[1], false);
427 // Save the ShortcutInfo for binding in the workspace
428 addedShortcutsFinal.add(shortcutInfo);
429 }
430 }
431
Winson Chung76828c82013-08-19 15:43:29 -0700432 // Update the workspace screens
433 updateWorkspaceScreenOrder(context, workspaceScreens);
434
Adam Cohen76a47a12014-02-05 11:47:43 -0800435 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700436 runOnMainThread(new Runnable() {
437 public void run() {
438 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
439 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700440 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
441 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700442 if (!addedShortcutsFinal.isEmpty()) {
443 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
444 long lastScreenId = info.screenId;
445 for (ItemInfo i : addedShortcutsFinal) {
446 if (i.screenId == lastScreenId) {
447 addAnimated.add(i);
448 } else {
449 addNotAnimated.add(i);
450 }
Winson Chung997a9232013-07-24 15:33:46 -0700451 }
452 }
Winson Chungd64d1762013-08-20 14:37:16 -0700453 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800454 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500455 if (!restoredAppsFinal.isEmpty()) {
456 callbacks.bindAppsUpdated(restoredAppsFinal);
457 }
Winson Chung997a9232013-07-24 15:33:46 -0700458 }
Winson Chung64359a52013-07-08 17:17:08 -0700459 }
Winson Chung997a9232013-07-24 15:33:46 -0700460 });
461 }
Winson Chung64359a52013-07-08 17:17:08 -0700462 }
463 };
464 runOnWorkerThread(r);
465 }
466
Joe Onorato56d82912010-03-07 14:32:10 -0500467 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700468 if (mDefaultIcon == null) {
469 final Context context = LauncherAppState.getInstance().getContext();
470 mDefaultIcon = Utilities.createIconBitmap(
471 mIconCache.getFullResDefaultActivityIcon(), context);
472 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800473 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475
Winson Chung81b52252012-08-27 15:34:29 -0700476 public void unbindItemInfosAndClearQueuedBindRunnables() {
477 if (sWorkerThread.getThreadId() == Process.myTid()) {
478 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
479 "main thread");
480 }
481
482 // Clear any deferred bind runnables
483 mDeferredBindRunnables.clear();
484 // Remove any queued bind runnables
485 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
486 // Unbind all the workspace items
487 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700488 }
489
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700490 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700491 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700492 // Ensure that we don't use the same workspace items data structure on the main thread
493 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700494 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
495 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700496 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700497 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
498 tmpAppWidgets.addAll(sBgAppWidgets);
499 }
500 Runnable r = new Runnable() {
501 @Override
502 public void run() {
503 for (ItemInfo item : tmpWorkspaceItems) {
504 item.unbind();
505 }
506 for (ItemInfo item : tmpAppWidgets) {
507 item.unbind();
508 }
509 }
510 };
511 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700512 }
513
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 /**
515 * Adds an item to the DB if it was not created previously, or move it to a new
516 * <container, screen, cellX, cellY>
517 */
518 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700519 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400520 if (item.container == ItemInfo.NO_ID) {
521 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700522 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400523 } else {
524 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700525 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 }
527 }
528
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700529 static void checkItemInfoLocked(
530 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
531 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
532 if (modelItem != null && item != modelItem) {
533 // check all the data is consistent
534 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
535 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
536 ShortcutInfo shortcut = (ShortcutInfo) item;
537 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
538 modelShortcut.intent.filterEquals(shortcut.intent) &&
539 modelShortcut.id == shortcut.id &&
540 modelShortcut.itemType == shortcut.itemType &&
541 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700542 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700543 modelShortcut.cellX == shortcut.cellX &&
544 modelShortcut.cellY == shortcut.cellY &&
545 modelShortcut.spanX == shortcut.spanX &&
546 modelShortcut.spanY == shortcut.spanY &&
547 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
548 (modelShortcut.dropPos != null &&
549 shortcut.dropPos != null &&
550 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
551 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
552 // For all intents and purposes, this is the same object
553 return;
554 }
555 }
556
557 // the modelItem needs to match up perfectly with item if our model is
558 // to be consistent with the database-- for now, just require
559 // modelItem == item or the equality check above
560 String msg = "item: " + ((item != null) ? item.toString() : "null") +
561 "modelItem: " +
562 ((modelItem != null) ? modelItem.toString() : "null") +
563 "Error: ItemInfo passed to checkItemInfo doesn't match original";
564 RuntimeException e = new RuntimeException(msg);
565 if (stackTrace != null) {
566 e.setStackTrace(stackTrace);
567 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800568 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700569 }
570 }
571
Michael Jurka816474f2012-06-25 14:49:02 -0700572 static void checkItemInfo(final ItemInfo item) {
573 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
574 final long itemId = item.id;
575 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700576 public void run() {
577 synchronized (sBgLock) {
578 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700579 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700580 }
581 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700582 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700583 }
584
Michael Jurkac9d95c52011-08-29 14:03:34 -0700585 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
586 final ItemInfo item, final String callingFunction) {
587 final long itemId = item.id;
588 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
589 final ContentResolver cr = context.getContentResolver();
590
Adam Cohen487f7dd2012-06-28 18:12:10 -0700591 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700592 Runnable r = new Runnable() {
593 public void run() {
594 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700595 updateItemArrays(item, itemId, stackTrace);
596 }
597 };
598 runOnWorkerThread(r);
599 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700600
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700601 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
602 final ArrayList<ItemInfo> items, final String callingFunction) {
603 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700604
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700605 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
606 Runnable r = new Runnable() {
607 public void run() {
608 ArrayList<ContentProviderOperation> ops =
609 new ArrayList<ContentProviderOperation>();
610 int count = items.size();
611 for (int i = 0; i < count; i++) {
612 ItemInfo item = items.get(i);
613 final long itemId = item.id;
614 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
615 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700616
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700617 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
618 updateItemArrays(item, itemId, stackTrace);
619
620 }
621 try {
622 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
623 } catch (Exception e) {
624 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700625 }
626 }
627 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700628 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700629 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700630
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700631 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
632 // Lock on mBgLock *after* the db operation
633 synchronized (sBgLock) {
634 checkItemInfoLocked(itemId, item, stackTrace);
635
636 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
637 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
638 // Item is in a folder, make sure this folder exists
639 if (!sBgFolders.containsKey(item.container)) {
640 // An items container is being set to a that of an item which is not in
641 // the list of Folders.
642 String msg = "item: " + item + " container being set to: " +
643 item.container + ", not in the list of folders";
644 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700645 }
646 }
647
648 // Items are added/removed from the corresponding FolderInfo elsewhere, such
649 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
650 // that are on the desktop, as appropriate
651 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800652 if (modelItem != null &&
653 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
654 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700655 switch (modelItem.itemType) {
656 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
657 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
658 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
659 if (!sBgWorkspaceItems.contains(modelItem)) {
660 sBgWorkspaceItems.add(modelItem);
661 }
662 break;
663 default:
664 break;
665 }
666 } else {
667 sBgWorkspaceItems.remove(modelItem);
668 }
669 }
670 }
671
Michael Jurkac7700af2013-05-14 20:17:58 +0200672 public void flushWorkerThread() {
673 mFlushingWorkerThread = true;
674 Runnable waiter = new Runnable() {
675 public void run() {
676 synchronized (this) {
677 notifyAll();
678 mFlushingWorkerThread = false;
679 }
680 }
681 };
682
683 synchronized(waiter) {
684 runOnWorkerThread(waiter);
685 if (mLoaderTask != null) {
686 synchronized(mLoaderTask) {
687 mLoaderTask.notify();
688 }
689 }
690 boolean success = false;
691 while (!success) {
692 try {
693 waiter.wait();
694 success = true;
695 } catch (InterruptedException e) {
696 }
697 }
698 }
699 }
700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700703 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700704 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 item.cellX = cellX;
708 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700709
Winson Chung3d503fb2011-07-13 17:25:49 -0700710 // We store hotseat items in canonical form which is this orientation invariant position
711 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700712 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700713 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700715 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718
719 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700721 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
722 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724
Michael Jurkac9d95c52011-08-29 14:03:34 -0700725 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700726 }
727
728 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700729 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
730 * cellX, cellY have already been updated on the ItemInfos.
731 */
732 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
733 final long container, final int screen) {
734
735 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
736 int count = items.size();
737
738 for (int i = 0; i < count; i++) {
739 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700740 item.container = container;
741
742 // We store hotseat items in canonical form which is this orientation invariant position
743 // in the hotseat
744 if (context instanceof Launcher && screen < 0 &&
745 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700746 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 item.cellY);
748 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700749 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 }
751
752 final ContentValues values = new ContentValues();
753 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
754 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
755 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700756 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700757
758 contentValues.add(values);
759 }
760 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
761 }
762
763 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700764 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800765 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700766 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700767 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700768 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800769 item.cellX = cellX;
770 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700771 item.spanX = spanX;
772 item.spanY = spanY;
773
774 // We store hotseat items in canonical form which is this orientation invariant position
775 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700777 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700778 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700779 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700781 }
Adam Cohend4844c32011-02-18 19:25:06 -0800782
Adam Cohend4844c32011-02-18 19:25:06 -0800783 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800784 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
786 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
787 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
788 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800790
Michael Jurka816474f2012-06-25 14:49:02 -0700791 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700792 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700793
794 /**
795 * Update an item to the database in a specified container.
796 */
797 static void updateItemInDatabase(Context context, final ItemInfo item) {
798 final ContentValues values = new ContentValues();
799 item.onAddToDatabase(values);
800 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
801 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800802 }
803
804 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400805 * Returns true if the shortcuts already exists in the database.
806 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808 static boolean shortcutExists(Context context, String title, Intent intent) {
809 final ContentResolver cr = context.getContentResolver();
810 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
811 new String[] { "title", "intent" }, "title=? and intent=?",
812 new String[] { title, intent.toUri(0) }, null);
813 boolean result = false;
814 try {
815 result = c.moveToFirst();
816 } finally {
817 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700820 }
821
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500823 * Returns true if the shortcuts already exists in the database.
824 * we identify a shortcut by the component name of the intent.
825 */
826 static boolean appWasRestored(Context context, Intent intent) {
827 final ContentResolver cr = context.getContentResolver();
828 final ComponentName component = intent.getComponent();
829 if (component == null) {
830 return false;
831 }
832 String componentName = component.flattenToString();
833 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
834 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
835 new String[]{"intent", "restored"}, where, null, null);
836 boolean result = false;
837 try {
838 result = c.moveToFirst();
839 } finally {
840 c.close();
841 }
842 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
843 return result;
844 }
845
846 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700847 * Returns an ItemInfo array containing all the items in the LauncherModel.
848 * The ItemInfo.id is not set through this function.
849 */
850 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
851 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
852 final ContentResolver cr = context.getContentResolver();
853 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
854 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
855 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
856 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
857
858 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
859 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
860 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
861 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
862 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
863 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
864 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
865
866 try {
867 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700868 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700869 item.cellX = c.getInt(cellXIndex);
870 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700871 item.spanX = Math.max(1, c.getInt(spanXIndex));
872 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700873 item.container = c.getInt(containerIndex);
874 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700876
877 items.add(item);
878 }
879 } catch (Exception e) {
880 items.clear();
881 } finally {
882 c.close();
883 }
884
885 return items;
886 }
887
888 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
890 */
891 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
892 final ContentResolver cr = context.getContentResolver();
893 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
894 "_id=? and (itemType=? or itemType=?)",
895 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700896 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700897
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 try {
899 if (c.moveToFirst()) {
900 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
901 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
902 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
903 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
904 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
905 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907 FolderInfo folderInfo = null;
908 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700909 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
910 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700912 }
913
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 folderInfo.title = c.getString(titleIndex);
915 folderInfo.id = id;
916 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700917 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700918 folderInfo.cellX = c.getInt(cellXIndex);
919 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920
921 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700922 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923 } finally {
924 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700925 }
926
927 return null;
928 }
929
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 /**
931 * Add an item to the database in a specified container. Sets the container, screen, cellX and
932 * cellY fields of the item. Also assigns an ID to the item.
933 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 item.cellX = cellX;
938 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 // We store hotseat items in canonical form which is this orientation invariant position
940 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947
948 final ContentValues values = new ContentValues();
949 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 item.onAddToDatabase(values);
951
Michael Jurka414300a2013-08-27 15:42:35 +0200952 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700953 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700954 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700955
Michael Jurkac9d95c52011-08-29 14:03:34 -0700956 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700957 public void run() {
958 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
959 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700961 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700962 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700963 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700964 sBgItemsIdMap.put(item.id, item);
965 switch (item.itemType) {
966 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
967 sBgFolders.put(item.id, (FolderInfo) item);
968 // Fall through
969 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
970 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
971 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
972 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
973 sBgWorkspaceItems.add(item);
974 } else {
975 if (!sBgFolders.containsKey(item.container)) {
976 // Adding an item to a folder that doesn't exist.
977 String msg = "adding item: " + item + " to a folder that " +
978 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700979 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700980 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700981 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700982 break;
983 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
984 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
985 break;
986 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700987 }
988 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700989 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700990 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700991 }
992
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700994 * Creates a new unique child id, for a given cell span across all layouts.
995 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700996 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700997 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700999 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001000 }
1001
Winson Chungaafa03c2010-06-11 17:34:16 -07001002 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001003 * Removes the specified item from the database
1004 * @param context
1005 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001009 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001010
Michael Jurka83df1882011-08-31 20:59:26 -07001011 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001012 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001013 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014
1015 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001016 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 switch (item.itemType) {
1018 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1019 sBgFolders.remove(item.id);
1020 for (ItemInfo info: sBgItemsIdMap.values()) {
1021 if (info.container == item.id) {
1022 // We are deleting a folder which still contains items that
1023 // think they are contained by that folder.
1024 String msg = "deleting a folder (" + item + ") which still " +
1025 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001026 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001028 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001029 sBgWorkspaceItems.remove(item);
1030 break;
1031 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1032 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1033 sBgWorkspaceItems.remove(item);
1034 break;
1035 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1036 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1037 break;
1038 }
1039 sBgItemsIdMap.remove(item.id);
1040 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001041 }
1042 }
Michael Jurka83df1882011-08-31 20:59:26 -07001043 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001044 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 }
1046
1047 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001048 * Update the order of the workspace screens in the database. The array list contains
1049 * a list of screen ids in the order that they should appear.
1050 */
Winson Chungc9168342013-06-26 14:54:55 -07001051 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001052 // Log to disk
1053 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1054 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1055
Winson Chung64359a52013-07-08 17:17:08 -07001056 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001057 final ContentResolver cr = context.getContentResolver();
1058 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1059
1060 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001061 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001062 while (iter.hasNext()) {
1063 long id = iter.next();
1064 if (id < 0) {
1065 iter.remove();
1066 }
1067 }
1068
1069 Runnable r = new Runnable() {
1070 @Override
1071 public void run() {
Yura085c8532014-02-11 15:15:29 +00001072 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001073 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001074 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001075 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 for (int i = 0; i < count; i++) {
1077 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001078 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1080 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001081 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001082 }
Yura085c8532014-02-11 15:15:29 +00001083
1084 try {
1085 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1086 } catch (Exception ex) {
1087 throw new RuntimeException(ex);
1088 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001089
Winson Chungba9c37f2013-08-30 14:11:37 -07001090 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001091 sBgWorkspaceScreens.clear();
1092 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001093 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001094 }
1095 };
1096 runOnWorkerThread(r);
1097 }
1098
1099 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001100 * Remove the contents of the specified folder from the database
1101 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001102 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001103 final ContentResolver cr = context.getContentResolver();
1104
Michael Jurkac9d95c52011-08-29 14:03:34 -07001105 Runnable r = new Runnable() {
1106 public void run() {
1107 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001108 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001109 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 sBgItemsIdMap.remove(info.id);
1111 sBgFolders.remove(info.id);
1112 sBgDbIconCache.remove(info);
1113 sBgWorkspaceItems.remove(info);
1114 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001115
Michael Jurkac9d95c52011-08-29 14:03:34 -07001116 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1117 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001119 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 for (ItemInfo childInfo : info.contents) {
1121 sBgItemsIdMap.remove(childInfo.id);
1122 sBgDbIconCache.remove(childInfo);
1123 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001124 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001125 }
1126 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 }
1129
1130 /**
1131 * Set this as the current Launcher activity object for the loader.
1132 */
1133 public void initialize(Callbacks callbacks) {
1134 synchronized (mLock) {
1135 mCallbacks = new WeakReference<Callbacks>(callbacks);
1136 }
1137 }
1138
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001139 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1141 * ACTION_PACKAGE_CHANGED.
1142 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001143 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001144 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001145 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001146
Joe Onorato36115782010-06-17 13:28:48 -04001147 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001148
Joe Onorato36115782010-06-17 13:28:48 -04001149 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1150 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1151 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1152 final String packageName = intent.getData().getSchemeSpecificPart();
1153 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001154
Joe Onorato36115782010-06-17 13:28:48 -04001155 int op = PackageUpdatedTask.OP_NONE;
1156
1157 if (packageName == null || packageName.length() == 0) {
1158 // they sent us a bad intent
1159 return;
1160 }
1161
1162 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1163 op = PackageUpdatedTask.OP_UPDATE;
1164 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1165 if (!replacing) {
1166 op = PackageUpdatedTask.OP_REMOVE;
1167 }
1168 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1169 // later, we will update the package at this time
1170 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1171 if (!replacing) {
1172 op = PackageUpdatedTask.OP_ADD;
1173 } else {
1174 op = PackageUpdatedTask.OP_UPDATE;
1175 }
1176 }
1177
1178 if (op != PackageUpdatedTask.OP_NONE) {
1179 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1180 }
1181
1182 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001183 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001184 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001185 if (!replacing) {
1186 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1187 if (mAppsCanBeOnRemoveableStorage) {
1188 // Only rebind if we support removable storage. It catches the case where
1189 // apps on the external sd card need to be reloaded
1190 startLoaderFromBackground();
1191 }
1192 } else {
1193 // If we are replacing then just update the packages in the list
1194 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1195 packages));
1196 }
Joe Onorato36115782010-06-17 13:28:48 -04001197 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001198 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1199 if (!replacing) {
1200 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1201 enqueuePackageUpdated(new PackageUpdatedTask(
1202 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1203 }
1204 // else, we are replacing the packages, so ignore this event and wait for
1205 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001206 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001207 // If we have changed locale we need to clear out the labels in all apps/workspace.
1208 forceReload();
1209 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1210 // Check if configuration change was an mcc/mnc change which would affect app resources
1211 // and we would need to clear out the labels in all apps/workspace. Same handling as
1212 // above for ACTION_LOCALE_CHANGED
1213 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001214 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001215 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001216 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001217 forceReload();
1218 }
1219 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001220 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001221 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1222 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001223 if (mCallbacks != null) {
1224 Callbacks callbacks = mCallbacks.get();
1225 if (callbacks != null) {
1226 callbacks.bindSearchablesChanged();
1227 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001228 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001229 }
1230 }
1231
Reena Lee93f824a2011-09-23 17:20:28 -07001232 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001233 resetLoadedState(true, true);
1234
Reena Lee93f824a2011-09-23 17:20:28 -07001235 // Do this here because if the launcher activity is running it will be restarted.
1236 // If it's not running startLoaderFromBackground will merely tell it that it needs
1237 // to reload.
1238 startLoaderFromBackground();
1239 }
1240
Winson Chungf0c6ae02012-03-21 16:10:31 -07001241 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1242 synchronized (mLock) {
1243 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1244 // mWorkspaceLoaded to true later
1245 stopLoaderLocked();
1246 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1247 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1248 }
1249 }
1250
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001251 /**
1252 * When the launcher is in the background, it's possible for it to miss paired
1253 * configuration changes. So whenever we trigger the loader from the background
1254 * tell the launcher that it needs to re-run the loader when it comes back instead
1255 * of doing it now.
1256 */
1257 public void startLoaderFromBackground() {
1258 boolean runLoader = false;
1259 if (mCallbacks != null) {
1260 Callbacks callbacks = mCallbacks.get();
1261 if (callbacks != null) {
1262 // Only actually run the loader if they're not paused.
1263 if (!callbacks.setLoadOnResume()) {
1264 runLoader = true;
1265 }
1266 }
1267 }
1268 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001269 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001270 }
Joe Onorato36115782010-06-17 13:28:48 -04001271 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001272
Reena Lee93f824a2011-09-23 17:20:28 -07001273 // If there is already a loader task running, tell it to stop.
1274 // returns true if isLaunching() was true on the old task
1275 private boolean stopLoaderLocked() {
1276 boolean isLaunching = false;
1277 LoaderTask oldTask = mLoaderTask;
1278 if (oldTask != null) {
1279 if (oldTask.isLaunching()) {
1280 isLaunching = true;
1281 }
1282 oldTask.stopLocked();
1283 }
1284 return isLaunching;
1285 }
1286
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001287 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001288 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1289 }
1290
1291 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001292 synchronized (mLock) {
1293 if (DEBUG_LOADERS) {
1294 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1295 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001296
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001297 // Clear any deferred bind-runnables from the synchronized load process
1298 // We must do this before any loading/binding is scheduled below.
1299 mDeferredBindRunnables.clear();
1300
Joe Onorato36115782010-06-17 13:28:48 -04001301 // Don't bother to start the thread if we know it's not going to do anything
1302 if (mCallbacks != null && mCallbacks.get() != null) {
1303 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001304 // also, don't downgrade isLaunching if we're already running
1305 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001306 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001307 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1308 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001309 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1310 } else {
1311 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1312 sWorker.post(mLoaderTask);
1313 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 }
1316 }
1317
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001318 void bindRemainingSynchronousPages() {
1319 // Post the remaining side pages to be loaded
1320 if (!mDeferredBindRunnables.isEmpty()) {
1321 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001322 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 }
1324 mDeferredBindRunnables.clear();
1325 }
1326 }
1327
Joe Onorato36115782010-06-17 13:28:48 -04001328 public void stopLoader() {
1329 synchronized (mLock) {
1330 if (mLoaderTask != null) {
1331 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001332 }
1333 }
Joe Onorato36115782010-06-17 13:28:48 -04001334 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001335
Winson Chung76828c82013-08-19 15:43:29 -07001336 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1337 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1338 final ContentResolver contentResolver = context.getContentResolver();
1339 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1340 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1341 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1342
1343 try {
1344 final int idIndex = sc.getColumnIndexOrThrow(
1345 LauncherSettings.WorkspaceScreens._ID);
1346 final int rankIndex = sc.getColumnIndexOrThrow(
1347 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1348 while (sc.moveToNext()) {
1349 try {
1350 long screenId = sc.getLong(idIndex);
1351 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001352 orderedScreens.put(rank, screenId);
1353 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001354 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001355 }
1356 }
1357 } finally {
1358 sc.close();
1359 }
Winson Chunga90303b2013-11-15 13:05:06 -08001360
1361 // Log to disk
1362 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1363 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1364 for (Integer i : orderedScreens.keySet()) {
1365 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1366 }
1367 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1368 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001369 return orderedScreens;
1370 }
1371
Michael Jurkac57b7a82011-08-09 22:02:20 -07001372 public boolean isAllAppsLoaded() {
1373 return mAllAppsLoaded;
1374 }
1375
Winson Chung36a62fe2012-05-06 18:04:42 -07001376 boolean isLoadingWorkspace() {
1377 synchronized (mLock) {
1378 if (mLoaderTask != null) {
1379 return mLoaderTask.isLoadingWorkspace();
1380 }
1381 }
1382 return false;
1383 }
1384
Joe Onorato36115782010-06-17 13:28:48 -04001385 /**
1386 * Runnable for the thread that loads the contents of the launcher:
1387 * - workspace icons
1388 * - widgets
1389 * - all apps icons
1390 */
1391 private class LoaderTask implements Runnable {
1392 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001393 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001394 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001395 private boolean mStopped;
1396 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001397 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001398
Winson Chungc3eecff2011-07-11 17:44:15 -07001399 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001400
Dan Sandlerd5024042014-01-09 15:01:33 -05001401 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001402 mContext = context;
1403 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001404 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001405 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001406 }
1407
Joe Onorato36115782010-06-17 13:28:48 -04001408 boolean isLaunching() {
1409 return mIsLaunching;
1410 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001411
Winson Chung36a62fe2012-05-06 18:04:42 -07001412 boolean isLoadingWorkspace() {
1413 return mIsLoadingAndBindingWorkspace;
1414 }
1415
Winson Chungc763c4e2013-07-19 13:49:06 -07001416 /** Returns whether this is an upgrade path */
1417 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001418 mIsLoadingAndBindingWorkspace = true;
1419
Joe Onorato36115782010-06-17 13:28:48 -04001420 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001421 if (DEBUG_LOADERS) {
1422 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001423 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001424
Winson Chungc763c4e2013-07-19 13:49:06 -07001425 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001426 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001427 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001428 synchronized (LoaderTask.this) {
1429 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001430 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001431 }
1432 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001433 }
1434 }
1435
Joe Onorato36115782010-06-17 13:28:48 -04001436 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001437 bindWorkspace(-1, isUpgradePath);
1438 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001439 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001440
Joe Onorato36115782010-06-17 13:28:48 -04001441 private void waitForIdle() {
1442 // Wait until the either we're stopped or the other threads are done.
1443 // This way we don't start loading all apps until the workspace has settled
1444 // down.
1445 synchronized (LoaderTask.this) {
1446 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001447
Joe Onorato36115782010-06-17 13:28:48 -04001448 mHandler.postIdle(new Runnable() {
1449 public void run() {
1450 synchronized (LoaderTask.this) {
1451 mLoadAndBindStepFinished = true;
1452 if (DEBUG_LOADERS) {
1453 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001454 }
Joe Onorato36115782010-06-17 13:28:48 -04001455 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001456 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001457 }
Joe Onorato36115782010-06-17 13:28:48 -04001458 });
1459
Michael Jurkac7700af2013-05-14 20:17:58 +02001460 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001461 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001462 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1463 // wait no longer than 1sec at a time
1464 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001465 } catch (InterruptedException ex) {
1466 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001467 }
1468 }
Joe Onorato36115782010-06-17 13:28:48 -04001469 if (DEBUG_LOADERS) {
1470 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001471 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001472 + "ms for previous step to finish binding");
1473 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001476
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001477 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001478 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001479 // Ensure that we have a valid page index to load synchronously
1480 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1481 "valid page index");
1482 }
1483 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1484 // Ensure that we don't try and bind a specified page when the pages have not been
1485 // loaded already (we should load everything asynchronously in that case)
1486 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1487 }
1488 synchronized (mLock) {
1489 if (mIsLoaderTaskRunning) {
1490 // Ensure that we are never running the background loading at this point since
1491 // we also touch the background collections
1492 throw new RuntimeException("Error! Background loading is already running");
1493 }
1494 }
1495
1496 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1497 // data structures, we can't allow any other thread to touch that data, but because
1498 // this call is synchronous, we can get away with not locking).
1499
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001500 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001501 // operations from the previous activity. We need to ensure that all queued operations
1502 // are executed before any synchronous binding work is done.
1503 mHandler.flush();
1504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001505 // Divide the set of loaded items into those that we are binding synchronously, and
1506 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001507 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1509 // arise from that.
1510 onlyBindAllApps();
1511 }
1512
Joe Onorato36115782010-06-17 13:28:48 -04001513 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001514 boolean isUpgrade = false;
1515
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001516 synchronized (mLock) {
1517 mIsLoaderTaskRunning = true;
1518 }
Joe Onorato36115782010-06-17 13:28:48 -04001519 // Optimize for end-user experience: if the Launcher is up and // running with the
1520 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1521 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001522 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001523 // Elevate priority when Home launches for the first time to avoid
1524 // starving at boot time. Staring at a blank home is not cool.
1525 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001526 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1527 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001528 android.os.Process.setThreadPriority(mIsLaunching
1529 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1530 }
Winson Chung64359a52013-07-08 17:17:08 -07001531 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001532 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001533
Joe Onorato36115782010-06-17 13:28:48 -04001534 if (mStopped) {
1535 break keep_running;
1536 }
1537
1538 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1539 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001540 synchronized (mLock) {
1541 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001542 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001543 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1544 }
1545 }
Joe Onorato36115782010-06-17 13:28:48 -04001546 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001547
1548 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001549 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1550 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001551
1552 // Restore the default thread priority after we are done loading items
1553 synchronized (mLock) {
1554 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1555 }
Joe Onorato36115782010-06-17 13:28:48 -04001556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001557
Winson Chungaac01e12011-08-17 10:37:13 -07001558 // Update the saved icons if necessary
1559 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001560 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001561 for (Object key : sBgDbIconCache.keySet()) {
1562 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1563 }
1564 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001565 }
Winson Chungaac01e12011-08-17 10:37:13 -07001566
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001567 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001568 // Ensure that all the applications that are in the system are
1569 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001570 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001571 verifyApplications();
1572 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001573 }
1574
Joe Onorato36115782010-06-17 13:28:48 -04001575 // Clear out this reference, otherwise we end up holding it until all of the
1576 // callback runnables are done.
1577 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001578
Joe Onorato36115782010-06-17 13:28:48 -04001579 synchronized (mLock) {
1580 // If we are still the last one to be scheduled, remove ourselves.
1581 if (mLoaderTask == this) {
1582 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001584 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001585 }
Joe Onorato36115782010-06-17 13:28:48 -04001586 }
1587
1588 public void stopLocked() {
1589 synchronized (LoaderTask.this) {
1590 mStopped = true;
1591 this.notify();
1592 }
1593 }
1594
1595 /**
1596 * Gets the callbacks object. If we've been stopped, or if the launcher object
1597 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1598 * object that was around when the deferred message was scheduled, and if there's
1599 * a new Callbacks object around then also return null. This will save us from
1600 * calling onto it with data that will be ignored.
1601 */
1602 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1603 synchronized (mLock) {
1604 if (mStopped) {
1605 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001606 }
Joe Onorato36115782010-06-17 13:28:48 -04001607
1608 if (mCallbacks == null) {
1609 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001610 }
Joe Onorato36115782010-06-17 13:28:48 -04001611
1612 final Callbacks callbacks = mCallbacks.get();
1613 if (callbacks != oldCallbacks) {
1614 return null;
1615 }
1616 if (callbacks == null) {
1617 Log.w(TAG, "no mCallbacks");
1618 return null;
1619 }
1620
1621 return callbacks;
1622 }
1623 }
1624
Winson Chungc763c4e2013-07-19 13:49:06 -07001625 private void verifyApplications() {
1626 final Context context = mApp.getContext();
1627
1628 // Cross reference all the applications in our apps list with items in the workspace
1629 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001630 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001631 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001632 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001633 tmpInfos = getItemInfoForComponentName(app.componentName);
1634 if (tmpInfos.isEmpty()) {
1635 // We are missing an application icon, so add this to the workspace
1636 added.add(app);
1637 // This is a rare event, so lets log it
1638 Log.e(TAG, "Missing Application on load: " + app);
1639 }
1640 }
1641 }
1642 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001643 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001644 }
1645 }
1646
Joe Onorato36115782010-06-17 13:28:48 -04001647 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001648 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001649 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001650 LauncherAppState app = LauncherAppState.getInstance();
1651 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001652 final int countX = (int) grid.numColumns;
1653 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001654
Adam Cohendcd297f2013-06-18 13:13:40 -07001655 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001656 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001657 // Return early if we detect that an item is under the hotseat button
1658 if (mCallbacks == null ||
1659 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001660 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001661 Log.e(TAG, "Error loading shortcut into hotseat " + item
1662 + " into position (" + item.screenId + ":" + item.cellX + ","
1663 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001664 return false;
1665 }
1666
Dan Sandler295ae182013-12-10 16:05:47 -05001667 final ItemInfo[][] hotseatItems =
1668 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1669
Adam Cohenae4409d2013-11-26 10:34:59 -08001670 if (item.screenId >= grid.numHotseatIcons) {
1671 Log.e(TAG, "Error loading shortcut " + item
1672 + " into hotseat position " + item.screenId
1673 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1674 + ")");
1675 return false;
1676 }
1677
Dan Sandler295ae182013-12-10 16:05:47 -05001678 if (hotseatItems != null) {
1679 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001680 Log.e(TAG, "Error loading shortcut into hotseat " + item
1681 + " into position (" + item.screenId + ":" + item.cellX + ","
1682 + item.cellY + ") occupied by "
1683 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1684 [(int) item.screenId][0]);
1685 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001686 } else {
1687 hotseatItems[(int) item.screenId][0] = item;
1688 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001689 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001690 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001691 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001692 items[(int) item.screenId][0] = item;
1693 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001694 return true;
1695 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001696 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1697 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001698 return true;
1699 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001700
Adam Cohendcd297f2013-06-18 13:13:40 -07001701 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001702 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001703 occupied.put(item.screenId, items);
1704 }
1705
Dan Sandler295ae182013-12-10 16:05:47 -05001706 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001707 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1708 item.cellX < 0 || item.cellY < 0 ||
1709 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1710 Log.e(TAG, "Error loading shortcut " + item
1711 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1712 + item.cellX + "," + item.cellY
1713 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1714 return false;
1715 }
1716
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001717 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001718 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1719 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001720 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001721 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001722 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001723 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001724 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001725 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001726 return false;
1727 }
1728 }
1729 }
1730 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1731 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001732 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001733 }
1734 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001735
Joe Onorato36115782010-06-17 13:28:48 -04001736 return true;
1737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001738
Winson Chungba9c37f2013-08-30 14:11:37 -07001739 /** Clears all the sBg data structures */
1740 private void clearSBgDataStructures() {
1741 synchronized (sBgLock) {
1742 sBgWorkspaceItems.clear();
1743 sBgAppWidgets.clear();
1744 sBgFolders.clear();
1745 sBgItemsIdMap.clear();
1746 sBgDbIconCache.clear();
1747 sBgWorkspaceScreens.clear();
1748 }
1749 }
1750
Dan Sandlerd5024042014-01-09 15:01:33 -05001751 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001752 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001753 // Log to disk
1754 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1755
Joe Onorato36115782010-06-17 13:28:48 -04001756 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757
Joe Onorato36115782010-06-17 13:28:48 -04001758 final Context context = mContext;
1759 final ContentResolver contentResolver = context.getContentResolver();
1760 final PackageManager manager = context.getPackageManager();
1761 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1762 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001763
Winson Chung892c74d2013-08-22 16:15:50 -07001764 LauncherAppState app = LauncherAppState.getInstance();
1765 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1766 int countX = (int) grid.numColumns;
1767 int countY = (int) grid.numRows;
1768
Dan Sandlerd5024042014-01-09 15:01:33 -05001769 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1770 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1771 LauncherAppState.getLauncherProvider().deleteDatabase();
1772 }
1773
1774 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1775 // append the user's Launcher2 shortcuts
1776 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1777 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1778 } else {
1779 // Make sure the default workspace is loaded
1780 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1781 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1782 }
Adam Cohene25af792013-06-06 23:08:25 -07001783
Winson Chungc763c4e2013-07-19 13:49:06 -07001784 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001785 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001786 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001787
Winson Chung9f9f00b2013-11-15 13:27:00 -08001788 // Log to disk
1789 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001790
Winson Chung2abf94d2012-07-18 18:16:38 -07001791 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001792 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001793
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001794 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001795 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001796 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001797 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001798 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001799
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001800 // +1 for the hotseat (it can be larger than the workspace)
1801 // Load workspace in reverse order to ensure that latest items are loaded first (and
1802 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001803 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001805 try {
1806 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1807 final int intentIndex = c.getColumnIndexOrThrow
1808 (LauncherSettings.Favorites.INTENT);
1809 final int titleIndex = c.getColumnIndexOrThrow
1810 (LauncherSettings.Favorites.TITLE);
1811 final int iconTypeIndex = c.getColumnIndexOrThrow(
1812 LauncherSettings.Favorites.ICON_TYPE);
1813 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1814 final int iconPackageIndex = c.getColumnIndexOrThrow(
1815 LauncherSettings.Favorites.ICON_PACKAGE);
1816 final int iconResourceIndex = c.getColumnIndexOrThrow(
1817 LauncherSettings.Favorites.ICON_RESOURCE);
1818 final int containerIndex = c.getColumnIndexOrThrow(
1819 LauncherSettings.Favorites.CONTAINER);
1820 final int itemTypeIndex = c.getColumnIndexOrThrow(
1821 LauncherSettings.Favorites.ITEM_TYPE);
1822 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1823 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001824 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1825 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 final int screenIndex = c.getColumnIndexOrThrow(
1827 LauncherSettings.Favorites.SCREEN);
1828 final int cellXIndex = c.getColumnIndexOrThrow
1829 (LauncherSettings.Favorites.CELLX);
1830 final int cellYIndex = c.getColumnIndexOrThrow
1831 (LauncherSettings.Favorites.CELLY);
1832 final int spanXIndex = c.getColumnIndexOrThrow
1833 (LauncherSettings.Favorites.SPANX);
1834 final int spanYIndex = c.getColumnIndexOrThrow(
1835 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001836 final int restoredIndex = c.getColumnIndexOrThrow(
1837 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1839 //final int displayModeIndex = c.getColumnIndexOrThrow(
1840 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 ShortcutInfo info;
1843 String intentDescription;
1844 LauncherAppWidgetInfo appWidgetInfo;
1845 int container;
1846 long id;
1847 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001850 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001851 try {
1852 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001853 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001854
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001855 switch (itemType) {
1856 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1857 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001858 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 intentDescription = c.getString(intentIndex);
1860 try {
1861 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001862 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001863 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001864 if (restored) {
1865 // might be installed later
1866 Launcher.addDumpLog(TAG,
1867 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001868 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001869 if (!mAppsCanBeOnRemoveableStorage) {
1870 // Log the invalid package, and remove it
1871 Launcher.addDumpLog(TAG,
1872 "Invalid package removed: " + cn, true);
1873 itemsToRemove.add(id);
1874 } else {
1875 // If apps can be on external storage, then we just
1876 // leave them for the user to remove (maybe add
1877 // visual treatment to it)
1878 Launcher.addDumpLog(TAG,
1879 "Invalid package found: " + cn, true);
1880 }
1881 continue;
Winson Chungee055712013-07-30 14:46:24 -07001882 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001883 } else if (restored) {
1884 // no special handling necessary for this restored item
1885 restoredRows.add(id);
1886 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001887 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001889 Launcher.addDumpLog(TAG,
1890 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 continue;
1892 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001893
Chris Wrenf4d08112014-01-16 18:13:56 -05001894 if (restored) {
1895 Launcher.addDumpLog(TAG,
1896 "constructing info for partially restored package",
1897 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001898 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001899 intent = getRestoredItemIntent(c, context, intent);
1900 } else if (itemType ==
1901 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001902 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1903 titleIndex, mLabelCache);
1904 } else {
1905 info = getShortcutInfo(c, context, iconTypeIndex,
1906 iconPackageIndex, iconResourceIndex, iconIndex,
1907 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001908
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 // App shortcuts that used to be automatically added to Launcher
1910 // didn't always have the correct intent flags set, so do that
1911 // here
1912 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001913 intent.getCategories() != null &&
1914 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001915 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 intent.addFlags(
1917 Intent.FLAG_ACTIVITY_NEW_TASK |
1918 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1919 }
Michael Jurka96879562012-03-22 05:54:33 -07001920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001923 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001925 container = c.getInt(containerIndex);
1926 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 info.cellX = c.getInt(cellXIndex);
1929 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001930 info.spanX = 1;
1931 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001932
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001933 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001934 deleteOnInvalidPlacement.set(false);
1935 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1936 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001937 itemsToRemove.add(id);
1938 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001939 break;
1940 }
1941
1942 switch (container) {
1943 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1944 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1945 sBgWorkspaceItems.add(info);
1946 break;
1947 default:
1948 // Item is in a user folder
1949 FolderInfo folderInfo =
1950 findOrMakeFolder(sBgFolders, container);
1951 folderInfo.add(info);
1952 break;
1953 }
1954 sBgItemsIdMap.put(info.id, info);
1955
1956 // now that we've loaded everthing re-save it with the
1957 // icon in case it disappears somehow.
1958 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001959 } else {
1960 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 }
1962 break;
1963
1964 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1965 id = c.getLong(idIndex);
1966 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1967
1968 folderInfo.title = c.getString(titleIndex);
1969 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001970 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001971 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001972 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001973 folderInfo.cellX = c.getInt(cellXIndex);
1974 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001975 folderInfo.spanX = 1;
1976 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977
Daniel Sandler8802e962010-05-26 16:28:16 -04001978 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001979 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001980 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001981 deleteOnInvalidPlacement)) {
1982 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001983 itemsToRemove.add(id);
1984 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001985 break;
1986 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001987
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1990 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1991 sBgWorkspaceItems.add(folderInfo);
1992 break;
Joe Onorato36115782010-06-17 13:28:48 -04001993 }
Joe Onorato17a89222011-02-08 17:26:11 -08001994
Chris Wrenf4d08112014-01-16 18:13:56 -05001995 if (restored) {
1996 // no special handling required for restored folders
1997 restoredRows.add(id);
1998 }
1999
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2001 sBgFolders.put(folderInfo.id, folderInfo);
2002 break;
2003
2004 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2005 // Read all Launcher-specific widget details
2006 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002007 String savedProvider = c.getString(appWidgetProviderIndex);
2008
Joe Onorato36115782010-06-17 13:28:48 -04002009 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002010
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 final AppWidgetProviderInfo provider =
2012 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002013
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 if (!isSafeMode && (provider == null || provider.provider == null ||
2015 provider.provider.getPackageName() == null)) {
2016 String log = "Deleting widget that isn't installed anymore: id="
2017 + id + " appWidgetId=" + appWidgetId;
2018 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002019 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 itemsToRemove.add(id);
2021 } else {
2022 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2023 provider.provider);
2024 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002025 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 appWidgetInfo.cellX = c.getInt(cellXIndex);
2027 appWidgetInfo.cellY = c.getInt(cellYIndex);
2028 appWidgetInfo.spanX = c.getInt(spanXIndex);
2029 appWidgetInfo.spanY = c.getInt(spanYIndex);
2030 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2031 appWidgetInfo.minSpanX = minSpan[0];
2032 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002033
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002034 container = c.getInt(containerIndex);
2035 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2036 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2037 Log.e(TAG, "Widget found where container != " +
2038 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2039 continue;
2040 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041
Adam Cohene25af792013-06-06 23:08:25 -07002042 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002044 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002045 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002046 deleteOnInvalidPlacement)) {
2047 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002048 itemsToRemove.add(id);
2049 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 break;
2051 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002052 String providerName = provider.provider.flattenToString();
2053 if (!providerName.equals(savedProvider)) {
2054 ContentValues values = new ContentValues();
2055 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2056 providerName);
2057 String where = BaseColumns._ID + "= ?";
2058 String[] args = {Integer.toString(c.getInt(idIndex))};
2059 contentResolver.update(contentUri, values, where, args);
2060 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2062 sBgAppWidgets.add(appWidgetInfo);
2063 }
Joe Onorato36115782010-06-17 13:28:48 -04002064 break;
2065 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002067 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002068 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 }
2070 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002071 if (c != null) {
2072 c.close();
2073 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 }
2075
Winson Chungba9c37f2013-08-30 14:11:37 -07002076 // Break early if we've stopped loading
2077 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002078 clearSBgDataStructures();
2079 return false;
2080 }
2081
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 if (itemsToRemove.size() > 0) {
2083 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002084 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 // Remove dead items
2086 for (long id : itemsToRemove) {
2087 if (DEBUG_LOADERS) {
2088 Log.d(TAG, "Removed id = " + id);
2089 }
2090 // Don't notify content observers
2091 try {
2092 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2093 null, null);
2094 } catch (RemoteException e) {
2095 Log.w(TAG, "Could not remove id = " + id);
2096 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002097 }
2098 }
2099
Chris Wrenf4d08112014-01-16 18:13:56 -05002100 if (restoredRows.size() > 0) {
2101 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2102 LauncherSettings.Favorites.CONTENT_URI);
2103 // Update restored items that no longer require special handling
2104 try {
2105 StringBuilder selectionBuilder = new StringBuilder();
2106 selectionBuilder.append(LauncherSettings.Favorites._ID);
2107 selectionBuilder.append(" IN (");
2108 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2109 selectionBuilder.append(")");
2110 ContentValues values = new ContentValues();
2111 values.put(LauncherSettings.Favorites.RESTORED, 0);
2112 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2113 values, selectionBuilder.toString(), null);
2114 } catch (RemoteException e) {
2115 Log.w(TAG, "Could not update restored rows");
2116 }
2117 }
2118
Winson Chungc763c4e2013-07-19 13:49:06 -07002119 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002120 long maxScreenId = 0;
2121 // If we're importing we use the old screen order.
2122 for (ItemInfo item: sBgItemsIdMap.values()) {
2123 long screenId = item.screenId;
2124 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2125 !sBgWorkspaceScreens.contains(screenId)) {
2126 sBgWorkspaceScreens.add(screenId);
2127 if (screenId > maxScreenId) {
2128 maxScreenId = screenId;
2129 }
2130 }
2131 }
2132 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002133 // Log to disk
2134 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2135 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2136 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002137
Michael Jurka414300a2013-08-27 15:42:35 +02002138 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002140
2141 // Update the max item id after we load an old db
2142 long maxItemId = 0;
2143 // If we're importing we use the old screen order.
2144 for (ItemInfo item: sBgItemsIdMap.values()) {
2145 maxItemId = Math.max(maxItemId, item.id);
2146 }
Michael Jurka414300a2013-08-27 15:42:35 +02002147 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002148 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002149 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2150 for (Integer i : orderedScreens.keySet()) {
2151 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002152 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002153 // Log to disk
2154 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2155 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002156
2157 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002158 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002159 for (ItemInfo item: sBgItemsIdMap.values()) {
2160 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002161 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2162 unusedScreens.contains(screenId)) {
2163 unusedScreens.remove(screenId);
2164 }
2165 }
2166
2167 // If there are any empty screens remove them, and update.
2168 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002169 // Log to disk
2170 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2171 TextUtils.join(", ", unusedScreens), true);
2172
Winson Chung933bae62013-08-29 11:42:30 -07002173 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002174 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2175 }
2176 }
2177
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 if (DEBUG_LOADERS) {
2179 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2180 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002182 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002183 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002184
Daniel Sandler566da102013-06-25 23:43:45 -04002185 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002186 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002187 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002188 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 line += " | ";
2190 }
Winson Chung892c74d2013-08-22 16:15:50 -07002191 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002192 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 }
Joe Onorato36115782010-06-17 13:28:48 -04002194 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002197 }
Joe Onorato36115782010-06-17 13:28:48 -04002198 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002199 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002200 }
2201
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 /** Filters the set of items who are directly or indirectly (via another container) on the
2203 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002204 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002205 ArrayList<ItemInfo> allWorkspaceItems,
2206 ArrayList<ItemInfo> currentScreenItems,
2207 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002208 // Purge any null ItemInfos
2209 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2210 while (iter.hasNext()) {
2211 ItemInfo i = iter.next();
2212 if (i == null) {
2213 iter.remove();
2214 }
2215 }
2216
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 // Order the set of items by their containers first, this allows use to walk through the
2218 // list sequentially, build up a list of containers that are in the specified screen,
2219 // as well as all items in those containers.
2220 Set<Long> itemsOnScreen = new HashSet<Long>();
2221 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2222 @Override
2223 public int compare(ItemInfo lhs, ItemInfo rhs) {
2224 return (int) (lhs.container - rhs.container);
2225 }
2226 });
2227 for (ItemInfo info : allWorkspaceItems) {
2228 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002229 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 currentScreenItems.add(info);
2231 itemsOnScreen.add(info.id);
2232 } else {
2233 otherScreenItems.add(info);
2234 }
2235 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2236 currentScreenItems.add(info);
2237 itemsOnScreen.add(info.id);
2238 } else {
2239 if (itemsOnScreen.contains(info.container)) {
2240 currentScreenItems.add(info);
2241 itemsOnScreen.add(info.id);
2242 } else {
2243 otherScreenItems.add(info);
2244 }
2245 }
2246 }
2247 }
2248
2249 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002250 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 ArrayList<LauncherAppWidgetInfo> appWidgets,
2252 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2253 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254
2255 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002256 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002258 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 currentScreenWidgets.add(widget);
2260 } else {
2261 otherScreenWidgets.add(widget);
2262 }
2263 }
2264 }
2265
2266 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002267 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268 HashMap<Long, ItemInfo> itemsIdMap,
2269 HashMap<Long, FolderInfo> folders,
2270 HashMap<Long, FolderInfo> currentScreenFolders,
2271 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272
2273 for (long id : folders.keySet()) {
2274 ItemInfo info = itemsIdMap.get(id);
2275 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002276 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002278 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002279 currentScreenFolders.put(id, folder);
2280 } else {
2281 otherScreenFolders.put(id, folder);
2282 }
2283 }
2284 }
2285
2286 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2287 * right) */
2288 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002289 final LauncherAppState app = LauncherAppState.getInstance();
2290 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291 // XXX: review this
2292 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002293 @Override
2294 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002295 int cellCountX = (int) grid.numColumns;
2296 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002297 int screenOffset = cellCountX * cellCountY;
2298 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002300 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002301 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002302 rhs.cellY * cellCountX + rhs.cellX);
2303 return (int) (lr - rr);
2304 }
2305 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002307
Adam Cohendcd297f2013-06-18 13:13:40 -07002308 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2309 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 final Runnable r = new Runnable() {
2311 @Override
2312 public void run() {
2313 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2314 if (callbacks != null) {
2315 callbacks.bindScreens(orderedScreens);
2316 }
2317 }
2318 };
2319 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2320 }
2321
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2323 final ArrayList<ItemInfo> workspaceItems,
2324 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2325 final HashMap<Long, FolderInfo> folders,
2326 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002327
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 final boolean postOnMainThread = (deferredBindRunnables != null);
2329
2330 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002331 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002333 final int start = i;
2334 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 final Runnable r = new Runnable() {
2336 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002337 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002338 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002339 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002340 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2341 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002342 }
2343 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 };
2345 if (postOnMainThread) {
2346 deferredBindRunnables.add(r);
2347 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002348 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 }
Joe Onorato36115782010-06-17 13:28:48 -04002350 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351
2352 // Bind the folders
2353 if (!folders.isEmpty()) {
2354 final Runnable r = new Runnable() {
2355 public void run() {
2356 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2357 if (callbacks != null) {
2358 callbacks.bindFolders(folders);
2359 }
2360 }
2361 };
2362 if (postOnMainThread) {
2363 deferredBindRunnables.add(r);
2364 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002365 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366 }
2367 }
2368
2369 // Bind the widgets, one at a time
2370 N = appWidgets.size();
2371 for (int i = 0; i < N; i++) {
2372 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2373 final Runnable r = new Runnable() {
2374 public void run() {
2375 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2376 if (callbacks != null) {
2377 callbacks.bindAppWidget(widget);
2378 }
2379 }
2380 };
2381 if (postOnMainThread) {
2382 deferredBindRunnables.add(r);
2383 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002384 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 }
2386 }
2387 }
2388
2389 /**
2390 * Binds all loaded data to actual views on the main thread.
2391 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002392 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 final long t = SystemClock.uptimeMillis();
2394 Runnable r;
2395
2396 // Don't use these two variables in any of the callback runnables.
2397 // Otherwise we hold a reference to them.
2398 final Callbacks oldCallbacks = mCallbacks.get();
2399 if (oldCallbacks == null) {
2400 // This launcher has exited and nobody bothered to tell us. Just bail.
2401 Log.w(TAG, "LoaderTask running with no launcher");
2402 return;
2403 }
2404
Winson Chung9b9fb962013-11-15 15:39:34 -08002405 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002406 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2407 ArrayList<LauncherAppWidgetInfo> appWidgets =
2408 new ArrayList<LauncherAppWidgetInfo>();
2409 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2410 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002411 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002412 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413 workspaceItems.addAll(sBgWorkspaceItems);
2414 appWidgets.addAll(sBgAppWidgets);
2415 folders.putAll(sBgFolders);
2416 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002417 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 }
2419
Derek Prothro7aff3992013-12-10 14:00:37 -05002420 final boolean isLoadingSynchronously =
2421 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002422 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002423 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002424 if (currScreen >= orderedScreenIds.size()) {
2425 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002426 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002427 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002428 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002429 final long currentScreenId = currentScreen < 0
2430 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002431
2432 // Load all the items that are on the current page first (and in the process, unbind
2433 // all the existing workspace items before we call startBinding() below.
2434 unbindWorkspaceItemsOnMainThread();
2435
2436 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2438 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2439 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2440 new ArrayList<LauncherAppWidgetInfo>();
2441 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2442 new ArrayList<LauncherAppWidgetInfo>();
2443 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2444 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2445
Winson Chung9b9fb962013-11-15 15:39:34 -08002446 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002447 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002448 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002450 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451 otherFolders);
2452 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2453 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2454
2455 // Tell the workspace that we're about to start binding items
2456 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002457 public void run() {
2458 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2459 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002460 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002461 }
2462 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002463 };
Winson Chung81b52252012-08-27 15:34:29 -07002464 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002465
Adam Cohendcd297f2013-06-18 13:13:40 -07002466 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2467
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002468 // Load items on the current page
2469 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2470 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002471 if (isLoadingSynchronously) {
2472 r = new Runnable() {
2473 public void run() {
2474 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002475 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002476 callbacks.onPageBoundSynchronously(currentScreen);
2477 }
2478 }
2479 };
Winson Chung81b52252012-08-27 15:34:29 -07002480 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002481 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482
Winson Chung4a2afa32012-07-19 14:53:05 -07002483 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2484 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 mDeferredBindRunnables.clear();
2486 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002487 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488
2489 // Tell the workspace that we're done binding items
2490 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002491 public void run() {
2492 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2493 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002494 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002495 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002496
Winson Chung98e030b2012-05-07 16:01:11 -07002497 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002498 if (DEBUG_LOADERS) {
2499 Log.d(TAG, "bound workspace in "
2500 + (SystemClock.uptimeMillis()-t) + "ms");
2501 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002502
2503 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002504 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002506 if (isLoadingSynchronously) {
2507 mDeferredBindRunnables.add(r);
2508 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002509 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002510 }
Joe Onorato36115782010-06-17 13:28:48 -04002511 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002512
Joe Onorato36115782010-06-17 13:28:48 -04002513 private void loadAndBindAllApps() {
2514 if (DEBUG_LOADERS) {
2515 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2516 }
2517 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002518 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002519 synchronized (LoaderTask.this) {
2520 if (mStopped) {
2521 return;
2522 }
2523 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002524 }
Joe Onorato36115782010-06-17 13:28:48 -04002525 } else {
2526 onlyBindAllApps();
2527 }
2528 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002529
Joe Onorato36115782010-06-17 13:28:48 -04002530 private void onlyBindAllApps() {
2531 final Callbacks oldCallbacks = mCallbacks.get();
2532 if (oldCallbacks == null) {
2533 // This launcher has exited and nobody bothered to tell us. Just bail.
2534 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2535 return;
2536 }
2537
2538 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002539 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002540 final ArrayList<AppInfo> list
2541 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002542 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002543 public void run() {
2544 final long t = SystemClock.uptimeMillis();
2545 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2546 if (callbacks != null) {
2547 callbacks.bindAllApplications(list);
2548 }
2549 if (DEBUG_LOADERS) {
2550 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2551 + (SystemClock.uptimeMillis()-t) + "ms");
2552 }
2553 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002554 };
2555 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002556 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002557 r.run();
2558 } else {
2559 mHandler.post(r);
2560 }
Joe Onorato36115782010-06-17 13:28:48 -04002561 }
2562
Winson Chung64359a52013-07-08 17:17:08 -07002563 private void loadAllApps() {
2564 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002565
Joe Onorato36115782010-06-17 13:28:48 -04002566 final Callbacks oldCallbacks = mCallbacks.get();
2567 if (oldCallbacks == null) {
2568 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002569 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002570 return;
2571 }
2572
Winson Chung64359a52013-07-08 17:17:08 -07002573 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002574 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2575 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2576
Winson Chung64359a52013-07-08 17:17:08 -07002577 // Clear the list of apps
2578 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002579
Winson Chung64359a52013-07-08 17:17:08 -07002580 // Query for the set of apps
2581 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2582 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2583 if (DEBUG_LOADERS) {
2584 Log.d(TAG, "queryIntentActivities took "
2585 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2586 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2587 }
2588 // Fail if we don't have any apps
2589 if (apps == null || apps.isEmpty()) {
2590 return;
2591 }
2592 // Sort the applications by name
2593 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2594 Collections.sort(apps,
2595 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2596 if (DEBUG_LOADERS) {
2597 Log.d(TAG, "sort took "
2598 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002599 }
2600
Winson Chung64359a52013-07-08 17:17:08 -07002601 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002602 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002603 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002604 // This builds the icon bitmaps.
2605 mBgAllAppsList.add(new AppInfo(packageManager, app,
2606 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002607 }
2608
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002609 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002610 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2611 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002612
2613 // Post callback on main thread
2614 mHandler.post(new Runnable() {
2615 public void run() {
2616 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002617 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002618 if (callbacks != null) {
2619 callbacks.bindAllApplications(added);
2620 if (DEBUG_LOADERS) {
2621 Log.d(TAG, "bound " + added.size() + " apps in "
2622 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2623 }
2624 } else {
2625 Log.i(TAG, "not binding apps: no Launcher activity");
2626 }
2627 }
2628 });
2629
Joe Onorato36115782010-06-17 13:28:48 -04002630 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002631 Log.d(TAG, "Icons processed in "
2632 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002633 }
2634 }
2635
2636 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002637 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002638 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2639 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2640 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2641 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2642 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2643 }
Joe Onorato36115782010-06-17 13:28:48 -04002644 }
2645 }
2646
2647 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002648 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002649 }
2650
2651 private class PackageUpdatedTask implements Runnable {
2652 int mOp;
2653 String[] mPackages;
2654
2655 public static final int OP_NONE = 0;
2656 public static final int OP_ADD = 1;
2657 public static final int OP_UPDATE = 2;
2658 public static final int OP_REMOVE = 3; // uninstlled
2659 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2660
2661
2662 public PackageUpdatedTask(int op, String[] packages) {
2663 mOp = op;
2664 mPackages = packages;
2665 }
2666
2667 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002668 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002669
2670 final String[] packages = mPackages;
2671 final int N = packages.length;
2672 switch (mOp) {
2673 case OP_ADD:
2674 for (int i=0; i<N; i++) {
2675 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Chris Wren6d0dde02014-02-10 12:16:54 -05002676 mIconCache.remove(packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002677 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002678 }
2679 break;
2680 case OP_UPDATE:
2681 for (int i=0; i<N; i++) {
2682 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002683 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002684 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002685 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002686 }
2687 break;
2688 case OP_REMOVE:
2689 case OP_UNAVAILABLE:
2690 for (int i=0; i<N; i++) {
2691 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002692 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002693 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002694 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002695 }
2696 break;
2697 }
2698
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002699 ArrayList<AppInfo> added = null;
2700 ArrayList<AppInfo> modified = null;
2701 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002702
Adam Cohen487f7dd2012-06-28 18:12:10 -07002703 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002704 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002705 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002706 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002707 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002708 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002709 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002710 }
Winson Chung5d55f332012-07-16 20:45:03 -07002711 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002712 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002713 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002714 }
2715
Joe Onorato36115782010-06-17 13:28:48 -04002716 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2717 if (callbacks == null) {
2718 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2719 return;
2720 }
2721
2722 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002723 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002724 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002725 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002726 addAndBindAddedWorkspaceApps(context, addedInfos);
2727 } else {
2728 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002729 }
Joe Onorato36115782010-06-17 13:28:48 -04002730 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002731
Joe Onorato36115782010-06-17 13:28:48 -04002732 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002733 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002734
2735 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002736 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002737 ArrayList<ItemInfo> infos =
2738 getItemInfoForComponentName(a.componentName);
2739 for (ItemInfo i : infos) {
2740 if (isShortcutInfoUpdateable(i)) {
2741 ShortcutInfo info = (ShortcutInfo) i;
2742 info.title = a.title.toString();
2743 updateItemInDatabase(context, info);
2744 }
2745 }
2746 }
2747
Joe Onorato36115782010-06-17 13:28:48 -04002748 mHandler.post(new Runnable() {
2749 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002750 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2751 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002752 callbacks.bindAppsUpdated(modifiedFinal);
2753 }
2754 }
2755 });
2756 }
Winson Chung83892cc2013-05-01 16:53:33 -07002757
Winson Chungdf95eb12013-10-16 14:57:07 -07002758 final ArrayList<String> removedPackageNames =
2759 new ArrayList<String>();
2760 if (mOp == OP_REMOVE) {
2761 // Mark all packages in the broadcast to be removed
2762 removedPackageNames.addAll(Arrays.asList(packages));
2763 } else if (mOp == OP_UPDATE) {
2764 // Mark disabled packages in the broadcast to be removed
2765 final PackageManager pm = context.getPackageManager();
2766 for (int i=0; i<N; i++) {
2767 if (isPackageDisabled(pm, packages[i])) {
2768 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002769 }
2770 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002771 }
2772 // Remove all the components associated with this package
2773 for (String pn : removedPackageNames) {
2774 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2775 for (ItemInfo i : infos) {
2776 deleteItemFromDatabase(context, i);
2777 }
2778 }
2779 // Remove all the specific components
2780 for (AppInfo a : removedApps) {
2781 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2782 for (ItemInfo i : infos) {
2783 deleteItemFromDatabase(context, i);
2784 }
2785 }
2786 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2787 // Remove any queued items from the install queue
2788 String spKey = LauncherAppState.getSharedPreferencesKey();
2789 SharedPreferences sp =
2790 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2791 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2792 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002793 mHandler.post(new Runnable() {
2794 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002795 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2796 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002797 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002798 }
2799 }
2800 });
Joe Onoratobe386092009-11-17 17:32:16 -08002801 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002802
Michael Jurkac402cd92013-05-20 15:49:32 +02002803 final ArrayList<Object> widgetsAndShortcuts =
2804 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002805 mHandler.post(new Runnable() {
2806 @Override
2807 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002808 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2809 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002810 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002811 }
2812 }
2813 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002814
2815 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002816 mHandler.post(new Runnable() {
2817 public void run() {
2818 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2819 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002820 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002821 }
2822 }
2823 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 }
2825 }
2826
Michael Jurkac402cd92013-05-20 15:49:32 +02002827 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2828 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2829 PackageManager packageManager = context.getPackageManager();
2830 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2831 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2832 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2833 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2834 Collections.sort(widgetsAndShortcuts,
2835 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2836 return widgetsAndShortcuts;
2837 }
2838
Adam Cohen556f6132014-01-15 15:18:08 -08002839 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002840 try {
2841 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2842 return !pi.applicationInfo.enabled;
2843 } catch (NameNotFoundException e) {
2844 // Fall through
2845 }
2846 return false;
2847 }
Adam Cohen556f6132014-01-15 15:18:08 -08002848
2849 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002850 if (cn == null) {
2851 return false;
2852 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002853 if (isPackageDisabled(pm, cn.getPackageName())) {
2854 return false;
2855 }
Winson Chungee055712013-07-30 14:46:24 -07002856
2857 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002858 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002859 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002860 return (pm.getActivityInfo(cn, 0) != null);
2861 } catch (NameNotFoundException e) {
2862 return false;
2863 }
2864 }
2865
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002867 * Make an ShortcutInfo object for a restored application or shortcut item that points
2868 * to a package that is not yet installed on the system.
2869 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002870 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002871 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002872 if (cursor != null) {
2873 info.title = cursor.getString(titleIndex);
2874 } else {
2875 info.title = "";
2876 }
Chris Wren6d0dde02014-02-10 12:16:54 -05002877 info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
Chris Wrenf4d08112014-01-16 18:13:56 -05002878 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002879 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002880 return info;
2881 }
2882
2883 /**
2884 * Make an Intent object for a restored application or shortcut item that points
2885 * to the market page for the item.
2886 */
2887 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002888 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002889 ComponentName componentName = intent.getComponent();
2890 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2891 Uri marketUri = new Uri.Builder()
2892 .scheme("market")
2893 .authority("details")
2894 .appendQueryParameter("id", componentName.getPackageName())
2895 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002896 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002897 marketIntent.setData(marketUri);
2898 return marketIntent;
2899 }
2900
2901 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002902 * This is called from the code that adds shortcuts from the intent receiver. This
2903 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002904 */
Joe Onorato56d82912010-03-07 14:32:10 -05002905 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002906 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908
Joe Onorato56d82912010-03-07 14:32:10 -05002909 /**
2910 * Make an ShortcutInfo object for a shortcut that is an application.
2911 *
2912 * If c is not null, then it will be used to fill in missing data like the title and icon.
2913 */
2914 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002915 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002916 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002917 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002918 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002919 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002920 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002921 } else {
2922 try {
2923 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2924 info.initFlagsAndFirstInstallTime(pi);
2925 } catch (NameNotFoundException e) {
2926 Log.d(TAG, "getPackInfo failed for package " +
2927 componentName.getPackageName());
2928 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002929 }
2930
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002931 // TODO: See if the PackageManager knows about this case. If it doesn't
2932 // then return null & delete this.
2933
Joe Onorato56d82912010-03-07 14:32:10 -05002934 // the resource -- This may implicitly give us back the fallback icon,
2935 // but don't worry about that. All we're doing with usingFallbackIcon is
2936 // to avoid saving lots of copies of that in the database, and most apps
2937 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002938
2939 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2940 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2941 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002942 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002943 ResolveInfo resolveInfo = null;
2944 ComponentName oldComponent = intent.getComponent();
2945 Intent newIntent = new Intent(intent.getAction(), null);
2946 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2947 newIntent.setPackage(oldComponent.getPackageName());
2948 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2949 for (ResolveInfo i : infos) {
2950 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2951 i.activityInfo.name);
2952 if (cn.equals(oldComponent)) {
2953 resolveInfo = i;
2954 }
2955 }
2956 if (resolveInfo == null) {
2957 resolveInfo = manager.resolveActivity(intent, 0);
2958 }
Joe Onorato56d82912010-03-07 14:32:10 -05002959 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002960 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002961 }
Joe Onorato56d82912010-03-07 14:32:10 -05002962 // the db
2963 if (icon == null) {
2964 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002965 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002966 }
2967 }
2968 // the fallback icon
2969 if (icon == null) {
2970 icon = getFallbackIcon();
2971 info.usingFallbackIcon = true;
2972 }
2973 info.setIcon(icon);
2974
2975 // from the resource
2976 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002977 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2978 if (labelCache != null && labelCache.containsKey(key)) {
2979 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002980 } else {
2981 info.title = resolveInfo.activityInfo.loadLabel(manager);
2982 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002983 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002984 }
2985 }
Joe Onorato56d82912010-03-07 14:32:10 -05002986 }
2987 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002989 if (c != null) {
2990 info.title = c.getString(titleIndex);
2991 }
2992 }
2993 // fall back to the class name of the activity
2994 if (info.title == null) {
2995 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2998 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003000
Winson Chung64359a52013-07-08 17:17:08 -07003001 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3002 ItemInfoFilter f) {
3003 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3004 for (ItemInfo i : infos) {
3005 if (i instanceof ShortcutInfo) {
3006 ShortcutInfo info = (ShortcutInfo) i;
3007 ComponentName cn = info.intent.getComponent();
3008 if (cn != null && f.filterItem(null, info, cn)) {
3009 filtered.add(info);
3010 }
3011 } else if (i instanceof FolderInfo) {
3012 FolderInfo info = (FolderInfo) i;
3013 for (ShortcutInfo s : info.contents) {
3014 ComponentName cn = s.intent.getComponent();
3015 if (cn != null && f.filterItem(info, s, cn)) {
3016 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003017 }
3018 }
Winson Chung64359a52013-07-08 17:17:08 -07003019 } else if (i instanceof LauncherAppWidgetInfo) {
3020 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3021 ComponentName cn = info.providerName;
3022 if (cn != null && f.filterItem(null, info, cn)) {
3023 filtered.add(info);
3024 }
Winson Chung8a435102012-08-30 17:16:53 -07003025 }
3026 }
Winson Chung64359a52013-07-08 17:17:08 -07003027 return new ArrayList<ItemInfo>(filtered);
3028 }
3029
3030 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07003031 ItemInfoFilter filter = new ItemInfoFilter() {
3032 @Override
3033 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3034 return cn.getPackageName().equals(pn);
3035 }
3036 };
3037 return filterItemInfos(sBgItemsIdMap.values(), filter);
3038 }
3039
3040 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003041 ItemInfoFilter filter = new ItemInfoFilter() {
3042 @Override
3043 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3044 return cn.equals(cname);
3045 }
3046 };
3047 return filterItemInfos(sBgItemsIdMap.values(), filter);
3048 }
3049
3050 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3051 if (i instanceof ShortcutInfo) {
3052 ShortcutInfo info = (ShortcutInfo) i;
3053 // We need to check for ACTION_MAIN otherwise getComponent() might
3054 // return null for some shortcuts (for instance, for shortcuts to
3055 // web pages.)
3056 Intent intent = info.intent;
3057 ComponentName name = intent.getComponent();
3058 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3059 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3060 return true;
3061 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003062 // placeholder shortcuts get special treatment, let them through too.
3063 if (info.getRestoredIntent() != null) {
3064 return true;
3065 }
Winson Chung64359a52013-07-08 17:17:08 -07003066 }
3067 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003068 }
3069
3070 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003071 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003073 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003074 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3075 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076
Joe Onorato56d82912010-03-07 14:32:10 -05003077 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003078 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003079 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003081 // TODO: If there's an explicit component and we can't install that, delete it.
3082
Joe Onorato56d82912010-03-07 14:32:10 -05003083 info.title = c.getString(titleIndex);
3084
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 int iconType = c.getInt(iconTypeIndex);
3086 switch (iconType) {
3087 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3088 String packageName = c.getString(iconPackageIndex);
3089 String resourceName = c.getString(iconResourceIndex);
3090 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003091 info.customIcon = false;
3092 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003095 if (resources != null) {
3096 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003097 icon = Utilities.createIconBitmap(
3098 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003099 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003101 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 }
Joe Onorato56d82912010-03-07 14:32:10 -05003103 // the db
3104 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003105 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003106 }
3107 // the fallback icon
3108 if (icon == null) {
3109 icon = getFallbackIcon();
3110 info.usingFallbackIcon = true;
3111 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 break;
3113 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003114 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003115 if (icon == null) {
3116 icon = getFallbackIcon();
3117 info.customIcon = false;
3118 info.usingFallbackIcon = true;
3119 } else {
3120 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003122 break;
3123 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003124 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003125 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126 info.customIcon = false;
3127 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003129 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 return info;
3131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003132
Michael Jurka931dc972011-08-05 15:08:15 -07003133 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003134 @SuppressWarnings("all") // suppress dead code warning
3135 final boolean debug = false;
3136 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003137 Log.d(TAG, "getIconFromCursor app="
3138 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3139 }
3140 byte[] data = c.getBlob(iconIndex);
3141 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003142 return Utilities.createIconBitmap(
3143 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003144 } catch (Exception e) {
3145 return null;
3146 }
3147 }
3148
Winson Chung3d503fb2011-07-13 17:25:49 -07003149 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3150 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003151 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003152 if (info == null) {
3153 return null;
3154 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003155 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003156
3157 return info;
3158 }
3159
Winson Chunga9abd0e2010-10-27 17:18:37 -07003160 /**
Winson Chung55cef262010-10-28 14:14:18 -07003161 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3162 */
3163 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3164 ComponentName component) {
3165 List<AppWidgetProviderInfo> widgets =
3166 AppWidgetManager.getInstance(context).getInstalledProviders();
3167 for (AppWidgetProviderInfo info : widgets) {
3168 if (info.provider.equals(component)) {
3169 return info;
3170 }
3171 }
3172 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003173 }
3174
Winson Chung68846fd2010-10-29 11:00:27 -07003175 /**
3176 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3177 */
3178 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3179 final PackageManager packageManager = context.getPackageManager();
3180 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3181 new ArrayList<WidgetMimeTypeHandlerData>();
3182
3183 final Intent supportsIntent =
3184 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3185 supportsIntent.setType(mimeType);
3186
3187 // Create a set of widget configuration components that we can test against
3188 final List<AppWidgetProviderInfo> widgets =
3189 AppWidgetManager.getInstance(context).getInstalledProviders();
3190 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3191 new HashMap<ComponentName, AppWidgetProviderInfo>();
3192 for (AppWidgetProviderInfo info : widgets) {
3193 configurationComponentToWidget.put(info.configure, info);
3194 }
3195
3196 // Run through each of the intents that can handle this type of clip data, and cross
3197 // reference them with the components that are actual configuration components
3198 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3199 PackageManager.MATCH_DEFAULT_ONLY);
3200 for (ResolveInfo info : activities) {
3201 final ActivityInfo activityInfo = info.activityInfo;
3202 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3203 activityInfo.name);
3204 if (configurationComponentToWidget.containsKey(infoComponent)) {
3205 supportedConfigurationActivities.add(
3206 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3207 configurationComponentToWidget.get(infoComponent)));
3208 }
3209 }
3210 return supportedConfigurationActivities;
3211 }
3212
Winson Chunga9abd0e2010-10-27 17:18:37 -07003213 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003214 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3215 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3216 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3217
Adam Cohend9198822011-11-22 16:42:47 -08003218 if (intent == null) {
3219 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3220 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3221 return null;
3222 }
3223
Joe Onorato0589f0f2010-02-08 13:44:00 -08003224 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003225 boolean customIcon = false;
3226 ShortcutIconResource iconResource = null;
3227
3228 if (bitmap != null && bitmap instanceof Bitmap) {
3229 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003230 customIcon = true;
3231 } else {
3232 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3233 if (extra != null && extra instanceof ShortcutIconResource) {
3234 try {
3235 iconResource = (ShortcutIconResource) extra;
3236 final PackageManager packageManager = context.getPackageManager();
3237 Resources resources = packageManager.getResourcesForApplication(
3238 iconResource.packageName);
3239 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003240 icon = Utilities.createIconBitmap(
3241 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003242 } catch (Exception e) {
3243 Log.w(TAG, "Could not load shortcut icon: " + extra);
3244 }
3245 }
3246 }
3247
Michael Jurkac9d95c52011-08-29 14:03:34 -07003248 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003249
3250 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003251 if (fallbackIcon != null) {
3252 icon = fallbackIcon;
3253 } else {
3254 icon = getFallbackIcon();
3255 info.usingFallbackIcon = true;
3256 }
Joe Onorato56d82912010-03-07 14:32:10 -05003257 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003258 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003259
Joe Onorato0589f0f2010-02-08 13:44:00 -08003260 info.title = name;
3261 info.intent = intent;
3262 info.customIcon = customIcon;
3263 info.iconResource = iconResource;
3264
3265 return info;
3266 }
3267
Winson Chungaac01e12011-08-17 10:37:13 -07003268 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3269 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003270 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003271 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003272 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003273 }
Joe Onorato56d82912010-03-07 14:32:10 -05003274 // If this icon doesn't have a custom icon, check to see
3275 // what's stored in the DB, and if it doesn't match what
3276 // we're going to show, store what we are going to show back
3277 // into the DB. We do this so when we're loading, if the
3278 // package manager can't find an icon (for example because
3279 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003280 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003281 cache.put(info, c.getBlob(iconIndex));
3282 return true;
3283 }
3284 return false;
3285 }
3286 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3287 boolean needSave = false;
3288 try {
3289 if (data != null) {
3290 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3291 Bitmap loaded = info.getIcon(mIconCache);
3292 needSave = !saved.sameAs(loaded);
3293 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003294 needSave = true;
3295 }
Winson Chungaac01e12011-08-17 10:37:13 -07003296 } catch (Exception e) {
3297 needSave = true;
3298 }
3299 if (needSave) {
3300 Log.d(TAG, "going to save icon bitmap for info=" + info);
3301 // This is slower than is ideal, but this only happens once
3302 // or when the app is updated with a new icon.
3303 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003304 }
3305 }
3306
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003308 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003309 * or make a new one.
3310 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003311 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 // See if a placeholder was created for us already
3313 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003314 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003315 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003316 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 folders.put(id, folderInfo);
3318 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003319 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003320 }
3321
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003322 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003323 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003324 return new Comparator<AppInfo>() {
3325 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003326 int result = collator.compare(a.title.toString().trim(),
3327 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003328 if (result == 0) {
3329 result = a.componentName.compareTo(b.componentName);
3330 }
3331 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003332 }
Winson Chung11904872012-09-17 16:58:46 -07003333 };
3334 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003335 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3336 = new Comparator<AppInfo>() {
3337 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003338 if (a.firstInstallTime < b.firstInstallTime) return 1;
3339 if (a.firstInstallTime > b.firstInstallTime) return -1;
3340 return 0;
3341 }
3342 };
Winson Chung11904872012-09-17 16:58:46 -07003343 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3344 final Collator collator = Collator.getInstance();
3345 return new Comparator<AppWidgetProviderInfo>() {
3346 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003347 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003348 }
3349 };
3350 }
Winson Chung5308f242011-08-18 12:12:41 -07003351 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3352 if (info.activityInfo != null) {
3353 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3354 } else {
3355 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3356 }
3357 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003358 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003359 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003360 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003361 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003362 ShortcutNameComparator(PackageManager pm) {
3363 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003364 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003365 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003366 }
3367 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3368 mPackageManager = pm;
3369 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003370 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003371 }
3372 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003373 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003374 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3375 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3376 if (mLabelCache.containsKey(keyA)) {
3377 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003378 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003379 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003380
Winson Chung5308f242011-08-18 12:12:41 -07003381 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003382 }
Winson Chung5308f242011-08-18 12:12:41 -07003383 if (mLabelCache.containsKey(keyB)) {
3384 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003385 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003386 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003387
Winson Chung5308f242011-08-18 12:12:41 -07003388 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003389 }
Winson Chung11904872012-09-17 16:58:46 -07003390 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003391 }
3392 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003393 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003394 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003395 private PackageManager mPackageManager;
3396 private HashMap<Object, String> mLabelCache;
3397 WidgetAndShortcutNameComparator(PackageManager pm) {
3398 mPackageManager = pm;
3399 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003400 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003401 }
3402 public final int compare(Object a, Object b) {
3403 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003404 if (mLabelCache.containsKey(a)) {
3405 labelA = mLabelCache.get(a);
3406 } else {
3407 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003408 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003409 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003410 mLabelCache.put(a, labelA);
3411 }
3412 if (mLabelCache.containsKey(b)) {
3413 labelB = mLabelCache.get(b);
3414 } else {
3415 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003416 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003417 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003418 mLabelCache.put(b, labelB);
3419 }
Winson Chung11904872012-09-17 16:58:46 -07003420 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003421 }
3422 };
Joe Onoratobe386092009-11-17 17:32:16 -08003423
3424 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003425 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003426 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3427 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3428 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3429 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003430 if (mLoaderTask != null) {
3431 mLoaderTask.dumpState();
3432 } else {
3433 Log.d(TAG, "mLoaderTask=null");
3434 }
Joe Onoratobe386092009-11-17 17:32:16 -08003435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003436}